$ \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}} } $
Gephyrophobia = fear of bridges
Question. How to get from one landmass to another, crossing the fewest possible number of bridges?
Find shortest (fewest hops) route by:
Continue until destination is found
Definition. $G = (V, E)$ a graph, $u, v \in V$ vertices. The graph distance between $u$ and $v$, denoted $d(u, v)$, is the length of the shortest path from $u$ to $v$ in $G$.
What is $d(1, 3)$? What is $d(1, 5)$?
Unweighted version
Input.
Output.
A map $d_u: V \to \{-1, 0, 1, 2, \ldots\}$ such that $d_u(v) = d(u, v)$ is the graph distance from $u$ to $v$
Breadth-First Search
Greedily examine closest vertices that have not yet been examined…
Abstract data type (ADT)
add(x)
adds element x
to queueremove()
removes and returns element BFS(V, E, u):
intialize d[v] <- -1 for all v
d[u] <- 0
queue.add(u)
while queue is not empty do
v <- queue.remove()
for each neighbor w of v do
if d[w] = -1 then
d[w] <- d[v] + 1
queue.add(w)
return d
Theorem. When BFS(V, E, u)
terminates, for every vertex $v \in V$, $d[v]$ stores the distance (minimum number of hops) from $u$ to $v$.
Analysis. Break $V$ into layers
For $i = 0, 1, 2, \ldots$, Define $L_i$ by
Claim. $L_i$ contains precisely the vertices in $V$ at distance $i$ from $u$.
To Show
Idea. Break execution of BFS into phases
Claim. Consider an execution of BFS procedure. Then for every phase $i$:
Proof. Use induction on $i$
Base case $i = 0$. $u$ is the only element in $L_0$, and it is added before any other elements, and $d[u]$ is initialized to $0$.
Suppose claim holds for $j \leq i$ (inductive hypothesis). Then:
BFS(V, E, u):
intialize d[v] <- -1 for all v
d[u] <- 0
queue.add(u)
while queue is not empty do
v <- queue.remove()
for each neighbor w of v do
if d[w] = -1 then
d[w] <- d[v] + 1
queue.add(w)
return d
BFS procedure correctly computes all distances from $u$!
BFS(V, E, u):
intialize d[v] <- -1 for all v
d[u] <- 0
queue.add(u)
while queue is not empty do
v <- queue.remove()
for each neighbor w of v do
if d[w] = -1 then
d[w] <- d[v] + 1
queue.add(w)
return d
Definition. A weighted graph is a graph $G(V, E)$ where each edge $e \in E$ is additionally assigned a (real valued) weight $w(e)$.
Examples.
$G = (V, E)$ a graph, $w$ weights
$P = v_0 e_1 v_1 e_2 v_2 \cdots e_k v_k$ a path
The (weighted) length of $P$ is
\[w(P) = w(e_1) + w(e_2) + \cdots + w(e_k)\]Given weights $w$, define $d_w(u, v)$ to be minimum (weighted) length of any path $P$ from $u$ to $v$.
What is $d_w(1, 3)$? What about $d_w(1, 5)$?
Input.
Output.
A map $d_u: V \to \mathbf{R}$ such that $d_u(v) = d(u, v)$ is the graph distance from $u$ to $v$
Does BFS compute weighted distances from $u$?
Does this work?
BFS succeeds on unweighted graphs because closer vertices are processed before farther vertices
Could we get similar behavior for weighted distances?
must ensure: vertices processed in order of weighted distance from $u$
how can we do this?
How could we efficiently implement a modified procedure?
Dijkstra’s Algorithm