-
Notifications
You must be signed in to change notification settings - Fork 238
/
Copy pathTopologicalSorting.java
53 lines (52 loc) · 1.22 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
import java.util.*;
class Graph{
int v;
LinkedList<Integer> adj[];
@SuppressWarnings("unchecked")
Graph(int v){
this.v=v;
adj=new LinkedList[v];
for(int i=0;i<v;i++)
adj[i]=new LinkedList<>();
}
}
class TopologicalSorting{
public static void addEdge(Graph g,int u,int v){
g.adj[u].add(v);
}
public static void toposortutil(Graph g,int node,boolean visit[],Stack st)
{
visit[node]=true;
int i;
Iterator<Integer>it=g.adj[node].iterator();
while(it.hasNext())
{
i=it.next();
System.out.println(i);
if(!visit[i])
toposortutil(g,i,visit,st);
}
System.out.println("node"+node);
st.push(node);
}
public static void toposort(Graph g){
Stack st=new Stack();
boolean visit[]=new boolean[g.v];
for(int i=0;i<g.v;i++)
if(visit[i]==false)
toposortutil(g,i,visit,st);
while(st.empty()==false)
System.out.println(st.pop()+" ");
}
public static void main(String[] args){
Graph g = new Graph(6);
TopologicalSorting tp=new TopologicalSorting();
tp.addEdge(g,5, 2);
tp.addEdge(g,5, 0);
tp.addEdge(g,4, 0);
tp.addEdge(g,4, 1);
tp.addEdge(g,2, 3);
tp.addEdge(g,3, 1);
tp.toposort(g);
}
}