39
Euler Graphs Overview of algorithms for finding Eulerian cycles and paths in connected graphs Deyan Yosifov Telerik Corporation http:// deyan-yosifov.com

Euler Graphs

  • Upload
    adair

  • View
    102

  • Download
    0

Embed Size (px)

DESCRIPTION

Euler Graphs. Overview of algorithms for finding Eulerian cycles and paths in connected graphs. Deyan Yosifov. Telerik Corporation. http:// deyan-yosifov.com. Table of Contents. Defining the problem Euler’s theorem Algorithms: Fleury’s Hierholzer’s Resources Questions. - PowerPoint PPT Presentation

Citation preview

Page 1: Euler Graphs

Euler GraphsOverview of algorithms for finding

Eulerian cycles and paths in connected graphs

Deyan YosifovTelerik

Corporationhttp://deyan-yosifov.com

Page 2: Euler Graphs

Table of Contents1. Defining the problem2. Euler’s theorem3. Algorithms:

Fleury’s Hierholzer’s

4. Resources5. Questions

2

Page 3: Euler Graphs

Defining the problem

Page 4: Euler Graphs

Definitions In graph theory Euler path is a path that visits each node from the graph exactly once.

Euler cycle is a Euler path that starts and ends with the same node.

Euler graph is a graph with graph which contains Euler cycle.

4

Page 5: Euler Graphs

Euler’s theorem

Page 6: Euler Graphs

Euler’s theorem Connected undirected graph is Euler graph if and only if every node in the graph is of even degree (has even number of edges starting from that node).

6

0

4

2

1

5

3

Page 7: Euler Graphs

Euler’s theorem – half proof

Let’s first assume we have Euler cycle in some connected graph. For the example below:

0 -> 1 -> 2 -> 0 -> 3 -> 1 -> 5- > 2 -> 4 -> 3 -> 5 -> 4 -> 0

7

0

4

2

1

5

3

Page 8: Euler Graphs

Euler’s theorem – half proof

Once we “enter” a node, right afterwards we “exit” from it ! => on each visit we add +2 to

node’s degree0 -> 1 -> 2 -> 0 -> 3 -> 1 -> 5- > 2 -> 4 -> 3 -> 5 -> 4 -> 0

8

0

4

2

1

5

3

Page 9: Euler Graphs

Euler’s theorem – half proof

For the first node at the beginning we add +1 to its degree and in the end again we add +1 => All nodes have even degree!0 -> 1 -> 2 -> 0 -> 3 -> 1 -> 5- > 2 -> 4 -> 3 -> 5 ->

4 -> 0

9

0

4

2

1

5

3

Page 10: Euler Graphs

Euler’s theorem – properties

Undirected connected graph has Euler’s path: <=> there are exactly 2 nodes with

odd degree Directed connected graph has Euler’ cycle if and only if d+ = d- for every node. d+ and d- are the number of edges

in and out of the node Directed connected graph has Euler’s path if: there is exactly one node with d+ =

1 + d- and there is exactly one node with

d- = 1 + d+

10

Page 11: Euler Graphs

Algorithms

Page 12: Euler Graphs

Fleury’s algorithm

Page 13: Euler Graphs

Fleury’s algorithm Input: A connected graph G = (V, E)

with no vertices of odd degree Output: A sequence P of vertices and

their connecting edges indicating the Euler circuit.1 Choose any vertex u0 in G.2 P = u03 if Pi = u0e1u1e2…eiui choose edge ei+1 so that

1. ei+1 is adjacent to ei2. Removal of ei+1 does not disconnect G –

{e1,…, ei} (i.e. ei+1 is not a bridge).4 if P includes all edges in E return P5 goto 3

13

Page 14: Euler Graphs

Fleury’s algorithm example (1/3)We start with the following graph

1.Choose any vertex (for example F)

2.Travel from F to C (arbitrary choice).

14

Step 1

Step 2

Start

Page 15: Euler Graphs

Fleury’s algorithm example (2/3)3.Travel from C to D (arbitrary

choice).

4.Travel from D to A (arbitrary choice).

5.Travel from A to C (Can’t go to B!).15

Step 4

Step 5

Step 3

Page 16: Euler Graphs

Fleury’s algorithm example (3/3)Erasing the passed edges makes it

easier to see: It’s impossible to erase edge A->B

in step 5 because this will disconnect the graph and we wouldn’t be able to visit each edge!

The rest of the path is obvious. So the cycle is: F -> C -> D -> A -> C -> E -> A -> B -> D -> F

16

Step 4

Step 5

Page 17: Euler Graphs

Fleury’s algorithm summary

Fleury's algorithm is an elegant but inefficient algorithm. While the graph traversal in

Fleury's algorithm is linear in the number of edges, i.e. O(|E|), we also need to factor in the complexity of detecting bridges.

Linear time bridge-finding algorithm after the removal of every edge will make a time complexity of O(|E|2).

A dynamic bridge-finding algorithm allows this to be improved but this is still significantly slow.

17

Page 18: Euler Graphs

Hierholzer’s algorithm

Page 19: Euler Graphs

Hierholzer’s algorithm Input: A connected graph G = (V, E)

with two or zero vertices of odd degree (depending on whether we are searching for Euler’s path or Euler’s cycle).

Output: The graph with its edges labeled according to their order of appearance in the path found.1 Find a simple cycle C in G.2 Delete the edges belonging in C.3 Apply algorithm to the remaining graph.4 Amalgamate Euler cycles found to obtain the complete Euler cycle.

19

Page 20: Euler Graphs

Hierholzer’s algorithm exampleWe will use two stacks in this

example: tempPath and finalPath in order to be able to combine the simple cycles we’ve found into one big cycle (the searched Euler’s cycle).

20

0

3

45

1

2

Page 21: Euler Graphs

Hierholzer’s algorithm example

1.Lets start with edge 0 (arbitrary choice)

Adding 0 to the tempPath stack.tempPath:0finalPath:<empty>

21

0

3

45

1

2

Page 22: Euler Graphs

Hierholzer’s algorithm example

2.Lets choose edge 0->1 (arbitrary choice).

Erasing the edge and adding 1 to tempPath stacktempPath:0 1finalPath:<empty>

22

0

3

45

1

2

Page 23: Euler Graphs

Hierholzer’s algorithm example

3.Lets choose edge 1->2 (arbitrary choice).

Erasing the edge and adding 2 to tempPath stacktempPath:0 1 2finalPath:<empty>

23

0

3

45

1

2

Page 24: Euler Graphs

Hierholzer’s algorithm example

4.Lets choose edge 2->3 (arbitrary choice).

Erasing the edge and adding 3 to tempPath stacktempPath:0 1 2 3finalPath:<empty>

24

0

3

45

1

2

Page 25: Euler Graphs

Hierholzer’s algorithm example

5.Lets choose edge 3->0 (only possible choice).

Erasing the edge and adding 0 to tempPath stacktempPath:0 1 2 3 0finalPath:<empty>

25

0

3

45

1

2

Page 26: Euler Graphs

Hierholzer’s algorithm example

We’ve created a simple cycle and there is nowhere to go, but there are still unvisited edges! Go back to vertex with unvisited edge, moving elements from tempPath to finalPath.tempPath:0 1 2 3 0finalPath:<empty>

26

0

3

45

1

2

Page 27: Euler Graphs

Hierholzer’s algorithm example

6.Move back from o to 3.Move o from tempPath to finalPath.

tempPath:0 1 2 3finalPath:0

27

0

3

45

1

2

Page 28: Euler Graphs

Hierholzer’s algorithm example

7.Move back from 3 to 2.Move 3 from tempPath to finalPath.

tempPath:0 1 2finalPath:0 3

28

0

3

45

1

2

Page 29: Euler Graphs

Hierholzer’s algorithm example

8.Lets choose edge 2->4 (arbitrary choice).

Erasing the edge and adding 4 to tempPath stacktempPath:0 1 2 4finalPath:0 3

29

0

3

45

1

2

Page 30: Euler Graphs

Hierholzer’s algorithm example

9.Lets choose edge 4->1 (only possible choice).

Erasing the edge and adding 1 to tempPath stacktempPath:0 1 2 4 1finalPath:0 3

30

0

3

45

1

2

Page 31: Euler Graphs

Hierholzer’s algorithm example

10.Lets choose edge 1->5 (only possible choice).

Erasing the edge and adding 5 to tempPath stacktempPath:0 1 2 4 1 5finalPath:0 3

31

0

3

45

1

2

Page 32: Euler Graphs

Hierholzer’s algorithm example

11.Lets choose edge 5->2 (only possible choice).

Erasing the edge and adding 2 to tempPath stacktempPath:0 1 2 4 1 5 2finalPath:0 3

32

0

3

45

1

2

Page 33: Euler Graphs

Hierholzer’s algorithm example

So we have passed through all the edges! Now all we have to do is move elements from tempPath stack to finalPath stack!tempPath:0 1 2 4 1 5 2finalPath:0 3

33

0

3

45

1

2

Page 34: Euler Graphs

Hierholzer’s algorithm example

The result sequence in finalPath stack is an Euler’s cycle!

tempPath:0 1 2 4 1 5 2finalPath:0 3

34

0

3

45

1

2

tempPath:<empty>finalPath:0 3 2 5 1 4 2 1 0

Page 35: Euler Graphs

Hierholzer’s algorithm summary

Hierholzer's algorithm is an elegant and efficient algorithm. The algorithm takes about linear

time. Can be easily changed to find all

Euler’s cycles if necessary • when we meet the word “arbitrary

choice” in previous example, we may use recursion on all possible choices

Can be applied for searching Euler’s path as well• must start from a vertex with odd

degree.

35

Page 37: Euler Graphs

Summary

37

Euler’s path is a path passing through each graph’s edge exactly once.

An undirected graph has an Euler’s cycle if and only if every vertex has even degree, and all of its vertices with nonzero degree belong to a single connected component.

For finding an Euler’s cycle or path the fastest way is by using Hierholzer’s constructive algorithm.

Page 38: Euler Graphs

форум програмиране, форум уеб дизайнкурсове и уроци по програмиране, уеб дизайн – безплатно

програмиране за деца – безплатни курсове и уроцибезплатен SEO курс - оптимизация за търсачки

уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop

уроци по програмиране и уеб дизайн за ученициASP.NET MVC курс – HTML, SQL, C#, .NET, ASP.NET MVC

безплатен курс "Разработка на софтуер в cloud среда"

BG Coder - онлайн състезателна система - online judge

курсове и уроци по програмиране, книги – безплатно от Наков

безплатен курс "Качествен програмен код"

алго академия – състезателно програмиране, състезанияASP.NET курс - уеб програмиране, бази данни, C#, .NET, ASP.NET

курсове и уроци по програмиране – Телерик академия

курс мобилни приложения с iPhone, Android, WP7, PhoneGapfree C# book, безплатна книга C#, книга Java, книга C# Дончо Минков - сайт за програмиране

Николай Костов - блог за програмиранеC# курс, програмиране, безплатно

?? ? ?

??? ?

?

? ?

??

?

?

? ?

Questions?

?

Euler Graphs

http://algoacademy.telerik.com

Page 39: Euler Graphs

Free Trainings @ Telerik Academy

“C# Programming @ Telerik Academy csharpfundamentals.telerik.com

Telerik Software Academy academy.telerik.com

Telerik Academy @ Facebook facebook.com/TelerikAcademy

Telerik Software Academy Forums forums.academy.telerik.com