Skip to content

Commit 955ffca

Browse files
committed
fixed a couple typos
1 parent 5d249eb commit 955ffca

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

graph.md

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
###Graph
1+
##Graph
22
==========
33

44
NOTES:
@@ -10,7 +10,6 @@ class Node { //using adjacency list
1010
int data;
1111
boolean visited;
1212
List<Node> list = new ArrayList<>();
13-
void print() {};
1413
}
1514

1615
void bfs(Node root) {
@@ -19,7 +18,6 @@ void bfs(Node root) {
1918
root.visited = true;
2019
while (!queue.isEmpty()) {
2120
Node current = queue.poll();
22-
current.print();
2321
for (Node i : current.list) {
2422
if (!i.visited) {
2523
current.visited = true;
@@ -92,7 +90,7 @@ enum Color {
9290
}
9391

9492
void topSort(List<Node> nodes) {
95-
List<Node> sorted = new ArrayList<>();
93+
List<Node> sorted = new ArrayList<>(); //will contain the sorted graph
9694
Map<Node, Color> state = new HashMap<>();
9795
Set<Node> path = new LinkedHashSet<>();
9896
List<Set<Node>> paths = new ArrayList<>();
@@ -117,17 +115,24 @@ void dfs(Node node,
117115

118116
for (Node n : node.list) {
119117
Color color = state.get(n);
120-
if (color == GRAY) throw new IllegalStateException("Cycle Detection");
121-
if (color == BLACK) continue;
118+
if (color == GRAY) {
119+
throw new IllegalStateException("Cycle Detection");
120+
}
121+
122+
if (color == BLACK) {
123+
continue;
124+
}
125+
122126
path.add(n);
123127
Set<Node> newPath = new LinkedHashSet<>(path);
124-
dfs(n, sorted, state, new LinkedHashSet(newPath));
128+
dfs(n, sorted, state, new LinkedHashSet(newPath), paths, outOrder);
125129
}
126130

127131
state.put(node, Color.BLACK);
128132
sorted.add(node);
129133
paths.add(path);
130134

135+
// update outorder
131136
for (Node n : node.list) {
132137
outOrder(n, outOrder.get(n) + 1);
133138
}
@@ -138,6 +143,7 @@ Map<Node, Integer> getNodesOutOrder(List<Node> nodes) {
138143
Map<Node, Color> state = new HashMap<>();
139144
Set<Node> path = new LinkedHashSet<>();
140145
List<Set<Node>> paths = new ArrayList<>();
146+
// initialize
141147
for (Node n : nodes) {
142148
outOrder.put(n, 0);
143149
}

0 commit comments

Comments
 (0)