@@ -58,7 +58,7 @@ public List<Integer> topologicalSort() {
5858
5959 for (int i = 0 ; i < vertices ; i ++) {
6060 if (!visited [i ]) {
61- topologicalSortDFS (i , visited , stack );
61+ dfs (i , visited , stack );
6262 }
6363 }
6464
@@ -71,66 +71,87 @@ public List<Integer> topologicalSort() {
7171 }
7272
7373 /**
74- * DFS helper method to detect cycles
74+ * Recursive DFS helper method for topological sort
7575 *
7676 * @param vertex Current vertex
7777 * @param visited Visited array
78- * @param recursionStack Recursion stack to detect back edges
79- * @return true if cycle is detected, false otherwise
78+ * @param stack Stack to store the topological order
8079 */
81- private boolean hasCycleDFS (int vertex , boolean [] visited , boolean [] recursionStack ) {
80+ private void dfs (int vertex , boolean [] visited , Stack < Integer > stack ) {
8281 visited [vertex ] = true ;
83- recursionStack [vertex ] = true ;
8482
8583 for (int neighbor : adjList .get (vertex )) {
8684 if (!visited [neighbor ]) {
87- if (hasCycleDFS (neighbor , visited , recursionStack )) {
88- return true ;
89- }
90- } else if (recursionStack [neighbor ]) {
91- return true ; // Back edge found - cycle detected
85+ dfs (neighbor , visited , stack );
9286 }
9387 }
9488
95- recursionStack [vertex ] = false ;
96- return false ;
89+ stack .push (vertex );
90+ }
91+
92+ /**
93+ * Check if the graph is a Directed Acyclic Graph (DAG)
94+ *
95+ * @return true if graph is DAG, false otherwise
96+ */
97+ private boolean isDAG () {
98+ boolean [] visited = new boolean [vertices ];
99+ boolean [] recStack = new boolean [vertices ];
100+
101+ for (int i = 0 ; i < vertices ; i ++) {
102+ if (hasCycle (i , visited , recStack )) {
103+ return false ;
104+ }
105+ }
106+
107+ return true ;
97108 }
98109
99110 /**
100- * DFS helper method for topological sort
111+ * Helper method to detect cycle in the graph
101112 *
102113 * @param vertex Current vertex
103114 * @param visited Visited array
104- * @param stack Stack to store topological order
115+ * @param recStack Recursion stack to track vertices in current path
116+ * @return true if cycle is detected, false otherwise
105117 */
106- private void topologicalSortDFS (int vertex , boolean [] visited , Stack <Integer > stack ) {
118+ private boolean hasCycle (int vertex , boolean [] visited , boolean [] recStack ) {
119+ if (recStack [vertex ]) {
120+ return true ;
121+ }
122+
123+ if (visited [vertex ]) {
124+ return false ;
125+ }
126+
107127 visited [vertex ] = true ;
128+ recStack [vertex ] = true ;
108129
109130 for (int neighbor : adjList .get (vertex )) {
110- if (! visited [ neighbor ] ) {
111- topologicalSortDFS ( neighbor , visited , stack ) ;
131+ if (hasCycle ( neighbor , visited , recStack ) ) {
132+ return true ;
112133 }
113134 }
114135
115- stack .push (vertex );
136+ recStack [vertex ] = false ;
137+ return false ;
116138 }
117139
118140 /**
119- * Check if the graph is a DAG (Directed Acyclic Graph)
141+ * Get the adjacency list of the graph
120142 *
121- * @return true if graph is DAG, false otherwise
143+ * @return Adjacency list
122144 */
123- public boolean isDAG () {
124- boolean [] visited = new boolean [ vertices ] ;
125- boolean [] recursionStack = new boolean [ vertices ];
145+ public List < List < Integer >> getAdjList () {
146+ return adjList ;
147+ }
126148
127- for (int i = 0 ; i < vertices ; i ++) {
128- if (!visited [i ]) {
129- if (hasCycleDFS (i , visited , recursionStack )) {
130- return false ;
131- }
132- }
133- }
134- return true ;
149+ /**
150+ * Get the number of vertices in the graph
151+ *
152+ * @return Number of vertices
153+ */
154+ public int getVertices () {
155+ return vertices ;
135156 }
136157}
0 commit comments