logoNEURAL COOK
SearchAll PostsTagsNotesAuthor
instagramtwitterfacebookyoutubelinkedinmailgithubrss
Copyright © 2026 All rights reserved.
•
VIKI
Built with Vikramaditya Singh
Built with
VIKI - A posts created with Next.js and Tailwind.css
About
arrays
Arrays
Boyer-Moore Majority Vote Algorithm
Kadane’s Algorithm
Triple Reverse algorithm aka Reversal Algorithm
graph
Cycle detection
Graph
Topological Sort
Tree Traversal
heap
Binary Heap
Binomial Heap
Heap
linked-list
Cycle Detection
Linked List
Sorting
miscellaneous
Finding the majority element
Rotate array by K steps
numerical-algorithms
Finding If Number is Prime
GCD of two numbers
Performing Exponentiation
Prime Numbers
Randomising Data
partitioning
3. Max Consecutive Ones
Hoare'S Partition Scheme
Lomuto Partition Scheme
queue
Deques
Priority Queue
Queue
search
The Binary Search Master Guide
The Rotated Sorted Array Master Guide
sort
Bubble Sort
Counting Sort
Sorting
Insertion Sort
Merge Sort
Pigeon Hole Sort
Quick Sort
Selection Sort
stack
Monotonic Stack
Monotonic Stack
Stack
Reversing an Array
Stack
Tower of Hanoi
tree
Binary Tree Traversal
Centre of gravity
Diameter of Tree
Shortest Path
Algorithmic Pattern›graph

Cycle detection

  • Graphs without cycles are called acyclic, and those with cycles are called cyclic.

# Detect Cycles in a Directed Graph

  • A sequence of vertices and directed edges in which the last vertex is connected back to the first vertex, forming a closed loop.
  • There is a cycle in a graph only if there is a back edge

# Steps

  • Track the visited vertices and the vertices in current path.
  • Iterate over all the vertex, if vertex is already visited skip it.
  • For selected verted explore as far as possible along each branch before backtracking.
  • If we encounter a vertex that has already been visited then return.
    • Since this vertex is already visited it means the paths beginning from this vertex have been traverssed once and since we are still searching for a cycle the paths originating from this vertex don't have a cycle.
  • If we encounter a vertex is part of the current path, we have found a cycle

# Usecase

  • Real world usecase are
    • Deadlock detection in concurrent systems

      Detecting cycles in the resource allocation graph is a common technique to identify deadlocks in a system.

    • Dependency management

      Detecting cycles in the dependency graph helps in identifying circular dependencies, which can cause issues during the build process.

    • Infinite loop detection in computer programs

      detecting cycles in the control flow graph of a program can help in identifying infinite loops.
      \

# References

  • Cycle detection from book "Competitive Programmer’s Handbook"
  • Detect Cycles in a Directed Graph

\

PreviousTriple Reverse algorithm aka Reversal AlgorithmNextGraph
CONTENTS
Detect Cycles in a Directed GraphStepsUsecaseReferences