<<

10/18/2014

Ma/CS 6a Class 8: Eulerian Cycles

By Adam Sheffer

The Bridges of Königsberg

 Can we travel the city while crossing every exactly once?

1 10/18/2014

How Was Born

Leonhard Euler 1736

Eulerian

 An Eulerian in a graph 퐺 is a path that passes through every edge of 퐺 exactly once.  An Eulerian cycle is an Eulerian path that starts and ends at the same vertex.

a b

h c 퐸 = 푎 → 푏 → 푐 → 푑 → 푒 → 푓 → 푔 → 푕 → 푎 → 푑 → 푕 → 푒 → 푏 g d → 푔 → 푐 → 푓 → 푎 f e

2 10/18/2014

Is There an Eulerian Cycle in the Graph?

퐾7 Königsberg

What Graphs Contain an Eulerian Cycle?  Claim. An undirected connected (possibly not simple) graph 퐺 = 푉, 퐸 contains an Eulerian cycle if and only if every vertex of 푉 has an even .

3 10/18/2014

Proving One Direction

 Assume that 퐺 contains an Eulerian cycle and prove that the degrees are even:  Choose an arbitrary Eulerian cycle and traverse it starting a vertex 푠 ∈ 푉.

 For every 푢 ∈ 푉, let 푘푢 denote the number of times that we visit 푢. ◦ Each time we visit 푢, we enter through one edge and leave through another. Thus, deg 푢 = 2푘푢. ◦ The only exception is deg 푠 = 2푘푠 − 2.

Proving the Other Direction

 Assume that every vertex has an even degree and prove that there exists an Eulerian cycle. ◦ We prove the claim by presenting an algorithm that always finds such a cycle.

4 10/18/2014

The Algorithm – Part 1

 Choose an arbitrary vertex 푣 ∈ 푉 and start a path from it. ◦ At each step, choose an edge, cross it, and throw it away from the graph. ◦ Stop only when returning to 푣. 푏 푎 푐 푎 → 푓 → 푑 → 푎 푓 푑 푒

Correctness

 Claim. As long as we did not return to 푣, we cannot get stuck:  Proof. For any vertex 푢 ∈ 푉 ∖ 푣 , we claim that we cannot get stuck in 푢: ◦ Before every visit of 푢, it has an even degree.

◦ While visiting 푢, it has an odd degree.

◦ It is impossible to visit 푢 when it has degree 0.

5 10/18/2014

The Algorithm – Part 2

 If the cycle that we found contains every edge of the graph, we are done.  Otherwise, one of the vertices that we visited still has a positive degree. ◦ Because the graph is connected!

 Choose such a vertex and traverse 푏 the graph as before, until we 푎 푐 return to our starting point. 푓 → 푒 → 푑 → 푐 → 푓 푓 푑 푒

The Algorithm – Part 2 (cont.)

 We now have two edge-disjoint cycles, with at least one common vertex between them. We combine them into one cycle. af  d  a

푏 푎 푐 f e d c f

푓 푑 af e d c f d  a 푒

6 10/18/2014

The Algorithm – Part 3

 Repeat part 2 until no edges remain in the graph.

af e d c f d  a

푏 푎 푐 c  b  ac

푓 푑           푒 a f e d c b a c f d a

Correctness of the Algorithm

 Already proved: The algorithm cannot get stuck while constructing a cycle.  Since 퐺 is connected, if there are remaining edges at the beginning of a step, at least one edge must be connected to the existing cycle.  Termination. At the end of each step we obtain a cycle with a larger number of edges. Thus, the process terminates after ≤ |퐸| steps.

7 10/18/2014

Eulerian Cycles in Directed Graphs

 Does the even degree condition still hold in a ? 푏

푎 푐

NO! 푓 푑

푒  What should the new condition be? ◦ Indegree = Outdegree.

Eulerian Paths

 Claim. A connected undirected graph contains an Eulerian path (which is not a cycle) if and only if it contains exactly two vertices with odd degrees.

 Recall. An Eulerian path is an Eulerian cycle that does not necessarily start and end in the same vertex.

8 10/18/2014

Example: Eulerian Paths

 

Proof – One Direction

 Assume that a (non-cycle) Eulerian path exists and show that exactly two vertices have an odd degree: ◦ Similarly to the previous proof, we travel along the path. ◦ A vertex that we visit 푘 times has degree 2푘. ◦ If we visited the first/last vertex 푘 times, it is of degree 2푘 − 1.

9 10/18/2014

Proof – The Other Direction

 Assume that exactly two vertices have an odd degree and prove that an Eulerian path exists: ◦ Add a new edge between these two vertices. ◦ We obtain a graph with no odd degrees. Thus, it contains an Eulerian cycle. ◦ Removing the new edge turns this cycle to an Eulerian path in the origianl graph.

Covering with Several Paths

 Problem. Let 퐺 = 푉, 퐸 be a connected (not necessarily simple) graph with exactly 2푘 vertices of an odd degree. Prove that the edges of the graph can be covered by 푘 edge-disjoint paths.

10 10/18/2014

Solution

 By induction on 푘: ◦ Basis: when 푘 = 1, this is the case of a graph containing an Eulerian path. ◦ Step: Assume for 2푘 − 2 and prove for 2푘. ◦ Add an edge 푒 between two vertices with odd degrees. The resulting graph has 2푘 − 2 vertices with odd degrees. ◦ Induction hypothesis: the edges of the graph can be covered by 푘 − 1 edge disjoint paths. ◦ Removing 푒 may split one path into two, resulting in 푘 edge disjoint paths

Running Times

 Given a graph 퐺 = 푉, 퐸 with 푛 = 푉 + 퐸 . ◦ Algorithm A computes something on 퐺 in 105푛2 steps. ◦ Algorithm B computes the same thing in 10−2푛3 steps.  Which algorithm is better? ◦ Algorithm A is better when 푛 ≥ 107. ◦ Asymptotic computational complexity – we only care about large values of 푛.

11 10/18/2014

Asymptotic complexity – Shortest Paths  Given a graph 퐺 = 푉, 퐸 and vertices 푠, 푡 ∈ 푉, we wish to find the shortest path between 푠 and 푡. ◦ Go over every possible path in 퐺. There could be about 푉 ! such simple paths. ◦ BFS from 푠 – finds the shortest path in at most 푐 푉 + |퐸| time (for some constant 푐).  Which is better? ◦ Since 퐺 is simple, 퐸 < 푉 2. ◦ 푉 2 is MUCH better than 푉 !.

Checking Some Values of |푉|

|V| ퟏퟎퟔ 푽 ퟐ |V|! 1 106 1 5 2.5 ⋅ 107 120 10 108 3.6 ⋅ 106 50 2.5 ⋅ 109 ~3 ⋅ 1064 100 1010 ~9 ⋅ 10157 1000 1012 ~4 ⋅ 102567

12 10/18/2014

What Can a Computer Do?

1016 ⋅ 3 ⋅ 108 = 3 ⋅ 1024. Second per year

In This Course

 We will not seriously analyze running times of algorithms.  We will consider a running time to be “reasonable” if it is polynomial in 푛 (the size of the input).  For example, 푐푛!, 푐2푛, 푐2 푛 are not reasonable running times.

13 10/18/2014

The End

I need more jokes for last slides! Send me stuff!

14