0% found this document useful (0 votes)
8 views61 pages

10 Graph 02

The document discusses algorithms related to graph theory, specifically focusing on Minimum Spanning Trees (MST) and their computation using Prim's and Kruskal's algorithms. It explains the conditions for spanning trees, the greedy approach used in these algorithms, and provides examples and running times for each method. Additionally, it touches on topological sorting and shortest path algorithms, including Dijkstra's algorithm, highlighting their principles and applications.

Uploaded by

5sumin0519
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views61 pages

10 Graph 02

The document discusses algorithms related to graph theory, specifically focusing on Minimum Spanning Trees (MST) and their computation using Prim's and Kruskal's algorithms. It explains the conditions for spanning trees, the greedy approach used in these algorithms, and provides examples and running times for each method. Additionally, it touches on topological sorting and shortest path algorithms, including Dijkstra's algorithm, highlighting their principles and applications.

Uploaded by

5sumin0519
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

2024S - Algorithm

Graph (part 2)

Jaesik Park
[email protected]
Computer Science Dept.

43
Minimum Spanning Trees

2024S - Algorithm - Seoul National University 44


Spanning Tree
Condition
• Undirected connected graph
Tree
• A connected graph with no cycles
• A tree with n vertices always has n-1 edges.
Spanning tree of graph G
• A tree as a subgraph of G that contains all of the G’s vertices

2024S - Algorithm - Seoul National University 45


A Graph and Spanning Trees
8

1 7

6 4

root 2 5

3
Making a connected graph using |V|-1 edges
= Making an acyclic graph using |V|-1 edges
= Making a tree with a vertex set V
2

1 5

3 8

4 6

2024S - Algorithm - Seoul National University 46


Minimum Spanning Trees
Condition
• Weighted undirected graph
The cost of a spanning tree
• The sum of the edge weights in the spanning tree
Minimum spanning tree
• A spanning tree with the minimum cost

2024S - Algorithm - Seoul National University 47


Prim Algorithm
An intermediate state in an inductive view
Grow a vertex set S by a sequence of greedy choices

S V-S S V-S

r r

least-cost cross edge

2024S - Algorithm - Seoul National University 48


Prim Algorithm
ü Prim algorithm is an example of greedy algorithm.
ü It is a rare case that a greedy algorithm guarantees global optimality.

Convention: {x, u} or (x, u)


Grow a vertex set S by a sequence of greedy choices But here, (x-u): undirected edge bet’n x and u
Prim(v):
Mark v as visited and include it in the m.s.t.
while (there are unvisited vertices)
Find a least-cost edge (x-u) from a visited vertex x
to an unvisited vertex u
Mark u as visited
Add the vertex u and the edge (x-u) to the m.s.t.

2024S - Algorithm - Seoul National University 49


A Working Example of Prim Algorithm
8 S 8 8 10
10
r
0 ∞
9 5 9 5
11 9
13 12 11 13 12

11

8 7 8 7

8 8 10
8 10
S 0 10
S 0 5
9 9 5
5 9
9 11 13 12
11 13 12
11 ∞
11 12
: vertex just added to S
8 7
8 7 ∞
∞ : vertex just having done relaxation

2024S - Algorithm - Seoul National University 50


A Working Example of Prim Algorithm
8 10 8
10
0 5 0 5
5
S
9 11 9
11 13 12 13 12
11 12 12
11 8
S 10
8 7 8 7
∞ 8 0 10

9
S 13 12
8 8 7
10 11
0 5 0 10 8 7
8

9 9
13 12
11 7 11 7

8 8 7
S

2024S - Algorithm - Seoul National University 51


More Detailed Description of Prim Algorithm
Prim(G, r):
dv : minimum connection cost of vertex v calculated so far
◄ G=(V, E): given graph
◄ r: starting vertex
S ← {r} ◄ S: set of vertices
for each u ∈ V
du ← wr,u
dr ← 0
while (S≠V) ◄ iterate n times
x ← extractMin(V-S, d)
S ← S ∪ {x}
for each v∈x.adjList ◄ x.adjList: set of vertices adjacent to x
if (v∈V-S and wx,v< dv)
dv ← wx,v
Relaxation!
extractMin(Q, d):
Return a vertex x with the minimum d value in a vertex set Q

2024S - Algorithm - Seoul National University 52


Running Time: O(E log V)
Prim(G, r): r
◄ G=(V, E): given graph S V-S
◄ r: starting vertex
S ← {r} ◄ S: set of vertices
for each u∈V
du ← wr,u
dr ← 0
while (S≠V) ◄ iterate n times Maintain the connection costs
x ← extractMin(V-S, d) of vertices in V-S in a heap
S ← S ∪{x}
for each v∈x.adjList ◄ x.adjList: set of vertices adjacent to x V-1 times. O(VlogV) in total
if (v∈V-S and wx,v< dv)
dv ← wx,v
O(ElogV) in total
extractMin(Q, d): to repair the heap
Return a vertex x with the minimum d value in a vertex set Q

Repairing cost of a heap: O(logV)


2024S - Algorithm - Seoul National University 53
Kruskal Algorithm
Start with n singleton set of vertices,
keep merging two sets by greedy choices

An intermediate state in an inductive view

least-cost cross edge

2024S - Algorithm - Seoul National University 54


Kruskal Algorithm
Start with n singleton set of vertices,
keep merging two sets by greedy choices

Kruskal(G, r):
1. T ← Ф ◄ T : spanning tree
2. Initialize n singleton sets of vertices
3. Sort all the edges (Q(=E)) in nondecreasing order
4. while (# of edges in T < n-1)
Remove an edge (u-v) of the minimum cost from Q
if (vertices u and v belong to different sets)
Merge the two sets of u’s and v’s
T ← T ∪ {(u-v)}

2024S - Algorithm - Seoul National University 55


A Working Example of Kruskal Algorithm
8 10 8 10

5 9 5
9
11 11 13 12
13 12
8 10
8 8
8 7
8 7
9
11 13 12

8
10 8 10 8 7

9 9
11 13 12 11 13 12

8 8
8 : edge just looked at
: edge successfully added

2024S - Algorithm - Seoul National University 56


10
10
10

9 9 11
11 12 12
12 13
11 13 13

9 5 11
11 13 12

8 7

2024S - Algorithm - Seoul National University 57


Running Time: O(E log V)

Kruskal(G, r):
1. T ← Ф ◄ T : spanning tree O(ElogE) = O(ElogV)
2. Initialize n singleton sets of vertices
3. Sort all the edges (Q(=E)) in nondecreasing order O((V+E)log*V) in total
4. while (# of edges in T < n-1) by an efficient set handling
Remove an edge (u-v) of the minimum cost from Q
if (vertices u and v belong to different sets) O(E) in total
Merge the two sets of u’s and v’s
T ← T ∪ {(u-v)}

2024S - Algorithm - Seoul National University 58


Theoretical Base of Prim, Kruskal Algorithm

[Safety Theorem]

Let (S,V-S) an arbitrary partition of vertices and an edge (u-v) be the min-weight
cross edge between S and V-S. Then there exists at least a min. spanning tree
containing (u-v).

<Proof> next page

2024S - Algorithm - Seoul National University 59


<Proof> ❶

In any m.s.t. T not containing the edge (u-v), there is only one path bet’n u & v.
The path contains at least one edge crossing S & V-S. Take such a crossing edge
(x-y).

Note that wuv ≤ wxy . (given condition)


Case 1: wuv < wxy
T ∪{(u-v)}−{(x-y)} is less weighted than T.
T is not a m.s.t. Contradiction to ❶!
Case 2: wuv = wxy
T ∪ {(u-v)}−{(x-y)} has the same weight as T.
Hence T ∪{(u-v)}−{(x-y)} is also a m.s.t.
2024S - Algorithm - Seoul National University 60
Properties

If edge weights are all distinct, then there is only one mst.
If not, there can be more than one mst.

For two or more different mst’s, the set of weights are the same.
- proof: p.386(in context of matroid)

2024S - Algorithm - Seoul National University 61


Topological Sorting

2024S - Algorithm - Seoul National University 62


Topological Sorting
Condition
• Directed acyclic graph
Topological order
• An order of vertices in which vertex x precedes vertex y if there is an
edge (x→y)
• Usually, there are many topological orders for a directed graph.

2024S - Algorithm - Seoul National University 63


A Graph and Two Examples of Topological Order

A graph and examples of topological orders

2024S - Algorithm - Seoul National University 64


Algorithm 1
topologicalSort(G): ◄ G: graph, n = |V|
for i ← 1 to n
Select a vertex v that has no successors
vertex[i] ← v
Delete from G vertex v and the edges to v
List vertex[n],…,vertex[1] ◄ a topological order

a b c a b c

d e f d e f

g g

2024S - Algorithm - Seoul National University 65


An Example
a b c a b c a b c

d e f d e f d e f

g g vertex[ ] f g fe

a b c a b c a b c

d e f d e f d e f

g fecdg g fecd g fec


2024S - Algorithm - Seoul National University 66
a b c a b c

d e f d e f

g fecdgb g fecdgba

reverse

a b g d c e f
A topological order

2024S - Algorithm - Seoul National University 67


Algorithm 2
topologicalSort2(G): ◄ G: graph
for i ← 1 to n
Select a vertex v that has no predecessors
vertex[i] ← v
Delete from G vertex v and the edges outgoing from v
List vertex[1], …, vertex[n]; ◄ a topological order

a b c a b c

d e f d e f

g g

2024S - Algorithm - Seoul National University 68


Algorithm 3
topologicalSort3(G):
◄ If there are unvisited vertices, run DFS-TS(v) starting any unvisited vertex v.
◄ Repeat this until there is no unvisited vertex.
for each v∈V
v.visited ← NO
for each v∈V ◄ in any order of vertices
if (v.visited = NO) DFS(v)

DFS(v):
◄ In the end, vertices are lined up in a topological order in the list R
v.visited ← YES
for each x∈v.AdjList ◄ v.AdjList: set of adjacent vertices of v
if (x.visited = NO) DFS(x)
Insert vertex v in the front of the list R vertex v finished
This is the moment to perform stack pop

üRunning time: Θ(V+E)


2024S - Algorithm - Seoul National University 69
An Example
DFS from here(random start) DFS from here(random start among the remaining)

a b c a b c a b c

d e f d e f d e f

g g list becf g
last finished first finished

a b c a b c a b c

d e f d e f d e f

gadbecf
g g g adbecf
A topological order
DFS from here(the only remaining)
2024S - Algorithm - Seoul National University 70
Shortest Paths

2024S - Algorithm - Seoul National University 71


Shortest Path
Condition
• Weighted digraph
• Undirected graph can be also considered to be a digraph in which an
undirected edge (u-v) means two directed edges (u→v) and (v→u).
Shortest path bet’n two vertices
• A path that has the smallest sum of edge weights

2024S - Algorithm - Seoul National University 72


Dijkstra Algorithm
Restriction: only non-negative weights
dv : shortest path length of r ⇝ v
Dijkstra(G, r): ◄ r: the starting vertex calculated so far
S ← {r}
for each v∈V
dv ← wr,v
for i ← 2 to |V|
Find the smallest dv s.t. v is not in S
S ← S ∪{v}
for each vertex u not in S
if (du > dv + wv,u)
du ← dv + wv,u Relaxation!

ü Dijkstra’s algorithm is an example of greedy algorithm.


ü It is another rare case that a greedy algorithm guarantees global optimality.

ü Note that the logic of Dijkstra’s algorithm is almost the same as Prim

2024S - Algorithm - Seoul National University 73


Example of Relaxation
0

A weighted digraph

(c) Relaxation
8→7
0

2024S - Algorithm - Seoul National University 74


A Working Example of Dijkstra Algorithm
8 ∞ 10 8 8 10 8 10
r ∞ ∞
0 6 2 0 6 0 18
2 6 2
9 1 9 1 1
11 ∞ ∞ 11 9 ∞ ∞
3 12 5 3 12 5 9 12 5
3
∞ ∞ 11 ∞ ∞
8 8 11 8
8 7 4 8 7 4 8 7 4
∞ ∞ ∞

8 8 8

0 6 10 0 10 0 6 10 2
2
1
9 12 9 12 9 12 5 ∞
12 5 12 5 3
19 ∞ 11 ∞
11 8 11 8 8
8 7 4 8 7 4 8 7 4
19 ∞ ∞

2024S - Algorithm - Seoul National University 75


8 8

0 6 10 0 10

9 12 9 12
12 5
19 11 19
11 8
8 7 4
19 16

8 8 10
8
0 10
2 10
1 0
9
11 9 12
3 12 5 9 12
8 12 5
11 19 8 10
11 19
7 4 0 10
16 2
9 1 16
11 9 12
3 12 5
11 19
7 4
16

2024S - Algorithm - Seoul National University 76


Running Time: O(E log V)
The same time as Prim algorithm’s

S V-S
Dijkstra(G, r): ◄ r: the starting vertex
S ← {r} r
for each v∈V
dv ← wr,v
for i ← 2 to |V|
Find the smallest dv s.t. v is not in S
S ← S ∪{v} --- ❶
for each vertex u not in S
if (du > dv + wv,u)
du ← dv + wv,u Maintain the connection costs
of vertices in V-S in a heap

2024S - Algorithm - Seoul National University 77


Proof of Correctness
[Claim] S r V-S
v
Assume du is optimal for all u’s in S,
dv is also optimal after adding v in ❶ (i.e., adding v in ❶ is safe)
<Sketch of Proof>
Assume for contradiction that, after adding v in ❶,
there is a shorter-than-dv path P to v. Then P should be in the form:
“r → some vertices in S → some vertices in V-S (❷) → v.”

Let the first vertex of ❷ be x.


S V-S
dx ≥ dv since dv is the smallest in V-S.
Since the edges are non-negative, P ≥ dx ≥ dv . r
v
Contradiction!

After initialization with S={r}, dr is optimal. x


This claim guarantees the optimal paths for all the vertices by induction.
P

2024S - Algorithm - Seoul National University 78


Bellman-Ford Algorithm
Condition: Negative weights are allowed, but negative cycles are not allowed

dv : 지금까지 계산해놓은
BellmanFord(G, r): r ⇝ v 최단경로 길이
for each u∈V
du ← ∞
dr ← 0 Right after completing the loop with i = k,
du has the shortest path length to u
for i ← 1 to |V|-1
with at most k intermediate edges
for each (u→v) ∈ E
if (du + wu,v < dv )
dv ← du + wu,v Relaxation!

Right before starting the for loop,


du has the shortest path length to u
with no intermediate edges (initial state)

2024S - Algorithm - Seoul National University 79


A Working Example of Bellman-Ford Algorithm
the shortest path lengths
with at most one intermediate edge
∞ i =1 8 8 10
8 10
r ∞ 0 -15 ∞
0 -15 2 2
9 1 9 1 the shortest path lengths
11 9 ∞
11 ∞ 12 5 ∞ 3 12 5 with at most two intermediate edge
3
∞ 11 ∞ i =2
∞ 8 8 8 -6 10
-7 8 -7 4
8 4 ∞
∞ 0 -15 10
2
9 1
11 9 ∞
3 12 5
11 19
i =4 -6 i =3 -6 10 8
8 10 8 -7
8 4
4 19
0 -15 4 0 -15 2
2
9 1 9 1
11 9 6 11 9 12
3 12 5 3 12 5
12 11 12
11 8 8
8 -7 4 8 -7 4 the shortest path lengths
16 19
with at most three intermediate edges

2024S - Algorithm - Seoul National University 80


the shortest path lengths
i =5 8 -6 10 i =6 -6 with at most six intermediate edges
8 10
0 -15 4 0 -15 4
2 2
9 1 9 1
11 9 5 6 11 9 6
3 12 3 12 5
11 9 11 3
8 8
8 -7 4 8 -7 4
10 10

i =7 8 -6 10
8 -6 10

4 0 -15 4 2
0 -15 2
1 9 1
9 11 9 6
11 9 6 3 12 5
3 12 5
3 11 3
11 8 8
-7 8 -7 4 the shortest path lengths
8 4 10
10 with at most seven intermediate edges

2024S - Algorithm - Seoul National University 81


Running Time: Θ(E V)

BellmanFord(G, r):
for each u∈V
Θ(V) iterations
du ← ∞
dr ← 0
for i ← 1 to |V|-1 Θ(V) iterations
for each (u→v) ∈ E Θ(E) iterations
if (du + wu,v < dv )
Θ(1) operation
dv ← du + wu,v

ü Running time: Θ(EV)


ü Possible to change to O(EV)

2024S - Algorithm - Seoul National University 82


Bellman-Ford is a DP Algorithm
dtk : Shortest path length from r to t with at most k intermediate edges
Object: dtn-1

ü Optimal substructure
dvk = min {duk-1+ wu, v}, k > 0
(u →v) ∈ E v

dr0 = 0
duk-1 wu, v
dt0 = ∞, t ≠ r
u

2024S - Algorithm - Seoul National University 83


Floyd-Warshall Algorithm
Condition: Negative weights are allowed, but negative cycles are not
allowed

• Shortest paths bet’n all pairs of vertices


• Application examples
• Road Atlas
• Data center network design
• Traffic routing
• A naïve approach
• Run Bellman-Ford |V| times (starting each vertex)
• Running time: Θ(EV2)
• Objective running time: Θ(V3)

2024S - Algorithm - Seoul National University 84


Optimal Substructure
dijk : shortest path length from vertex i to vertex j
with the vertex set {1, 2, …, k} as intermediate vertices
k=
wij , k=0
dij
min {dijk-1, dikk-1+ dkjk-1}, k≥1
❷ ❶

e r t ices All
i
diate v are nterm
e r me , k-1} in { e
1, 2 diate
nt … ❶
All i n{1, 2, ,… v
, k- ertice
i
are k 1} s

❶ dijk includes vertex k j


Two cases i
❷ dijk doesn’t include vertex k

All intermediate vertices are in {1, 2, …, k-1}

2024S - Algorithm - Seoul National University 85


Floyd-Warshall Algorithm

FloydWarshall(G):
for i ← 1 to n
for j ← 1 to n
dij0 ← wij
for k ← 1 to n ◄ intermediate vertex set {1, 2, …, k}
for i ← 1 to n ◄ i : starting vertex
for j ← 1 to n ◄ j : final vertex
dijk ← min {dijk-1 , dikk-1 + dkjk-1}

dijk : shortest path length from vertex i to vertex j


with the vertex set {1, 2, …, k} as intermediate vertices

2024S - Algorithm - Seoul National University 86


Running Time: Θ(V3)

FloydWarshall(G):
for i ← 1 to n
for j ← 1 to n
dij0 ← wij
for k ← 1 to n Θ(V) iterations
for i ← 1 to n Θ(V) iterations
for j ← 1 to n Θ(V) iterations
dijk ← min {dijk-1 , dikk-1 + dkjk-1} Θ(1) operation

üRunning time: Θ(V3)


ü# of problems Θ(V3), Θ(1) for each problem

2024S - Algorithm - Seoul National University 87


Warshall Algorithm for Transitive Closure
Want to find the existence of a path bet’n every pair of vertices

0 1 2 3 4 5 6 7
0 1 1 0 0 1 0 0 1
There is a path from vertex 3 to vertex 6 1 0 1 0 0 1 1 1 1
2 1 0 1 0 1 0 1 1
3 1 0 0 1 0 1 1 1
4 1 1 0 0 1 1 0 0

There is no path from vertex 7 to vertex 4 5 0 1 1 1 0 1 1 1


6 1 1 1 0 0 0 1 0
7 0 1 1 1 0 1 0 1

Transitive closure of a graph

Have to fill in the binary matrix


2024S - Algorithm - Seoul National University 88
Optimal Substructure
1, if there is an edge from vertex i to vertex j
wij =
0, otherwise

1, if there is a path from vertex i to vertex j


dij = k
with the vertex set {1, 2, …, k} as intermediate vertices
0, otherwise
wij , k=0
dij k=
dijk-1 ∨ (dikk-1 ∧dkjk-1), k≥1
❷ ❶ r t ices All
i
ate v
e are nterm
di in { e
t e r me , k-1} ❶ 1, 2 diate
n … ,… v
All i n{1, 2, , k- ertice
i k 1} s
are

❶ dijk includes vertex k j


Two cases i
❷ dijk doesn’t include vertex k

All intermediate vertices are in {1, 2, …, k-1}
2024S - Algorithm - Seoul National University 89
Shortest Paths of DAG

Shortest paths on a DAG (Directed Acyclic Graph)


Simple
possible to complete in Θ(V+E)

a DAG

2024S - Algorithm - Seoul National University 90


Shortest Paths of DAG
dv : shortest path length from r to v

DAG-ShortestPath(G, r):
Topologically sort all the vertices of G
for each u∈V
du ← ∞
dr ← 0
for each x∈V in topological order starting at r
for each v∈x.adjList ◄ x.adjList: set of vertices adjacent to x
if (dx + wx,v < dv )
dv ← dx + wx,v

Relaxation!

2024S - Algorithm - Seoul National University 91


A Working Example
7
5 1
3
(a) 1 -2 Given DAG
6
-3
4

7 1

6 3 -2 -3
(b) Topological sorting
4
1 5
7 1
r
(c) 6 3 -2 -3
∞ 0 ∞ ∞ ∞ ∞ Initialization
4
1 5
2024S - Algorithm - Seoul National University 92
A Working Example
7 1

6 3 -2 -3
(d) ∞ 0 3 7 ∞ Start relaxation
5
4
1 5
7 1

6 3 -2 -3
(e) ∞ 0 3 7 7 5
4
1 5

7 1

(f) ∞ 6 3 -2 -3
0 3 7 5 5
4
1 5

2024S - Algorithm - Seoul National University 93


A Working Example
7 1

∞ 6 3 -2 -3
(g) 0 3 7 5 2
4
1 5

7 1

∞ 6 3 -2 -3
(h) 0 3 7 5 2
4
1 5

2024S - Algorithm - Seoul National University 94


Running Time: Θ(V+E)

DAG-ShortestPath(G, r):
for each u∈V
du ← ∞
dr ← 0
Topologically sort all the vertices of G Θ(V+E) time operation
for each x∈V in topological order starting at r
for each v∈x.adjList O(E) cases in total
if (dx + wx,v < dv )
dv ← dx + wx,v Θ(1) time operation

2024S - Algorithm - Seoul National University 95


Strongly Connected Components

2024S - Algorithm - Seoul National University 96


Strongly Connected

Given a directed graph (digraph)


Strongly connected
For every vertex pair (x, y), there are paths for both x〰>y and y〰>x
If a subgraph of G is strongly connected,
we say it is a strongly connected component

Objective:
find all strongly connected components in a graph G

2024S - Algorithm - Seoul National University 97


Strongly Connected Component Algorithm

stronglyConnectedComponent(G):
1. Conduct DFS’s to visit all vertices and compute the finishing time fv of vertex v
(Each DFS takes any vertex out of unvisited ones)
2. Generate GR by reversing the directions of all the edges
3. Conduct DFS’s with GR to visit all vertices
(Each DFS takes as the starting vertex the vertex with the greatest fv out of unvisited ones)
4. The set of vertices visited in each DFS in step 3 constitutes a strongly connected component

2024S - Algorithm - Seoul National University 98


A Working Example G

Given digraph

7 : Visiting order
5
: finishing time of the node

Step 1: a DFS Step 1: another DFS Step 2: Generate GR


G G 10 9
5
GR
5 10 9
7 9 7 10 5
6 6
8 8 6
6 6
4 4
1 5 1 5 4
7 7
8 8 7
2 2 8
3 3 3
3 3
2 2
4 4 2
1 1
1
2024S - Algorithm - Seoul National University 99
Step 3: a DFS Step 3: another DFS Step 3: another DFS
9 9 9
GR GR GR
10 5 10 5 10 5
6 6 6
4 4 4
7 8 7 7
8 8
3 3 3

2 2 2
1 1 1
Step 3: another DFS
9 9
GR GR
10 5 10 5
6 6
4 4

8 7 8 7
Step 4: Final set of 3 3
strongly connected components
2 2
1 1
2024S - Algorithm - Seoul National University 100
Running Time: Θ(V+E)

stronglyConnectedComponent(G):
1. Conduct DFS’s to visit all vertices and compute the finishing time fv of vertex v (Each Θ(V+E) time operation
DFS takes any vertex out of unvisited ones)
2. Generate GR by reversing the directions of all the edges Θ(V+E) time operation
3. Conduct DFS with GR to visit all vertices Θ(V+E) time operation
(Each DFS takes as the starting vertex the vertex with the greatest fv out of unvisited
ones)
4. The set of vertices visited in each DFS in step 3 constitutes a strongly connected
component

ü Running time: Θ(V+E)

2024S - Algorithm - Seoul National University 101


Proof of Correctness
[Claim]
v & w are in the same strongly connected component
GR
iff
v & w are in the same tree in the DFS forest of Step 3 x

<Proof>
⇒ easy (trivial)
Assume v & w are in the same strongly connected component.
Then ᴲ(v →+ w) and ᴲ(w →+ v) in G. Hence ᴲ(w →+ v) and ᴲ(v →+ w) in G.
w
Suppose a DFS in GR reaches v (or w), then it also reaches w(or v); v
i.e., they are in the same tree in the forest.
⇒ G

=
Assume v & w are in the same tree in the forest of GR
Then ᴲ(x →+ v) in GR ⇒ ᴲ(v →+ x) in G. x fx is the greatest
Claim: ᴲ(x →+ v) in this tree
Suppose, for the contradiction, that ᴲno path x →+ v in G.
Then, fx < fv , contradiction! (Since fx > fv by Step 3)
In the same way, we can show that ᴲ(x →+ w) in G.
Therefore ᴲ(v →+ w) and ᴲ(w →+ v) in G. v
w
v & w are in the same strongly connected component.
Core of proof
2024S - Algorithm - Seoul National University 102
Notes
• Lecture slides courtesy of
• Prof. Byung-Ro Moon
• Prof. Kunsoo Park
• Prof. Sun Kim
• Please use QnA board

2024S - Algorithm - Seoul National University 103

You might also like