1- // A Java program to print topological
2- // sorting of a DAG
3- import java .io .*;
41import java .util .*;
5-
6- // This class represents a directed graph
7- // using adjacency list representation
8- class Graph {
9- // No. of vertices
10- private int V ;
11-
12- // Adjacency List as ArrayList of ArrayList's
13- private ArrayList <ArrayList <Integer > > adj ;
14-
15- // Constructor
16- Graph (int v )
17- {
18- V = v ;
19- adj = new ArrayList <ArrayList <Integer > >(v );
20- for (int i = 0 ; i < v ; ++i )
21- adj .add (new ArrayList <Integer >());
2+ class Graph {
3+ int v ;
4+ LinkedList <Integer > adj [];
5+ @ SuppressWarnings ("unchecked" )
6+ Graph (int v ){
7+ this .v =v ;
8+ adj =new LinkedList [v ];
9+ for (int i =0 ;i <v ;i ++)
10+ adj [i ]=new LinkedList <>();
11+ }
12+ }
13+ class TopologicalSorting {
14+ public static void addEdge (Graph g ,int u ,int v ){
15+ g .adj [u ].add (v );
2216 }
23-
24- // Function to add an edge into the graph
25- void addEdge (int v , int w ) { adj .get (v ).add (w ); }
26-
27- // A recursive function used by topologicalSort
28- void topologicalSortUtil (int v , boolean visited [],
29- Stack <Integer > stack )
17+ public static void toposortutil (Graph g ,int node ,boolean visit [],Stack st )
3018 {
31- // Mark the current node as visited.
32- visited [v ] = true ;
33- Integer i ;
34-
35- // Recur for all the vertices adjacent
36- // to thisvertex
37- Iterator <Integer > it = adj .get (v ).iterator ();
38- while (it .hasNext ()) {
39- i = it .next ();
40- if (!visited [i ])
41- topologicalSortUtil (i , visited , stack );
19+ visit [node ]=true ;
20+ int i ;
21+ Iterator <Integer >it =g .adj [node ].iterator ();
22+ while (it .hasNext ())
23+ {
24+ i =it .next ();
25+ System .out .println (i );
26+ if (!visit [i ])
27+ toposortutil (g ,i ,visit ,st );
4228 }
43-
44- // Push current vertex to stack
45- // which stores result
46- stack .push (new Integer (v ));
29+ System .out .println ("node" +node );
30+ st .push (node );
4731 }
48-
49- // The function to do Topological Sort.
50- // It uses recursive topologicalSortUtil()
51- void topologicalSort ()
52- {
53- Stack <Integer > stack = new Stack <Integer >();
54-
55- // Mark all the vertices as not visited
56- boolean visited [] = new boolean [V ];
57- for (int i = 0 ; i < V ; i ++)
58- visited [i ] = false ;
59-
60- // Call the recursive helper
61- // function to store
62- // Topological Sort starting
63- // from all vertices one by one
64- for (int i = 0 ; i < V ; i ++)
65- if (visited [i ] == false )
66- topologicalSortUtil (i , visited , stack );
67-
68- // Print contents of stack
69- while (stack .empty () == false )
70- System .out .print (stack .pop () + " " );
32+ public static void toposort (Graph g ){
33+ Stack st =new Stack ();
34+ boolean visit []=new boolean [g .v ];
35+ for (int i =0 ;i <g .v ;i ++)
36+ if (visit [i ]==false )
37+ toposortutil (g ,i ,visit ,st );
38+ while (st .empty ()==false )
39+ System .out .println (st .pop ()+" " );
7140 }
72-
73- // Driver code
74- public static void main (String args [])
75- {
76- // Create a graph given in the above diagram
77- Graph g = new Graph (6 );
78- g .addEdge (5 , 2 );
79- g .addEdge (5 , 0 );
80- g .addEdge (4 , 0 );
81- g .addEdge (4 , 1 );
82- g .addEdge (2 , 3 );
83- g .addEdge (3 , 1 );
84-
85- System .out .println ("Following is a Topological "
86- + "sort of the given graph" );
87- // Function Call
88- g .topologicalSort ();
41+ public static void main (String [] args ){
42+ Graph g = new Graph (6 );
43+ TopologicalSorting tp =new TopologicalSorting ();
44+ tp .addEdge (g ,5 , 2 );
45+ tp .addEdge (g ,5 , 0 );
46+ tp .addEdge (g ,4 , 0 );
47+ tp .addEdge (g ,4 , 1 );
48+ tp .addEdge (g ,2 , 3 );
49+ tp .addEdge (g ,3 , 1 );
50+ tp .toposort (g );
8951 }
9052}
91- // This code is contributed by Aakash Hasija
53+
0 commit comments