views:

72

answers:

1

I'm trying to understand the algorithm described here, but the explanation really isn't very clear:

'tour' is a stack

find_tour(u):
    for each edge e=(u,v) in E:
     remove e from E
     find_tour(v)
    prepend u to tour

to find the tour, clear stack 'tour' and call find_tour(u),
where u is any vertex with a non-zero degree.

What does it mean to 'prepend' u to a stack? How are the elements in tour even used in find_tour? Would be glad if someone could explain it to me, thanks!

+2  A: 

The stack 'prepend' is a push. So you push the vertex u on top of the stack. The idea is that you start with any vertex that has at least one edge on it. Follow all edges leaving the start vertex calling the function recursively after having removed the edge (so that you won't come back that same edge).

The elements in tour aren't used by find_tour at all. It's just a datastore to get back the order in which the graph was traversed after the algorithm is done. To get back the tour just keep calling tour.pop() until the stack is empty. It might contain the same vertex many times, if there are many edges to that vertex, but because every time before you call the find_tour recursively you remove the edges leaving a vertex the function will eventually finish.

Oh and E is all the edges in the graph, (u,v) is an edge from u to v.

Matti
Now that you've explained it, it does seem kind of obvious. Thanks!
int3