CS 401
Dijkstra’s Algorithm / Minimum Spanning Tree
Xiaorui Sun
1
CS 401 Dijkstras Algorithm / Minimum Spanning Tree Xiaorui Sun 1 - - PowerPoint PPT Presentation
CS 401 Dijkstras Algorithm / Minimum Spanning Tree Xiaorui Sun 1 Single Source Shortest Path Given an (un)directed connected graph ! = ($, &) with non- negative edge weights ( ) 0 and a start vertex , . Find length of shortest paths
Xiaorui Sun
1
Cost of path s-2-3-4-t = 9 + 23 + 6 + 6 = 44.
s 3 t 2 6 7 4 5 23 18 2 9 14 15 5 30 20 44 16 11 6 19 6 length of path = sum of edge weights in path
Dijkstra(!, #, $) { Initialize set of explored nodes % ← {$} // Maintain distance from $ to each vertices in % ) $ ← * while (% ≠ ,) { Pick an edge (., /) such that . ∈ % and / ∉ % and )[.] + #(.,/) is as small as possible. Add / to % and define )[/] = )[.] + #(.,/). 789:;< / ← .. }
Theorem: ) / is the length of shortest path from $ to /
Dijkstra(!, #, $) { Initialize set of explored nodes % ← {$} // Maintain distance from $ to each vertices in % ) $ ← * while (% ≠ ,) { Pick an edge (., /) such that . ∈ % and / ∉ % and )[.] + #(.,/) is as small as possible. Add / to % and define )[/] = )[.] + #(.,/). 789:;< / ← .. }
O(m) time O(n) iterations Overall: O(nm) time O(1) time
'∈): ',, ∈- .[0] + 3(0,!) 2 4 16 8 9 15 ¥ 10 ¥ 7 5 13
4 2 5 10 1 8 4 3 4 3 1 5 8 2 5 6 7 9
s
'∈): ',, ∈- .[0] + 3(0,!)
Dijkstra(6, 3, 7) { Initialize set of explored nodes # ← {7} // Maintain distance from 7 to each vertices in # . 7 ← ; while (# ≠ =) { Pick an edge (0, !) such that 0 ∈ # and ! ∉ # and .[0] + 3(0,!) is as small as possible. Add ! to # and define .[!] = .[0] + 3(0,!). ?@ABCD ! ← 0. Maintain min
E∈): E,F ∈- .[@] + 3(@,G) for each G ∉ #
}
O(n) time O(n) iterations Overall: O(n2+m) time O(1) time O(deg(u)) time
– Return the element with the smallest key
– Return the element with the smallest key and delete it from the data structure
– Decrease the key value of some element
Implementations
Read wiki!
,∈.: ,,1 ∈2 3[5] + 8(5,9)
Dijkstra(!, #, $) { Initialize set of explored nodes % ← {$} // Maintain distance from $ to each vertices in % ) $ ← * Insert all neighbors + of s into a priority queue with value #($,+). while (% ≠ /) { Pick an edge (0, +) such that 0 ∈ % and + ∉ % and )[0] + #(0,+) is as small as possible. v ¬ delete min element from 6 Add + to % and define )[+] = )[0] + #(0,+). 89:;<= + ← 0. foreach (edge ; = (+, >) incident to +) if (> ∉ %) if (> is not in the 6) Insert > into 6 with value ) + + #(+,>) else (the key of > > ) + + #(+,>)) Decrease key of ? to )[+] + #(+,>). }
@(A) of delete min, each in @(log A) @(A) of insert, each in @(1) @(F) of decrease/insert key, each runs in @(1) Overall: O((nlog n)+m) time
10
11
& !
12
5 23 10 21 14 24 16 6 4 18 9 7 11 8
! = ($, &)
5 6 4 9 7 11 8
( , = -
)∈/
() = 50
Kruskal(G, c) { Sort edges weights so that !" ≤ !$ ≤ ⋯ ≤ !&. ' ← ∅ foreach (* ∈ ,) make a set containing singleton {*} for / = " to & Let *, 2 = 3/ if (* and 2 are in different sets) { ' ← ' ∪ {3/} merge the sets containing * and 2 } return ' }
Kruskal
Sort edges weight. Add edges whenever it does not create cycle.
14
S V-S u v x S V-S
15 10
S red edge is in the MST Green edge is not in the MST
5 7 2 3 5 4 7
V-S
17 f T* e
S u v (coz all tree has 4 − 1 edges)
18 f T* e
S
Every connected graph has a spanning tree. Hence it has at least 1 − 1 edges.
v u
Case 2
e
S Case 1
! This proves MST is unique if weights are distinct.
20
How to order your input Strategy for every step
Greedy algorithm stays ahead Structural Exchange argument