$ \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?
Question. Is it possible to walk around Königsberg, cross every bridge exactly once, and return to where you started?
Theorem (Euler, 1736). No.
A graph $G = (V, E)$ consists of
Example. Königsberg graph
Note. Terminology varies from source to source.
A path $P$ of length $k$ in $G$ is a sequence of the form $v_0 e_1 v_1 e_2 v_2 \cdots e_k v_k$ where
$G$ is connected if for every pair of vertices $u, v \in V$, there is a path from $u$ to $v$.
$P$ is a circuit if $v_0 = v_k$.
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?
Question 1. Under what conditions is a graph $G$ Eulerian?
Question 2. If $G$ is Eulerian, how can we find an Eulerian circuit?
Let $G = (V, E)$ be a graph, and $v \in V$ a vertex. The degree of $v$, $\deg(v)$ is the number of edges in $E$ incident to $v$.
Claim (Euler 1736). If $G$ is Eulerian, then every vertex has even degree.
Why?
If all vertices have even degrees, is $G$ necessarily Eulerian?
Theorem (Euler 1736). If every vertex $v$ in a graph $G$ has even degree and $G$ is connected, then $G$ is Eulerian.
Proof technique:
Algorithmic technique:
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. If every vertex in $G = (V, E)$ has even degree, then FindCircuit(V, E, v)
returns a circuit beginning and ending at $v$.
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,
More (greedy) graph algorithms!