Circuits(Eulerian and Hamiltonian)
Ioan Despi
University of New England
July 26, 2013
Outline
1 Recall
2 Bridges of Konigsberg
3 The Traveling Salesperson Problem
4 Eulerian
5 Euler’s Theorem
6 Hamiltonian
7 Fleury’s Algorithm
Ioan Despi – Discrete Mathematics 2 of 31
Recall
A graph is a relational structure made up of vertices and edges.I The edges of a graph express the relationships among the vertices.
An edge that connects a vertex to itself is a loop.
Two vertices are adjacent if there is an edge joining them.I If a vertex has a loop, the vertex is adjacent to itself!
The degree of a vertex is the total number of edges at that vertex.I A loop adds 2 to the degree of a vertex!
A vertex with degree 0 is an isolated vertex.
A path (walk) in a graph is a route among vertices along the graph’sedges such that no edge is used more than once.
I Paths are specified by listing adjacent vertices from the beginning of thepath to the end of the path.
A path that starts and ends at the same vertex is called a circuit.
Ioan Despi – Discrete Mathematics 3 of 31
Recall (cont.)
A graph is connected if any two of its vertices can be joined by a path.
A graph that is not connected is disconnected.I The resulting pieces of a disconnected graph are called components of the
graph.
If erasing an edge of a connected graph would cause the graph to bedisconnected, that edge is called a bridge.
Ioan Despi – Discrete Mathematics 4 of 31
Bridges of Konigsberg
The city of Koningsberg occupied both banks and two islands of a river. Theislands and the riverbanks were connected by seven bridges, as indicated inFigure 1.The question is if there is a walk around the city that crosses eachbridge exactly once.
Figure 1 : The city of KoningsbergIoan Despi – Discrete Mathematics 5 of 31
Bridges of Konigsberg (cont.)
Your tasks are:
1 Represent the problem as a graph.
2 Represent the problem using predicate calculus: devise a predicate anduse it to represent facts and rules in a Prolog-style manner.(Prolog is a programming language used in Artificial Intelligence)
3 Are the two representations equivalent?
4 Try to solve the problem (optional).
Ioan Despi – Discrete Mathematics 6 of 31
Trying to solve the problem. . .(from http://mathforum.org/isaac/problems/bridges1.html)
Figure 2 : First attempt Figure 3 : Second attempt
Ioan Despi – Discrete Mathematics 7 of 31
Trying to solve the problem. . .What if we delete a bridge?
Figure 4 : Third attempt
Ioan Despi – Discrete Mathematics 8 of 31
Bridges of Konigsberg (solution)
Euler invented graph theory to solve this problem.We will use riverbanks (rb1 and rb2) and islands (i1 and i2) as nodes inthe graph.The bridges are represented by labeled arcs between nodes (b1, b2, . . . ,b7).We ignore bridge lengths, distances, aso.
Figure 5 : The graph of the city of Koningsberg bridge systemIoan Despi – Discrete Mathematics 9 of 31
Bridges of Konigsberg (solution)
The connect/3 predicate corresponds to an arc of the graph, assertingthat two land pieces are connected by a particular bridge.As each bridge requires two facts, one for each direction in which thebridge may be crossed, we can use a rule
connect(X, Y, Z) = connect(Y, X, Z).
to specify that bridge Z can be crossed in both ways, from land piece Xto land piece Y and vice-versa.Then we have the following facts:
connect(i1, i2, b1).
connect(rb1, i1, b2).
connect(rb1, i1, b3).
connect(rb1, i2, b4).
connect(rb2, i1, b5).
connect(rb2, i1, b6).
connect(rb2, i2, b7).
The predicate calculus representation is equivalent to the graphrepresentation in that the connectedness is preserved. An algorithm cantranslate between the two representations with no loss of information.
Ioan Despi – Discrete Mathematics 10 of 31
Bridges of Konigsberg (the truth)
The walk is impossible.Euler defined the degree of a node – odd or even – depending on thenumber of arcs joining it to neighbouring nodes.If the degree of a node is odd, then the traveler could not exit the nodewithout using a previously traveled arc.So, with the exception of starting and ending nodes, the desired walkwould have to leave each node exactly as often as it entered it, that is thenodes should have an even degree.As Euler noticed, the graph should have exactly zero or two nodes of odddegree for the walk to be possible.Bridges of Konigsberg problem has four nodes of odd degree so the walkis impossible.
Ioan Despi – Discrete Mathematics 11 of 31
The Traveling Salesperson Problem [TSP]
Given a number of cities to be visited and the mileage between each pair ofcities, find a minimum-mileage trip beginning and ending at city A that visitseach of the other cities exactly once. Use the following sample mileage chart tocreate the corresponding state-space graph and (then) to find the solution.
A B C DA – 30 50 40B – 30 35C – 25D –
30
30
25
40
5035
B
A
C
D
Ioan Despi – Discrete Mathematics 12 of 31
The Traveling Salesperson Problem (solution)
The search space contains (4 − 1)! = 3! = 6 paths:
A
AB AC AD
ABC
ABCD
ABCDA
ABD
ABDC
ABDCA
ACB
ACBD
ACBDA
ACD
ACDB
ACDBA
ADB
ADBC
ADBCA
ADC
ADCB
ADCBA
3050
40
3035 30 25 35 25
2525 35 35 30 30
4050 40 30 50 30
125 140 155140
155125
Ioan Despi – Discrete Mathematics 13 of 31
The Traveling Salesperson Problem (solution)
This is an example of touring problem.
Because different paths to the same city represent distinct partialsolutions, each state is identified not just as a city name but as a list ofthe cities visited so far.
So, the initial state is (In A; visited A) and a typical intermediate statewould be (In B; visited A, C, B).
The goal test would check whether the agent is in A and all four citieshave been visited.
The desired solution is A – D – C – B – A or its reversal, A – B – C – D –A, with a total mileage of 125.
Ioan Despi – Discrete Mathematics 14 of 31
Useful Definitions
Let 𝐺 be a connected graph.
Eulerian Path.A path that contains every edge of a connected graph exactly once, or
A walk from one vertex to another, that passes all vertices of G andtraverses exactly once every edge of G.
Eulerian Circuit.An Eulerian path that starts and ends at the same vertex, or
A circuit that includes all vertices and edges of a graph G, or
A circuit passing through every edge just once (and every vertex at leastonce).
Eulerian Graph.
A graph which has an Eulerian circuit.
Ioan Despi – Discrete Mathematics 15 of 31
Useful Definitions
Remember:I An Eulerian circuit is a type of Eulerian path, butI An Eulerian path is not necessarily an Eulerian circuit.
The left graph (Figure 6) has an Eulerian cycle (circuit):a, c, d, e, c, b, a
The right graph (Figure 7) has an Eulerian pathb, a, e, d, b, e
a b
c
d e
Figure 6 : Eulerian circuit
a b
de
Figure 7 : Eulerian path
Ioan Despi – Discrete Mathematics 16 of 31
Euler’s Theorem
Theorem (Euler).
Let 𝐺 be a connected graph.
1 𝐺 has an Eulerian circuit (i.e., is Eulerian) if and only if every vertex of 𝐺has even degree.
2 𝐺 has an Eulerian path, but not an Eulerian circuit, if and only if 𝐺 hasexactly two vertices of odd degree.
I The Eulerian path in this case must start at any of the two ’odd-degree’vertices and finish at the other one ’odd-degree’ vertex.
Proof.We only consider the case (i). Euler proved necessity in 1736.
Necessity is trivial: 𝐺 Eulerian =⇒ all vertices have even degree.
Let C be an Eulerian (circuit) path of G and 𝑣 an arbitrary vertex of𝐺. Then, each edge in 𝐶 that enters 𝑣 must be followed by an edge in𝐶 that leaves 𝑣, hence the total number of edges incident at 𝑣 mustbe even, therefore 𝑣 has an even degree.
Ioan Despi – Discrete Mathematics 17 of 31
Euler’s Theorem (cont.)
Proof.
In 1873 (137 years later), a young German mathematician, Carl Hierholzerpublished the proof of sufficiency.
Sufficiency : all vertices have even degree =⇒ 𝐺 Eulerian
Proof by induction.
Let 𝑆𝑛 be the statement that a connected graph of 𝑛vertices must be Eulerian if every vertex has even degree.for 𝑛 = 1, G contains either a single vertex or a single vertexwith loops. Hence, 𝑆1 is true, because an Eulerian circuitcan be obtained by traversing all loops (if any) one by one.The smallest possible number of edges is 3 (i.e., a triangle)in the case of simple graphs, and 2 (i.e., a digon) in the caseof multigraphs. In both cases, the graph trivially containsan Eulerian circuit.
Ioan Despi – Discrete Mathematics 18 of 31
Euler’s Theorem (cont.)
Proof.Assume 𝑆𝑘 is true, that isLet 𝐻 be a connected graph with ≤ 𝑘 edges. If every vertex
of H has even degree, H contains an Eulerian circuit.
Let 𝐺 be a graph with 𝑘 + 1 edges, and every vertex has an even degree.
Since there is no odd degree vertex, 𝐺 cannot be a tree. Thus, 𝐺 mustcontain a cycle 𝐶.
We start with any vertex and choose a next vertex from all adjacentvertices in random order. We continues in this manner. After finitenumber of steps, we will end in the vertex we started with.
I If we traverse all edges, we build an Eulerian cycle,I otherwise, consider a subgraph 𝐻 obtained from the original graph by
deleting that cycle.F Each vertex of 𝐻 has even degree. In the above left picture (Figure 6),
graph 𝐻 might consist of vertices 𝑐, 𝑑, 𝑒.F The original graph and 𝐻 must have one vertex in common. We start with
this vertex and repeat the procedure. We repeat all these until all edges aretraversed.
Ioan Despi – Discrete Mathematics 19 of 31
Euler’s Theorem (in other words. . . )
Proof.Choose any starting vertex 𝑣, and follow a path of edges from that vertexuntil returning to 𝑣.
The even degree of all vertices ensures that, when the path enters anothervertex 𝑤, there must be an unused edge leaving 𝑤.
Therefore, it is not possible to get stuck at any vertex other than 𝑣.
The walk formed in this way is a closed one, but may not cover all thevertices and edges of the initial graph.
As long as there exists a vertex 𝑣 that belongs to the current tour butthat has adjacent edges not part of the tour, start another trail from 𝑣,following unused edges until returning to 𝑣, and join the tour formed inthis way to the previous tour.
Ioan Despi – Discrete Mathematics 20 of 31
Examples
1 Due to the above Euler’s theorem, the seven bridge problem has nosolution, because all vertices (𝑉 = {𝑖1, 𝑖2, 𝑟𝑏1, 𝑟𝑏2}) have odd degrees.
2 In the graph from Figure 8: 𝑣1 − 𝑒1 − 𝑣2 − 𝑒2 − 𝑣2 − 𝑒3 − 𝑣3 − 𝑒4 − 𝑣1 isan Eulerian circuit.
3 The graph in Figure 9 has an Eulerian path but is not Eulerian.
v1
v2
v3
e1e3
e4
e2
Figure 8 : Eulerian circuit
Figure 9 : Eulerian path
4 𝐾3 is an Eulerian graph, but 𝐾4 is not Eulerian.
Ioan Despi – Discrete Mathematics 21 of 31
Hamiltonian Circuit
Hamiltonian Circuit.
A circuit that includes every vertex (except first/last vertex) of G exactlyonce.
A circuit that visits each vertex once without touching any vertex morethan once.
A cycle that traverses every vertex of a graph exactly once.
Hamiltonian Path.A walk that includes every vertex of G exactly once.
An Hamiltonian path is therefore not a circuit.
There are no known conditions which are both necessary and sufficient.
There are a few sufficient considitons.
For a graph to have a Hamiltonian circuit, the degree of each vertex mustbe two or more.
Ioan Despi – Discrete Mathematics 22 of 31
Examples
Notice thatI for an Euler path you may visit each vertex more than once andI in a Hamilton path it is not necessary to travel every edge.
The best known algorithm for finding a Hamiltonian cycle has anexponential worst-case complexity.In the graph from Figure 8: 𝑣1 − 𝑒1 − 𝑣2 − 𝑒2 − 𝑣2 − 𝑒3 − 𝑣3 − 𝑒4 − 𝑣1 isan Eulerian circuit but not an Hamiltonian circuit.In the graph from Figure 8: 𝑣1 − 𝑒1 − 𝑣2 − 𝑒3 − 𝑣3 − 𝑒4 − 𝑣1 is aHamiltonian circuit, but not an Eulerian circuit.
v1
v2
v3
e1e3
e4
e2
Figure 10 : Eulerian circuit
Figure 11 : Icosian puzzle, invented byHamilton (1857)Ioan Despi – Discrete Mathematics 23 of 31
Examples (cont.)
Figure 12 is an Eulerian circuit but not an Hamiltonian circuit.
Figure 13 is an Hamiltonian circuit but not an Eulerian circuit.
Figure 12 : Eulerian circuit but notHamiltonian
Figure 13 : Hamiltonian circuit, but notEulerian
Ioan Despi – Discrete Mathematics 24 of 31
Examples (cont.)
Pictures (a) and (b) have no nodes of odd degree and must, therefore,have an Euler circuit.
For instance, the circuit B, C, D, A, C, A, B for picture (b).
Nnote that the A-C-A portion of the circuit can be traversed in either oftwo ways.
A B
H
G
IJ
N
K L
M
F
E
C D
(a)
A
BC
D
(b)
Ioan Despi – Discrete Mathematics 25 of 31
Examples (cont.)
Pictures (c) and (d) have exactly two nodes of odd degree, thus they mustcontain an Euler path.For instance, A, B, D, E, C, A, F, C, B, E, F is an Euler path for picture(d).It is impossible to find an Euler path that does not begin at A andterminate at F (or vie-versa).
A B
(c)A
D
C
E
F
(d)
BIoan Despi – Discrete Mathematics 26 of 31
Examples (cont.)
Pictures (e) and (f) have four nodes of odd degree, and thus neither anEuler circuit nor an Euler path can be found on either.
Picture (f) is actually the representation of the Konigsberg bridgesproblem.
A B
D E
(e)
A
B C
D(f)
Ioan Despi – Discrete Mathematics 27 of 31
Examples (cont.)
For the travelling salesman problem, we need to find all the Hamiltoniancircuits for the graph, calculate the respective total distance and thenchoose the shortest route.
route total distance𝐴 → 𝐵 → 𝐶 → 𝐷 → 𝐴 30 + 30 + 25 + 40 = 125𝐴 → 𝐵 → 𝐷 → 𝐶 → 𝐴 140𝐴 → 𝐶 → 𝐵 → 𝐷 → 𝐴 155𝐴 → 𝐶 → 𝐷 → 𝐵 → 𝐴 140𝐴 → 𝐷 → 𝐵 → 𝐶 → 𝐴 155𝐴 → 𝐷 → 𝐶 → 𝐵 → 𝐴 125
Hence the best route is either 𝐴𝐵𝐶𝐷𝐴 or 𝐴𝐷𝐶𝐵𝐴.
Ioan Despi – Discrete Mathematics 28 of 31
Fleury’s AlgorithmFinding an Eulerian Path or Circuit
Fleury’s Algorithm1 Check if the graph is connected and it has either 0 or 2 odd vertices.
Reject otherwise.
2 If there are 0 odd vertices, pick any vertex to start.If there are 2 odd vertices, start at one of them.Call it 𝑣start.
3 While the graph contains at least one edge:1 If you have a choice, always pick an edge that is not a bridge.2 Traverse that edge, and remove it from G.
Ioan Despi – Discrete Mathematics 29 of 31
Proof of Correctness
To see that this algorithm terminates, suppose we are stuck at somevertex 𝑣 before all the edges are removed.
Then either 𝑣 must have an odd degree, or 𝐺 is not a connected graph tobegin with.
Both of these cases are impossible due to our first check. This is acontradiction.
Note that, when the algorithm terminates, we must return to 𝑣startbecause every vertex has an even degree.
Furthermore, the edge removal operation guarantees that each edge isvisited exactly once.
Therefore, the discovered tour is an Eulerian circuit.
Ioan Despi – Discrete Mathematics 30 of 31
ExampleFind an Eulerian path for the grapth in Figure 14
v1
v2
v3
v4
v6
v7
e1
e2 e3
e4
e5
e7
e8
e9
v5
e6
Figure 14 : Fleury’s Algorithm
We start at 𝑣5 because 𝛿(𝑣5) is odd.We can’t choose edge 𝑒5 to travel next because the removal of 𝑒5 breaks 𝐺into two disconnected parts.We can choose 𝑒6 or 𝑒7 or 𝑒9.; we choose 𝑒6.One Eulerian path is
𝑣5𝑒6−→ 𝑣5
𝑒7−→ 𝑣6𝑒8−→ 𝑣7
𝑒9−→ 𝑣5𝑒5−→ 𝑣4
𝑒3−→ 𝑣3𝑒2−→ 𝑣2
𝑒1−→ 𝑣1𝑒4−→ 𝑣4
Ioan Despi – Discrete Mathematics 31 of 31