Skip to content
  • Dashboard
  • Review
  • Go: plan & chat
  • Roadmap
  • Patterns & lessons49
  • Sheets
  • Problems1.5k
  • Ascend
  • Toolkit
  • Core CS + SQL
  • System Design
  • Machine Coding
  • Interview hub
  • Mock interview
  • Companies & OA
  • Leaderboard
  • Battles
  • College Cup
  • People

Keep your progress

Sync across devices with a free account.

Sign upSign in
Patterns & lessons
Sign inSign up
Patterns/Graphs/Graph BFS / DFS

Pattern

Functional / Successor Graphs

Every node has exactly one outgoing edge, so each component is a cycle with trees hanging off it; reason about tails, cycles, and k-step successors.

Learn firstGraph BFS / DFSFast & Slow Pointers
0%
mastery
0/9 solved
Practise nextCyclic Array
IdeaPracticePitfalls

Recognise it

The signal

next[i] arrays, permutations, teleporters

  1. 1Brute force

    Simulate k steps per query.

  2. 2The observation

    Step-by-step simulation: cycles repeat, and binary lifting jumps 2j steps.

  3. 3Why it works

    Following successors from any node eventually enters exactly one cycle.

Reach for it when

  • +where am I after k steps
  • +length of cycle reached from each node

Not when

  • ×out-degree > 1 (general graph)

Practice

Next

Cyclic Array

0/9 solved
  • BadgeCodeforces1000
  • Permutation GameCodeforces1300
  • Cyclic ArrayCSESMedium
  • Planets Queries ICSESMedium
  • Throw the Ball (k up to 1e12)OracleOAMedium
  • Find the Duplicate NumberMedium
  • Planets Queries IICSESHard
  • Planets CyclesCSESHard
  • Longest Cycle in a GraphHard

Pitfalls

Misconceptions

  • !treating it as a tree (there is always a cycle per component)
Go deeper1 combinations · resources

Combinations

  • Functional / Successor Graphs+Binary Lifting & LCA

    teleporters/successor after k steps; first time reaching b from a

    Functional / Successor Graphs:
    every node has one successor
    Binary Lifting & LCA:
    k-step successor in O(log k)

    Planets Queries IPlanets Queries II

Resources

  • Graph Theory playlistWilliam Fiset · video
  • Graph TraversalUSACO Guide · course
  • Disjoint Set Unioncp-algorithms · article
  • Dijkstracp-algorithms · article
  • Topological sortingcp-algorithms · article
  • Shortest paths visualizerVisuAlgo · tool
  • Graph cheatsheetTech Interview Handbook · article
TodayReviewPatternsProblems