$ \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}} } $
Question. Is it possible to walk around Königsberg, cross every bridge exactly once, and return to where you started?
Original Question. Is it possible to walk around Königsberg, cross every bridge exactly once, and return to where you started?
Rephrasing as Graph Problem. Given a graph $G = (V, E)$, is there a circuit that contains every edge $e \in E$ exactly once?
Theorem (Euler 1736). $G$ is Eulerian if and only if $G$ is connected and every vertex has even degree.
Assume:
Strategy:
Input:
Output:
FindCircuit
Subroutine FindCircuit(V, E, v):
cur <- v
P <- v
while deg(cur) > 0
e <- any edge in E incident to cur
(prev, cur) <- e
append e, cur to P
remove e from E
if deg(prev) = 0 then remove prev from V
endwhile
remove cur from V
return P
Claim 1. If every vertex in $G = (V, E)$ has even degree, then FindCircuit(V, E, v)
returns a circuit beginning and ending at $v$.
$C = $ circuit found, removed after FindCircuit(V, E, v)
Question. What can we say about remaining graph?
Claim 2. Remaining graph $G - C$ has all even degrees.
Why?
Strategy.
FindCircuit
to find a circuit $P = v_0 e_1 v_1\cdots v_k$FindCircuit
to $v_i$ to get a circuit $Q$ EulerCircuit(V, E, v):
P <- FindCircuit(V, E, v)
for each edge e = (u, w) in P do
if deg(w) > 0 then
Q <- EulerianCircuit(V, E, w)
Splice(P, Q, w)
endif
endfor
Claim. If $G$ is even and connected, then EulerCircuit
returns an Eulerian circuit.
Argue by induction on $m = $ number of edges in $G$.
Base Case, $m = 0$. If $G$ is connected and has no edges, then $G$ has only one vertex, so EulerCircuit
correctly outputs an Eulerian circuit (of length $0$)
Suppose EulerCircuit
finds an Eulerian circuit on all connected, even graphs with fewer than $m$ edges. Then:
EulerCircuit
finds Eulerian circuit in each component$G$ is Eulerian if and only if $G$ is even and connected.
If $G$ is Eulerian, then an Eulerian circuit can be found by greedily traversing the graph, and recursively finding Eulerian circuits on remaining components.
Gephyrophobia = fear of bridges
Question. How to get from one landmass to another, crossing the fewest possible?
Find shortest (fewest hops) route by:
Continue until destination is found
Unweighted version
Input.
Output.
A map $d: V \to \{-1, 0, 1, 2, \ldots\}$ such that $d(v)$ is length of shortest path (fewest hops) from $u$ to $v$
Breadth-First Search
Greedily examine closest vertices that have not yet been examined…
Abstract data type (ADT)
enqueue(x)
adds element x
to queuedequeue()
removes and returns element BFS(V, E, u):
intialize d[v] <- -1 for all v
d[u] <- 0
queue.enqueue(u)
while queue is not empty do
v <- queue.dequeue()
for each neighbor w of v do
if d[w] = -1 then
d[w] <- d[v] + 1
queue.enqueue(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
Claim 1. A vertex $v$ is in $L_k$ if and only if the shortest path from $u$ to $v$ has length $k$.
Claim 2. In an execution of BFS the following hold for every $k$:
Proofs: use induction on $k$ (details in text)
Conclusion. BFS correctly computes $d[v]$ for every v.
To find shortest paths (fewest hops) from $u$ to all other vertices: