logoNEURAL COOK
SearchAll PostsTagsNotesAuthor
logoNEURAL COOK

A posts created with Next.js and Tailwind.css

Explore

  • All posts
  • Notes
  • Snippets
  • Tags
  • RSS feed

Connect

instagramtwitterfacebookyoutubelinkedinmailgithub

© 2026 VIKI. All rights reserved.

Built with
Contents
ArraysBoyer-Moore Majority Vote AlgorithmKadane’s AlgorithmTriple Reverse algorithm aka Reversal Algorithm
Cycle detectionDijkstraDisjoint Set UnionGraphTopological SortTraversal algorithmsTree Traversal
Binary HeapBinomial HeapHeap
Cycle DetectionLinked ListSorting
Finding the majority elementRotate array by K steps
Finding If Number is PrimeGCD of two numbersPerforming ExponentiationPrime NumbersRandomising Data
3. Max Consecutive OnesHoare'S Partition SchemeLomuto Partition Scheme
DequesPriority QueueQueue
The Binary Search Master GuideThe Rotated Sorted Array Master Guide
Bubble SortCounting SortInsertion SortMerge SortPigeon Hole SortQuick SortSelection Sort
Monotonic StackMonotonic StackStackReversing an ArrayStackTower of Hanoi
Binary Tree TraversalCentre of gravityDiameter of TreeShortest Path
Algorithmic Pattern/graph

Cycle detection

2 min read
On this pageDetect Cycles in a Directed Graph
Detect Cycles in a Directed GraphStepsUsecaseReferences 
  • 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 AlgorithmNext→Dijkstra

On this page

Detect Cycles in a Directed GraphStepsUsecaseReferences