Skip to content

Commit 37261e9

Browse files
committed
added test code for graph operations
1 parent 41e37d0 commit 37261e9

File tree

1 file changed

+69
-13
lines changed

1 file changed

+69
-13
lines changed

β€ŽInterview Prep/section20_Graph/GraphClient.java

Lines changed: 69 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,39 +27,95 @@ public static void main(String[] args) {
2727

2828
graph.dispaly();
2929

30+
/* test 1 */
31+
3032
// System.out.println(graph.containsVertex("D"));
3133
// System.out.println(graph.containsVertex("P"));
32-
//
34+
3335
// System.out.println(graph.numVertex());
3436
// System.out.println(graph.numEdges());
3537
//
3638
// System.out.println(graph.containsEdge("A", "D"));
3739
// System.out.println(graph.containsEdge("C", "D"));
3840
// System.out.println(graph.containsEdge("C", "G"));
39-
//
41+
4042
// System.out.println("\nbefore: " + graph.numEdges());
4143
// graph.removeEdge("E", "F");
4244
// System.out.println("after: " + graph.numEdges());
43-
//
45+
4446
// graph.dispaly();
45-
//
47+
48+
/* test 2 */
49+
4650
// graph.addEdge("C", "F", 2);
4751
// graph.dispaly();
48-
//
52+
4953
// System.out.println("before: " + graph.numVertex());
5054
// graph.removeVertex("F");
5155
// System.out.println("after: " + graph.numVertex());
5256
// graph.dispaly();
5357

54-
System.out.println("tesing hasPath() method..");
55-
HashMap<String, Boolean> processed = new HashMap<>();
56-
System.out.println(graph.hasPath("A", "F", processed));
58+
/* test 3 */
59+
60+
// System.out.println("tesing hasPath() method..");
61+
// HashMap<String, Boolean> processed = new HashMap<>();
62+
// System.out.println(graph.hasPath("A", "F", processed));
63+
64+
// System.out.println(graph.hasPath("A", "E", new HashMap<String,
65+
// Boolean>()));
66+
// System.out.println(graph.hasPath("A", "G", new HashMap<String,
67+
// Boolean>()));
68+
// System.out.println(graph.hasPath("G", "C", new HashMap<String,
69+
// Boolean>()));
70+
// graph.removeEdge("D", "E");
71+
// System.out.println(graph.hasPath("B", "F", new HashMap<String,
72+
// Boolean>()));
73+
74+
/* test 4 */
75+
76+
// System.out.println("\ntesting Breadth First Search...\n");
77+
// System.out.println(graph.bfs("A", "F")); // ADEF true
78+
79+
// // break the edge between D and E
80+
// // graph.removeEdge("D", "E");
81+
// // System.out.println(graph.bfs("A", "F")); // false
82+
83+
// // break the edge between A and D
84+
// graph.removeEdge("A", "D");
85+
// System.out.println(graph.bfs("A", "F")); // ABCDEF true
86+
87+
/* test 5 */
88+
89+
// System.out.println("\ntesting Depth First Search...");
90+
// // System.out.println(graph.dfs("A", "F")); // ADEF true
91+
92+
// // test by replacing vertex name B to H in the Graph
93+
// System.out.println(graph.dfs("A", "F")); // AHCDEF true
94+
95+
// /* test 6 */
96+
97+
// System.out.println("breadth first traversal...");
98+
// graph.breadthFirstTraversal();
99+
// System.out.println("\nremoving edge between D & E vertex and doing
100+
// bft");
101+
// graph.removeEdge("D", "E");
102+
// graph.breadthFirstTraversal();
103+
104+
/* test 7 */
105+
106+
// System.out.println("depth first traversal...");
107+
// graph.depthFirstTraversal();
108+
// System.out.println("\nremoving edge between D & E vertex and doing
109+
// dft");
110+
// graph.removeEdge("D", "E");
111+
// graph.depthFirstTraversal();
57112

58-
System.out.println(graph.hasPath("A", "E", new HashMap<String, Boolean>()));
59-
System.out.println(graph.hasPath("A", "G", new HashMap<String, Boolean>()));
60-
System.out.println(graph.hasPath("G", "C", new HashMap<String, Boolean>()));
61-
graph.removeEdge("D", "E");
62-
System.out.println(graph.hasPath("B", "F", new HashMap<String, Boolean>()));
113+
/* test 8 */
114+
System.out.println("cyclic: " + graph.isCyclic()); // true
115+
graph.removeEdge("C", "D");
116+
System.out.println("cyclic: " + graph.isCyclic()); // true
117+
graph.removeEdge("F", "G");
118+
System.out.println("cyclic: " + graph.isCyclic()); // false
63119

64120
}
65121
}

0 commit comments

Comments
Β (0)