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/Topological Sort

Pattern

Strongly Connected Components & Condensation

Group vertices that reach each other (Tarjan or Kosaraju) and contract them into a DAG for further DP/topological reasoning.

Learn firstGraph BFS / DFSTopological Sort
0%
mastery
0/7 solved
Practise nextReachability Queries
IdeaPracticePitfalls

Recognise it

The signal

directed graph with cycles where you need DAG reasoning

  1. 1Brute force

    Reachability from every vertex: O(V (V + E)).

  2. 2The observation

    Repeated searches: one DFS with low-links finds all SCCs.

  3. 3Why it works

    The condensation graph is a DAG.

Reach for it when

  • +mutual reachability
  • +max collected value along a path in a cyclic directed graph

Not when

  • ×undirected graph (connected components / bridges)

Practice

Next

Reachability Queries

0/7 solved
  • Reachability QueriesCSESMedium
  • Flight Routes CheckCSESMedium
  • Planets and KingdomsCSESMedium
  • Coin CollectorCSESHard
  • Giant PizzaCSESHard
  • New Flight RoutesCSESHard
  • Reachability from the CapitalCodeforces2000

Pitfalls

Misconceptions

  • !Kosaraju needs the second pass on the TRANSPOSED graph in decreasing finish order
Go deepervariants and techniques · 3 combinations · resources

Variants and techniques

  • 2-SATModel each boolean variable as two literals, each clause (a or b) as implications not a -> b and not b -> a, and solve with SCCs.

Combinations

  • Strongly Connected Components & Condensation+Topological Sort+1D / Fibonacci-style

    maximum total collected on a walk in a directed graph with cycles

    Strongly Connected Components & Condensation:
    condenses cycles into super-nodes
    Topological Sort:
    orders the condensation DAG
    1D / Fibonacci-style:
    accumulates values along DAG paths

    Coin Collector

  • Strongly Connected Components & Condensation+2-SAT

    each item chooses one of two options with pairwise 'at least one' constraints

    Strongly Connected Components & Condensation:
    finds contradictions x <-> not x
    2-SAT:
    models the clauses as implications

    Giant Pizza

  • Strongly Connected Components & Condensation+Topological Sort+Bitset Optimisation

    many 'can a reach b' queries on a general directed graph with n ~ 5e4

    Strongly Connected Components & Condensation:
    condense cycles so reachability lives on a DAG
    Topological Sort:
    an order for the DP over components
    Bitset Optimisation:
    per-component reachability sets

    Reachability Queries

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