Created
July 26, 2019 01:58
-
-
Save dfparker2002/baac6c8ab82f7ecd6e9dcefe465b4046 to your computer and use it in GitHub Desktop.
Directed Graph
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src: https://github.com/eugenp/tutorials/blob/5b5733239c06b595649324d744e97c6a2dc97d68/data-structures/src/main/java/com/baeldung/graph/Graph.java | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Stack; | |
public class Graph { | |
private List<List<Integer>> neighbours; | |
private int n; | |
public Graph(int n) { | |
this.n = n; | |
this.neighbours = new ArrayList<List<Integer>>(n); | |
for (int i = 0; i < n; i++) { | |
this.neighbours.add(new ArrayList<Integer>()); | |
} | |
} | |
public void addEdge(int src, int dest) { | |
this.neighbours.get(src) | |
.add(dest); | |
} | |
public void dfsWithoutRecursion(int start) { | |
Stack<Integer> stack = new Stack<Integer>(); | |
boolean[] isVisited = new boolean[n]; | |
stack.push(start); | |
while (!stack.isEmpty()) { | |
int current = stack.pop(); | |
isVisited[current] = true; | |
System.out.print(" " + current); | |
for (int dest : neighbours.get(current)) { | |
if (!isVisited[dest]) | |
stack.push(dest); | |
} | |
} | |
} | |
public void dfs(int start) { | |
boolean[] isVisited = new boolean[n]; | |
dfsRecursive(start, isVisited); | |
} | |
private void dfsRecursive(int current, boolean[] isVisited) { | |
isVisited[current] = true; | |
System.out.print(" " + current); | |
for (int dest : neighbours.get(current)) { | |
if (!isVisited[dest]) | |
dfsRecursive(dest, isVisited); | |
} | |
} | |
public void topologicalSort(int start) { | |
Stack<Integer> result = new Stack<Integer>(); | |
boolean[] isVisited = new boolean[n]; | |
topologicalSortRecursive(start, isVisited, result); | |
while (!result.isEmpty()) { | |
System.out.print(" " + result.pop()); | |
} | |
} | |
private void topologicalSortRecursive(int current, boolean[] isVisited, Stack<Integer> result) { | |
isVisited[current] = true; | |
for (int dest : neighbours.get(current)) { | |
if (!isVisited[dest]) | |
topologicalSortRecursive(dest, isVisited, result); | |
} | |
result.push(current); | |
} | |
} | |
////////// | |
//src: https://github.com/eugenp/tutorials/blob/5b5733239c06b595649324d744e97c6a2dc97d68/data-structures/src/test/java/com/baeldung/graph/GraphUnitTest.java | |
import org.junit.Test; | |
public class GraphUnitTest { | |
@Test | |
public void givenDirectedGraph_whenDFS_thenPrintAllValues() { | |
Graph graph = createDirectedGraph(); | |
graph.dfs(0); | |
System.out.println(); | |
graph.dfsWithoutRecursion(0); | |
} | |
@Test | |
public void givenDirectedGraph_whenGetTopologicalSort_thenPrintValuesSorted() { | |
Graph graph = createDirectedGraph(); | |
graph.topologicalSort(0); | |
} | |
private Graph createDirectedGraph() { | |
Graph graph = new Graph(6); | |
graph.addEdge(0, 1); | |
graph.addEdge(0, 2); | |
graph.addEdge(1, 3); | |
graph.addEdge(2, 3); | |
graph.addEdge(3, 4); | |
graph.addEdge(4, 5); | |
return graph; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment