Skip to content

refactor: Add tests, fix removeEdge bug in MatrixGraphs #5968

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,7 @@
* [HamiltonianCycleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/HamiltonianCycleTest.java)
* [JohnsonsAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/JohnsonsAlgorithmTest.java)
* [KosarajuTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/KosarajuTest.java)
* [MatrixGraphsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/MatrixGraphsTest.java)
* [TarjansAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithmTest.java)
* [WelshPowellTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/WelshPowellTest.java)
* hashmap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public int numberOfVertices() {
/**
* Updates the number of edges in the graph
*
* @param newNumberOfEdges
* @param newNumberOfEdges the new number of edges
*
*/
private void setNumberOfEdges(int newNumberOfEdges) {
Expand Down Expand Up @@ -202,7 +202,7 @@ public boolean addEdge(int from, int to) {
* exists and is removed
*/
public boolean removeEdge(int from, int to) {
if (!this.vertexDoesExist(from) || !this.vertexDoesExist(to)) {
if (this.vertexDoesExist(from) && this.vertexDoesExist(to)) {
if (this.adjacencyOfEdgeDoesExist(from, to)) {
this.adjacency()[from][to] = AdjacencyMatrixGraph.EDGE_NONE;
this.adjacency()[to][from] = AdjacencyMatrixGraph.EDGE_NONE;
Expand All @@ -223,14 +223,14 @@ public boolean removeEdge(int from, int to) {
public List<Integer> depthFirstOrder(int startVertex) {
// If the startVertex is invalid, return an empty list
if (startVertex >= vertexCount || startVertex < 0) {
return new ArrayList<Integer>();
return new ArrayList<>();
}

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

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

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

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

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

// Create a queue for our BFS algorithm and add the startVertex
// to the queue
Queue<Integer> queue = new LinkedList<Integer>();
Queue<Integer> queue = new LinkedList<>();
queue.add(startVertex);

// Continue until the queue is empty
Expand Down Expand Up @@ -327,19 +327,19 @@ public List<Integer> breadthFirstOrder(int startVertex) {
* @return returns a string describing this graph
*/
public String toString() {
String s = " ";
StringBuilder s = new StringBuilder(" ");
for (int i = 0; i < this.numberOfVertices(); i++) {
s = s + i + " ";
s.append(i).append(" ");
}
s = s + " \n";
s.append(" \n");

for (int i = 0; i < this.numberOfVertices(); i++) {
s = s + i + " : ";
s.append(i).append(" : ");
for (int j = 0; j < this.numberOfVertices(); j++) {
s = s + this.adjMatrix[i][j] + " ";
s.append(this.adjMatrix[i][j]).append(" ");
}
s = s + "\n";
s.append("\n");
}
return s;
return s.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package com.thealgorithms.datastructures.graphs;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;

class MatrixGraphsTest {

@Test
void testGraphConstruction() {
AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(5);
assertEquals(5, graph.numberOfVertices());
assertEquals(0, graph.numberOfEdges());
}

@Test
void testAddEdge() {
AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(5);
assertTrue(graph.addEdge(0, 1));
assertTrue(graph.edgeDoesExist(0, 1));
assertTrue(graph.edgeDoesExist(1, 0));
assertEquals(1, graph.numberOfEdges());

// Adding the same edge again should return false
assertFalse(graph.addEdge(0, 1));
assertFalse(graph.addEdge(5, 1));
assertFalse(graph.addEdge(-1, 1));
}

@Test
void testRemoveEdge() {
AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(5);
graph.addEdge(0, 1);
graph.addEdge(1, 2);

assertTrue(graph.removeEdge(0, 1));
assertFalse(graph.edgeDoesExist(0, 1));
assertFalse(graph.edgeDoesExist(1, 0));
assertEquals(1, graph.numberOfEdges());

assertFalse(graph.removeEdge(0, 3));
assertFalse(graph.removeEdge(5, 1));
assertFalse(graph.removeEdge(-1, 1));
}

@Test
void testVertexDoesExist() {
AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(5);
assertTrue(graph.vertexDoesExist(0));
assertTrue(graph.vertexDoesExist(4));
assertFalse(graph.vertexDoesExist(5));
assertFalse(graph.vertexDoesExist(-1));
}

@Test
void testDepthFirstOrder() {
AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(5);
graph.addEdge(0, 1);
graph.addEdge(0, 2);
graph.addEdge(1, 3);
graph.addEdge(2, 4);

List<Integer> dfs = graph.depthFirstOrder(0);
assertEquals(5, dfs.size());
assertEquals(0, dfs.getFirst());

assertTrue(dfs.containsAll(Arrays.asList(0, 1, 2, 3, 4)));

List<Integer> emptyDfs = graph.depthFirstOrder(5);
assertTrue(emptyDfs.isEmpty());
}

@Test
void testBreadthFirstOrder() {
AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(5);
graph.addEdge(0, 1);
graph.addEdge(0, 2);
graph.addEdge(1, 3);
graph.addEdge(2, 4);

List<Integer> bfs = graph.breadthFirstOrder(0);
assertEquals(5, bfs.size());
assertEquals(0, bfs.getFirst());

assertTrue(bfs.containsAll(Arrays.asList(0, 1, 2, 3, 4)));

List<Integer> emptyBfs = graph.breadthFirstOrder(5);
assertTrue(emptyBfs.isEmpty());
}

@Test
void testToString() {
AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(3);
graph.addEdge(0, 1);
graph.addEdge(1, 2);

String expected = " 0 1 2 \n"
+ "0 : 0 1 0 \n"
+ "1 : 1 0 1 \n"
+ "2 : 0 1 0 \n";

assertEquals(expected, graph.toString());
}

@Test
void testCyclicGraph() {
AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(4);
graph.addEdge(0, 1);
graph.addEdge(1, 2);
graph.addEdge(2, 3);
graph.addEdge(3, 0);

List<Integer> dfs = graph.depthFirstOrder(0);
List<Integer> bfs = graph.breadthFirstOrder(0);

assertEquals(4, dfs.size());
assertEquals(4, bfs.size());
assertTrue(dfs.containsAll(Arrays.asList(0, 1, 2, 3)));
assertTrue(bfs.containsAll(Arrays.asList(0, 1, 2, 3)));
}

@Test
void testDisconnectedGraph() {
AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(5);
graph.addEdge(0, 1);
graph.addEdge(2, 3);

List<Integer> dfs = graph.depthFirstOrder(0);
List<Integer> bfs = graph.breadthFirstOrder(0);

assertEquals(2, dfs.size());
assertEquals(2, bfs.size());
assertTrue(dfs.containsAll(Arrays.asList(0, 1)));
assertTrue(bfs.containsAll(Arrays.asList(0, 1)));
}
}