The following is pseudocode implement depth-first search(DFS) with one stack and a large table to mark visited nodes:
DFS(N0):
StackInit(S)
S.push((N0, null))
if isGoal(N0) then do
return true
markVisited(N0)
S.push((null, N0))
while !isEmpty(S) then do
(N, parent) := S.pop()
R := next((N, parent))
if isNull(R) then do
continue // So no new node add to this layer.
S.push((R, parent))
if marked(R) then do
continue
if isGoal(R) then do // If it's goal don't have to explore it.
return true
markVisited(R)
if depthMax((R, parent)) then do
continue
S.push((null, R))
return false
The problem I want to solve is a modification from it: it substitutes the stack S with PriorityQueue PQ. This algorithm is used to simulate IDA* algorithm (this is stated in a textbook, which unfortunately not written in English, so I won't provide reference / book title):
DFS2(N0, f, LIMIT):
PriorityQueueInit(PQ)
// A node (N, parent) stored in PQ represents a path from `N0` to `N`\
passing the node `parent`; A node with smaller value on f() is \
prioritized than those with larger value.
PQ.push((N0, null))
if isGoal(N0) then do
return true
markVisited(N0)
PQ.push((null, N0))
while !isEmpty(PQ) then do // (1)
(N, parent) := PQ.poll()
R := next((N, parent)) // (2)
if isNull(R) then do
continue
PQ.offer((R, parent))
if marked(R) then do
continue
if isGoal(R) then do
return true
markVisited(R)
if f((R, parent)) > LIMIT then do
continue
PQ.offer((null, R))
return false
- (1): In A* algorithm, a priority queue is used to store nodes which haven't been explored, i.e. open list. While in the first DFS pseudocode I provided, the stack
Sis close list, so I assume that in the second pseudocodePQis close list, too. So how can the second pseudocode simulate IDA* algorithm, with a close list? - (2): It fetch the current smallest node from
PQ, which is probably not a sibling of nodeN, i.e. It jumps to another subtree from current subtree containsN. What's the purpose of this line?
Can anyone show me the correctness of the second algorithm, i.e. why it can be used in IDA* algorithm?
updated for more information: I have spent many time and effort on this question, but it seems to be very hard because of the following points:
All graphs appear in the textbook are drawn tree-like, i.e. only one parent for each node, to show the concepts. This makes me confused: Does the second algorithm only work for trees?
Considering the line
if f((R, parent)) > LIMIT then do ...If the second one also works for graph, not just tree, then there might be many parents go to
R, should I consider all cases or just the current one,parent?