10 Graph 02
10 Graph 02
Graph (part 2)
Jaesik Park
[email protected]
Computer Science Dept.
43
Minimum Spanning Trees
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
S V-S S V-S
r r
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
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
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)}
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
9 9 11
11 12 12
12 13
11 13 13
9 5 11
11 13 12
8 7
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)}
[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).
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).
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)
a b c a b c
d e f d e f
g g
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
d e f d e f
g fecdgb g fecdgba
reverse
a b g d c e f
A topological order
a b c a b c
d e f d e f
g g
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
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
ü Note that the logic of Dijkstra’s algorithm is almost the same as Prim
A weighted digraph
(c) Relaxation
8→7
0
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 ∞ ∞
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
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
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!
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
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
ü Optimal substructure
dvk = min {duk-1+ wu, v}, k > 0
(u →v) ∈ E v
dr0 = 0
duk-1 wu, v
dt0 = ∞, t ≠ r
u
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
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}
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
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
a DAG
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!
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
∞ 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
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
Objective:
find all strongly connected components in a graph G
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
Given digraph
7 : Visiting order
5
: finishing time of the node
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
<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