Eulerian Tour What is a Eulerian tour and how to find one.

37
Eulerian Tour Eulerian Tour What is a Eulerian tour What is a Eulerian tour and how to find one and how to find one

Transcript of Eulerian Tour What is a Eulerian tour and how to find one.

Eulerian TourEulerian Tour

What is a Eulerian tour and What is a Eulerian tour and how to find onehow to find one

Sample ProblemSample Problem

Farmer John owns a large number of fences, Farmer John owns a large number of fences, which he must periodically check for integrity.which he must periodically check for integrity.

Farmer John keeps track of his fences by Farmer John keeps track of his fences by maintaining a list of their intersection points, maintaining a list of their intersection points, along with the fences which end at each along with the fences which end at each point.point.

Each fence has two end points, each at an Each fence has two end points, each at an intersection point, although the intersection intersection point, although the intersection point may be the end point of only a single point may be the end point of only a single fence.fence.

Of course, more than two fences might share Of course, more than two fences might share an endpoint.an endpoint.

Sample ProblemSample Problem

Given the fence layout, calculate if there Given the fence layout, calculate if there is a way for Farmer John to ride his horse is a way for Farmer John to ride his horse to all of his fences without riding along a to all of his fences without riding along a fence more than once.fence more than once.

Farmer John can start and end anywhere, Farmer John can start and end anywhere, but cannot cut across his fields (the only but cannot cut across his fields (the only way he can travel between intersection way he can travel between intersection points is along a fence).points is along a fence).

If there is a way, find one way. If there is a way, find one way.

Eulerian TourEulerian Tour

Given an undirected graph.Given an undirected graph. Find a path which uses every Find a path which uses every

edge exactly once.edge exactly once. This is called an Eulerian tour.This is called an Eulerian tour. If the path begins and ends at the If the path begins and ends at the

same vertex, it is called a same vertex, it is called a Eulerian circuit. Eulerian circuit.

Detecting a Eulerian Detecting a Eulerian TourTour A graph has an Eulerian circuit if and A graph has an Eulerian circuit if and

only if it is connected and every node only if it is connected and every node has ‘even degree’.has ‘even degree’.

A graph has an Eulerian tour if and A graph has an Eulerian tour if and only if it is connected and every node only if it is connected and every node except two has even degree.except two has even degree.

In a tour, the two nodes with odd In a tour, the two nodes with odd degree must be the start and end degree must be the start and end nodes.nodes.

AlgorithmAlgorithm

The idea of the algorithm is to start at The idea of the algorithm is to start at some node of the graph and determine some node of the graph and determine a circuit back to that same node.a circuit back to that same node.

Now, as the circuit is added (in reverse Now, as the circuit is added (in reverse order, as it turns out), the algorithm order, as it turns out), the algorithm ensures that all the edges of all the ensures that all the edges of all the nodes along that path have been used.nodes along that path have been used.

AlgorithmAlgorithm

If there is some node along that path If there is some node along that path which has an edge that has not been which has an edge that has not been used, then the algorithm finds a circuit used, then the algorithm finds a circuit starting at that node which uses that edge starting at that node which uses that edge and splices this new circuit into the and splices this new circuit into the current one.current one.

This continues until all the edges of every This continues until all the edges of every node in the original circuit have been node in the original circuit have been used, which, since the graph is connected, used, which, since the graph is connected, implies that all the edges have been used, implies that all the edges have been used, so the resulting circuit is Eulerian.so the resulting circuit is Eulerian.

Formal AlgorithmFormal Algorithm

Pick a starting node and recurse on Pick a starting node and recurse on that node. At each step: that node. At each step: – If the node has no neighbors, then append If the node has no neighbors, then append

the node to the circuit and returnthe node to the circuit and return– If the node has a neighbor, then make a If the node has a neighbor, then make a

list of the neighbors and process them list of the neighbors and process them (which includes deleting them from the list (which includes deleting them from the list of nodes on which to work) until the node of nodes on which to work) until the node has no more neighborshas no more neighbors

– To process a node, delete the edge To process a node, delete the edge between the current node and its between the current node and its neighbor, recurse on the neighbor, and neighbor, recurse on the neighbor, and postpend the current node to the circuit.postpend the current node to the circuit.

Pseudo-CodePseudo-Code

find_circuit (node i)find_circuit (node i) if node i has no neighborsif node i has no neighbors circuit [circuitpos] = node icircuit [circuitpos] = node i circuitpos++circuitpos++ elseelse while (node i has neighbors)while (node i has neighbors) pick a neighbor j of node ipick a neighbor j of node i delete_edges (node j, node i)delete_edges (node j, node i) find_circuit (node j)find_circuit (node j) circuit [circuitpos] = node icircuit [circuitpos] = node i circuitpos++circuitpos++

AnalysisAnalysis

To find an Eulerian tour, find one of the To find an Eulerian tour, find one of the nodes which has odd degree (or any nodes which has odd degree (or any node if there are no nodes with odd node if there are no nodes with odd degree) and call find_circuit with it.degree) and call find_circuit with it.

This algorithm runs in O(m + n) time, This algorithm runs in O(m + n) time, where m is the number of edges and n where m is the number of edges and n is the number of nodes, if you store is the number of nodes, if you store the graph in adjacency list form.the graph in adjacency list form.

With larger graphs, there's a danger of With larger graphs, there's a danger of overflowing the run-time stack, so you overflowing the run-time stack, so you might have to use your own stack.might have to use your own stack.

Execution ExampleExecution Example

Stack: Stack: Location: Location: Circuit: Circuit:

1

2

3

4

5

6

7

Execution ExampleExecution Example

Stack: Stack: Location: 1Location: 1 Circuit: Circuit:

1

2

3

4

5

6

7

Execution ExampleExecution Example

Stack: 1Stack: 1 Location: 4Location: 4 Circuit: Circuit:

1

2

3

4

5

6

7

Execution ExampleExecution Example

Stack: 1 4Stack: 1 4 Location: 2Location: 2 Circuit: Circuit:

1

2

3

4

5

6

7

Execution ExampleExecution Example

Stack: 1 4 2Stack: 1 4 2 Location: 5Location: 5 Circuit: Circuit:

1

2

3

4

5

6

7

Execution ExampleExecution Example

Stack: 1 4 2 5Stack: 1 4 2 5 Location: 1Location: 1 Circuit: Circuit:

1

2

3

4

5

6

7

Execution ExampleExecution Example

Stack: 1 4 2Stack: 1 4 2 Location: 5Location: 5 Circuit: 1Circuit: 1

1

2

3

4

5

6

7

Execution ExampleExecution Example

Stack: 1 4 2 5Stack: 1 4 2 5 Location: 6Location: 6 Circuit: 1Circuit: 1

1

2

3

4

5

6

7

Execution ExampleExecution Example

Stack: 1 4 2 5 6Stack: 1 4 2 5 6 Location: 2Location: 2 Circuit: 1Circuit: 1

1

2

3

4

5

6

7

Execution ExampleExecution Example

Stack: 1 4 2 5 6 2Stack: 1 4 2 5 6 2 Location: 7Location: 7 Circuit: 1Circuit: 1

1

2

3

4

5

6

7

Execution ExampleExecution Example

Stack: 1 4 2 5 6 2 7Stack: 1 4 2 5 6 2 7 Location: 3Location: 3 Circuit: 1Circuit: 1

1

2

3

4

5

6

7

Execution ExampleExecution Example

Stack: 1 4 2 5 6 2 7 3Stack: 1 4 2 5 6 2 7 3 Location: 4Location: 4 Circuit: 1Circuit: 1

1

2

3

4

5

6

7

Execution ExampleExecution Example

Stack: 1 4 2 5 6 2 7 3 4Stack: 1 4 2 5 6 2 7 3 4 Location: 6Location: 6 Circuit: 1Circuit: 1

1

2

3

4

5

6

7

Execution ExampleExecution Example

Stack: 1 4 2 5 6 2 7 3 4 6Stack: 1 4 2 5 6 2 7 3 4 6 Location: 7Location: 7 Circuit: 1Circuit: 1

1

2

3

4

5

6

7

Execution ExampleExecution Example

Stack: 1 4 2 5 6 2 7 3 4 6 7Stack: 1 4 2 5 6 2 7 3 4 6 7 Location: 5Location: 5 Circuit: 1Circuit: 1

1

2

3

4

5

6

7

Execution ExampleExecution Example

Stack: 1 4 2 5 6 2 7 3 4 6Stack: 1 4 2 5 6 2 7 3 4 6 Location: 7Location: 7 Circuit: 1 5Circuit: 1 5

1

2

3

4

5

6

7

Execution ExampleExecution Example

Stack: 1 4 2 5 6 2 7 3 4Stack: 1 4 2 5 6 2 7 3 4 Location: 6Location: 6 Circuit: 1 5 7Circuit: 1 5 7

1

2

3

4

5

6

7

Execution ExampleExecution Example

Stack: 1 4 2 5 6 2 7 3Stack: 1 4 2 5 6 2 7 3 Location: 4Location: 4 Circuit: 1 5 7 6Circuit: 1 5 7 6

1

2

3

4

5

6

7

Execution ExampleExecution Example

Stack: 1 4 2 5 6 2 7Stack: 1 4 2 5 6 2 7 Location: 3Location: 3 Circuit: 1 5 7 6 4Circuit: 1 5 7 6 4

1

2

3

4

5

6

7

Execution ExampleExecution Example

Stack: 1 4 2 5 6 2Stack: 1 4 2 5 6 2 Location: 7Location: 7 Circuit: 1 5 7 6 4 3Circuit: 1 5 7 6 4 3

1

2

3

4

5

6

7

Execution ExampleExecution Example

Stack: 1 4 2 5 6Stack: 1 4 2 5 6 Location: 2Location: 2 Circuit: 1 5 7 6 4 3 7Circuit: 1 5 7 6 4 3 7

1

2

3

4

5

6

7

Execution ExampleExecution Example

Stack: 1 4 2 5Stack: 1 4 2 5 Location: 6Location: 6 Circuit: 1 5 7 6 4 3 7 2Circuit: 1 5 7 6 4 3 7 2

1

2

3

4

5

6

7

Execution ExampleExecution Example

Stack: 1 4 2Stack: 1 4 2 Location: 5Location: 5 Circuit: 1 5 7 6 4 3 7 2 6Circuit: 1 5 7 6 4 3 7 2 6

1

2

3

4

5

6

7

Execution ExampleExecution Example

Stack: 1 4Stack: 1 4 Location: 2Location: 2 Circuit: 1 5 7 6 4 3 7 2 6 5Circuit: 1 5 7 6 4 3 7 2 6 5

1

2

3

4

5

6

7

Execution ExampleExecution Example

Stack: 1Stack: 1 Location: 4Location: 4 Circuit: 1 5 7 6 4 3 7 2 6 5 2Circuit: 1 5 7 6 4 3 7 2 6 5 2

1

2

3

4

5

6

7

Execution ExampleExecution Example

Stack: Stack: Location: 1Location: 1 Circuit: 1 5 7 6 4 3 7 2 6 5 2 4Circuit: 1 5 7 6 4 3 7 2 6 5 2 4

1

2

3

4

5

6

7

Execution ExampleExecution Example

Stack: Stack: Location: Location: Circuit: 1 5 7 6 4 3 7 2 6 5 2 4 1Circuit: 1 5 7 6 4 3 7 2 6 5 2 4 1

1

2

3

4

5

6

7