Skip to content

Commit ea3fae8

Browse files
committed
Updated 1 solution
1 parent c96a25d commit ea3fae8

File tree

5 files changed

+58
-27
lines changed

5 files changed

+58
-27
lines changed

Chp. 04 - Trees and Graphs/_4_07_Build_Order/BuildOrder.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
11
package _4_07_Build_Order;
22

3-
import java.util.List;
43
import java.util.ArrayDeque;
54

65
// From Jeff Erickson's Algorithms.pdf, Section 19.5 Topological Sort
76

87
public class BuildOrder {
9-
public static ArrayDeque<Node> topologicalSort(List<Node> nodes) throws Exception {
10-
/* Create new "source" Node which has a directed edge to each node in our original graph */
11-
Node source = new Node('s');
12-
for (Node node : nodes) {
8+
9+
// Converts our inconveniently formatted input into a graph
10+
public static ArrayDeque<Node> topologicalSort(String[] projects, String[][] dependencies) throws Exception {
11+
Graph graph = new Graph();
12+
for (String project : projects) {
13+
graph.addNode(project);
14+
}
15+
for (String[] dependency : dependencies) {
16+
String source = dependency[0];
17+
String destination = dependency[1];
18+
graph.addDirectedEdge(source, destination);
19+
}
20+
return topologicalSort(graph);
21+
}
22+
23+
private static ArrayDeque<Node> topologicalSort(Graph graph) throws Exception {
24+
Node source = new Node("Source");
25+
for (Node node : graph.nodes) {
1326
source.addDirectedNeighbor(node);
1427
}
1528

@@ -25,7 +38,7 @@ private static void topoSortDFS(Node n, ArrayDeque<Node> result) throws Exceptio
2538
if (neighbor.status == Visited.NEW) {
2639
topoSortDFS(neighbor, result);
2740
} else if (neighbor.status == Visited.ACTIVE) {
28-
throw new Exception("Not a DAG");
41+
throw new Exception("Not a DAG. Graph has a cycle.");
2942
}
3043
}
3144
n.status = Visited.DONE;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package _4_07_Build_Order;
2+
3+
import java.util.*;
4+
5+
public class Graph { // public variables for convenience
6+
public List<Node> nodes;
7+
public Map<String, Node> map;
8+
9+
public Graph() {
10+
nodes = new ArrayList<>();
11+
map = new HashMap<>();
12+
}
13+
14+
public void addDirectedEdge(String s1, String s2) {
15+
Node source = map.get(s1);
16+
Node destination = map.get(s2);
17+
source.addDirectedNeighbor(destination);
18+
}
19+
20+
public void addNode(String str) {
21+
Node node = new Node(str);
22+
nodes.add(node);
23+
map.put(str, node);
24+
}
25+
}

Chp. 04 - Trees and Graphs/_4_07_Build_Order/Node.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
import java.util.ArrayList;
44

5-
class Node {
6-
public char data;
5+
class Node { // public variables for convenience
6+
public String data;
77
public Visited status;
88
public ArrayList<Node> neighbors; // could alternatively use a HashSet (if I give nodes unique IDs)
99

10-
/* Constructor */
11-
public Node(char data) {
10+
public Node(String data) {
1211
this.data = data;
1312
status = Visited.NEW;
1413
neighbors = new ArrayList<>();

Chp. 04 - Trees and Graphs/_4_07_Build_Order/Tester.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
11
package _4_07_Build_Order;
22

3-
import java.util.Arrays;
4-
import java.util.List;
5-
63
public class Tester {
74
public static void main(String[] args) {
85
System.out.println("*** Test 4.7: Build Order\n");
9-
Node a = new Node('a');
10-
Node b = new Node('b');
11-
Node c = new Node('c');
12-
Node d = new Node('d');
13-
Node e = new Node('e');
14-
Node f = new Node('f');
15-
a.addDirectedNeighbor(d);
16-
f.addDirectedNeighbor(b);
17-
b.addDirectedNeighbor(d);
18-
f.addDirectedNeighbor(a);
19-
d.addDirectedNeighbor(c);
20-
List<Node> nodes = Arrays.asList(a, b, c, d, e, f);
6+
7+
String[] projects = {"a", "b", "c", "d", "e", "f"};
8+
String[][] dependencies = {
9+
{"a", "d"},
10+
{"f", "b"},
11+
{"b", "d"},
12+
{"f", "a"},
13+
{"d", "c"}
14+
};
2115

2216
try {
23-
System.out.println(BuildOrder.topologicalSort(nodes));
17+
System.out.println(BuildOrder.topologicalSort(projects, dependencies));
2418
} catch (Exception exc) {
2519
System.out.println(exc.getMessage());
2620
}

Chp. 16 - More Problems (Moderate)/_16_25_LRU_Cache/LRUCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.util.Map;
44
import java.util.HashMap;
55

6-
// - Excellent explanation in Cracking the Coding Interview, 6th Edition solutions
6+
// Excellent explanation in Cracking the Coding Interview, 6th Edition solutions
77

88
// FAQ
99
//

0 commit comments

Comments
 (0)