Click to See Complete Forum and Search --> : Algorithm code help


xblade
April 6th, 2008, 07:52 AM
I need to create a depth first traversal structure in psuedocode and below is what i have come up with:
GraphSearch(G,v,u)
G Graph being searched
v Node at the start of the search.
n Node being searched for

visit(v)
mark(v) as visited
putstack(v)
putarraylist(v) // Will add the node visited to the ArrayList
while stack is not empty
getstack(x)
for all edges x > w in G
if w is not marked then
GraphSearch(G,w,u)
end if
if w = u return w
end for
end while
return 0


Im wondering if this is the right code for depthsearch or will there be a fault somewhere inside. ?

kinderchocolate
April 16th, 2008, 08:49 PM
I don't totally understand the code in the 'for all edges x > w in G" loop. You need to go and visit each of the node you get from x, so what does the "all edges x > w" do? I am not sure why you want to pass u to GraphSearch. To me, n is the node being searched for and it will always be the case. Why not just pass n to the next level of the iteration?

for all edges x in G
if x = n return x
if x is not marked then
GraphSearch(G,x,n)
end if
end for

prometheuzz
April 17th, 2008, 05:21 AM
...

Im wondering if this is the right code for depthsearch or will there be a fault somewhere inside. ?

Why not write a bit of "real" code? Implement that pseudo code, and run a couple of tests to see if you got it correct.