问题
So currently i have a DFS with the following pseudocode
procedure DFS(Graph,source):
create a stack S
push source onto S
mark source
while S is not empty:
pop an item from S into v
for each edge e incident on v in Graph:
let w be the other end of e
if w is not marked:
mark w
push w onto S
How do I alter this function to accept a third argument that limits the depth of the search?
回答1:
Let Node a structure for each node of the graph, add a field called level and then:
procedure DFS(Graph,source, depth):
create a stack S
source.level = 0
push source onto S
mark source
while S is not empty:
pop an item from S into v
if v.level > depth
continue
for each edge e incident on v in Graph:
let w be the other end of e
if w is not marked:
mark w
w.level = v.level + 1
push w onto S
来源:https://stackoverflow.com/questions/7493823/writing-a-dfs-with-iterative-deepening-without-recursion