EULER TOUR. Example: Consider the following graph, which has an Euler tour. c d

Similar documents
1. Suppose you are given a magic black box that somehow answers the following decision problem in polynomial time:

THE EULER TOUR TECHNIQUE: EVALUATION OF TREE FUNCTIONS

THE EULER TOUR TECHNIQUE: EVALUATION OF TREE FUNCTIONS

Chapter 3: Paths and Cycles

CPS 102: Discrete Mathematics. Quiz 3 Date: Wednesday November 30, Instructor: Bruce Maggs NAME: Prob # Score. Total 60

Fundamental Properties of Graphs

γ(ɛ) (a, b) (a, d) (d, a) (a, b) (c, d) (d, d) (e, e) (e, a) (e, e) (a) Draw a picture of G.

Number Theory and Graph Theory

Discrete Mathematics and Probability Theory Fall 2009 Satish Rao,David Tse Note 8

Discrete Mathematics and Probability Theory Fall 2013 Vazirani Note 7

Notes for Recitation 8

SAT-CNF Is N P-complete

Discrete Mathematics and Probability Theory Summer 2016 Dinh, Psomas, and Ye HW 2

NOTE ON MINIMALLY k-connected GRAPHS

Discrete Mathematics for CS Spring 2008 David Wagner Note 13. An Introduction to Graphs

1 Connected components in undirected graphs

Byzantine Consensus in Directed Graphs

Recursive Definitions Structural Induction Recursive Algorithms

Recursion defining an object (or function, algorithm, etc.) in terms of itself. Recursion can be used to define sequences

Ma/CS 6a Class 8: Eulerian Cycles

Analyze the obvious algorithm, 5 points Here is the most obvious algorithm for this problem: (LastLargerElement[A[1..n]:

Solution for Homework set 3

CS473-Algorithms I. Lecture 13-A. Graphs. Cevdet Aykanat - Bilkent University Computer Engineering Department

Eulerian tours. Russell Impagliazzo and Miles Jones Thanks to Janine Tiefenbruck. April 20, 2016

Bipartite Roots of Graphs

Graph Algorithms. Tours in Graphs. Graph Algorithms

Section 3.1: Nonseparable Graphs Cut vertex of a connected graph G: A vertex x G such that G x is not connected. Theorem 3.1, p. 57: Every connected

by conservation of flow, hence the cancelation. Similarly, we have

Theorem 2.9: nearest addition algorithm

Assignment 1. Stefano Guerra. October 11, The following observation follows directly from the definition of in order and pre order traversal:

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

Definition: A graph G = (V, E) is called a tree if G is connected and acyclic. The following theorem captures many important facts about trees.

Solutions to relevant spring 2000 exam problems

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Module 11. Directed Graphs. Contents

CS250: Discrete Math for Computer Science. L20: Complete Induction and Proof of Euler s Characterization of Eulerian-Walks

11.2 Eulerian Trails

Assignment 4 Solutions of graph problems

MC 302 GRAPH THEORY 10/1/13 Solutions to HW #2 50 points + 6 XC points

Matching Algorithms. Proof. If a bipartite graph has a perfect matching, then it is easy to see that the right hand side is a necessary condition.

Varying Applications (examples)

Advanced algorithms. topological ordering, minimum spanning tree, Union-Find problem. Jiří Vyskočil, Radek Mařík 2012

Solutions to the Second Midterm Exam

Algorithms and Data Structures: Minimum Spanning Trees I and II - Prim s Algorithm. ADS: lects 14 & 15 slide 1

Paths, Flowers and Vertex Cover

Elements of Graph Theory

These are not polished as solutions, but ought to give a correct idea of solutions that work. Note that most problems have multiple good solutions.

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

Lecture 20 : Trees DRAFT

Graph Definitions. In a directed graph the edges have directions (ordered pairs). A weighted graph includes a weight function.

Course Introduction / Review of Fundamentals of Graph Theory

Homework Assignment #3 Graph

The divide and conquer strategy has three basic parts. For a given problem of size n,

Theorem 3.1 (Berge) A matching M in G is maximum if and only if there is no M- augmenting path.

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs

CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Dan Grossman Fall 2013

Lecture 6 Sorting and Searching

Lecture 22 Tuesday, April 10

Root Cover Pebbling on Graphs

Definition For vertices u, v V (G), the distance from u to v, denoted d(u, v), in G is the length of a shortest u, v-path. 1

Solutions. (a) Claim: A d-ary tree of height h has at most 1 + d +...

Greedy algorithms is another useful way for solving optimization problems.

looking ahead to see the optimum

Homework #5 Algorithms I Spring 2017

MAT 145: PROBLEM SET 4

Chapter 2 Graphs. 2.1 Definition of Graphs

Some Upper Bounds for Signed Star Domination Number of Graphs. S. Akbari, A. Norouzi-Fard, A. Rezaei, R. Rotabi, S. Sabour.

Trees. 3. (Minimally Connected) G is connected and deleting any of its edges gives rise to a disconnected graph.

Advanced Algorithms Class Notes for Monday, October 23, 2012 Min Ye, Mingfu Shao, and Bernard Moret

e-pg PATHSHALA- Computer Science Design and Analysis of Algorithms Module 10

Eulerian Paths and Cycles

Ma/CS 6b Class 5: Graph Connectivity

Homework 6 Solutions CS330 Discrete Structures, Fall 2017

CSE 21 Spring 2016 Homework 5. Instructions

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Approximation algorithms Date: 11/27/18

Advanced algorithms. strongly connected components algorithms, Euler trail, Hamiltonian path. Jiří Vyskočil, Radek Mařík 2012

Dartmouth Computer Science Technical Report TR Chain Match: An Algorithm for Finding a Perfect Matching of a Regular Bipartite Multigraph

Parallel Breadth First Search

Solutions to In-Class Problems Week 4, Fri

DO NOT RE-DISTRIBUTE THIS SOLUTION FILE

time using O( n log n ) processors on the EREW PRAM. Thus, our algorithm improves on the previous results, either in time complexity or in the model o

Problem Set 2 Solutions

Bipartite Perfect Matching in O(n log n) Randomized Time. Nikhil Bhargava and Elliot Marx

{ 1} Definitions. 10. Extremal graph theory. Problem definition Paths and cycles Complete subgraphs

Problem Set 3. MATH 776, Fall 2009, Mohr. November 30, 2009

22 Elementary Graph Algorithms. There are two standard ways to represent a

SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION

Outline. Introduction. 2 Proof of Correctness. 3 Final Notes. Precondition P 1 : Inputs include

Exercise set 2 Solutions

Lecture 8: The Traveling Salesman Problem

CHAPTER 9. GRAPHS 310. Figure 9.1 An undirected graph. Figure 9.2 A directed graph

Reference Sheet for CO142.2 Discrete Mathematics II

2. CONNECTIVITY Connectivity

Lecture 8: PATHS, CYCLES AND CONNECTEDNESS

Treewidth and graph minors

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

Matching Theory. Figure 1: Is this graph bipartite?

COMP3121/3821/9101/ s1 Assignment 1

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

Transcription:

Sathish Kumar Reddy Yenna EULER TOUR PROBLEM An Euler Tour of a connected 1 directed graph G(V, E) is a cycle that traverses each edge of G exactly once although it may visit a vertex more than once. I give an Algorithm to check whether a given graph c ontains an Euler tour and if it does the algorithm will give the Euler tour also. APPROACH We check whether a graph contains an Euler tour or not by checking the condition in-degree(v)=out-degree(v) for all the vertices v in the graph. If the condition holds for all the vertices in the graph then we can say that it has an Euler tour. We start with a node and continue as long as we can move along the edges. The only restriction is to use each edge exactly once. Since a graph with Euler Tour will have in-degree(v)=out-degree(v) for all vertices in the graph, we eventually reach the starting node. Since the selection of the next edge during our traversal is arbitrary, we may reach the start node without actually covering all the edges. So, we see if there are any edges that are not yet covered and start once again with a node connected to the uncovered edge and continue as in the previous case and in the same way we reach the start node at the end. In this way we will get a set of cycles formed by disjoint sets of edges. Since our graph is a connected graph, each cycle will have at least a node common with another cycle. So, we can merge the cycles using the common nodes in the cycles and get a large cycle covering all the edges exactly once. Example: Consider the following graph, which has an Euler tour. a e b c d 1 connected-a graph is connected if you can get from any node to any other by following a sequence of edges; in the case of a directed graph you are allowed to go the wrong way along an arrow.

Applying my approach we will get the following cycles: a b d c a b c e b Merging the two cycles using the common node that is b, we get the following cycle, which is an Euler tour for the graph: ALGORITHM a b c e b d c a We can write the algorithm using my approach as follows: Procedure MAIN(*A[1 n]) Begin /*Program to find whether a given graph contains an Euler tour or not. If it contains this program will give the Euler tour. *A[1..n] is an array of pointers containing the adjacency lists for all the nodes named 1..n */ if(!check(*a[1 n] )) then{ return( No Euler Tour in the given graph ); else EULER( *A[1 n]); endmain; Procedure CHECK(*A[1 N]) Begin /* Procedure to check whether a graph contains an Euler tour or not*/ i 1; while(i<=n) do { s A[i]; while(s!=null) { i++; out[i] out[i]+1; in[s value] in[s value] + 1; s s next; for i 1 to n step by 1 do { if(in[i]!= out[i]) then return (FALSE); return(true); end CHECK;

Procedure EULER(*a[1..N]) Begin /* Procedure to generate the Euler tour in the given graph. A linked list L is used to store the nodes and start is the pointer pointing to the start node in the linked list. ADD adds an edge to the linked list and ADD_REMOVE adds an edge to the linked list and removes the edge from the adjacency list. */ position start; ptr start; k 1; j 1; ADD(position, j); i A[j] value; while (k!= n) { /* Finding a cycle arbitrarily */ while (j!=i) { ADD_REMOVE(position, i); position i A[i] value; position next; /* Adding the edge towards the first node at which we have started to complete the cycle*/ ADD_REMOVE(position, i); /*Updating the variables for finding another cycle in the next iteration */ s ptr; while ((A[k]=NULL) or (k!= s value)) do { if (A[k]=NULL) then k k+1; else s s next; ptr s; position s; j k; i A[j] value; /* Printing the Euler tour */ s start; while(s!=null) do { PRINT(s value); s s next; end EULER;

PROOF OF THE ALGORITHM ClaimA: A connected directed graph G (V, E) has an Euler path if it satisfies the following predicate (say C). In-degree(v)=out-degree(v) for all but at most two vertices, v V. One of which has in-degree one greater than its out -degree and the other has in -degree one less than its out-degree. If it has in -degree=out-degree for all the nodes then the path will be a cycle covering all the edges. If it has two nodes with unequal degrees then the path will be from the node with in -degree one less than its out -degree to the node with in -degree one greater than its out-degree. Basis: E is empty. That is the case when the number of edges, n=0. The above claim is true for this base case as there are no edges in the graph then surely there is an Euler path! Induction Step: Assume the induction hypothesis that any connected directed graph G (V, E) with number of edges less than or equal to n, which satisfies the predicate C has an Euler path. The Euler path will be from the node with in -degree-1=out-degree to the node with in-degree=out-degree+1. If G has in-degree=out-degree for all the nodes then the path will be a cycle. Consider any connected directed graph G 1(V 1, E 1 ) with n+1 edges that satisfies the predicate C. Here we have two cases. G 1 can have zero or two nodes with degrees unequal. Case1:If G 1 has zero nodes with unequal degrees: Here all the nodes in the graph G 1 has in-degree=out-degree. Now remove any edge e (v e, v s ) from the g raph. The in -degree of v e becomes one greater than its out - degree and in -degree of v s becomes one less than its out -degree. The new graph will be connected as we have other edges connected to the nodes v e and v s in addition to the edge e. We have two nodes here after removal of edge e with unequal degrees. The newly formed graph after the removal of edge e has n edges and it satisfies the predicate C. So G 1 without edge e has an Euler path from v s to v e according to our assumption. The graph G 1 (V 1, E 1 -e) (after the removal of the edge e) has an Euler path from v s to v e. Now consider the graph G 1 (V 1, E 1 ). All the edges excluding the edge e form a path from v s to v e. We can add the edge e to the path at the end node and the path becomes a cycle as we are adding an edge from the end node to the start node of the path. This cycles covers all the edges including the edge e and hence it is an Euler path (also Euler tour). Thus G 1 has an Euler path. So, any graph G with n+1 edges that satisfies the predicate C (with all nodes having equal degrees) has an Euler path. This path is actually a cycle that covers all the edges.

Case2:If G 1 has two nodes with unequal degrees: Let v s and v e be the two nodes, which have their degrees unequal. Let v e has its in-degree one greater than its out -degree and v s has its out -degree one greater than its in-degree. Now, remove an edge from the graph G 1 so that the graph still satisfies the predicate C. We can remove an edge, e s from v s to some node, v p in the graph. Now, in - degree of v s becomes equal to its out-degree and in-degree of v p becomes one less than its out-degree. the edge. The Resulting graph may be connected or disconnected after the removal of If the Resulting graph is connected: If we remove e s, v p and v e are the only nodes, which have their degrees unequal. So there will be only two nodes with unequal degrees. Hence the new graph G 1 (V 1, E 1 -{e s ), will satisfy the predicate C. Now, the new graph G 1 is connected and has n edges and satisfies the predicate C. So, according to our induction hypothesis we can say that the graph G 1 has an Euler path from v p to v e (after the removal of e s ). The graph G 1 (V 1, E 1 -e s ) has an Euler path from v p to v e. Now, consider the graph G 1 (V 1, E 1 ). We know that all the edges in G 1 excluding e s (v s, v p ) form a path from v p to v e. We can add the edge e s to the starting node (v p ) of the path. Then the new path will be from v s to v e. Since this path covers all the edges including e s in the graph G 1, it is the Euler path of the graph G 1. If the Resulting graph is disconnected: The resulting graph will contain two connected components. The nodes v s and v p will be in different components. Let the component containing v s be G 2 and the component containing v p be G 3. The two components will have number of edges less than n. The compon ent G 2 will have in -degree=out-degree for all the nodes. According to our assumption we can say that the edges of the graph G 2 will form a path (actually a cycle) since it satisfies the predicate C. The component G 3 will have two nodes (v p and v e ) with degrees unequal and hence satisfies the predicate C. Since the number of edges in G 3 is less than n, based on our assumption we can say that G 3 has an Euler path and is from v p to v e. Now consider the graph G 1 (V 1, E 1 ). We have an edge from a no de in G 2 to a node in G 3. Since we have a cycle in G 2 covering all the edges, we can say that we have a path from v s to v s with all edges in G 2 covered. We have a path from v p to v e with all the edges in G 3 covered. In addition to the edges in both paths w e have edge e s from v s to v p

in the graph G 1. This edge connects both paths and result in long path with all the edges in graph G 1 covered. This path is nothing but the Euler path since it covers all the edges in the graph G1. Thus G 1 has an Euler path. So, any graph G with n+1 edges that satisfies the predicate C (with exactly two nodes having unequal degrees) has an Euler path. induction. This completes the induction step and completes the proof by mathematical ClaimB: A connected directed graph G ( V, E) has an Euler tour if in -degree(v) = out - degree(v) for all nodes, v in the graph. Consider any graph, G (V, E) that has in -degree(v)=out-degree(v) for all the nodes v in the graph. Now remove any edge e from the graph. Let the removed edge be e k f rom some node v e to some other node v s. The in-degree of v e becomes one greater than its outdegree and the in-degree of v s becomes one less than its out-degree. Let the new graph be G 1. It is a connected graph since we have other edges connecting the nodes v s and v e. The graph G 1 satisfies the predicate C defined in the previous claim. So, according to claima G 1 has an Euler path and is from the node v s to the node v e. Considering the graph G again, we found that the edges in the graph G excluding the edge e k form a path, P from v s to v e. Add this edge e k to the path P. Now it becomes a cycle and this cycle covers all the edges in the graph exactly once (as we know that the path P covers all the edges except e k exactly once). This cycle is nothing but the Euler tour as it is a cycle and covers all the edges in the graph G. Thus the claimb is TRUE. ClaimC: If a connected directed graph G (V, E) has an Euler tour then in - degree(v)=out-degree(v) for all nodes v in the graph G. Any cycle must leave a node exactly as many times as it enters it. Since Euler tour covers all the edges exact ly once and doesn t leave any edge, In -degree of a node is nothing but the number of times the Euler cycle enters the node and out -degree of a node is nothing but the number of times the Euler cycle leaves the node. Thus, in-degree(v)=out-degree(v) for all v V Thus, if a graph G(V,E) has an Euler tour then in -degree(v)=out-degree(v) for all the nodes, v V.

ClaimD: A connected directed graph G (V, E) has an Euler tour if and only if in - degree(v)=out-degree(v) for all nodes v in the graph. By looking at the proofs for claimb and claimc, we can say that the above claim is TRUE. In my algorithm, I first check whether the given graph contains an Euler tour or not using the claimc. Since the graph has an Euler tour we have in-degree=out-degree for all the nodes in the graph. I start with an arbitrary node and continue adding the edges to the linked list. Since in-degree=out-degree for all the nodes whenever we enter a node there will be an edge leaving that node and hence we can reach another node. Th us addition of edges is stopped only when we reach the node at which we started. This set of edges traversed form a cycle. We start with another node whose all edges are not covered yet. Similar to the procedure above we will get another cycle. So, we can construct a set of cycles with disjoint sets of edges each according to my approach. Since the graph G is connected we have one of the following. 1.There are few edges, which are not in any of the cycles and each edge connects one cycle with other. 2.There will be at least a node in each cycle, which also exists at least once in other cycle. As there are no edges left after constructing the cycles, there will be at least a node in one cycle, which also exists at least once in another cycle. We can merge the two cycles using the common node in the two cycles. In this way we can merge all the cycles and get an Euler Tour, which covers all the edges exactly once. In my algorithm, I start constructing the second cycle from a node within the already formed cycle whose all edges are not yet covered. This ends being a cycle inserted within the existing cycle. This insertion of a new cycle within the existing cycle ends when there are no edges left to cover. As we are inserting cycles within the existing cycle at the common node the final list will be a cycle covering all the edges of the node. Thus my algorithm constructs an Euler Tour of the given graph if one exists.

TIME COMPLEXITY CHECK( ): The time complexity of the procedure CHECK( ) is O (n(e)). The inner while loop in the CHECK is executed exactly equal to the number of edges in the graph and hence it takes time in O (n(e)). The for loop in the CHECK is executed exactly equal to the number of vertices in the graph and hence it takes time in O (n(v)). Hence, Time complexity of CHECK( ) Θ (n(e)+n(v)) Θ (n(e)). EULER( ): Time complexity of the procedure EULER( ) is O (n(e)). Consider the first inner while loop: The body of the first inner while loop is executed at most n(e) tim es because in each iteration we remove an edge after inserting it into the cycle and there are n(e) edges in the graph. So, the time taken by the first inner while loop is in O (n(e)). Consider the second inner while loop: Similarly, the body of the seco nd inner while loop is also executed at most n(e) times because either we consider an edge or an adjacency list. It is executed at least n(v) times and at most n(e) times. So, the time taken by the second inner while loop is in O(n(E) + n(v)) which is equal to O(n(E)). Time Complexity of the procedure EULER( ) O (n(e)+n(e)). O (n(e)). MAIN( ): Time Complexity of MAIN( ) O (n(e)+n(e)). O (n(e)). References: Introduction to Algorithms, Cormen, et al., McGraw Hill. Fundamentals of Algorithms, Brassard and Bratley, Prentice Hall Inc.