Wednesday, November 28, 2012

Eulerian Trail

Remember the puzzle your friends gave you when you were a kid "Traverse the graph without lifting your pencil" ? Had you known about the "Eulerian Trails" then, you could have dazzled your friends. A more advanced version of the puzzle would be "Draw the longest trail on the graph without lifting your pencil". So how do you handle this?

In Graph Theory, Eular showed the necessary conditions for a graph to be traversed as such. Such a trail or path which traverses the entire graph is called an "Eulerian Tail". To say this more mathematically "A trail which visits every edge exactly once is an Eulerian Trail". If the Eulerian Trail ends at the point where it was started, then the trail becomes a closed circuit which would be named as an "Eulerian Circuit". A graph which has an Eulerian Circuit is an "Eulerian Graph". If the graph does not have an Eulerian Circuit but just an Eulerian Trail which does not end at the point where it was started, then the graph is called "Semi-Eulerian". Got it so far?

So the bottom line is, "a trail which can be drawn to traverse the entire graph without lifting the pencil will exist if and only if the graph is Eulerian or Semi-Eulerian".

Euler gives the necessary conditions for a graph to be Eulerian or Semi-Eulerian.
  • Every vertex in the graph must have an even degree, for the graph to be Eulerian.
  • If the graph has two odd vertices, Then the graph is Semi-Eulerian where the Eulerian trail will start from one odd vertex and end at the other.
  • If there are more than two odd vertices, then the graph does not have an Eulerian tail.
What Eular says is logically correct because,
  • If a vertex has an even degree, then that vertex will not be a dead end where you would get stuck when traversing, because when you enter such a vertex there is always a way to exit. So you can visit every edge connected to that vertex.
  • If all the vertices in the graph are even, then you can traverse the entire graph and come back to the vertex where you started.
  • If you meet an odd vertex on your way traversing the graph, then could get stuck there, unless of course this vertex is the end of the Eulerian trail, because in an odd vertex, the number of edges to exit the vertex will be one less than the number of edges to enter the vertex. (Just like Hotel California, "You can checkout any time you like, but you can never leave" :P)
  • Logically, if you start the Eulerian trail from one odd vertex, you wont end your trail there, and neither will you at an even vertex, but you will end the trail in another odd vertex.
  • So obviously if you have more than two odd vertices, some of the edges connected to those odd vertices will be not be visited. In such a case, the graph will not be Eulerian.
There is a special property in Non-Eulerian graphs. That is, the number of odd vertices is always even. To understand this logically, lets take the smallest graph you can draw which is a single line. Here you have two odd vertices. When you create larger graphs by adding edges to this smaller graph,
  • if the vertex to which you connect the new edge, is odd, it will become even.
  • If you don't connect the other end of the new edge to any vertex in the graph, like the edge (6,4) in the bellow graph, then the total number of odd vertices in the graph won't change.
  • If you connect the other end to an odd vertex it will become even too, in which case the total number of odd vertices will be reduced by 2.
  • If the other end is connected to an even vertex, then that vertex will become odd and the total number of odd vertices will be unchanged.
If you apply this logic to the case where you connect the first end of the new edge to an even vertex, you'll see that to total number of odd vertices will always be even.
Get the idea? For more information, read the Wikipedia article Eulerian Path.

Now lets do some coding. :)

First lets assume a graph like this is represented as an "Adjacency List". In Java we can create an Adjacency List as follows, assuming vertices are numbered with integers.


When such a graph is given, it should be checked if it's Eulerian , Semi-Eulerian or Non-Eulerian. We can use the Eulers' conditions to decide that. Check this piece of code.


If the graph is not Eulerian, we can make it Semi-Eulerian so that we can find the longest trail to visit the maximum number of edges without lifting the pencil.

To do this we have to remove the minimum number of edges from the graph such that the graph becomes Semi-Eulerian. The following code shows how a Non-Eulerian graph is converted to a Semi-Eulerian graph when the set of odd vertices is given. Here the edges will be removed such that the number of odd vertices in the set be reduced to 2. So here 2 odd vertices will be chosen from the set such that the path connecting the the 2 chosen odd vertices will be the shortest. This will make sure that the number of edges removed to make the 2 chosen odd vertices even, be minimal. This procedure is continued until you get 2 odd vertices in the graph. For this we need a shortest path finding algorithm. I'm using the famous Breadth First Search algorithm for its simplicity. Explaining BFS is out of the scope of this post. Here is the code.


Get the code for BFS from here.

Excellent!! Now we have a graph which has an Eulerian trail. Now we have to find it :). How ?? Simple !! Hierholzer's algorithm provides a simple yet effective solution. The idea is straight forward.
  • If the graph is Eulerian, start from any vertex, if its Semi-Eulerian start from one of the odd vertices.
  • When an edge is visited, add the vertices to the path and remove the edge from the graph, so that it won't be visited again.
  • If the graph is Semi-Eulerian you might come to the other odd vertex without traversing the entire graph where as if the graph is Eulerian, you might come to the starting vertex without traversing the whole graph. Here, the unvisited edges make up an Eulerian graph. 
  • In this case, look for vertices which are already in the path with non visited edges. Start traversing from one of them until you come back to that vertex.  If you still have edges left continue the same process recursively until you run out of edges.
  • You have to keep track of the path accordingly. 
Check the code below. This algorithm returns the Eulerian Trail.

Hope this would come in handy. GL & HF :D