REW

Is A Graph Cyclic?

Published Aug 29, 2025 6 min read
On this page

A graph is cyclic if it contains at least one cycle, which is a closed path that starts and ends at the same vertex.

A graph that contains no cycles is called an acyclic graph. The method for detecting a cycle differs significantly depending on whether the graph is directed or undirected.

What is a cycle?

A cycle is a path of connected vertices and edges that forms a loop. For a simple graph, a cycle has a length of at least 3, as it requires three distinct vertices to form a closed loop. In a cycle, the sequence of vertices is such that the first and last vertex are the same, while all intermediate vertices are distinct.

Detecting cycles in an undirected graph

In an undirected graph, an edge connects two vertices symmetrically. The primary challenge in cycle detection for undirected graphs is to avoid treating the simple path of an edge and its immediate reverse as a cycle. Algorithms typically address this by keeping track of the parent of the current vertex in the traversal.

Using Depth-First Search (DFS)

The DFS algorithm can be adapted for cycle detection in undirected graphs by performing a traversal and keeping track of visited vertices and their parents.

  1. Initialize: Maintain a visited array to track visited vertices and a parent variable for the current traversal.

  2. Traverse: Iterate through all vertices. If a vertex has not been visited, start a DFS traversal from it.

  3. Detect a cycle: During the DFS traversal from a source vertex u:

    • Mark u as visited.
    • For each adjacent vertex v:
      • If v is not visited, recursively call DFS on v, with u as its parent. If the recursive call finds a cycle, propagate true back up the call stack.
      • If v has been visited and is not the parent of u, a cycle is detected. This occurs when a back edge is found that links to a previously visited vertex, closing a loop.
  4. Time and Space Complexity: The time complexity is O(V+E)cap O open paren cap V plus cap E close paren

    𝑂(𝑉+𝐸)

    , as every vertex and edge is processed once. The space complexity is O(V)cap O open paren cap V close paren

    𝑂(𝑉)

    for the visited array and the recursion stack.

Using Breadth-First Search (BFS)

BFS can also detect cycles in an undirected graph using a similar parent-tracking approach.

  1. Initialize: Use a visited array and a queue to manage the traversal, storing a pair of (vertex, parent).

  2. Traverse: Iterate through all vertices. If a vertex is not visited, start a BFS from it.

  3. Detect a cycle: In the BFS traversal from a source vertex u:

    • Mark u as visited and enqueue (u, -1).
    • While the queue is not empty, dequeue (current, parent).
    • For each adjacent vertex v:
      • If v is visited and v is not the parent of current, a cycle is detected.
      • If v is not visited, mark it as visited and enqueue (v, current).
  4. Time and Space Complexity: Time complexity is O(V+E)cap O open paren cap V plus cap E close paren

    𝑂(𝑉+𝐸)

    , and space complexity is O(V)cap O open paren cap V close paren

    𝑂(𝑉)

    .

Using the Union-Find algorithm

The Union-Find (or Disjoint Set) algorithm is particularly efficient for cycle detection in undirected graphs, especially for large graphs or scenarios where edges are added incrementally.

  1. Initialize: Create a disjoint set for each vertex, where each vertex is its own parent.

  2. Iterate Edges: Process each edge (u,v)open paren u comma v close paren

    (𝑒,𝑣)

    in the graph.

  3. Detect a cycle: For each edge:

    • Find the root (or representative) of the set containing uu

      𝑒

      and the set containing vv

      𝑣

      .

    • If the roots of uu

      𝑒

      and vv

      𝑣

      are the same, they already belong to the same set, meaning adding this edge would create a cycle.

    • If the roots are different, union the two sets.

  4. Time and Space Complexity: With optimizations like Union by Rank and Path Compression, the time complexity is nearly constant for each operation, leading to a total time complexity of O(EΞ±(V))cap O open paren cap E alpha open paren cap V close paren close paren

    𝑂(𝐸𝛼(𝑉))

    , where Ξ±alpha

    𝛼

    is the inverse Ackermann function, which is highly efficient. Space complexity is O(V)cap O open paren cap V close paren

    𝑂(𝑉)

    .

Detecting cycles in a directed graph

Detecting cycles in directed graphs is different from undirected graphs because multiple paths to the same vertex don't necessarily form a cycle. A cycle is only present if there's a back edge connecting a vertex to one of its ancestors in the DFS tree.

Using DFS with a recursion stack

A DFS-based method for directed graphs uses a "recursion stack" to track the current path.

  1. Initialize visited and recursion_stack arrays to false for all vertices.

  2. Iterate through vertices. If a vertex is unvisited, start a DFS.

  3. In the DFS from vertex u: Mark u as visited and add it to the recursion_stack.

  4. For each adjacent vertex v:

    • If v is unvisited, recursively call DFS. If it returns true, a cycle is found.
    • If v is in the recursion_stack, a cycle is detected (a back edge).
  5. Remove u from the recursion_stack after visiting all neighbors.The time complexity is O(V+E)cap O open paren cap V plus cap E close paren

    𝑂(𝑉+𝐸)

    and space complexity is O(V)cap O open paren cap V close paren

    𝑂(𝑉)

    .

Using Topological Sort (Kahn's algorithm)

Topological sort orders vertices in a Directed Acyclic Graph (DAG) and is impossible for graphs with cycles.

  1. Calculate the in-degree for each vertex.

  2. Add vertices with an in-degree of 0 to a queue.

  3. While the queue is not empty, dequeue a vertex u. For each adjacent vertex v, decrement its in-degree. If v's in-degree becomes 0, enqueue it.

  4. A cycle exists if the number of vertices in the topological sort is less than the total vertices in the graph, as cycle vertices won't reach an in-degree of zero.Time complexity is O(V+E)cap O open paren cap V plus cap E close paren

    𝑂(𝑉+𝐸)

    , and space complexity is O(V)cap O open paren cap V close paren

    𝑂(𝑉)

    .

Why is cycle detection important?

Cycle detection is crucial in computer science for various applications:

  • Deadlock Detection: Identifying deadlocks in resource allocation graphs in operating systems.
  • Dependency Management: Resolving circular dependencies in systems like build tools or package managers.
  • Version Control Systems: Handling merging and branching in revision graphs.
  • Blockchain Technology: Ensuring integrity and order in DAG-based blockchains.
  • Garbage Collection: Identifying circular references for memory management.
  • Network Routing: Preventing infinite loops in network protocols.
Enjoyed this article? Share it with a friend.