πDay 4. Undirected Graph Cycle π§
The problem can be found at the following link: Question Link
π‘ Problem Description:
Given an undirected graph with V vertices and E edges, represented as a 2D vector edges[][], where each entry edges[i] = [u, v] denotes an edge between vertices u and v, determine whether the graph contains a cycle or not.
Note: Sorry for uploading late, my exam is going on.
π Example Walkthrough:
Example 1:
Input:
V = 4, E = 4
edges = [[0, 1], [0, 2], [1, 2], [2, 3]]Output:
TrueExplanation:
There exists a cycle: 1 β 2 β 0 β 1
Example 2:
Input:
Output:
Explanation:
No cycle exists in the given graph.
Constraints:
$1 \leq V \leq 10^5$
$1 \leq E = \text{edges.size()} \leq 10^5$
π― My Approach:
Optimized Approach: Union-Find with Path Compression
Algorithm Steps:
Initialize a parent array where each node is its own parent (
-1).Find function with path compression to efficiently determine the representative parent of each node.
Union function with union by size to merge sets efficiently.
Iterate over all edges and apply union-find:
If both nodes of an edge belong to the same set, a cycle is detected.
Otherwise, perform a union operation to merge them.
Return true if a cycle is found, otherwise return false.
π Time and Auxiliary Space Complexity
Expected Time Complexity:
O(E * Ξ±(V)), whereΞ±(V)is the inverse Ackermann function, which grows extremely slowly and is nearly constant.Expected Auxiliary Space Complexity:
O(V), as we store only the parent array.
π Solution Code
Code (C++)
β‘ Alternative Approaches
π 1οΈβ£ DFS Based Cycle Detection Approach
Algorithm Steps:
Build an adjacency list from the edge list.
Use a DFS traversal to detect a cycle by marking visited nodes and ensuring that a visited neighbor (other than the parent) is detected.
π Complexity Analysis:
Time Complexity:
O(V + E)Space Complexity:
O(V + E)
β Why This Approach?
This DFS approach is intuitive for many graph problems. It clearly separates the concept of a visited node and its parent, making it straightforward to detect back edges that indicate cycles.
π 2οΈβ£ Union-Find (Iterative Path Compression) Approach
Algorithm Steps:
Initialize a parent array with
-1for all vertices.For each edge, find the roots iteratively.
Merge the sets if roots are different; if not, a cycle exists.
π Complexity Analysis:
Time Complexity:
O(E * Ξ±(V))(Ξ± is the inverse Ackermann function)Space Complexity:
O(V)
β Why This Approach?
The Union-Find structure with iterative path compression is highly efficient for cycle detection when the graph is provided as an edge list.
π Comparison of Approaches
Approach
β±οΈ Time Complexity
ποΈ Space Complexity
β Pros
β οΈ Cons
Union-Find (Recursive Path Compression)
π’ O(E * Ξ±(V))
π‘ O(V)
Efficient for edge-list input, compact code.
Recursion depth may be an issue for large graphs.
DFS-Based Cycle Detection (Adjacency List)
π’ O(V + E)
π‘ O(V)
Simple, intuitive cycle detection method.
Requires adjacency list construction.
Union-Find (Iterative Path Compression)
π’ O(E * Ξ±(V))
π‘ O(V)
Avoids recursion, better readability.
Slightly more verbose than recursive version.
β Best Choice?
Union-Find (Recursive Path Compression) is the most efficient when edges are given as an edge list.
Union-Find (Iterative Path Compression) is preferred when recursion is not suitable.
DFS-Based Cycle Detection is best when the graph is naturally stored as an adjacency list.
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