$ \def\compare{ {\mathrm{compare}} } \def\swap{ {\mathrm{swap}} } \def\sort{ {\mathrm{sort}} } \def\insert{ {\mathrm{insert}} } \def\true{ {\mathrm{true}} } \def\false{ {\mathrm{false}} } \def\BubbleSort{ {\mathrm{BubbleSort}} } \def\SelectionSort{ {\mathrm{SelectionSort}} } \def\Merge{ {\mathrm{Merge}} } \def\MergeSort{ {\mathrm{MergeSort}} } \def\QuickSort{ {\mathrm{QuickSort}} } \def\Split{ {\mathrm{Split}} } \def\Multiply{ {\mathrm{Multiply}} } \def\Add{ {\mathrm{Add}} } \def\cur{ {\mathrm{cur}} } \def\gets{ {\leftarrow} } $
Single Source Shortest Paths
Dijkstra’s Algorithm:
Winter is coming to Königsberg!
Question. How to find the cheapest set of bridges to de-ice that maintain connectivity?
Input:
Output:
a set $F$ of edges in $E$ such that
The graph $T = (V, F)$ is called a minimum spanning tree of $G$
Recall. A tree $T$ is a graph that:
Input:
Output:
a set $F$ of edges in $E$ such that
Question. Why must $T = (V, F)$ be a tree?
Claim. Suppose $T$ is an MST for a weighted graph $G$. Then $T$ is a tree.
Proof. Argue by contradiction.
Grow a tree outward from a seed vertex:
Question. How should we pick the “next” edge?
What information do we need to maintain in order to implement this procedure?
PrimMST(V, E):
initialize set S = {v} with v arbitrary
initialize set F = {} of MST edges, priority queue Q
for each neighbor x of v
add (v, x) to Q with priority w(v, x)
while Q is not empty
(u, v) <- removeMin(Q)
if S doesn't contain v
add (u, v) to F
for each neighbor x of v
add (v, x) to Q with priority w(v, x)
return (S, F)
Why is graph returned by Prim a tree?
PrimMST(V, E):
initialize set S = {v} with v arbitrary
initialize set F = {} of MST edges, priority queue Q
for each neighbor x of v
add (v, x) to Q with priority w(v, x)
while Q is not empty
(u, v) <- removeMin(Q)
if S doesn't contain v
add (u, v) to F
for each neighbor x of v
add (v, x) to Q with priority w(v, x)
return (S, F)
Why is graph returned by Prim a MST?
Must show. Every edge added to $F$ is in an MST.
Definition. Let $G = (V, E)$ be a graph. A cut in $G$ is a partition of $V$ into two (non-empty) subsets $U$ and $V - U$.
Cut Claim. Suppose:
Then:
PrimMST(V, E):
initialize set S = {v} with v arbitrary
initialize set F = {} of MST edges, priority queue Q
for each neighbor x of v
add (v, x) to Q with priority w(v, x)
while Q is not empty
(u, v) <- removeMin(Q)
if S doesn't contain v
add (u, v) to F
for each neighbor x of v
add (v, x) to Q with priority w(v, x)
return (S, F)
Cut claim $\implies$ Prim produces an MST.
Why?
Consider $k$th edge $e_k$ added by Prim
What can we say about the cut $S_k, V - S_k$?
First Conclusion. Every edge $e$ added by Prim’s algorithm is in the MST.
Still to show. All MST edges are added.
Why is this so?
Conclusion. Prim’s algorithm produces an MST.
PrimMST(V, E):
initialize set S = {v} with v arbitrary
initialize set F = {} of MST edges, priority queue Q
for each neighbor x of v
add (v, x) to Q with priority w(v, x)
while Q is not empty
(u, v) <- removeMin(Q)
if S doesn't contain v
add (u, v) to F
for each neighbor x of v
add (v, x) to Q with priority w(v, x)
return (S, F)
Prim’s algorithm:
Prim’s algorithm is greedy
Another greedy MST algorithm!