forked from anthonynsimon/java-ds-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGraph.java
More file actions
124 lines (94 loc) · 3.21 KB
/
Graph.java
File metadata and controls
124 lines (94 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package com.anthonynsimon.datastructures;
import com.anthonynsimon.datastructures.util.GraphNode;
import java.util.ArrayList;
// Undirected Graph
public class Graph {
// Keep track of all vertices as some of them might not have edges
protected ArrayList<GraphNode> vertices;
public Graph() {
this.vertices = new ArrayList<>();
}
public boolean isEmpty() {
return this.vertices.isEmpty();
}
public boolean containsVertex(String id) {
return getVertex(id) != null;
}
// Adds a new vertex without any edges as long as it doesn't exist already
public void addVertex(String id) {
if (containsVertex(id)) {
return;
}
this.vertices.add(new GraphNode(id));
}
public void removeVertex(String id) {
GraphNode vertex = getVertex(id);
if (vertex == null) {
return;
}
this.vertices.remove(this.vertices.indexOf(vertex));
}
// Creates an edge between a pair of vertices
// No new object instantiated, simple created as a reference to each other
public void addEdge(String idFrom, String idTo) {
GraphNode vertexA = getVertex(idFrom);
GraphNode vertexB = getVertex(idTo);
if (vertexA == null || vertexB == null) {
return;
}
vertexA.addNeighbor(vertexB);
vertexB.addNeighbor(vertexA);
}
// Removes an edge if it exists
public void removeEdge(String idFrom, String idTo) {
GraphNode vertexA = getVertex(idFrom);
GraphNode vertexB = getVertex(idTo);
if (vertexA == null || vertexB == null) {
return;
}
vertexA.removeNeighbor(vertexB);
vertexB.removeNeighbor(vertexA);
}
// Returns the number of edges between the vertices with given id's
// Result of 0 means there is no path between them.
public int distanceBetween(String idFrom, String idTo) {
GraphNode vertexA = getVertex(idFrom);
GraphNode vertexB = getVertex(idTo);
if (vertexA == null || vertexB == null) {
return 0;
}
ArrayList<GraphNode> visited = new ArrayList<>();
Queue<GraphNode> queue = new Queue<>();
int distance = 0;
queue.enqueue(vertexA);
while (!queue.isEmpty()) {
GraphNode current = queue.dequeue();
visited.add(current);
distance++;
for (GraphNode currentNeighbor : current.neighbors()) {
if (currentNeighbor == vertexB) {
return distance;
}
if (!visited.contains(currentNeighbor)) {
queue.enqueue(currentNeighbor);
}
}
}
return 0;
}
// Returns true if there is a path that connects the pair of vertices
public boolean pathExists(String idFrom, String idTo) {
return distanceBetween(idFrom, idTo) > 0;
}
public void clear() {
this.vertices = new ArrayList<>();
}
protected GraphNode getVertex(String id) {
for (GraphNode vertex : this.vertices) {
if (vertex.getId() == id) {
return vertex;
}
}
return null;
}
}