-
Notifications
You must be signed in to change notification settings - Fork 0
/
UndirectedGraph.java
156 lines (135 loc) · 4.96 KB
/
UndirectedGraph.java
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import java.util.*;
public class UndirectedGraph<T> implements Graph<T> {
protected List<Node> nodes = new ArrayList<Node>();
protected int nextId=0;
public void addNode(T data) {
nodes.add(new Node(getNextId(), data));
}
protected int getNextId() {
return nextId++;
}
protected class Node implements GraphNode<T> {
int id;
T data;
Map<GraphNode<T>, Integer> neighbors = new HashMap<GraphNode<T>, Integer>();
public Node(int id, T data) {
this.id = id;
this.data = data;
}
public T getData() {
return this.data;
}
public Map<GraphNode<T>, Integer> getNeighbors() {
return this.neighbors;
}
public void addNeighbor(GraphNode<T> node, int distance) {
this.getNeighbors().put(node, distance);
node.getNeighbors().put(this, distance);
}
public int getDistanceFromNeighbor(GraphNode<T> node) {
Integer distance = neighbors.get(node);
if(distance != null) {
return distance.intValue();
} else {
throw new IllegalArgumentException("Node " + node + " not connected.");
}
}
public void removeNeighbor(GraphNode<T> node) {
this.neighbors.remove(node);
}
public String toString() {
return "[Id=" + this.id + ", Data=" + this.data.toString() + "]";
}
}
public List<T> doDFS() {
Map<GraphNode<T>, NodeColor> colorMap = new HashMap<GraphNode<T>, NodeColor>();
Map<GraphNode<T>, GraphNode<T>> parentNodeMap = new HashMap<GraphNode<T>, GraphNode<T>>();
for (GraphNode<T> node : nodes) {
colorMap.put(node, NodeColor.WHITE);
parentNodeMap.put(node, null);
}
List<T> visitedData = new ArrayList<T>();
for (GraphNode<T> node : nodes) {
if (colorMap.get(node) == NodeColor.WHITE) {
doDFS(node, colorMap, parentNodeMap, visitedData);
}
}
return visitedData;
}
private void doDFS(GraphNode<T> node, Map<GraphNode<T>, NodeColor> colorMap, Map<GraphNode<T>, GraphNode<T>> parentNodeMap, List<T> visitedData) {
if (colorMap.get(node) != NodeColor.WHITE) {
System.out.println("BUG: Algorithm is incorrect");
return;
}
visitedData.add(node.getData());
colorMap.put(node, NodeColor.GREY);
for (GraphNode<T> neighbor : node.getNeighbors().keySet()) {
if (colorMap.get(neighbor) == NodeColor.WHITE) {
parentNodeMap.put(neighbor, node);
doDFS(neighbor, colorMap, parentNodeMap, visitedData);
}
}
colorMap.put(node, NodeColor.BLACK);
}
public List<T> doBFS() {
List<GraphNode<T>> visited = new ArrayList<GraphNode<T>>();
for (GraphNode<T> node : nodes) {
doBFS(node, visited);
}
List<T> visitedData = new ArrayList<T>();
for(GraphNode<T> node : visited) {
visitedData.add(node.getData());
}
return visitedData;
}
public void doBFS(GraphNode<T> node, List<GraphNode<T>> visited) {
LinkedList<GraphNode<T>> queue = new LinkedList<GraphNode<T>>();
if(!visited.contains(node)) {
queue.addLast(node);
}
while (!queue.isEmpty()) {
GraphNode<T> nodeFromQueue = queue.removeFirst();
visited.add(nodeFromQueue);
for(GraphNode<T> neighbor : nodeFromQueue.getNeighbors().keySet()) {
if (!visited.contains(neighbor) && !queue.contains(neighbor)) {
queue.addLast(neighbor);
}
}
}
}
public void makeRandomConnections() {
System.out.println("Inside UG's random conn");
Random random = new Random();
int total = nodes.size();
for(GraphNode<T> node : nodes) {
int numConnections = random.nextInt(total/2 + 1);
for(int i = 0; i < numConnections; i++) {
GraphNode<T> peer = nodes.get(random.nextInt(total));
if(peer == node) {
i--;
continue;
}
node.addNeighbor(peer, random.nextInt(100));
}
}
}
public String toString() {
StringBuilder sb = new StringBuilder();
for(Node node : nodes) {
sb.append("[Node = " + node.toString() + ", neighbors = " + node.neighbors.toString() + "]\n");
}
return sb.toString();
}
public boolean isReachable(int id1, int id2) {
GraphNode<T> node1 = nodes.get(id1);
GraphNode<T> node2 = nodes.get(id2);
List<GraphNode<T>> visited = new ArrayList<GraphNode<T>>();
doBFS(node1, visited);
return visited.contains(node2);
}
public enum NodeColor {
WHITE,
GREY,
BLACK;
}
}