Question: Exercise 4.27 Write a non-recursive version of the DFS algorithm. You will need to use a stack, that is, a list whose elements are inserted


Exercise 4.27 Write a non-recursive version of the DFS algorithm. You will need to use a stack, that is, a list whose elements are inserted and taken out at the front (also called a first-in-last-out, or FILO, list). Compare with Algorithm 4.31. Algorithm 4.23 Depth-First Search (recursive version) procedure DFS(G: connected graph with vertices u1, U2, ..., Un) T:= tree consisting only of vertex ui visit(ui) {T is the depth-first spanning tree of G} procedure visit(v: vertex of G) {the graph G and its tree T are global variables here} while v has neighbours not yet in T do begin w := first neighbour of v not yet in T T:=T + VW visit(w) end
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
