-
-
Notifications
You must be signed in to change notification settings - Fork 609
/
Copy pathTopologicalSorting.java
68 lines (61 loc) · 2.02 KB
/
TopologicalSorting.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package algo.graph;
import ds.graph.DirectedGraph;
import ds.graph.Graph;
import ds.graph.Vertex;
import java.util.*;
/**
* Created by sherxon on 1/4/17.
*/
public class TopologicalSorting<T> {
private Graph<T> graph;
public TopologicalSorting(Graph<T> graph) {
this.graph = graph;
}
public static void main(String[] args) {
Graph<String> graph= new DirectedGraph<>();
graph.addVertex("a");
graph.addVertex("b");
graph.addVertex("c");
graph.addVertex("d");
graph.addVertex("e");
graph.addVertex("f");
graph.addVertex("g");
graph.addVertex("q");
graph.addVertex("p");
graph.addEdge("a", "b");
graph.addEdge("a", "d");
graph.addEdge("a", "c");
graph.addEdge("b", "f");
graph.addEdge("c", "e");
graph.addEdge("e", "g");
graph.addEdge("q", "p");
TopologicalSorting<String> t= new TopologicalSorting<>(graph);
List<String> list=t.topSort();
System.out.println(list);
}
// this works with DAG Only
// first we will choose any vertex who who does not have incoming edges (sources)
// sources can be found easier if incoming edge count is recorded in each vertex
List<T> topSort(){
Stack<T> stack=new Stack<>();//stack is also good option
Set<Vertex<T>> sources=new HashSet<>();
for (Vertex<T> vertex : graph.getVertices())
sources.add(vertex);
for (Vertex<T> vertex : graph.getVertices())
for (Vertex<T> tVertex : vertex.getNeighbors())
sources.remove(tVertex);
for (Vertex<T> source : sources)
if(!source.isVisited())
dfs(source, stack);
return stack;
}
private void dfs(Vertex<T> source, List<T> list) {
list.add(source.getValue());
source.setVisited(true);
for (Vertex<T> vertex : source.getNeighbors()) {
if(!vertex.isVisited()){
dfs(vertex, list);
}
}
}
}