Skip to content

Commit 53081da

Browse files
committed
All test methods done for chapter 19
1 parent 9e4b72b commit 53081da

16 files changed

+737
-20
lines changed

datastructures/src/main/java/GraphVertex.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
import java.util.List;
44

55
public class GraphVertex {
6-
public int label;
6+
public int data;
77
public List<GraphVertex> edges;
88
public boolean visited;
99

10-
public GraphVertex(int label) {
11-
this.label = label;
10+
public GraphVertex(int data) {
11+
this.data = data;
1212
this.edges = new ArrayList<>();
1313
this.visited = false;
1414
}
1515

16-
public GraphVertex(int label, GraphVertex... graphVertices) {
17-
this.label = label;
16+
public GraphVertex(int data, GraphVertex... graphVertices) {
17+
this.data = data;
1818
this.edges = Arrays.asList(graphVertices);
1919
this.visited = false;
2020
}
@@ -26,7 +26,7 @@ public boolean equals(Object o) {
2626

2727
GraphVertex that = (GraphVertex) o;
2828

29-
if (label != that.label) return false;
29+
if (data != that.data) return false;
3030
return edges != null ? edges.equals(that.edges) : that.edges == null;
3131
}
3232
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
public class Vertex {
5+
public Map<Vertex, Integer> edges = new HashMap<>();
6+
public Character id;
7+
public boolean visited = false;
8+
9+
public Vertex(Character id) {
10+
this.id = id;
11+
}
12+
13+
@Override
14+
public boolean equals(Object o) {
15+
if (this == o) return true;
16+
if (o == null || getClass() != o.getClass()) return false;
17+
18+
Vertex vertex = (Vertex) o;
19+
20+
if (visited != vertex.visited) return false;
21+
if (edges != null ? !edges.equals(vertex.edges) : vertex.edges != null) return false;
22+
return id != null ? id.equals(vertex.id) : vertex.id == null;
23+
}
24+
}

graphs/src/main/java/CloneAGraph.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class CloneAGraph {
44
19.5
55
66
Consider a vertex type for a directed graph in which there
7-
are two fields: an integer label and a list of references
7+
are two fields: an integer data and a list of references
88
to other vertices. Design an algorithm that takes a reference
99
to a vertex u, and creates a copy of the graph on the vertices
1010
reachable from u. Return the copy of u.

graphs/src/main/java/ComputeEnclosedRegions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class ComputeEnclosedRegions {
1010
that cannot reach the boundary with a B.
1111
*/
1212

13-
public static void fillSurroundingRegions(List<List<Character>> board) {
13+
public static void fillSurroundingRegions(List<List<Boolean>> board) {
1414

1515
}
1616
}

graphs/src/main/java/ComputeShortestPath.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import java.util.Collections;
2+
import java.util.List;
3+
14
public class ComputeShortestPath {
25

36
/*
@@ -9,7 +12,8 @@ public class ComputeShortestPath {
912
with the fewest edges amongst all shortest paths from s to t.
1013
*/
1114

12-
public static void compute(GraphVertex s, GraphVertex t) {
15+
public static List<Vertex> compute(int cost, List<Vertex> graph, Vertex s, Vertex t) {
1316

17+
return Collections.emptyList();
1418
}
1519
}

graphs/src/main/java/DeadlockDetection.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,7 @@ public class DeadlockDetection {
99
and checks if the graph contains a cycle.
1010
*/
1111

12-
public static class ColoredGraphVertex {
13-
public static enum Color {WHITE, GRAY, BLACK}
14-
15-
public Color color;
16-
public List<ColoredGraphVertex> edges;
17-
}
18-
19-
public static boolean isDeadlocked(List<ColoredGraphVertex> G) {
12+
public static boolean isDeadlocked(List<GraphVertex> G) {
2013

2114
return false;
2215
}

graphs/src/main/java/SearchMaze.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ public class SearchMaze {
1111
path from the entrance to teh exit, if one exists.
1212
*/
1313

14-
public static enum Color {WHITE, BLACK}
15-
16-
public static List<Tuple> searchMaze(List<List<Color>> maze, Tuple s, Tuple e) {
14+
public static List<Tuple> searchMaze(List<List<Boolean>> maze, Tuple s, Tuple e) {
1715

1816
return Collections.emptyList();
1917
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import org.junit.Test;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
import static org.junit.Assert.*;
7+
8+
public class CloneAGraphTest {
9+
10+
private GraphVertex expected;
11+
private GraphVertex G;
12+
13+
@Test
14+
public void cloneGraph1() throws Exception {
15+
GraphVertex a = new GraphVertex(0);
16+
GraphVertex b = new GraphVertex(1);
17+
GraphVertex c = new GraphVertex(2);
18+
GraphVertex d = new GraphVertex(3);
19+
a.edges.add(b);
20+
a.edges.add(c);
21+
b.edges.add(c);
22+
b.edges.add(d);
23+
24+
expected = a;
25+
G = a;
26+
27+
test(expected, G);
28+
}
29+
30+
private void test(GraphVertex expected, GraphVertex G) {
31+
assertEquals(expected, CloneAGraph.cloneGraph(G));
32+
}
33+
34+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import org.junit.Test;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
import static org.junit.Assert.*;
7+
8+
public class ComputeEnclosedRegionsTest {
9+
10+
private final Boolean w = true;
11+
private final Boolean b = false;
12+
private List<List<Boolean>> expected;
13+
private List<List<Boolean>> board;
14+
15+
@Test
16+
public void fillSurroundingRegions1() throws Exception {
17+
expected = Arrays.asList(
18+
Arrays.asList(b,b,b,b),
19+
Arrays.asList(w,b,b,b),
20+
Arrays.asList(b,b,b,b),
21+
Arrays.asList(b,b,b,b)
22+
);
23+
board = Arrays.asList(
24+
Arrays.asList(b,b,b,b),
25+
Arrays.asList(w,b,w,b),
26+
Arrays.asList(b,w,w,b),
27+
Arrays.asList(b,b,b,b)
28+
);
29+
30+
test(expected, board);
31+
}
32+
33+
@Test
34+
public void fillSurroundingRegions2() throws Exception {
35+
expected = Arrays.asList(
36+
Arrays.asList(b,b,b,b,b,b,b,b,b,b),
37+
Arrays.asList(b,b,b,b,b,b,w,w,b,b),
38+
Arrays.asList(b,b,b,b,b,b,b,w,b,b),
39+
Arrays.asList(b,b,w,b,b,b,b,w,b,b),
40+
Arrays.asList(b,b,b,w,w,w,w,b,b,b),
41+
Arrays.asList(b,b,b,w,w,b,w,b,b,b),
42+
Arrays.asList(b,b,b,b,b,b,b,w,w,b),
43+
Arrays.asList(b,b,b,w,b,b,b,w,w,b),
44+
Arrays.asList(b,b,b,b,b,b,b,b,b,b),
45+
Arrays.asList(b,b,b,b,b,b,b,b,b,b)
46+
);
47+
board = Arrays.asList(
48+
Arrays.asList(b,w,b,w,w,w,b,b,b,b),
49+
Arrays.asList(w,w,b,w,w,b,w,w,b,b),
50+
Arrays.asList(b,b,b,w,w,b,b,w,b,b),
51+
Arrays.asList(w,b,w,b,b,b,b,w,b,w),
52+
Arrays.asList(b,w,b,w,w,w,w,b,w,w),
53+
Arrays.asList(b,w,b,w,w,b,w,b,b,b),
54+
Arrays.asList(w,w,w,b,b,w,b,w,w,b),
55+
Arrays.asList(b,w,b,w,b,w,b,w,w,b),
56+
Arrays.asList(b,w,b,b,w,w,w,b,b,b),
57+
Arrays.asList(w,w,w,w,w,w,w,b,b,w)
58+
);
59+
60+
test(expected, board);
61+
}
62+
63+
private void test(List<List<Boolean>> expected, List<List<Boolean>> board) {
64+
ComputeEnclosedRegions.fillSurroundingRegions(board);
65+
assertEquals(expected, board);
66+
}
67+
68+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import org.junit.Before;
2+
import org.junit.Test;
3+
4+
import javax.annotation.PostConstruct;
5+
6+
import java.util.Arrays;
7+
import java.util.Collections;
8+
import java.util.List;
9+
10+
import static org.junit.Assert.*;
11+
12+
public class ComputeShortestPathTest {
13+
14+
private List<Vertex> expected;
15+
private int cost;
16+
private List<Vertex> graph;
17+
private Vertex s;
18+
private Vertex t;
19+
20+
@Test
21+
public void compute1() throws Exception {
22+
cost = 1;
23+
expected = Arrays.asList(
24+
graph.get(0),
25+
graph.get(2),
26+
graph.get(4),
27+
graph.get(3),
28+
graph.get(7)
29+
);
30+
s = graph.get(0);
31+
t = graph.get(7);
32+
33+
test(expected,cost,graph,s,t);
34+
}
35+
36+
@Test
37+
public void compute2() throws Exception {
38+
cost = 1;
39+
expected = Collections.emptyList();
40+
s = graph.get(9);
41+
t = graph.get(12);
42+
43+
test(expected,cost,graph,s,t);
44+
}
45+
46+
private void test(List<Vertex> expected, int cost, List<Vertex> graph, Vertex s, Vertex t) {
47+
assertEquals(expected,ComputeShortestPath.compute(cost, graph, s, t));
48+
}
49+
50+
@Before
51+
public void setup() {
52+
Vertex a = new Vertex('A');
53+
Vertex b = new Vertex('B');
54+
Vertex c = new Vertex('C');
55+
Vertex d = new Vertex('D');
56+
Vertex e = new Vertex('E');
57+
Vertex f = new Vertex('F');
58+
Vertex g = new Vertex('G');
59+
Vertex h = new Vertex('H');
60+
Vertex i = new Vertex('I');
61+
Vertex j = new Vertex('J');
62+
Vertex k = new Vertex('K');
63+
Vertex l = new Vertex('L');
64+
Vertex m = new Vertex('M');
65+
Vertex n = new Vertex('N');
66+
a.edges.put(b,3);
67+
a.edges.put(c,2);
68+
c.edges.put(e,8);
69+
e.edges.put(d,7);
70+
d.edges.put(c,5);
71+
d.edges.put(h,5);
72+
b.edges.put(a,4);
73+
b.edges.put(k,1);
74+
k.edges.put(i,1);
75+
i.edges.put(k,6);
76+
j.edges.put(l,7);
77+
l.edges.put(i,9);
78+
j.edges.put(f,1);
79+
f.edges.put(g,6);
80+
g.edges.put(f,7);
81+
g.edges.put(h,4);
82+
m.edges.put(n,5);
83+
n.edges.put(m,12);
84+
85+
graph = Arrays.asList(
86+
a,b,c,d,e,
87+
f,g,h,i,j,
88+
k,l,m,n
89+
);
90+
}
91+
92+
}

0 commit comments

Comments
 (0)