πDay 7. Directed Graph Cycle π§
The problem can be found at the following link: Question Link
Note: Sorry for uploading late, my Final Sem exam is going on.
π‘ Problem Description:
Given a Directed Graph with V vertices (numbered from 0 to V-1) and E edges, determine whether the graph contains any cycle.
The graph is represented as a 2D vector edges[][], where each entry edges[i] = [u, v] denotes an edge from vertex u to v.
π Example Walkthrough:
Example 1:
Input:
V = 4, edges[][] = [[0, 1], [1, 2], [2, 3], [3, 3]]Output:
trueExplanation:
There is a self-loop at vertex 3 (3 -> 3) which forms a cycle.
Example 2:
Input:
Output:
Explanation:
There is no cycle in the graph.
π― My Approach:
Kahnβs Algorithm (BFS-based Cycle Detection)
Build the Graph and Compute In-Degrees:
Convert the edge list into an adjacency list.
Compute the in-degree for each vertex.
Initialize a Queue:
Add all vertices with zero in-degree into a queue.
Process Vertices:
While the queue is not empty, remove a vertex and decrement the in-degree of its neighbors.
If any neighborβs in-degree becomes zero, add it to the queue.
Count the number of vertices processed.
Cycle Check:
If the count of processed vertices is not equal to V, a cycle exists.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(V + E), since each vertex and edge is processed once.
Expected Auxiliary Space Complexity: O(V + E), due to the in-degree array, adjacency list, and queue.
π Solution Code
Code (C)
Code (C++)
β‘ Alternative Approaches
π 2οΈβ£ DFS-Based Cycle Detection (Recursive)
Algorithm Steps:
Build an adjacency list from the edge list.
Use two arrays:
visited(to mark processed nodes) andrecStack(to track nodes in the current DFS recursion).For each unvisited node, run a DFS.
If you reach a node that is already in the recursion stack, a cycle exists.
π Complexity Analysis:
Time Complexity:
O(V + E)Space Complexity:
O(V)for recursion stack and visited tracking
β Why This Approach?
This DFS-based method is intuitive for cycle detection and often simpler to implement, but may face recursion depth issues for very deep graphs.
π 3οΈβ£ DFS-Based Cycle Detection (Iterative using Stack)
Algorithm Steps:
Convert the edge list into an adjacency list.
Instead of recursion, use an explicit stack along with two marker arrays to track visited nodes and nodes currently in the stack.
Check for back edges indicating a cycle.
π Complexity Analysis:
Time Complexity:
O(V + E)Space Complexity:
O(V)
β Why This Approach?
It avoids recursion by using an explicit stack, which is useful in environments with limited recursion depth, though the logic can be slightly more complex.
π Comparison of Approaches
Approach
β±οΈ Time Complexity
ποΈ Space Complexity
β Pros
β οΈ Cons
Kahnβs Algorithm (BFS)
π’ O(V + E)
π‘ O(V + E)
Iterative; detects cycles via topological order
Requires extra space for the in-degree array
DFS (Recursive)
π’ O(V + E)
π’ O(V)
Simple; intuitive for cycle detection
Risk of stack overflow on deep graphs
DFS (Iterative using Stack)
π’ O(V + E)
π’ O(V)
Avoids recursion; explicit stack control
Slightly more complex to implement
β Best Choice?
Use Kahnβs Algorithm when you also need topological ordering or a fully iterative solution.
Use DFS (Recursive) for simpler implementations on smaller or moderately sized graphs.
Use DFS (Iterative) in environments where recursion depth is a concern.
Code (Java)
Code (Python)
π― Contribution and Support:
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated