Learning About the Graph Construct using Games, Part III - The Steps Required
(Page 4 of 6 )
Given a graph, here is the method we are going to use:
- Count the number of edges coming out of every node (This is called the outdegree).
- If exactly two nodes have an odd number of edges, let's mark them as u,v.
- If no nodes have an odd number of edges, set u and v to point to the first node.
- In all other cases, exit, and report no solution.
Up until now, we have seen nothing new. We have just made sure a solution exists, and we marked the start and end node of the path (If all nodes have an even outdegree, we mark the first node of the graph as both the first and last nodes).
Now, the real fun starts.
- Find a path from node u to node v (a path is just a collection of connected edges starting at node u and ending at node v), and mark all edges as taken.
- If there are still edges not taken, start at any node that has an untaken edge coming out of it, and find a loop that starts and ends at this node. (A loop is simply a path that starts and ends at the same node). Mark all edges of the loop as taken.
- If there are still edges not taken, go back to step six.
Up until now, we came up with a path, and a collection of zero or more loops.
There are four steps remaining. But, before going on, let's look at an example of this in action.
For this figure:

We may start by determining that the start and end points are going to be 3 and 4 (The only one having an odd number of edges)
First we find a path. Let's suppose it will be like this:

Then we find a loop starting at a certain point, such as 3. It is marked in blue here.

Then we find another loop, marked below in green.

Now, the whole graph is covered, and we are nearly finished. All that remains is to output the sequence of edges to follow.
- Start at the start node we chose in step 2 or 3 above
- If you find a new loop starting at this node, go for it else…
- If you are already in a loop, continue walking in it else…
- Walk on any edge you have not yet visited.
The last four steps are the steps required to actually output the solutions. We have found the path, and all the loops. Intuitively, what these steps do is simply merge all the loops, and the path together to form a full Eulerian tour on the graph.
Ok, so these were the steps required to solve the problem. Is everything over? Umm...no actually, because we still don't know how to find a path between two nodes! Also, how can we find a loop starting at a certain node? In the previous part, we tackled the problem of finding the shortest path. Certainly we can use it here to find paths too, but this will be an over-complication, since we are not really interested in the shortest path. We are only interested in finding any path. We will show an easier way to find paths and loops. First let's look at…
Next: Depth First Traversals >>
More Development Cycles Articles
More By Mohamed Saad