diff --git a/src/main/java/com/dataStructures/graphs/AdjacencyMatrixGraph.java b/src/main/java/com/dataStructures/graphs/AdjacencyMatrixGraph.java new file mode 100644 index 000000000000..bf7873833ac6 --- /dev/null +++ b/src/main/java/com/dataStructures/graphs/AdjacencyMatrixGraph.java @@ -0,0 +1,161 @@ +package com.dataStructures.graphs; + +/** + * Creates an adjacency matrix from a given graph + */ +public class AdjacencyMatrixGraph { + private int _numberOfVertices; + private int _numberOfEdges; + private int[][] _adjacency; + + static final int EDGE_EXIST = 1; + static final int EDGE_NONE = 0; + + /** + * This method sets the number of vertices + * @param numberOfVertices number of vertices in the graph + */ + private void setNumberOfVertices(int numberOfVertices) { + this._numberOfVertices = numberOfVertices; + } + + /** + * This method returns the number of vertices + * @return the number of vertices + */ + public int getNumberOfVertices() { + return this._numberOfVertices; + } + + /** + * This method sets the number of edges + * @param numberOfEdges the number of edges + */ + private void setNumberOfEdges(int numberOfEdges) { + this._numberOfEdges = numberOfEdges; + } + + /** + * This method returns the number of edges + * @return the number of edges + */ + public int getNumberOfEdges() { + return this._numberOfEdges; + } + + /** + * This method sets the adjacency matrix + * @param adjacency the adjacency matrix represented as a 2D array + */ + private void setAdjacency(int[][] adjacency) { + this._adjacency = adjacency; + } + + /** + * This method returns the adjacency matrix + * @return adjacency matrix as 2D array + */ + private int[][] adjacency() { + return this._adjacency; + } + + /** + * This methods checks if there is an edge between the two vertices + * @param from source vertex + * @param to target vertex + * @return true or false + */ + private boolean adjacencyOfEdgeDoesExist(int from, int to) { + return (this.adjacency()[from][to] != AdjacencyMatrixGraph.EDGE_NONE); + } + + /** + * Constructor for the adjacency matrix + * @param givenNumberOfVertices number of vertices + */ + public AdjacencyMatrixGraph(int givenNumberOfVertices) { + this.setNumberOfVertices(givenNumberOfVertices); + this.setNumberOfEdges(0); + this.setAdjacency(new int[givenNumberOfVertices][givenNumberOfVertices]); + for (int i = 0; i < givenNumberOfVertices; i++) { + for (int j = 0; j < givenNumberOfVertices; j++) { + this.adjacency()[i][j] = AdjacencyMatrixGraph.EDGE_NONE; + } + } + } + + /** + * This method checks if the given vertex exists + * @param vertex a vertex of the graph + * @return true or false + */ + public boolean vertexDoesExist(int vertex) { + if (vertex >= 0 && vertex < this.getNumberOfVertices()) { + return true; + } + return false; + } + + /** + * This method checks if the edge between the source and target vertex exists + * @param from source vertex + * @param to target vertex + * @return true or false + */ + public boolean edgeDoesExist(int from, int to) { + if (this.vertexDoesExist(from) && this.vertexDoesExist(to)) { + return (this.adjacencyOfEdgeDoesExist(from, to)); + } + return false; + } + /** + * This method adds an edge to the graph between two specified vertices + * @param from source vertex + * @param to target vertex + * @return returns true if the edge does not exist, return false if it already does + */ + public boolean addEdge(int from, int to) { + if (this.vertexDoesExist(from) && this.vertexDoesExist(to)) { + if (!this.adjacencyOfEdgeDoesExist(from, to)) { + this.adjacency()[from][to] = AdjacencyMatrixGraph.EDGE_EXIST; + this.adjacency()[to][from] = AdjacencyMatrixGraph.EDGE_EXIST; + this.setNumberOfEdges(this.getNumberOfEdges() + 1); + return true; + } + } + return false; + } + + /** + * This method removes an edge from the graph between two specified vertices + * @param from source vertex + * @param to target vertex + * @return returns false if the edge doesn't exist, returns true if the edge exists and is removed + */ + public boolean removeEdge(int from, int 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; + this.setNumberOfEdges(this.getNumberOfEdges() - 1); + return true; + } + } + return false; + } + + /** + * This method gives a list of vertices in the graph and their adjacencies + * @return returns a string describing this graph + */ + public String toString() { + StringBuilder text = new StringBuilder(); + for (int i = 0; i < this.getNumberOfVertices(); i++) { + for (int j = 0; j < this.getNumberOfVertices(); j++) { + text.append(this._adjacency[i][j]).append(" "); + } + text.append("\n"); + } + return text.toString(); + } +} diff --git a/src/main/java/com/dataStructures/graphs/BellmanFord.java b/src/main/java/com/dataStructures/graphs/BellmanFord.java new file mode 100644 index 000000000000..baa276d1477a --- /dev/null +++ b/src/main/java/com/dataStructures/graphs/BellmanFord.java @@ -0,0 +1,97 @@ +package com.dataStructures.graphs; + +/** + * This algorithm find shortest paths from src to all vertices in the given graph. The graph may contain negative weight edges. + */ +public class BellmanFord { + private int[] _dist; + private boolean _negativeWeightCycle; + + static class Edge { + int start, end, weight; + + public Edge(int start, int end, int weight) { + this.start = start; + this.end = end; + this.weight = weight; + } + } + + static class Graph { + int numberOfVertices; + int numberOfEdges; + Edge[] edge; + + public Graph(int vertices, int edges, Edge[] edge) { + this.numberOfEdges = edges; + this.numberOfVertices = vertices; + this.edge = edge; + } + } + + /** + * This method sets the distance matrix + * + * @param dist distance array + */ + private void setDist(int[] dist) { + this._dist = dist; + } + + /** + * Constructor for Bellman-Ford algorithm + * + * @param graph input graph + * @param source source vertex + */ + public BellmanFord(Graph graph, int source) { + int totalVertices = graph.numberOfVertices; + int totalEdges = graph.numberOfEdges; + + this.setDist(new int[totalVertices]); + + for (int i = 0; i < totalVertices; i++) { + this._dist[i] = Integer.MAX_VALUE; + } + this._dist[source] = 0; + + for (int i = 1; i < totalVertices; i++) { + for (int j = 0; j < totalEdges; j++) { + int u = graph.edge[j].start; + int v = graph.edge[j].end; + int w = graph.edge[j].weight; + + if (this._dist[u] != Integer.MAX_VALUE && this._dist[v] > this._dist[u] + w) { + this._dist[v] = this._dist[u] + w; + } + } + } + + for (int i = 0; i < totalEdges; i++) { + int u = graph.edge[i].start; + int v = graph.edge[i].end; + int w = graph.edge[i].weight; + if (this._dist[u] != Integer.MAX_VALUE && this._dist[v] > this._dist[u] + w) { + this._negativeWeightCycle = true; + return; + } + } + + } + + /** + * This method returns the vertices and their distance from the source vertex + * + * @return result as a String + */ + public String toString() { + StringBuilder text = new StringBuilder(); + for (int i = 0; i < this._dist.length; i++) { + text.append("Vertex: ").append(i).append(" distance: ").append(this._dist[i]).append("\n"); + } + if (this._negativeWeightCycle) { + text.append("Negative weight cycle detected."); + } + return text.toString(); + } +} diff --git a/src/main/java/com/dataStructures/graphs/BreadthFirstSearch.java b/src/main/java/com/dataStructures/graphs/BreadthFirstSearch.java new file mode 100644 index 000000000000..79d71d62911b --- /dev/null +++ b/src/main/java/com/dataStructures/graphs/BreadthFirstSearch.java @@ -0,0 +1,98 @@ +package com.dataStructures.graphs; + +import java.util.LinkedList; + +/** + * It is an algorithm for traversing or searching tree or graph data structures. + * It starts at the tree root in a breadthward motion. + */ + +public class BreadthFirstSearch { + private int _numberOfVertices; + private LinkedList[] _adjacencyList; + private int _source; + + /** + * This method returns the source vertex + * + * @return source vertex + */ + public int getSource() { + return this._source; + } + + /** + * This method sets the source + * + * @param source source vertex + */ + private void setSource(int source) { + this._source = source; + } + + /** + * This method sets the number of vertices + * + * @param numberOfVertices number of vertices in the graph + */ + private void setNumberOfVertices(int numberOfVertices) { + this._numberOfVertices = numberOfVertices; + } + + /** + * This method sets the adjacency list + * + * @param adjacencyList given adjacency list + */ + private void setAdjacencyList(LinkedList[] adjacencyList) { + this._adjacencyList = adjacencyList; + } + + /** + * Constructor for breadth first search + * + * @param givenNumberOfVertices number of vertices + */ + public BreadthFirstSearch(int givenNumberOfVertices, int source) { + this.setSource(source); + this.setNumberOfVertices(givenNumberOfVertices); + this.setAdjacencyList(new LinkedList[givenNumberOfVertices]); + for (int i = 0; i < givenNumberOfVertices; ++i) { + this._adjacencyList[i] = new LinkedList(); + } + } + + /** + * This method adds an edge between source vertex and target vertex + * + * @param source source vertex + * @param target target vertex + */ + public void addEdge(int source, int target) { + this._adjacencyList[source].add(target); + } + + /** + * This method prints BFS traversal from a given source vertex + * + * @return BFS traversal + */ + public String toString() { + StringBuilder text = new StringBuilder(); + boolean[] visited = new boolean[_numberOfVertices]; + LinkedList queue = new LinkedList<>(); + visited[this.getSource()] = true; + queue.add(this.getSource()); + while (queue.size() != 0) { + this.setSource(queue.poll()); + text.append(this.getSource() + " "); + for (int n : _adjacencyList[this.getSource()]) { + if (!visited[n]) { + visited[n] = true; + queue.add(n); + } + } + } + return text.toString(); + } +} diff --git a/src/main/java/com/dataStructures/graphs/DepthFirstSearch.java b/src/main/java/com/dataStructures/graphs/DepthFirstSearch.java new file mode 100644 index 000000000000..7ceb57e2df43 --- /dev/null +++ b/src/main/java/com/dataStructures/graphs/DepthFirstSearch.java @@ -0,0 +1,99 @@ +package com.dataStructures.graphs; + +import java.util.LinkedList; + +/** + * Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. + * The algorithm starts at the root node and explores as far as possible along each branch before backtracking. + */ + +public class DepthFirstSearch { + private int _numberOfVertices; + private LinkedList[] _adjacencyList; + private int _source; + + /** + * This method returns the source vertex + * + * @return source vertex + */ + public int getSource() { + return this._source; + } + + /** + * This method sets the source + * + * @param source source vertex + */ + private void setSource(int source) { + this._source = source; + } + + /** + * This method sets the number of vertices + * + * @param numberOfVertices number of vertices in the graph + */ + private void setNumberOfVertices(int numberOfVertices) { + this._numberOfVertices = numberOfVertices; + } + + /** + * This method sets the adjacency list + * + * @param adjacencyList given adjacency list + */ + private void setAdjacencyList(LinkedList[] adjacencyList) { + this._adjacencyList = adjacencyList; + } + + /** + * Constructor for depth first search + * + * @param givenNumberOfVertices number of vertices + */ + public DepthFirstSearch(int givenNumberOfVertices, int source) { + this.setSource(source); + this.setNumberOfVertices(givenNumberOfVertices); + this.setAdjacencyList(new LinkedList[givenNumberOfVertices]); + for (int i = 0; i < givenNumberOfVertices; ++i) + this._adjacencyList[i] = new LinkedList(); + } + + /** + * This method adds an edge between source vertex and target vertex + * + * @param source source vertex + * @param target target vertex + */ + public void addEdge(int source, int target) { + this._adjacencyList[source].add(target); + } + + /** + * This method marks the current node visited and recur for all the vertices adjacent to this vertex + * @param vertex a given vertex + * @param visited visited edges + * @param text vertices as strings + */ + public void DFSUtil(int vertex, boolean[] visited, StringBuilder text) { + visited[vertex] = true; + text.append(vertex + " "); + for (int n : this._adjacencyList[vertex]) { + if (!visited[n]) + DFSUtil(n, visited, text); + } + } + + /** + * This method prints DFS traversal from a given source vertex + * @return DFS traversal + */ + public String toString() { + StringBuilder text = new StringBuilder(); + boolean[] visited = new boolean[this._numberOfVertices]; + DFSUtil(this.getSource(), visited, text); + return text.toString(); + } +} diff --git a/src/main/java/com/dataStructures/graphs/FloydWarshall.java b/src/main/java/com/dataStructures/graphs/FloydWarshall.java new file mode 100644 index 000000000000..66b0a2feda77 --- /dev/null +++ b/src/main/java/com/dataStructures/graphs/FloydWarshall.java @@ -0,0 +1,82 @@ +package com.dataStructures.graphs; + +/** + * Finds shortest distances between every pair of vertices in a given weighted directed graph. + */ +public class FloydWarshall { + private int[][] _distanceMatrix; + private int _numberOfVertices; + final static int INF = 99999; + + /** + * This method sets the number of vertices + * + * @param numberOfVertices number of vertices in the graph + */ + private void setNumberOfVertices(int numberOfVertices) { + this._numberOfVertices = numberOfVertices; + } + + /** + * This method returns the number of vertices + * + * @return the number of vertices + */ + public int getNumberOfVertices() { + return this._numberOfVertices; + } + + /** + * This method sets the distance matrix + * + * @param distanceMatrix distances as a 2D array + */ + private void setDistanceMatrix(int[][] distanceMatrix) { + this._distanceMatrix = distanceMatrix; + } + + /** + * Constructor for the graph + * + * @param graph input graph + * @param givenNumberOfVertices number of vertices + */ + public FloydWarshall(int[][] graph, int givenNumberOfVertices) { + this.setNumberOfVertices(givenNumberOfVertices); + this.setDistanceMatrix(new int[this._numberOfVertices][this._numberOfVertices]); + for (int i = 0; i < this._numberOfVertices; i++) { + for (int j = 0; j < this._numberOfVertices; j++) { + this._distanceMatrix[i][j] = graph[i][j]; + } + } + for (int k = 0; k < this._numberOfVertices; k++) { + for (int i = 0; i < this._numberOfVertices; i++) { + for (int j = 0; j < this._numberOfVertices; j++) { + if (this._distanceMatrix[i][k] + this._distanceMatrix[k][j] < this._distanceMatrix[i][j]) { + this._distanceMatrix[i][j] = this._distanceMatrix[i][k] + this._distanceMatrix[k][j]; + } + } + } + } + } + + /** + * This method shows the shortest distances between every pair of vertices + * + * @return returns a string describing this graph + */ + public String toString() { + StringBuilder text = new StringBuilder(); + for (int i = 0; i < this._numberOfVertices; ++i) { + for (int j = 0; j < this._numberOfVertices; ++j) { + if (this._distanceMatrix[i][j] == INF) { + text.append("INF "); + } else { + text.append(this._distanceMatrix[i][j] + " "); + } + } + text.append("\n"); + } + return text.toString(); + } +} diff --git a/src/main/java/com/dataStructures/graphs/GraphColoring.java b/src/main/java/com/dataStructures/graphs/GraphColoring.java new file mode 100644 index 000000000000..f931b1eee406 --- /dev/null +++ b/src/main/java/com/dataStructures/graphs/GraphColoring.java @@ -0,0 +1,95 @@ +package com.dataStructures.graphs; + +import java.util.Arrays; +import java.util.LinkedList; + +/** + * This algorithm, given m colors, finds a way of coloring the vertices of a graph such that no two adjacent vertices + * are colored using same color. + */ + +public class GraphColoring { + private int _numberOfVertices; + private LinkedList[] _adjacencyList; + + /** + * This method sets the number of vertices + * + * @param numberOfVertices number of vertices in the graph + */ + private void setNumberOfVertices(int numberOfVertices) { + this._numberOfVertices = numberOfVertices; + } + + /** + * This method sets the adjacency list + * + * @param adjacencyList given adjacency list + */ + private void setAdjacencyList(LinkedList[] adjacencyList) { + this._adjacencyList = adjacencyList; + } + + /** + * Constructor for graph coloring + * + * @param givenNumberOfVertices given number of vertices + */ + GraphColoring(int givenNumberOfVertices) { + this.setNumberOfVertices(givenNumberOfVertices); + this.setAdjacencyList(new LinkedList[givenNumberOfVertices]); + for (int i = 0; i < givenNumberOfVertices; ++i) + this._adjacencyList[i] = new LinkedList(); + } + + /** + * This method adds an edge between source vertex and target vertex + * + * @param source source vertex + * @param target target vertex + */ + public void addEdge(int source, int target) { + this._adjacencyList[source].add(target); + this._adjacencyList[target].add(source); + } + + /** + * Assigns colors to all vertices starting from 0 + */ + public int[] greedyColoring() { + int[] result = new int[this._numberOfVertices]; + Arrays.fill(result, -1); + result[0] = 0; + boolean[] available = new boolean[this._numberOfVertices]; + Arrays.fill(available, true); + + for (int u = 1; u < this._numberOfVertices; u++) { + for (int i : this._adjacencyList[u]) { + if (result[i] != -1) + available[result[i]] = false; + } + int colorIndex; + for (colorIndex = 0; colorIndex < _numberOfVertices; colorIndex++) { + if (available[colorIndex]) + break; + } + result[u] = colorIndex; + Arrays.fill(available, true); + } + return result; + } + + /** + * This method prints all the vertexes with their color + * + * @return graph coloring + */ + public String toString() { + StringBuilder text = new StringBuilder(); + int[] result = this.greedyColoring(); + for (int u = 0; u < this._numberOfVertices; u++) { + text.append("Vertex ").append(u).append(" ---> Color ").append(result[u]).append("\n"); + } + return text.toString(); + } +} diff --git a/src/main/java/com/dataStructures/graphs/PrimMinimumSpanningTree.java b/src/main/java/com/dataStructures/graphs/PrimMinimumSpanningTree.java new file mode 100644 index 000000000000..f49a4a3ae8b2 --- /dev/null +++ b/src/main/java/com/dataStructures/graphs/PrimMinimumSpanningTree.java @@ -0,0 +1,112 @@ +package com.dataStructures.graphs; + +/** + * Creates a minimum spanning tree. A spanning tree of a graph is a subgraph that is a tree and connects all + * the vertices together. A minimum spanning tree has the smallest weight. + */ +public class PrimMinimumSpanningTree { + private int _numberOfVertices; + private int[][] _graph; + private int[] _parent; + + /** + * This method sets the graph + * + * @param graph input graph as an adjacency matrix + */ + private void setGraph(int[][] graph) { + this._graph = graph; + } + + /** + * This method sets the constructed MST + * + * @param parent the constructed MST + */ + private void setParent(int[] parent) { + this._parent = parent; + } + + /** + * This method sets the number of vertices + * + * @param numberOfVertices number of vertices in the graph + */ + private void setNumberOfVertices(int numberOfVertices) { + this._numberOfVertices = numberOfVertices; + } + + /** + * This method returns the number of vertices + * + * @return the number of vertices + */ + public int getNumberOfVertices() { + return this._numberOfVertices; + } + + /** + * This method finds the vertex with the minimum key value + * + * @param key array of keys + * @param mstSet set of vertices + * @return + */ + public int findMinimumKey(int key[], Boolean mstSet[]) { + int minimum = Integer.MAX_VALUE; + int minimumIndex = -1; + + for (int i = 0; i < _numberOfVertices; i++) + if (!mstSet[i] && key[i] < minimum) { + minimum = key[i]; + minimumIndex = i; + } + return minimumIndex; + } + + /** + * Constructor for MST represented by using adjacency matrix + * + * @param graph adjacency matrix for the graph + * @param givenNumberOfVertices number of vertices in the graph + */ + public PrimMinimumSpanningTree(int[][] graph, int givenNumberOfVertices) { + this.setNumberOfVertices(givenNumberOfVertices); + int[] parent = new int[givenNumberOfVertices]; + int[] key = new int[givenNumberOfVertices]; + Boolean[] mstSet = new Boolean[givenNumberOfVertices]; + + for (int i = 0; i < givenNumberOfVertices; i++) { + key[i] = Integer.MAX_VALUE; + mstSet[i] = false; + } + + key[0] = 0; + parent[0] = -1; + + for (int count = 0; count < givenNumberOfVertices - 1; count++) { + int u = findMinimumKey(key, mstSet); + mstSet[u] = true; + for (int i = 0; i < _numberOfVertices; i++) + if (graph[u][i] != 0 && !mstSet[i] && graph[u][i] < key[i]) { + parent[i] = u; + key[i] = graph[u][i]; + } + } + this.setGraph(graph); + this.setParent(parent); + } + + /** + * This method gives a list of vertices in the minimum spanning tree and their weights + * + * @return returns a string describing this graph + */ + public String toString() { + StringBuilder text = new StringBuilder(); + for (int i = 1; i < _numberOfVertices; i++) { + text.append(this._parent[i] + " - " + i + ", weight: " + this._graph[i][this._parent[i]] + "\n"); + } + return text.toString(); + } +} diff --git a/src/test/java/com/dataStructures/graphs/AdjacencyMatrixGraphTest.java b/src/test/java/com/dataStructures/graphs/AdjacencyMatrixGraphTest.java new file mode 100644 index 000000000000..ebbe0a4232cd --- /dev/null +++ b/src/test/java/com/dataStructures/graphs/AdjacencyMatrixGraphTest.java @@ -0,0 +1,70 @@ +package com.dataStructures.graphs; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class AdjacencyMatrixGraphTest { + + @Test + void testAdjacencyMatrixGraph() { + AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(5); + graph.addEdge(0, 2); + graph.addEdge(1, 4); + graph.addEdge(2, 4); + graph.addEdge(2, 3); + graph.addEdge(3, 4); + Assertions.assertEquals( + "0 0 1 0 0 \n" + + "0 0 0 0 1 \n" + + "1 0 0 1 1 \n" + + "0 0 1 0 1 \n" + + "0 1 1 1 0 \n", graph.toString()); + + AdjacencyMatrixGraph graph2 = new AdjacencyMatrixGraph(6); + graph2.addEdge(1, 2); + graph2.addEdge(0, 3); + graph2.addEdge(4, 5); + graph2.addEdge(3, 4); + Assertions.assertEquals("0 0 0 1 0 0 \n" + + "0 0 1 0 0 0 \n" + + "0 1 0 0 0 0 \n" + + "1 0 0 0 1 0 \n" + + "0 0 0 1 0 1 \n" + + "0 0 0 0 1 0 \n", graph2.toString()); + } + + @Test + void testEdgeDoesExist() { + AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(5); + graph.addEdge(0, 2); + graph.addEdge(1, 4); + graph.addEdge(2, 4); + graph.addEdge(2, 3); + Assertions.assertTrue(graph.edgeDoesExist(0, 2)); + Assertions.assertFalse(graph.edgeDoesExist(1, 5)); + } + + @Test + void testAddEdge() { + AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(5); + graph.addEdge(0, 2); + graph.addEdge(1, 4); + graph.addEdge(2, 4); + graph.addEdge(2, 3); + graph.addEdge(3, 4); + Assertions.assertFalse(graph.addEdge(0, 2)); + Assertions.assertTrue(graph.addEdge(0, 3)); + } + + @Test + void testRemoveEdge() { + AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(5); + graph.addEdge(0, 2); + graph.addEdge(1, 4); + graph.addEdge(2, 4); + graph.addEdge(2, 3); + graph.addEdge(3, 4); + Assertions.assertTrue(graph.removeEdge(0, 2)); + Assertions.assertFalse(graph.removeEdge(0, 3)); + } +} diff --git a/src/test/java/com/dataStructures/graphs/BellmanFordTest.java b/src/test/java/com/dataStructures/graphs/BellmanFordTest.java new file mode 100644 index 000000000000..c93071a3a540 --- /dev/null +++ b/src/test/java/com/dataStructures/graphs/BellmanFordTest.java @@ -0,0 +1,64 @@ +package com.dataStructures.graphs; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class BellmanFordTest { + @Test + void testBellmanFord() { + BellmanFord.Edge[] edges = new BellmanFord.Edge[5]; + + BellmanFord.Edge edge1 = new BellmanFord.Edge(0, 1, 5); + BellmanFord.Edge edge2 = new BellmanFord.Edge(0, 2, 4); + BellmanFord.Edge edge3 = new BellmanFord.Edge(1, 3, 3); + BellmanFord.Edge edge4 = new BellmanFord.Edge(2, 1, -6); + BellmanFord.Edge edge5 = new BellmanFord.Edge(3, 2, 2); + + edges[0] = edge1; + edges[1] = edge2; + edges[2] = edge3; + edges[3] = edge4; + edges[4] = edge5; + + BellmanFord.Graph graph = new BellmanFord.Graph(4, 5, edges); + + BellmanFord bellmanFord = new BellmanFord(graph, 0); + Assertions.assertEquals("Vertex: 0 distance: 0\n" + + "Vertex: 1 distance: -3\n" + + "Vertex: 2 distance: 3\n" + + "Vertex: 3 distance: 1\n" + + "Negative weight cycle detected.", bellmanFord.toString()); + } + + @Test + void testBellmanFord2() { + BellmanFord.Edge[] edges = new BellmanFord.Edge[8]; + + BellmanFord.Edge edge1 = new BellmanFord.Edge(0, 1, -1); + BellmanFord.Edge edge2 = new BellmanFord.Edge(0, 2, 4); + BellmanFord.Edge edge3 = new BellmanFord.Edge(1, 2, 3); + BellmanFord.Edge edge4 = new BellmanFord.Edge(1, 3, 2); + BellmanFord.Edge edge5 = new BellmanFord.Edge(1, 4, 2); + BellmanFord.Edge edge6 = new BellmanFord.Edge(3, 2, 5); + BellmanFord.Edge edge7 = new BellmanFord.Edge(3, 1, 1); + BellmanFord.Edge edge8 = new BellmanFord.Edge(4, 3, -3); + + edges[0] = edge1; + edges[1] = edge2; + edges[2] = edge3; + edges[3] = edge4; + edges[4] = edge5; + edges[5] = edge6; + edges[6] = edge7; + edges[7] = edge8; + + BellmanFord.Graph graph = new BellmanFord.Graph(5, 8, edges); + + BellmanFord bellmanFord = new BellmanFord(graph, 0); + Assertions.assertEquals("Vertex: 0 distance: 0\n" + + "Vertex: 1 distance: -1\n" + + "Vertex: 2 distance: 2\n" + + "Vertex: 3 distance: -2\n" + + "Vertex: 4 distance: 1\n", bellmanFord.toString()); + } +} diff --git a/src/test/java/com/dataStructures/graphs/BreadthFirstSearchTest.java b/src/test/java/com/dataStructures/graphs/BreadthFirstSearchTest.java new file mode 100644 index 000000000000..48c5921b06bf --- /dev/null +++ b/src/test/java/com/dataStructures/graphs/BreadthFirstSearchTest.java @@ -0,0 +1,31 @@ +package com.dataStructures.graphs; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class BreadthFirstSearchTest { + + @Test + void testBreadthFirstSearch() { + BreadthFirstSearch breadthFirstSearch = new BreadthFirstSearch(4, 2); + breadthFirstSearch.addEdge(0, 1); + breadthFirstSearch.addEdge(0, 2); + breadthFirstSearch.addEdge(1, 2); + breadthFirstSearch.addEdge(2, 0); + breadthFirstSearch.addEdge(2, 3); + breadthFirstSearch.addEdge(3, 3); + Assertions.assertEquals("2 0 3 1 ", breadthFirstSearch.toString()); + + BreadthFirstSearch breadthFirstSearch2 = new BreadthFirstSearch(6, 1); + breadthFirstSearch2.addEdge(0, 2); + breadthFirstSearch2.addEdge(0,3); + breadthFirstSearch2.addEdge(0, 4); + breadthFirstSearch2.addEdge(1, 1); + breadthFirstSearch2.addEdge(1,5); + breadthFirstSearch2.addEdge(1, 3); + breadthFirstSearch2.addEdge(3, 1); + breadthFirstSearch2.addEdge(3, 3); + breadthFirstSearch2.addEdge(4, 5); + Assertions.assertEquals("1 5 3 ", breadthFirstSearch2.toString()); + } +} diff --git a/src/test/java/com/dataStructures/graphs/DepthFirstSearchTest.java b/src/test/java/com/dataStructures/graphs/DepthFirstSearchTest.java new file mode 100644 index 000000000000..1356bbcfca4a --- /dev/null +++ b/src/test/java/com/dataStructures/graphs/DepthFirstSearchTest.java @@ -0,0 +1,30 @@ +package com.dataStructures.graphs; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class DepthFirstSearchTest { + @Test + void testDepthFirstSearch() { + DepthFirstSearch depthFirstSearch = new DepthFirstSearch(4, 2); + depthFirstSearch.addEdge(0, 1); + depthFirstSearch.addEdge(0, 2); + depthFirstSearch.addEdge(1, 2); + depthFirstSearch.addEdge(2, 0); + depthFirstSearch.addEdge(2, 3); + depthFirstSearch.addEdge(3, 3); + Assertions.assertEquals("2 0 1 3 ", depthFirstSearch.toString()); + + DepthFirstSearch depthFirstSearch2 = new DepthFirstSearch(6, 1); + depthFirstSearch2.addEdge(0, 2); + depthFirstSearch2.addEdge(0,3); + depthFirstSearch2.addEdge(0, 4); + depthFirstSearch2.addEdge(1, 1); + depthFirstSearch2.addEdge(1,5); + depthFirstSearch2.addEdge(1, 3); + depthFirstSearch2.addEdge(3, 1); + depthFirstSearch2.addEdge(3, 3); + depthFirstSearch2.addEdge(4, 5); + Assertions.assertEquals("1 5 3 ", depthFirstSearch2.toString()); + } +} diff --git a/src/test/java/com/dataStructures/graphs/FloydWarshallTest.java b/src/test/java/com/dataStructures/graphs/FloydWarshallTest.java new file mode 100644 index 000000000000..e81ad24f99a9 --- /dev/null +++ b/src/test/java/com/dataStructures/graphs/FloydWarshallTest.java @@ -0,0 +1,35 @@ +package com.dataStructures.graphs; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class FloydWarshallTest { + @Test + void testFloydWarshall() { + final int INF = 99999; + int[][] graph = { + {0, 5, INF, 10}, + {INF, 0, 3, INF}, + {INF, INF, 0, 1}, + {INF, INF, INF, 0}}; + FloydWarshall floydWarshall = new FloydWarshall(graph, 4); + Assertions.assertEquals("0 5 8 9 \n" + + "INF 0 3 4 \n" + + "INF INF 0 1 \n" + + "INF INF INF 0 \n", floydWarshall.toString()); + + int[][] graph2 = { + {0, 4, 2, INF, 3}, + {INF, 0, 1, INF, INF}, + {INF, INF, 0, 6, INF}, + {INF, INF, INF, 0, 5}, + {INF, INF, INF, INF, 0}}; + FloydWarshall floydWarshall2 = new FloydWarshall(graph2, 5); + Assertions.assertEquals("0 4 2 8 3 \n" + + "INF 0 1 7 12 \n" + + "INF INF 0 6 11 \n" + + "INF INF INF 0 5 \n" + + "INF INF INF INF 0 \n", floydWarshall2.toString()); + + } +} diff --git a/src/test/java/com/dataStructures/graphs/GraphColoringTest.java b/src/test/java/com/dataStructures/graphs/GraphColoringTest.java new file mode 100644 index 000000000000..1ffad9bd0643 --- /dev/null +++ b/src/test/java/com/dataStructures/graphs/GraphColoringTest.java @@ -0,0 +1,40 @@ +package com.dataStructures.graphs; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class GraphColoringTest { + @Test + void testGraphColoring() { + GraphColoring graphColoring = new GraphColoring(5); + graphColoring.addEdge(0, 1); + graphColoring.addEdge(0, 2); + graphColoring.addEdge(1, 2); + graphColoring.addEdge(1, 3); + graphColoring.addEdge(2, 3); + graphColoring.addEdge(3, 4); + Assertions.assertEquals("Vertex 0 ---> Color 0\n" + + "Vertex 1 ---> Color 1\n" + + "Vertex 2 ---> Color 2\n" + + "Vertex 3 ---> Color 0\n" + + "Vertex 4 ---> Color 1\n", graphColoring.toString()); + + GraphColoring graphColoring2 = new GraphColoring(8); + graphColoring2.addEdge(0, 1); + graphColoring2.addEdge(0, 3); + graphColoring2.addEdge(0, 7); + graphColoring2.addEdge(3, 5); + graphColoring2.addEdge(3, 6); + graphColoring2.addEdge(2, 6); + graphColoring2.addEdge(2, 7); + graphColoring2.addEdge(4, 7); + Assertions.assertEquals("Vertex 0 ---> Color 0\n" + + "Vertex 1 ---> Color 1\n" + + "Vertex 2 ---> Color 0\n" + + "Vertex 3 ---> Color 1\n" + + "Vertex 4 ---> Color 0\n" + + "Vertex 5 ---> Color 0\n" + + "Vertex 6 ---> Color 2\n" + + "Vertex 7 ---> Color 1\n", graphColoring2.toString()); + } +} diff --git a/src/test/java/com/dataStructures/graphs/PrimMinimumSpanningTreeTest.java b/src/test/java/com/dataStructures/graphs/PrimMinimumSpanningTreeTest.java new file mode 100644 index 000000000000..55d6bf1440c4 --- /dev/null +++ b/src/test/java/com/dataStructures/graphs/PrimMinimumSpanningTreeTest.java @@ -0,0 +1,33 @@ +package com.dataStructures.graphs; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class PrimMinimumSpanningTreeTest { + + @Test + void testMinimumSpanningTree() { + int[][] graph = new int[][]{ + {0, 2, 0, 6, 0}, + {2, 0, 3, 8, 5}, + {0, 3, 0, 0, 7}, + {6, 8, 0, 0, 9}, + {0, 5, 7, 9, 0}}; + PrimMinimumSpanningTree minimumSpanningTree = new PrimMinimumSpanningTree(graph, 5); + Assertions.assertEquals("0 - 1, weight: 2\n" + + "1 - 2, weight: 3\n" + + "0 - 3, weight: 6\n" + + "1 - 4, weight: 5\n", minimumSpanningTree.toString()); + + int[][] graph2 = new int[][]{ + {0, 2, 0, 0}, + {2, 0, 8, 4}, + {0, 8, 0, 5}, + {0, 4, 5, 0}}; + PrimMinimumSpanningTree minimumSpanningTree2 = new PrimMinimumSpanningTree(graph2, 4); + Assertions.assertEquals("0 - 1, weight: 2\n" + + "3 - 2, weight: 5\n" + + "1 - 3, weight: 4\n", minimumSpanningTree2.toString()); + + } +}