Javatpoint Logo
Javatpoint Logo

Detect Cycle in Graph using DSU

Graphs are basic data structures that are used to depict connections or relationships between various elements. Detecting cycles in a graph is an important topic in graph theory and has applications in many domains, including computer networking, social network research, and others. The Disjoint Set Union (DSU) algorithm is one of the most efficient ways of detecting cycles in a network.

Understanding Disjoint Set Union (DSU)

It is also recognized as Union-Find, serves to organize a collection of disjoint sets through three core operations:

  • MakeSet(x): Establishes a new set containing the element x.
  • Find(x): Determines the representative (often termed parent or root) of the set to which x is associated.
  • Union(x, y): Combines the sets to which elements x and y belong.

Detecting Cycles in a Graph using DSU

The principle behind using DSU to detect cycles in a graph involves traversing through the edges of the graph and checking if adding an edge between two vertices results in a cycle. It can be achieved by using DSU to keep track of connected components.

Here's a step-by-step approach to detecting cycles in an undirected graph using DSU:

Algorithm Steps

  1. Initialize DSU for each vertex of the graph. Initially, each vertex is its parent.
  2. Iterate through each edge of the graph.
  3. For each edge (u, v):
    • Find the representative (parent) of vertices u and v using the Find operation.
    • If the representatives of u and v are the same, adding the edge (u, v) will form a cycle.
    • If the representatives are different, perform the Union operation to merge the sets of vertices u and v.



Detect Cycle in Graph using DSU


  • detect_cycle(graph): This function takes a graph represented as an adjacency list (graph) and checks if the graph contains a cycle using DSU.
  • It initializes a DSU object with the number of vertices in the graph (n).
  • It iterates through each vertex u and its adjacent vertices v in the graph. For each edge (u, v), it finds the parent of u and v using the find method of use.
  • If the parents of u and v are the same, it means they belong to the same set, and adding the edge (u, v) will create a cycle. In this case, it returns True, indicating the presence of a cycle.
  • Otherwise, it performs the union operation to merge the sets containing u and v.
  • If no cycle is found after checking all edges, it returns False.
  • It creates an example graph represented as an adjacency list where vertex 0 is connected to vertices 1 and 2, and vertex 1 is connected to vertex 2.
  • It then calls the detect_cycle function with this graph and prints whether the graph contains a cycle or not.

Time Complexity

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

  • Initialization (DSU): Initializing the DSU structure takes

O(V) time since it involves creating a parent list for each vertex.

  • Edge Iteration: Iterating through each edge in the graph takes O(E) time as each edge is processed once.
  • Find and Union Operations:

The time complexity of the find operation in DSU with path compression is nearly constant or O(α(V)), where α(V) is the inverse Ackermann function, which grows extremely slowly and is considered effectively constant for practical purposes.

The union operation with the union by rank or size also has an amortized time complexity of O(α(V)).

Hence, the overall time complexity of detecting cycles in a graph using DSU is approximately O(E⋅α(V)), where α(V) is nearly constant and E is the number of edges.

Space Complexity

  • The space complexity of the algorithm is mainly determined by the storage needed for the DSU structure.
  • The DSU structure requires O(V) space to store the parent array representing the disjoint sets.
  • Overall, the space complexity for detecting cycles using DSU is O(V), where V is the number of vertices in the graph.

Applications in the Real World

Cycle detection in graphs has practical applications:

  • Networking: Detecting loops in network topologies to avoid data packet collisions.
  • Compiler Design: Identifying and eliminating redundant loops in code.
  • Circuit Analysis: Analysing electrical circuits to find feedback loops and possible problems.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA