Chapter 5: Graph Traversal

Algorithms
Kinds of graphsRepresentationDepth-first search (DFS)Breadth-first search (BFS) Mazes and shortest pathsTarjan SCCTopological sortComplexity
Sercan KÜLCÜ · Giresun University · Computer Engineering

Introduction

Learning Outcomes

Graph basics

Tell apart directed, undirected, weighted, bipartite and complete graphs and trees; store a graph as an adjacency list or matrix.

DFS

Write DFS recursively and with a stack, trace the visit order step by step, and explain backtracking.

BFS

Write BFS with a queue; apply levels, parents and unweighted shortest paths to a maze.

Tarjan and analysis

Find strongly connected components with index / lowlink values; justify the O(V + E) running time of every traversal.

Introduction

Graphs Are Everywhere

  • The world is a network: cities are linked by roads, people by relationships, computers by cables.
  • A graph G = (V, E) consists of vertices V and the edges E that join them.
  • Graph algorithms explore and make sense of these networks: shortest paths, clustering, connectivity, flow…
  • Uses: social networks, maps and navigation, network routing, compilers, games.
OrduGiresunTrabzonSamsunSivasErzurum

Cities are vertices, roads are edges.

V = number of vertices, E = number of edges. Running times are stated in terms of these two: O(V + E), O(V²)…

Introduction

Kinds of Graphs

ABCDDirected

Edges are traversed only in the direction of the arrow.

ABCDUndirected

Edges can be traversed both ways.

8312ABCDWeighted

Each edge carries a weight (cost, distance).

Bipartite

Edges only join vertices in different sets.

ADEBCCyclic

Some path returns to its start, such as A–D–C–B–A.

ADEBCTree

Connected and acyclic; exactly one path between any two vertices.

ABCDComplete

Every pair of vertices is joined: n(n−1)/2 edges.

ABNot simple

Has parallel edges and self-loops; a simple graph has neither.

Introduction

Families of Graph Algorithms

1 · Graph traversal

DFS goes deep from a vertex; BFS sweeps the network level by level.

2 · Shortest paths

Dijkstra: shortest paths from a source to every other vertex.

3 · Minimum spanning tree (MST)

Kruskal: picks the edges that connect the network at minimum total cost.

4 · Network flow

The maximum flow that can be sent from source to sink in a capacitated network.

AlgorithmEveryday example
DFSEscaping a maze: follow one corridor to its end, then back up
BFSNews spreading through a city: first close friends, then their friends
DijkstraFinding the fastest delivery route for a courier

Introduction

Storing a Graph

0123

V = 4, E = 4

Adjacency list

0: 1, 2
1: 0, 2
2: 0, 1, 3
3: 2

Adjacency matrix

0123
00110
11010
21101
30010
Adjacency listAdjacency matrix
MemoryO(V + E)O(V²)
All neighbors of uO(deg(u))O(V)
Is (u, v) an edge?O(deg(u))O(1)
Traversal (DFS / BFS)O(V + E)O(V²)

The Java code in this chapter uses adjacency lists: List<Integer>[] adj.

Traversal

Graph Traversal

  • A traversal visits every (reachable) vertex exactly once to explore the graph's structure systematically.
  • There are two main strategies; they differ in which data structure supplies the next vertex:
Depth-First Search (DFS)

Uses a stack (LIFO). It starts at a vertex and goes as deep as it can; when the neighbors run out it backtracks.

3203pushpop← top
Breadth-First Search (BFS)

Uses a queue (FIFO). It visits vertices at distance 1, then distance 2, … level by level.

0257dequeueenqueuefrontback
A visited array is essential: unlike a tree, a graph can have cycles. Without marking visited[], the algorithm circles the same cycle forever.

DFS

DFS: The Idea

  • In a maze, follow a corridor until it dead-ends; then back up to the last fork and try a corridor you have not taken yet.
  • In the recursive version the call stack remembers where to back up to; the iterative version uses an explicit stack.
  • The visited vertices, with the edges that first reached them, form a DFS tree.
  • The visit order depends on the order of neighbors; one graph can have several valid DFS orders.
Vertex states

white: not yet discovered
yellow: discovered, still in progress (on the stack)
dark: finished, all neighbors examined

abcdefon stackfinishedcurrentundiscovered
DFS answers: "Is it reachable?", "Is there a cycle?", "How many components?", "In what order?" (topological sort), "What are the strongly connected components?" (Tarjan).

DFS · Step by step

DFS Step by Step: Visit Order

Stack (recursion): empty

Start: no vertex is visited yet. DFS starts at the root; each vertex gets its visit number.

1Stack (recursion): 1

Visit the root (1). Pick its first neighbor and go deeper.

12Stack (recursion): 1 → 2

Descend to the first undiscovered neighbor of 1: 2. The stack grows.

123Stack (recursion): 1 → 2 → 3

Descend to the first undiscovered neighbor of 2: 3. The stack grows.

1234Stack (recursion): 1 → 2 → 3 → 4

Descend to the first undiscovered neighbor of 3: 4. The stack grows.

12534Stack (recursion): 1 → 5

4, 3 and 2 have no undiscovered neighbors left → backtrack. Visit the next child of 1: 5.

125364Stack (recursion): 1 → 5 → 6

Descend to the first undiscovered neighbor of 5: 6. The stack grows.

1253647Stack (recursion): 1 → 5 → 6 → 7

Descend to the first undiscovered neighbor of 6: 7. The stack grows.

12536847Stack (recursion): 1 → 5 → 8

7 and 6 have no undiscovered neighbors left → backtrack. Visit the next child of 5: 8.

125936847Stack (recursion): 1 → 9

8 and 5 have no undiscovered neighbors left → backtrack. Visit the next child of 1: 9.

12593681047Stack (recursion): 1 → 9 → 10

Descend to the first undiscovered neighbor of 9: 10. The stack grows.

12593681047Stack (recursion): empty

All vertices visited; the stack is empty. Visit order: 1, 2, 3, …, 10: each branch was followed to its end before backtracking.

DFS

Recursive DFS: Pseudocode

Standard pseudocode

procedure DFS(G, v) is
  label v as discovered
  for all directed edges from v to w
      that are in G.adjacentEdges(v) do
    if vertex w is not labeled
        as discovered then
      call DFS(G, w)

Plain version

DFS(node, visited):
  if node is not
        in visited:
    add node to visited
    process node (e.g., print it)
    for each neighbor of node:
      DFS(neighbor, visited)
  • The left version checks a neighbor before calling; the right one checks at the start of the call. Both give the same order; the right one makes extra calls that return at once.
  • To cover every vertex of a disconnected graph, restart DFS from each unvisited vertex (each restart finds one connected component).

DFS

Iterative DFS: Pseudocode

Standard pseudocode

procedure DFS_iterative(G, v) is
  let S be a stack
  S.push(v)
  while S is not empty do
    v = S.pop()
    if v is not labeled as discovered then
      label v as discovered
      for all edges from v to w
          in G.adjacentEdges(v) do
        S.push(w)

Plain version

DFS(start):
  create an empty stack
  push start onto the stack
  while the stack is not empty:
    current = pop the stack
    if current is not
          in visited:
      add current to visited
      for each neighbor of current:
        push neighbor onto the stack
  • An explicit stack instead of recursion: no risk of StackOverflowError on very deep graphs.
  • Marking happens on pop, so a vertex can enter the stack several times (stack size O(E)).
  • Since neighbors are pushed, the last one pushed comes out first: the order is the reverse of the recursive version. To match it, push the neighbors in reverse.

DFS · Step through the code

Recursive DFS: Tracing dfs(0)

01273456

Directed example: 0→1, 0→2, 0→7, 1→2, 2→0, 2→3, 2→4, 3→5, 4→6, 5→6. dfs(0): mark 0 and print it.

01273456

Neighbor 1 of 0 is unvisited → dfs(1): mark it, print it. A new frame is pushed.

01273456

Neighbor 2 of 1 is unvisited → dfs(2): mark it, print it. A new frame is pushed.

01273456

Neighbor 0 of 2 is already visited → skip it (no going around the cycle).

01273456

Neighbor 3 of 2 is unvisited → dfs(3): mark it, print it. A new frame is pushed.

01273456

Neighbor 5 of 3 is unvisited → dfs(5): mark it, print it. A new frame is pushed.

01273456

Neighbor 6 of 5 is unvisited → dfs(6): mark it, print it. A new frame is pushed.

01273456

6, 5 and 3 are finished (no neighbors left) → their calls return and control comes back to 2: backtracking.

01273456

Neighbor 4 of 2 is unvisited → dfs(4): mark it, print it. A new frame is pushed.

01273456

Neighbor 6 of 4 is already visited → skip it (no going around the cycle).

01273456

4, 2 and 1 are finished (no neighbors left) → their calls return and control comes back to 0: backtracking.

01273456

Neighbor 2 of 0 is already visited → skip it (no going around the cycle).

01273456

Neighbor 7 of 0 is unvisited → dfs(7): mark it, print it. A new frame is pushed.

01273456

7 and 0 are finished; dfs(0) returns. Output: 0 1 2 3 5 6 4 7. The bold edges form the DFS tree.

void dfs(int v, boolean[] visited) {
  visited[v] = true;
  System.out.print(v + " ");
  for (int w : adj[v])
    if (!visited[w])
      dfs(w, visited);
}

DFS · Step through the code

Stack DFS: Tracing dfsStack(0)

01273456stack (bottom → top)0

Push the start vertex 0. No vertex is marked yet.

01273456stack (bottom → top)127

Pop 0: mark and print; push 1, 2, 7 → the last one pushed, 7, is on top.

01273456stack (bottom → top)12

Pop 7: mark and print; no neighbors, nothing is pushed.

01273456stack (bottom → top)1034

Pop 2: mark and print; push 0, 3, 4 → the last one pushed, 4, is on top.

01273456stack (bottom → top)1036

Pop 4: mark and print; push 6 → the last one pushed, 6, is on top.

01273456stack (bottom → top)103

Pop 6: mark and print; no neighbors, nothing is pushed.

01273456stack (bottom → top)105

Pop 3: mark and print; push 5 → the last one pushed, 5, is on top.

01273456stack (bottom → top)106

Pop 5: mark and print; push 6 → the last one pushed, 6, is on top.

01273456stack (bottom → top)10

Pop 6, but it is already visited → do nothing. (A vertex can be pushed more than once.)

01273456stack (bottom → top)1

Pop 0, but it is already visited → do nothing. (A vertex can be pushed more than once.)

01273456stack (bottom → top)2

Pop 1: mark and print; push 2 → the last one pushed, 2, is on top.

01273456stack (bottom → top)

Pop 2, but it is already visited → do nothing. (A vertex can be pushed more than once.)

01273456stack (bottom → top)

The stack is empty and the loop ends. Output: 0 7 2 4 6 3 5 1, unlike the recursive order (0 1 2 3 5 6 4 7), because neighbors come out in reverse.

void dfsStack(int s) {
  boolean[] visited = new boolean[n];
  Deque<Integer> stack = new ArrayDeque<>();
  stack.push(s);
  while (!stack.isEmpty()) {
    int v = stack.pop();
    if (!visited[v]) {
      visited[v] = true;
      System.out.print(v + " ");
      for (int w : adj[v])
        stack.push(w);
    }
  }
}

DFS

DFS: Analysis and Uses

Complexity

  • Each vertex is marked once: O(V).
  • Each adjacency list is scanned once; the lists have total length E (2E if undirected): O(E).
  • Total: O(V + E) with adjacency lists, O(V²) with a matrix (a row of V entries per vertex).
  • Extra memory: visited[] O(V) plus the stack: O(V) for recursion (the longest path); O(E) for the "mark on pop" version.

Where is it used?

  • Reachability, counting connected components
  • Cycle detection: an edge to a vertex on the stack (yellow) = a back edge
  • Topological sort: reverse finishing order
  • Strongly connected components (Tarjan, Kosaraju)
  • Bridges and articulation points, solving mazes and puzzles, backtracking
DFS does not find shortest paths: the first path it finds to a target may be a long detour. For the fewest edges you need BFS.

BFS

BFS: The Idea

  • Like ripples from a stone in water: first everything 1 edge away, then everything 2 edges away…
  • It models news spreading: a person tells all their friends, who tell their own friends.
  • The queue (FIFO) keeps the order: first discovered, first processed.
  • A vertex is marked when it is enqueued, so each vertex enters the queue at most once.
  • In an unweighted graph it finds the path with the fewest edges from the start to every vertex.
Levels

L0 = {s}
L1 = neighbors of s
L2 = new neighbors of L1
…
Every vertex in Lk is at distance exactly k from s.

L1L2s111222222

BFS · Step by step

BFS Step by Step: Level by Level

12675384queueprocessed: —

Start: all vertices are white (undiscovered). Yellow = discovered, waiting in the queue · dark = processed.

12675384queue1processed: —

The start vertex 1 is marked discovered and enqueued.

12675384queue2345processed: 1

Dequeue 1; its undiscovered neighbors 2, 3, 4, 5 are marked and enqueued (level 1).

12675384queue34567processed: 1 2

Dequeue 2; its undiscovered neighbors 6, 7 are marked and enqueued (level 2).

12675384queue45678processed: 1 2 3

Dequeue 3; its undiscovered neighbor 8 is marked and enqueued (level 2).

12675384queue5678processed: 1 2 3 4

Dequeue 4; no undiscovered neighbors.

12675384queue678processed: 1 2 3 4 5

Dequeue 5; no undiscovered neighbors.

12675384queue78processed: 1 2 3 4 5 6

Dequeue 6; no undiscovered neighbors.

12675384queue8processed: 1 2 3 4 5 6 7

Dequeue 7; no undiscovered neighbors.

12675384queueprocessed: 1 2 3 4 5 6 7 8

Dequeue 8; no undiscovered neighbors → the queue is empty and BFS ends.

12675384queueprocessed: 1 2 3 4 5 6 7 8

Result: visit order 1, 2, 3, 4, 5, 6, 7, 8. First the vertices at distance 1 (2, 3, 4, 5), then those at distance 2 (6, 7, 8): level by level.

BFS

BFS: Pseudocode

Standard pseudocode

procedure BFS(G, root) is
  let Q be a queue
  label root as explored
  Q.enqueue(root)
  while Q is not empty do
    v := Q.dequeue()
    for all edges from v to w
        in G.adjacentEdges(v) do
      if w is not labeled as explored then
        label w as explored
        w.parent := v
        Q.enqueue(w)

Plain version

BFS(start):
  create an empty queue
  mark start as discovered
  enqueue start
  while the queue is not empty:
    current = dequeue
    for each neighbor of current
      if neighbor is undiscovered
        mark neighbor as discovered
        neighbor.parent = current
        enqueue neighbor
  • The w.parent := v links form the BFS tree; following them from the target back to the root gives the shortest path.
  • Mark a vertex when it is enqueued, not when it is dequeued; otherwise the same vertex enters the queue many times.

BFS · Step through the code

BFS: Tracing bfs(0)

01273456queue (front → back)0

Mark 0 and enqueue it. The queue holds only 0.

01273456queue (front → back)

Dequeue 0 from the front and print it.

01273456queue (front → back)127

Neighbors of 0: undiscovered 1, 2, 7 are marked and added to the back of the queue.

01273456queue (front → back)27

Dequeue 1 from the front and print it.

01273456queue (front → back)27

Neighbors of 1: 2 is already marked → skip.

01273456queue (front → back)7

Dequeue 2 from the front and print it.

01273456queue (front → back)734

Neighbors of 2: undiscovered 3, 4 are marked and added to the back of the queue; 0 is already marked → skip.

01273456queue (front → back)34

Dequeue 7 from the front and print it.

01273456queue (front → back)4

Dequeue 3 from the front and print it.

01273456queue (front → back)45

Neighbors of 3: undiscovered 5 is marked and added to the back of the queue.

01273456queue (front → back)5

Dequeue 4 from the front and print it.

01273456queue (front → back)56

Neighbors of 4: undiscovered 6 is marked and added to the back of the queue.

01273456queue (front → back)6

Dequeue 5 from the front and print it.

01273456queue (front → back)6

Neighbors of 5: 6 is already marked → skip.

01273456queue (front → back)

Dequeue 6 from the front and print it.

01273456queue (front → back)

The queue is empty. Output: 0 1 2 7 3 4 5 6, level by level: {0}, {1, 2, 7}, {3, 4}, {5, 6}. The bold edges form the BFS tree.

void bfs(int s) {
  boolean[] visited = new boolean[n];
  Queue<Integer> queue = new LinkedList<>();
  visited[s] = true; queue.add(s);
  while (!queue.isEmpty()) {
    int v = queue.poll();
    System.out.print(v + " ");
    for (int w : adj[v])
      if (!visited[w]) {
        visited[w] = true;
        queue.add(w);
      }
  }
}

BFS · Step by step

BFS in a Maze: Try Every Path at Once

0▸◂entry: top leftexit:bottom right

The entry cell is enqueued with distance 0. Walls (dark) are not vertices; every open cell is a vertex and adjacent open cells share an edge.

012▸◂entry: top leftexit:bottom right

At each step the wave advances one cell in every direction: cells at distance 1 and 2. Yellow = the current frontier.

01234▸◂entry: top leftexit:bottom right

A single corridor: the wave moves down. The number in each cell is the fewest steps from the entry.

01234566▸◂entry: top leftexit:bottom right

At distance 5 the path forks: the wave spreads right and down. BFS "tries every path at once".

0123485678678▸◂entry: top leftexit:bottom right

Both branches advance together: one toward the upper room, one downward.

01210394856789610789▸◂entry: top leftexit:bottom right

Distance 10: the wave has reached the upper corridor and, below, the bottom corridor.

0111122103948567896107891112▸◂entry: top leftexit:bottom right

The bottom corridor nears the exit; the upper branch wanders into dead ends.

0111121314210143948567896101478911121314▸◂entry: top leftexit:bottom right

The exit is reached in 14 steps. When BFS first reaches a cell, that distance is guaranteed to be the shortest.

0111121314210143948567896101478911121314▸◂entry: top leftexit:bottom right

Following parent links back from the exit gives the shortest path (green, 15 cells, 14 steps). The remaining cells (distance 15–17) were not needed for this path.

BFS

BFS Tree and Shortest Paths

vertex12345678
dist01111222
parent—1111223

The "BFS Step by Step" graph with source 1. Path(8): 8 → parent 3 → parent 1; reversed, 1 → 3 → 8 (2 edges).

  • On discovery: dist[w] = dist[v] + 1; parent[w] = v;
  • Distances leave the queue in nondecreasing order (0, 1, 1, 1, 1, 2, 2, 2), so the first path to reach a vertex is a shortest one.

Rebuilding the path

List<Integer> path(int[] parent, int s, int t) {
  LinkedList<Integer> path = new LinkedList<>();
  for (int v = t; v != -1; v = parent[v])
    path.addFirst(v);   // walk back from t
  if (path.getFirst() != s)
    return null;       // t unreachable
  return path;
}
Valid only for unweighted graphs (or graphs with all weights equal). Weighted graphs need Dijkstra.

BFS · Step through the code

BFS: Filling dist[] and parent[]

102675384queue1number: distance

Undirected graph, source 1 (arrays shown with indices 1–8). dist[] = −1 means "undiscovered"; parent[] also starts at −1 (no parent). dist of 1 is 0, and only 1 is in the queue.

102675384queuenumber: distance

Dequeue 1 from the front; now examine its neighbors 2, 3, 4, 5 in order.

1021675131841queue2345number: distance

All four neighbors are undiscovered: their distance becomes dist[1] + 1 = 1, their parent 1, and they are enqueued.

102162725131841queue34567number: distance

Dequeue 2. Neighbor 1 is already discovered, skip it; 6 and 7 are discovered: distance 1 + 1 = 2, parent 2.

1021627251318241queue45678number: distance

Dequeue 3. Neighbor 1 is already discovered, skip it; 8 is discovered: distance 1 + 1 = 2, parent 3.

1021627251318241queue5678number: distance

Dequeue 4 (distance 1). Its only neighbor, 1, is already discovered: nothing new is enqueued.

1021627251318241queue678number: distance

Dequeue 5 (distance 1). Its only neighbor, 1, is already discovered: nothing new is enqueued.

1021627251318241queue78number: distance

Dequeue 6 (distance 2). Its only neighbor, 2, is already discovered: nothing new is enqueued.

1021627251318241queue8number: distance

Dequeue 7 (distance 2). Its only neighbor, 2, is already discovered: nothing new is enqueued.

1021627251318241queuenumber: distance

Dequeue 8 (distance 2). Its only neighbor, 3, is already discovered: nothing new is enqueued.

1021627251318241queuenumber: distance

The queue is empty. The bold edges (parent links) form the BFS tree; e.g. 8 → parent 3 → parent 1: the shortest path is 1 → 3 → 8, of length dist[8] = 2.

void bfsDist(int s) {
  Arrays.fill(dist, -1);
  Arrays.fill(parent, -1); dist[s] = 0;
  Queue<Integer> queue = new LinkedList<>();
  queue.add(s);
  while (!queue.isEmpty()) {
    int v = queue.poll();
    for (int w : adj[v])
      if (dist[w] == -1) {  // new
        dist[w] = dist[v] + 1;
        parent[w] = v;
        queue.add(w);
      }
  }
}

BFS

BFS vs. DFS

DFSBFS
Helper structureStack (LIFO) / recursionQueue (FIFO)
Search patternone branch to its end, then backtracklevel by level (in order of distance)
TimeO(V + E) list · O(V²) matrixO(V + E) list · O(V²) matrix
Extra memoryO(V): the longest path is on the stackO(V): the widest level is in the queue
Shortest pathsnoyes (unweighted graphs)
Tree produceddeep, narrow DFS treeshallow, wide BFS tree
Typical usescycles, components, topological order, SCCs, backtrackingfewest steps, levels, bipartiteness test, spreading
On the same graph (8-vertex directed graph, source 0): DFS 0 1 2 3 5 6 4 7, BFS 0 1 2 7 3 4 5 6.

BFS

Try It: BFS and DFS

Neighbors in alphabetical order; DFS in recursive order. Yellow: queued / on stack · orange ring: current · dark: done.

Tarjan SCC

Strongly Connected Components

  • In a directed graph, u and v are mutually reachable if there is a path from u to v and from v to u.
  • A strongly connected component (SCC) is a maximal set of vertices in which every vertex can reach every other.
  • Every vertex belongs to exactly one SCC; a single vertex can be an SCC on its own ({8}).
  • "A vertex plus everything reachable from it" is not an SCC: 8 reaches every other vertex, yet none of them reaches 8.
  • Tarjan's algorithm (1972) finds all SCCs with a single DFS.
13672458

SCCs: {1, 2, 3}, {4, 5}, {6, 7}, {8}

Shrinking each SCC to one vertex gives the component graph, which is acyclic (a DAG): communities in social networks, mutually dependent modules in compilers, solving 2-SAT.

Tarjan SCC

Tarjan: Principles and Steps

  1. It is DFS-based: try the vertices in turn and start a DFS from each unvisited one.
  2. During the DFS each visited vertex gets an index: its discovery order (1, 2, 3, …). Vertices on the current DFS path whose SCC is not yet known are kept on a stack.
  3. For each vertex we compute its lowlink: the smallest index reachable from its DFS subtree using at most one edge to a vertex still on the stack.
    • Tree edge v → w: on return, lowlink(v) = min(lowlink(v), lowlink(w))
    • Edge to a w on the stack: lowlink(v) = min(lowlink(v), index(w))
    • Edge to a w in a finished SCC: ignored.
  4. When v is finished, if lowlink(v) = index(v), v is the root of an SCC: popping the stack down to v (inclusive) yields that SCC.
If lowlink(v) < index(v), v's subtree can reach an older stack vertex; v is in that vertex's SCC and is not a root.

Tarjan SCC · Step by step

Tarjan Step by Step: index, lowlink

13672458stackSCCs: —

Directed graph with 8 vertices. Next to each vertex we write index,lowlink. White = unvisited, yellow = on the stack, colored = SCC found.

11,13672458stack1SCCs: —

SCC(1): index = lowlink = 1; push 1.

11,136722,2458stack12SCCs: —

SCC(2): index = lowlink = 2; push 2.

11,133,36722,2458stack123SCCs: —

SCC(3): index = lowlink = 3; push 3.

11,133,16722,2458stack123SCCs: —

Edge 3 → 1: 1 is on the stack → lowlink(3) = min(3, index(1) = 1) = 1.

11,133,16722,1458stack123SCCs: —

3 is finished; lowlink 1 < index 3 → not a root, it stays on the stack. Back at 2: lowlink(2) = min(2, lowlink(3) = 1) = 1.

11,133,16722,1458stackSCCs: {1, 2, 3}

2 is finished; lowlink 1 < index 2 → not a root, it stays on the stack. Back at 1: lowlink(1) = 1 is unchanged. At 1, lowlink = index = 1 → 1 is a root: popping down to 1 gives an SCC: {1, 2, 3}.

11,133,16722,144,458stack4SCCs: {1, 2, 3}

SCC(4): index = lowlink = 4; push 4.

11,133,16722,144,455,58stack45SCCs: {1, 2, 3}

4 → 2 is ignored (2 is in a finished SCC, not on the stack). 4 → 3 is ignored (3 is in a finished SCC, not on the stack). SCC(5): index = lowlink = 5; push 5.

11,133,166,6722,144,455,58stack456SCCs: {1, 2, 3}

SCC(6): index = lowlink = 6; push 6.

11,133,166,677,722,144,455,58stack4567SCCs: {1, 2, 3}

6 → 3 is ignored (3 is in a finished SCC, not on the stack). SCC(7): index = lowlink = 7; push 7.

11,133,166,677,622,144,455,58stack4567SCCs: {1, 2, 3}

Edge 7 → 6: 6 is on the stack → lowlink(7) = min(7, index(6) = 6) = 6.

11,133,166,677,622,144,455,58stack45SCCs: {1, 2, 3} · {6, 7}

7 is finished; lowlink 6 < index 7 → not a root, it stays on the stack. Back at 6: lowlink(6) = 6 is unchanged. At 6, lowlink = index = 6 → 6 is a root: popping down to 6 gives an SCC: {6, 7}.

11,133,166,677,622,144,455,48stack45SCCs: {1, 2, 3} · {6, 7}

Back at 5: lowlink(5) = 5 is unchanged. Edge 5 → 4: 4 is on the stack → lowlink(5) = min(5, index(4) = 4) = 4.

11,133,166,677,622,144,455,48stackSCCs: {1, 2, 3} · {6, 7} · {4, 5}

5 is finished; lowlink 4 < index 5 → not a root, it stays on the stack. Back at 4: lowlink(4) = 4 is unchanged. At 4, lowlink = index = 4 → 4 is a root: popping down to 4 gives an SCC: {4, 5}.

11,133,166,677,622,144,455,488,8stack8SCCs: {1, 2, 3} · {6, 7} · {4, 5}

SCC(8): index = lowlink = 8; push 8.

11,133,166,677,622,144,455,488,8stack8SCCs: {1, 2, 3} · {6, 7} · {4, 5}

8 → 5 is ignored (5 is in a finished SCC, not on the stack). 8 → 7 is ignored (7 is in a finished SCC, not on the stack). Self-loop 8 → 8: 8 is on the stack, but lowlink = min(8, 8) = 8 stays the same.

11,133,166,677,622,144,455,488,8stackSCCs: {1, 2, 3} · {6, 7} · {4, 5} · {8}

At 8, lowlink = index = 8 → 8 is a root: popping down to 8 gives an SCC: {8}.

11,133,166,677,622,144,455,488,8stack emptySCCs: {1, 2, 3} · {6, 7} · {4, 5} · {8}

Result: 4 SCCs. Each SCC was found the moment its root (the vertex with index = lowlink: 1, 6, 4, 8) finished; they appear in reverse topological order.

Tarjan SCC

Tarjan: Pseudocode

TARJAN_SCC(G):
  index = 1
  stack = [ ]
  SCCs = [ ]
  for each v in V:
    if v.index is undefined:
      SCC(v)
  return SCCs
SCC(v):
  v.index = v.lowlink = index++
  stack.push(v)
  for each (v, w) in E:
    if w.index is undefined:
      SCC(w)
      v.lowlink = min(v.lowlink, w.lowlink)
    else if w is on the stack:
      v.lowlink = min(v.lowlink, w.index)
  if v.lowlink == v.index:
    scc = [ ]
    repeat until w == v:  // at least once
      w = stack.pop()
      scc.add(w)
    SCCs.add(scc)
  • In the figure the index starts at 1 (index = 1); starting at 0 gives the same SCCs.
  • "Is it on the stack?" is answered in O(1) time by an onStack[] array kept alongside the stack.

Tarjan SCC · Step through the code

Tarjan: Tracing scc(0)

01,11234stack0

Graph: 0→1, 1→2, 2→0, 2→3, 3→4, 4→3; index[w] == 0 means unvisited. scc(0): index and low = 1; push 0.

01,11234stack0

0 → 1: 1 is unvisited → recursive call.

01,112,2234stack01

scc(1): index and low = 2; push 1.

01,112,2234stack01

1 → 2: 2 is unvisited → recursive call.

01,112,223,334stack012

scc(2): index and low = 3; push 2.

01,112,223,134stack012

2 → 0: 0 is on the stack (back edge) → low[2] = min(3, index[0] = 1) = 1.

01,112,223,134stack012

2 → 3: 3 is unvisited → recursive call.

01,112,223,134,44stack0123

scc(3): index and low = 4; push 3.

01,112,223,134,44stack0123

3 → 4: 4 is unvisited → recursive call.

01,112,223,134,445,5stack01234

scc(4): index and low = 5; push 4.

01,112,223,134,445,4stack01234

4 → 3: 3 is on the stack (back edge) → low[4] = min(5, index[3] = 4) = 4.

01,112,223,134,445,4stack01234

low[4] = 4 ≠ index[4] = 5 → 4 is not a root and stays on the stack; return.

01,112,223,134,445,4stack01234

scc(4) returned: low[3] = min(4, low[4] = 4) = 4.

01,112,223,134,445,4stack012

low[3] == index[3] = 4 → 3 is a root; pop and print 4, 3: SCC {3, 4}.

01,112,223,134,445,4stack012

scc(3) returned: low[2] = min(1, low[3] = 4) = 1.

01,112,223,134,445,4stack012

low[2] = 1 ≠ index[2] = 3 → 2 is not a root and stays on the stack; return.

01,112,123,134,445,4stack012

scc(2) returned: low[1] = min(2, low[2] = 1) = 1.

01,112,123,134,445,4stack012

low[1] = 1 ≠ index[1] = 2 → 1 is not a root and stays on the stack; return.

01,112,123,134,445,4stack012

scc(1) returned: low[0] = min(1, low[1] = 1) = 1.

01,112,123,134,445,4stack

low[0] == index[0] = 1 → 0 is a root; pop and print 2, 1, 0: SCC {0, 1, 2}.

void scc(int v) {
  index[v] = low[v] = ++counter;
  stack.push(v); onStack[v] = true;
  for (int w : adj[v]) {
    if (index[w] == 0) {
      scc(w);
      low[v] = Math.min(low[v], low[w]);
    } else if (onStack[w])
      low[v] = Math.min(low[v], index[w]);
  }
  if (low[v] == index[v]) {
    int w;
    do {
      w = stack.pop(); onStack[w] = false;
      System.out.print(w + " ");
    } while (w != v);
    System.out.println();
  }
}

Tarjan SCC

Complexity and Kosaraju

Tarjan's running time: O(V + E)

  • V vertices, E edges.
  • Each vertex is visited, pushed and popped once: O(V).
  • Each edge is examined once: O(E).
  • Extra memory: index, lowlink, onStack and the stack: O(V).

Kosaraju's algorithm

  1. DFS pushes vertices in finishing order.
  2. Build the transpose of the graph.
  3. DFS on the transpose in pop order; each DFS tree is an SCC.
01234G01234Gᵀ (transpose)

On the "Tarjan Step by Step" graph Kosaraju finds the same SCCs: {1, 2, 3}, {4, 5}, {6, 7}, {8}.

TarjanKosaraju
DFS runs12 (one on the transpose)
Extraindex / lowlink, stacktranspose (O(V + E) memory)
TimeO(V + E)O(V + E)

Kosaraju SCC · Step through the code

Kosaraju: SCCs with Two DFS Passes

01234original graphstack← top on the rightcalls: dfs1(0)

Pass 1: DFS on the original graph. dfs1(0): mark 0. The stack (top on the right) is empty; a vertex is pushed only when it finishes.

01234original graphstack← top on the rightcalls: dfs1(0) → dfs1(1)

Neighbor 1 is unvisited → dfs1(1): mark 1.

01234original graphstack← top on the rightcalls: dfs1(0) → dfs1(1) → dfs1(2)

Neighbor 2 is unvisited → dfs1(2): mark 2.

01234original graphstack← top on the rightcalls: dfs1(0) → dfs1(1) → dfs1(2) → dfs1(3)

Neighbor 3 is unvisited → dfs1(3): mark 3. (2's first neighbor, 0, is already visited and was skipped.)

01234original graphstack4← top on the rightcalls: dfs1(0) → dfs1(1) → dfs1(2) → dfs1(3) → dfs1(4)

4's only neighbor, 3, is already visited: 4 finishes → push it. The stack accumulates the finishing order.

01234original graphstack43← top on the rightcalls: dfs1(0) → dfs1(1) → dfs1(2) → dfs1(3)

dfs1(4) returned; 3 has no other neighbor: 3 finishes and is pushed.

01234original graphstack43210← top on the rightcalls: dfs1(0)

2, 1 and 0 finish in turn and are pushed. 0 is on top: the last to finish, it lies in a source component.

01234transpose graph (arrows reversed)stack4321← top on the rightcalls: —

Pass 2: visited is cleared and the arrows are reversed. Pop 0; on the transpose dfs2(0) reaches 0 → 2 → 1 and prints them: SCC {0, 1, 2}.

01234transpose graph (arrows reversed)stack432← top on the rightcalls: —

Pop 1, but it is already visited (printed with 0's component): skip. The same happens to 2 next.

01234transpose graph (arrows reversed)stack43← top on the rightcalls: —

2 is visited too: skip.

01234transpose graph (arrows reversed)stack4← top on the rightcalls: —

Pop 3, unvisited: on the transpose dfs2(3) reaches 3 → 4 (the reversed edge 3 → 2 leads to visited 2 and is skipped). SCC {3, 4}.

01234transpose graph (arrows reversed)stack← top on the rightcalls: —

Pop 4, visited: skip. The stack is empty; two SCCs found: {0, 1, 2} and {3, 4}.

void kosaraju() {
  for (int v = 0; v < n; v++)  // pass 1
    if (!visited[v]) dfs1(v);
  Arrays.fill(visited, false); // pass 2
  while (!stack.isEmpty()) {
    int v = stack.pop();
    if (!visited[v]) {   // new SCC
      dfs2(v); System.out.println(); }
  }
}
void dfs1(int v) {
  visited[v] = true;
  for (int w : adj[v])
    if (!visited[w]) dfs1(w);
  stack.push(v);       // v finished
}

Applications

Topological Sort

01273456

In-degrees: 0:0, 1:1, 2:2, 3:1, 4:1, 5:1, 6:2, 7:1

  • In a directed acyclic graph (DAG), an ordering where u comes before v for every edge u → v: course prerequisites, build order, task scheduling.
  • Kahn (BFS-based): enqueue the vertices of in-degree 0; when a vertex is removed, delete its edges and enqueue any vertex whose in-degree drops to 0.
  • DFS-based: list vertices in finishing order, then reverse the list.
Kahn0 1 7 2 3 4 5 6
DFS (reverse finish)0 7 1 2 4 3 5 6
Both are valid: a topological order is not unique. If fewer than V vertices come out, the graph has a cycle (add the edge 2 → 0 from the DFS example and no ordering exists). Time O(V + E).

Applications · Step through the code

Topological Sort: Tracing Kahn's Algorithm

0011227131415162queuenumber: in-degree

A directed acyclic graph (DAG). First count each vertex's in-degree: how many edges enter it? The small numbers next to the vertices are the indeg[] values.

0011227131415162queue0number: in-degree

Vertex 0 is the only one with in-degree 0: it has no prerequisites, so it is enqueued.

0010217031415162queue17number: in-degree

Dequeue and print 0; its edges count as deleted, so the in-degrees of 1, 2 and 7 drop by 1. 1 and 7 reach zero and are enqueued.

010207031415162queue72number: in-degree

Dequeue and print 1; its edges count as deleted, so the in-degree of 2 drops by 1. 2 reaches zero and is enqueued.

01207031415162queue2number: in-degree

Dequeue and print 7. It has no outgoing edges; no in-degree changes.

0120730405162queue34number: in-degree

Dequeue and print 2; its edges count as deleted, so the in-degrees of 3 and 4 drop by 1. 3 and 4 reach zero and are enqueued.

012730405062queue45number: in-degree

Dequeue and print 3; its edges count as deleted, so the in-degree of 5 drops by 1. 5 reaches zero and is enqueued.

01273405061queue5number: in-degree

Dequeue and print 4; its edges count as deleted, so the in-degree of 6 drops by 1. Nothing reaches zero (6 still has 1 prerequisite).

0127345060queue6number: in-degree

Dequeue and print 5; its edges count as deleted, so the in-degree of 6 drops by 1. 6 reaches zero and is enqueued.

012734560queuenumber: in-degree

Dequeue and print 6. It has no outgoing edges; no in-degree changes. The queue is empty and all 8 vertices came out, so there is no cycle. Order: 0 1 7 2 3 4 5 6.

void kahn() {
  int[] indeg = new int[n];
  for (int v = 0; v < n; v++)
    for (int w : adj[v]) indeg[w]++;
  Queue<Integer> queue = new LinkedList<>();
  for (int v = 0; v < n; v++)
    if (indeg[v] == 0) queue.add(v);
  while (!queue.isEmpty()) {
    int v = queue.poll();
    System.out.print(v + " ");
    for (int w : adj[v])
      if (--indeg[w] == 0) queue.add(w);
  }
}

Applications

More Uses of Traversal

Connected componentsStart a new DFS/BFS from each unvisited vertex; the number of starts = the number of components.
Bipartiteness testColor the BFS levels with two colors; if two neighbors share a color there is an odd cycle and the graph is not bipartite.
Cycle detectionUndirected: a visited neighbor that is not the parent. Directed: a back edge to a vertex on the stack (yellow).
Bridges and articulation pointsTarjan's lowlink idea: if lowlink(w) > disc(v), then v–w is a bridge.
Iterative deepening (IDDFS)DFS runs with depth limits 0, 1, 2…: the memory of DFS with BFS's guarantee of the shallowest solution.
Random walkMove to a random neighbor at each step; PageRank and sampling large networks.

Applications · Step through the code

Bipartiteness Test with BFS

012345queue0color 0color 1

Undirected graph: the cycle 0-1-3-2 and the triangle 3-4-5. color = −1 means uncolored; source 0 gets color 0 (green) and is enqueued.

012345queue12color 0color 1

Dequeue 0 (color 0). Its uncolored neighbors 1 and 2 get the opposite color (1) and are enqueued.

012345queue23color 0color 1

Dequeue 1 (color 1). Uncolored neighbor 3 gets the opposite color (0) and is enqueued. Neighbor 0 already has the opposite color: the edge joins the two sets, fine.

012345queue3color 0color 1

Dequeue 2 (color 1). Neighbors 0 and 3 already have the opposite color: the edges join the two sets, fine.

012345queue45color 0color 1

Dequeue 3 (color 0). Uncolored neighbors 4 and 5 get the opposite color (1) and are enqueued. Neighbors 1 and 2 already have the opposite color: the edges join the two sets, fine.

012345same color!queue5color 0color 1

Dequeue 4; neighbor 3 has a different color (fine), but neighbor 5 has the same color (1). Both ends of an edge fell into one set: there is an odd cycle, like the triangle 3-4-5 → false.

boolean isBipartite(int s) {
  Arrays.fill(color, -1);
  color[s] = 0; queue.add(s);
  while (!queue.isEmpty()) {
    int v = queue.poll();
    for (int w : adj[v])
      if (color[w] == -1) {
        color[w] = 1 - color[v];
        queue.add(w);
      } else if (color[w] == color[v])
        return false;  // same color
  }
  return true;
}

Summary

Summary

TopicKey points
Representationlist O(V + E) memory, matrix O(V²); traversal O(V + E) with a list, O(V²) with a matrix
DFSstack / recursion; go deep, backtrack; a visited array guards against cycles
Stack DFSmark on pop; a vertex may be pushed several times; order is the reverse of the neighbor order
BFSqueue; mark on enqueue; level by level; unweighted shortest paths via the parent array
Mazecell = vertex; BFS tries all paths at once, and the first path to reach the exit is shortest
SCCmaximal mutually reachable set; the component graph is a DAG
Tarjanindex = discovery order; lowlink; lowlink = index → root, pop the SCC off the stack; O(V + E)
Applicationstopological order (Kahn / reverse finish), components, bipartiteness, cycles, bridges

Summary

Common Mistakes

Forgetting the visited arrayOn a cyclic graph DFS recurses forever and BFS's queue never empties. That is where the myth "BFS fails on cyclic graphs" comes from; with marking it works fine.
Marking on dequeue in BFSThe same vertex gets enqueued by many neighbors; the result may still be right, but the queue grows to O(E). Mark on enqueue instead.
Expecting shortest paths from DFSThe first path DFS finds to a target is not the shortest. Use BFS when unweighted, Dijkstra when weighted.
The order of stack DFSPushing neighbors in order reverses the result (0 7 2 … ≠ 0 1 2 …). To get the recursive order, push them in reverse.
Skipping the "on the stack?" check in TarjanOnly a w on the stack gets min(…, index(w)); a w whose SCC is already found is ignored. Otherwise roots are lost.
Disconnected graphsA traversal from one source sees only its component. To cover the whole graph, restart from every unvisited vertex.

Summary

Worked Problems

Undirected graph, neighbors in alphabetical order: A–B, A–C, B–D, C–D, C–E, D–F, E–F, F–G.

  1. Give the BFS visit order from A and the distance of each vertex.
  2. What is the recursive DFS visit order from A?
  3. What is the shortest path from A to G (using the BFS parent array)?
  4. Is the graph bipartite? Give the two sets.
  5. Directed graph 0→1, 1→2, 2→0, 1→3, 3→4, 4→5, 5→3, 5→6: list the SCCs in the order Tarjan finds them (start at 0, index from 1).
  6. What is Kahn's order for A→C, B→C, B→D, C→E, D→E, E→F (queue starts in alphabetical order)?
Show answers
  1. A B C D E F G; distances A0, B1, C1, D2, E2, F3, G4.
  2. A → B → D → C → E → F → G: A B D C E F G.
  3. Parents: G←F, F←D, D←B, B←A → A–B–D–F–G (4 edges).
  4. Yes: {A, D, E, G} and {B, C, F}; every cycle (A-B-D-C, C-D-F-E and the six-edge A-B-D-F-E-C) has even length.
  5. {6} (6: 7,7), {3, 4, 5} (index 4, 5, 6; lowlink 4), {0, 1, 2} (lowlink 1).
  6. Queue [A, B] → A B C D E F.

Summary

Exercises

  1. On the worked-problems graph, what order does stack DFS from A give (neighbors pushed alphabetically)? How many pushes in total?
  2. With V = 1000 and E = 3000, compare the work of BFS on an adjacency list and on a matrix.
  3. Give a small weighted graph where BFS misses the shortest path.
  4. How do you count the connected components of an undirected graph? Running time?
  5. In "Tarjan Step by Step", what if a w in a finished SCC also triggered min(v.lowlink, w.index)?
Show answers
  1. A C E F G D B; 2E = 16 neighbor pushes plus the start: 17 pushes.
  2. List: V + 2E ≈ 1000 + 6000 = 7000 operations. Matrix: V² = 1,000,000 cells, ~140 times more.
  3. s→t (weight 10), s→a (1), a→t (1): BFS reaches t in 1 edge (cost 10); s→a→t costs 2.
  4. For each unvisited v: count++, then DFS/BFS from v. O(V + E).
  5. Edges 4 → 2, 6 → 3, 8 → 5 lower lowlink(4), lowlink(6), lowlink(8) to 2, 3, 5: 4, 6, 8 are no longer roots, so {4, 5}, {6, 7}, {8} are never popped; 4–8 stay on the stack.

Summary

References

  • Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, 4th ed., MIT Press, 2022 (Chapter 20: Elementary Graph Algorithms).
  • Sedgewick and Wayne, Algorithms, 4th ed., Addison-Wesley, 2011 (Sections 4.1–4.2).
  • R. E. Tarjan, "Depth-First Search and Linear Graph Algorithms", SIAM Journal on Computing 1(2), 1972.
  • A. B. Kahn, "Topological Sorting of Large Networks", Communications of the ACM 5(11), 1962.
  • Kleinberg and Tardos, Algorithm Design, Pearson, 2006 (Chapter 3: Graphs).

The End

Chapter 5: Graph Traversal
Previous: Chapter 4: Searching Algorithms · Next: Chapter 6: Shortest Paths
1 / 1 Sercan KÜLCÜ. All rights reserved.