-
-
Notifications
You must be signed in to change notification settings - Fork 609
/
Copy pathTopologicalSorting.java
48 lines (41 loc) · 1.33 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
package algo.graph;
import ds.graph.Graph;
import ds.graph.Vertex;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Stack;
/**
* Created by sherxon on 1/4/17.
*/
public class TopologicalSorting<T> {
private Graph<T> graph;
public TopologicalSorting(Graph<T> graph) {
this.graph = graph;
}
// 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) {
source.setVisited(true);
for (Vertex<T> vertex : source.getNeighbors()) {
if(!vertex.isVisited()){
dfs(vertex, list);
}
}
list.add(source.getValue());
}
}