Skip to content

Commit d15b62a

Browse files
committed
refactor: Add tests, fix removeEdge bug in MatrixGraphs
1 parent 1b51e3e commit d15b62a

File tree

2 files changed

+153
-14
lines changed

2 files changed

+153
-14
lines changed

src/main/java/com/thealgorithms/datastructures/graphs/MatrixGraphs.java

+14-14
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public int numberOfVertices() {
102102
/**
103103
* Updates the number of edges in the graph
104104
*
105-
* @param newNumberOfEdges
105+
* @param newNumberOfEdges the new number of edges
106106
*
107107
*/
108108
private void setNumberOfEdges(int newNumberOfEdges) {
@@ -202,7 +202,7 @@ public boolean addEdge(int from, int to) {
202202
* exists and is removed
203203
*/
204204
public boolean removeEdge(int from, int to) {
205-
if (!this.vertexDoesExist(from) || !this.vertexDoesExist(to)) {
205+
if (this.vertexDoesExist(from) && this.vertexDoesExist(to)) {
206206
if (this.adjacencyOfEdgeDoesExist(from, to)) {
207207
this.adjacency()[from][to] = AdjacencyMatrixGraph.EDGE_NONE;
208208
this.adjacency()[to][from] = AdjacencyMatrixGraph.EDGE_NONE;
@@ -223,14 +223,14 @@ public boolean removeEdge(int from, int to) {
223223
public List<Integer> depthFirstOrder(int startVertex) {
224224
// If the startVertex is invalid, return an empty list
225225
if (startVertex >= vertexCount || startVertex < 0) {
226-
return new ArrayList<Integer>();
226+
return new ArrayList<>();
227227
}
228228

229229
// Create an array to track the visited vertices
230230
boolean[] visited = new boolean[vertexCount];
231231

232232
// Create a list to keep track of the order of our traversal
233-
ArrayList<Integer> orderList = new ArrayList<Integer>();
233+
ArrayList<Integer> orderList = new ArrayList<>();
234234

235235
// Perform our DFS algorithm
236236
depthFirstOrder(startVertex, visited, orderList);
@@ -278,18 +278,18 @@ private void depthFirstOrder(int currentVertex, boolean[] visited, List<Integer>
278278
public List<Integer> breadthFirstOrder(int startVertex) {
279279
// If the specified startVertex is invalid, return an empty list
280280
if (startVertex >= vertexCount || startVertex < 0) {
281-
return new ArrayList<Integer>();
281+
return new ArrayList<>();
282282
}
283283

284284
// Create an array to keep track of the visited vertices
285285
boolean[] visited = new boolean[vertexCount];
286286

287287
// Create a list to keep track of the ordered vertices
288-
ArrayList<Integer> orderList = new ArrayList<Integer>();
288+
ArrayList<Integer> orderList = new ArrayList<>();
289289

290290
// Create a queue for our BFS algorithm and add the startVertex
291291
// to the queue
292-
Queue<Integer> queue = new LinkedList<Integer>();
292+
Queue<Integer> queue = new LinkedList<>();
293293
queue.add(startVertex);
294294

295295
// Continue until the queue is empty
@@ -327,19 +327,19 @@ public List<Integer> breadthFirstOrder(int startVertex) {
327327
* @return returns a string describing this graph
328328
*/
329329
public String toString() {
330-
String s = " ";
330+
StringBuilder s = new StringBuilder(" ");
331331
for (int i = 0; i < this.numberOfVertices(); i++) {
332-
s = s + i + " ";
332+
s.append(i).append(" ");
333333
}
334-
s = s + " \n";
334+
s.append(" \n");
335335

336336
for (int i = 0; i < this.numberOfVertices(); i++) {
337-
s = s + i + " : ";
337+
s.append(i).append(" : ");
338338
for (int j = 0; j < this.numberOfVertices(); j++) {
339-
s = s + this.adjMatrix[i][j] + " ";
339+
s.append(this.adjMatrix[i][j]).append(" ");
340340
}
341-
s = s + "\n";
341+
s.append("\n");
342342
}
343-
return s;
343+
return s.toString();
344344
}
345345
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package com.thealgorithms.datastructures.graphs;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import java.util.Arrays;
8+
import java.util.List;
9+
import org.junit.jupiter.api.Test;
10+
11+
class MatrixGraphsTest {
12+
13+
@Test
14+
void testGraphConstruction() {
15+
AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(5);
16+
assertEquals(5, graph.numberOfVertices());
17+
assertEquals(0, graph.numberOfEdges());
18+
}
19+
20+
@Test
21+
void testAddEdge() {
22+
AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(5);
23+
assertTrue(graph.addEdge(0, 1));
24+
assertTrue(graph.edgeDoesExist(0, 1));
25+
assertTrue(graph.edgeDoesExist(1, 0));
26+
assertEquals(1, graph.numberOfEdges());
27+
28+
assertFalse(graph.addEdge(0, 1));
29+
assertFalse(graph.addEdge(5, 1));
30+
assertFalse(graph.addEdge(-1, 1));
31+
}
32+
33+
@Test
34+
void testRemoveEdge() {
35+
AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(5);
36+
graph.addEdge(0, 1);
37+
graph.addEdge(1, 2);
38+
39+
assertTrue(graph.removeEdge(0, 1));
40+
assertFalse(graph.edgeDoesExist(0, 1));
41+
assertFalse(graph.edgeDoesExist(1, 0));
42+
assertEquals(1, graph.numberOfEdges());
43+
44+
assertFalse(graph.removeEdge(0, 3));
45+
assertFalse(graph.removeEdge(5, 1));
46+
assertFalse(graph.removeEdge(-1, 1));
47+
}
48+
49+
@Test
50+
void testVertexDoesExist() {
51+
AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(5);
52+
assertTrue(graph.vertexDoesExist(0));
53+
assertTrue(graph.vertexDoesExist(4));
54+
assertFalse(graph.vertexDoesExist(5));
55+
assertFalse(graph.vertexDoesExist(-1));
56+
}
57+
58+
@Test
59+
void testDepthFirstOrder() {
60+
AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(5);
61+
graph.addEdge(0, 1);
62+
graph.addEdge(0, 2);
63+
graph.addEdge(1, 3);
64+
graph.addEdge(2, 4);
65+
66+
List<Integer> dfs = graph.depthFirstOrder(0);
67+
assertEquals(5, dfs.size());
68+
assertEquals(0, dfs.getFirst());
69+
70+
assertTrue(dfs.containsAll(Arrays.asList(0, 1, 2, 3, 4)));
71+
72+
List<Integer> emptyDfs = graph.depthFirstOrder(5);
73+
assertTrue(emptyDfs.isEmpty());
74+
}
75+
76+
@Test
77+
void testBreadthFirstOrder() {
78+
AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(5);
79+
graph.addEdge(0, 1);
80+
graph.addEdge(0, 2);
81+
graph.addEdge(1, 3);
82+
graph.addEdge(2, 4);
83+
84+
List<Integer> bfs = graph.breadthFirstOrder(0);
85+
assertEquals(5, bfs.size());
86+
assertEquals(0, bfs.getFirst());
87+
88+
assertTrue(bfs.containsAll(Arrays.asList(0, 1, 2, 3, 4)));
89+
90+
List<Integer> emptyBfs = graph.breadthFirstOrder(5);
91+
assertTrue(emptyBfs.isEmpty());
92+
}
93+
94+
@Test
95+
void testToString() {
96+
AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(3);
97+
graph.addEdge(0, 1);
98+
graph.addEdge(1, 2);
99+
100+
String expected = " 0 1 2 \n"
101+
+ "0 : 0 1 0 \n"
102+
+ "1 : 1 0 1 \n"
103+
+ "2 : 0 1 0 \n";
104+
105+
assertEquals(expected, graph.toString());
106+
}
107+
108+
@Test
109+
void testCyclicGraph() {
110+
AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(4);
111+
graph.addEdge(0, 1);
112+
graph.addEdge(1, 2);
113+
graph.addEdge(2, 3);
114+
graph.addEdge(3, 0);
115+
116+
List<Integer> dfs = graph.depthFirstOrder(0);
117+
List<Integer> bfs = graph.breadthFirstOrder(0);
118+
119+
assertEquals(4, dfs.size());
120+
assertEquals(4, bfs.size());
121+
assertTrue(dfs.containsAll(Arrays.asList(0, 1, 2, 3)));
122+
assertTrue(bfs.containsAll(Arrays.asList(0, 1, 2, 3)));
123+
}
124+
125+
@Test
126+
void testDisconnectedGraph() {
127+
AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(5);
128+
graph.addEdge(0, 1);
129+
graph.addEdge(2, 3);
130+
131+
List<Integer> dfs = graph.depthFirstOrder(0);
132+
List<Integer> bfs = graph.breadthFirstOrder(0);
133+
134+
assertEquals(2, dfs.size());
135+
assertEquals(2, bfs.size());
136+
assertTrue(dfs.containsAll(Arrays.asList(0, 1)));
137+
assertTrue(bfs.containsAll(Arrays.asList(0, 1)));
138+
}
139+
}

0 commit comments

Comments
 (0)