Depth-first Search (cont)
visited[] // store previously visited node, for each vertex 0..nV-1
findPath(G,src,dest):
| Input graph G, vertices src,dest
|
| for all vertices v∈G do
| visited[v]=-1
| end for
| visited[src]=src // starting node of the path
| if dfsPathCheck(G,src,dest) then // show path in dest..src order
| | v=dest
| | while v≠src do
| | print v"-"
| | v=visited[v]
| | end while
| | print src
| end if
dfsPathCheck(G,v,dest):
| if v=dest then // found edge from v to dest
| return true
| else
| | for all (v,w)∈edges(G) do
| | | if visited[w]=-1 then
| | | | visited[w]=v
| | | | if dfsPathCheck(G,w,dest) then
| | | | return true // found path via w to dest
| | | | end if
| | | end if
| | end for
| end if
| return false // no path from v to dest
|
|