Javatpoint Logo
Javatpoint Logo

Detect Cycle in Graph using DSU

Introduction:

Graphs are a fundamental data structure used in computer science to model relationships between objects. One common problem associated with graphs is cycle detection, which involves determining whether there is a closed path (cycle) within the graph. Cycle detection is crucial in various applications:

  • Network routing
  • Deadlock detection
  • Topological sorting

Disjoint-Set Union (DSU) is a powerful algorithmic technique that can be employed to efficiently detect cycles in a graph.

Understanding Disjoint-Set Union (DSU):

Disjoint-Set Union, also known as Union-Find, is a data structure and algorithm used to maintain a collection of disjoint sets. It supports two main operations:

  • Union: Merge two sets into one.
  • Find: Determine which set a particular element belongs to.

DSU employs the concept of representative elements to efficiently perform these operations. Each set has a representative element that serves as its identifier. By maintaining parent pointers and ranks, DSU ensures that these operations can be performed with an amortized time complexity of O(α(n)), where α is the extremely slow-growing inverse Ackermann function.

Detecting Cycles in Graph using DSU:

The process of detecting cycles in an undirected graph using DSU involves traversing through each edge and checking if the two vertices it connects belong to the same subset. If they do, adding that edge would create a cycle. Otherwise, we perform the union operation on the two subsets corresponding to the vertices of the edge.

Here's a step-by-step algorithm to detect cycles using DSU:

  1. Initialize DSU with each vertex as a separate subset.
  2. For each edge (u, v) in the graph:
    • Find the representative (root) of the subset containing vertex u.
    • Find the representative (root) of the subset containing vertex v.
    • If both vertices belong to the same subset, there is a cycle.
    • Otherwise, perform the union operation on the two subsets.
    • Repeat step 2 for all edges.
  3. If at any point during the traversal, a cycle is detected, halt and return "Cycle detected".
  4. If the traversal completes without detecting a cycle, return "No cycle detected".

Illustration:

Imagine a scenario where you have a group of cities connected by roads. Each city represents a vertex in a graph, and the roads between them represent edges. Now, suppose you want to determine if there's a way to start from one city and follow the roads such that you eventually return to the same city without traversing any road twice. This situation represents a cycle in the graph.

Consider the following cities and roads:

Detect Cycle in Graph using DSU

In this graph:

  • City A is connected to cities B, C, and D.
  • City B is connected to cities A and E.
  • City C is connected to cities A and D.
  • City D is connected to cities A, C, and E.
  • City E is connected to cities B and D.

In this illustration, there's a cycle: A -> B -> E -> D -> A.

Implementation:

Explanation:

  • UnionFind class implements the Union-Find data structure, which is used to efficiently perform operations like finding the root of an element and merging sets.
  • It maintains a vector called parent where each element represents the parent node of a particular element. Initially, all elements are considered as individual disjoint sets, so each element's parent is set to -1.
  • Graph class represents an undirected graph. It stores the number of vertices and a vector of edges. The addEdge() method is used to add edges to the graph.
  • The isCyclic() method checks whether the graph contains a cycle or not. It initializes a UnionFindinstance with the number of vertices. Then, it iterates over each edge in the graph.
  • For each edge, it finds the root nodes of the vertices connected by that edge using the UnionFind data structure.
  • If both vertices share the same root, it means they are already in the same connected component, indicating the existence of a cycle.
  • If not, it merges the two sets by calling unionSets(). After processing all edges, if no cycles are detected, it returns false; otherwise, it returns true.
  • In the main() function, a sample graph with 5 vertices is created. Edges are added to the graph using the addEdge() Then, the isCyclic() method is called to check if the graph contains a cycle or not.

Program Output:

Detect Cycle in Graph using DSU

Time and Space Complexity Analysis:

Let V be the number of vertices and E be the number of edges in the graph.

Time Complexity

  1. Initializing DSU: O(V)
  2. Iterating through all edges: O(E)
  3. Finding and union operations: O(α(V)), where α is the inverse Ackermann function, which grows extremely slowly.

Overall, the time complexity is dominated by the processing of edges, so the time complexity is O(E α(V)).

Space Complexity

The space complexity is determined by the DSU data structure, which requires O(V) space to store the parent array representing the sets.

Therefore, the space complexity is O(V).

Advantages of Using DSU:

  • Efficiency: DSU offers efficient union and find operations, which are crucial for cycle detection.
  • Ease of Implementation: The DSU algorithm for cycle detection is relatively straightforward to implement and understand.
  • Applicability: DSU can be used not only for cycle detection but also for other operations like connected components, minimum spanning tree algorithms, and more.

Limitations and Considerations:

  • Undirected Graphs Only: The DSU-based cycle detection algorithm discussed here is specifically for undirected graphs. For directed graphs, other algorithms like Depth-First Search (DFS) or Breadth-First Search (BFS) are commonly used.
  • Edge Weight: This implementation does not consider edge weights. If edge weights are present and need to be considered in cycle detection, a different approach would be required.
  • Disconnected Components: This implementation assumes that the graph is connected. If the graph consists of multiple disconnected components, additional handling would be needed.

Conclusion:

In conclusion, the use of Disjoint-Set Union (DSU) in detecting cycles within graphs presents a powerful and efficient approach to solving this fundamental problem in computer science. By employing DSU, we can determine whether a graph contains cycles with a time complexity of O(Eα(V)), where α is the inverse Ackermann function, and E represents the number of edges and V denotes the number of vertices in the graph. This complexity makes DSU an attractive solution for detecting cycles in large graphs efficiently.

Furthermore, DSU offers a versatile and straightforward implementation that lends itself well to various types of graphs, including undirected and directed graphs. Its simplicity in both implementation and understanding makes it accessible to a wide range of programmers and researchers, contributing to its popularity in graph theory and algorithm design.

Moreover, the union-find operations provided by DSU enable us to efficiently merge disjoint sets and determine whether two vertices belong to the same set, making it particularly well-suited for cycle detection tasks. This capability allows us to traverse the graph and detect cycles dynamically as edges are added, providing a dynamic and scalable solution to the cycle detection problem.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA




news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news