-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
135 lines (110 loc) · 3.34 KB
/
Graph.java
File metadata and controls
135 lines (110 loc) · 3.34 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
125
126
127
128
129
130
131
132
133
134
135
import java.util.LinkedList;
import java.util.HashMap;
import java.util.Queue;
// TODO: handle empty/null graph
// TODO: make generic
// NOTE: a common mistake you keep making is trying to reference
// all vertices from the key set, without adding all vertices
// i.e. because is a directed graph (so for 1->3 only adding 1)
public class Graph {
enum Direction {
DIRECTED, UNDIRECTED
}
Direction direction;
// NOTE: since underlying datastructure is a LL,
// have to worry about duplicate edges appearing twice
// i.e. graph.addEdge(1, 2); graph.addEdge(1, 2);
HashMap<Integer, LinkedList<Integer>> edges;
public static void main(String[] args) {
Graph graph = new Graph(Direction.UNDIRECTED);
// graph.addEdge(0, 1);
// graph.addEdge(1, 2);
// graph.addEdge(2, 3);
// graph.addEdge(3, 0);
graph.addEdge(1, 3);
graph.addEdge(1, 2);
graph.addEdge(2, 3);
graph.addEdge(1, 0);
// graph.printEdges();
graph.depthFirstTraverse();
}
public Graph(Direction direction) {
edges = new HashMap<Integer, LinkedList<Integer>>();
this.direction = direction;
}
public void addEdge(int from, int to) {
if (!edges.containsKey(from)) {
edges.put(from, new LinkedList<Integer>());
}
if (!edges.containsKey(to)) {
edges.put(to, new LinkedList<Integer>());
}
// add vertex_to -> vertex_form
LinkedList<Integer> linkedList = edges.get(from);
linkedList.addLast(to);
// if undirected, also add vertex_from -> vertex_to
if (direction == Direction.UNDIRECTED) {
linkedList = edges.get(to);
linkedList.addLast(from);
}
}
private HashMap<Integer, Boolean> buildVisited() {
HashMap<Integer, Boolean> visited = new HashMap<Integer, Boolean>();
// initialize all vertices to false
for (Integer vertexFrom: edges.keySet()) {
visited.put(vertexFrom, false);
for(Integer vertexTo: edges.get(vertexFrom)) {
visited.put(vertexTo, false);
}
}
return visited;
}
public void depthFirstTraverse() {
// arbitrarily get first element
Integer first = (Integer) edges.keySet().toArray()[0];
// track whether vertex was visited
HashMap<Integer, Boolean> visited = buildVisited();
depthFirstTraverse(first, visited);
}
public void depthFirstTraverse(Integer current, HashMap<Integer, Boolean> visited) {
// mark as visited
visited.put(current, true);
System.out.println("current: " + current);
// System.out.println("eddges: " + edges);
// for all neighbors
for (Integer neighbor: edges.get(current)) {
// if not visited
if (!visited.get(neighbor)) {
depthFirstTraverse(neighbor, visited);
}
}
}
// TODO: change so takes in node argument
public void breadthFirstTraverse() {
// track whether vertex was visited
HashMap<Integer, Boolean> visited = buildVisited();
// store neighbors
Queue<Integer> queue = new LinkedList<Integer>();
// arbitrarily add first element
Integer current = (Integer) edges.keySet().toArray()[0];
queue.add(current);
while (queue.peek() != null) {
current = queue.remove();
System.out.println("current: " + current);
visited.put(current, true);
for (Integer neighbor: edges.get(current)) {
if (!visited.get(neighbor)) {
queue.add(neighbor);
}
}
}
}
public void printEdges() {
for (Integer key: edges.keySet()) {
System.out.print(key + "->");
for (Integer edge: edges.get(key)) {
System.out.println(edge);
}
}
}
}