1

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?

dsolimano
  • 8,870
  • 3
  • 48
  • 63
Jonathan
  • 177
  • 2
  • 7
  • 4
    It's not fully correct DFS algorithm. It visits all successors of vertex first and then goes deeper. It should go deeper first, then backtrack to visit other child nodes. – MorioBoncz Jun 18 '12 at 19:26

2 Answers2

2

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
rendon
  • 2,323
  • 1
  • 19
  • 25
  • 1
    Pushing every neighbor into the stack would give a BFS because you end of looking at the neighbors first before the children. – Matilda Sep 27 '12 at 04:00
  • 4
    Nop. The key point here is the data structure, if you use a queue then you get a BFS, with a stack you get a DFS. – rendon Oct 01 '12 at 19:26
  • I think there are some problem in the above solution. You cannot pop an item from S into V immediately – spiralmoon Dec 01 '15 at 20:37
  • @ThinkRecursively I know it seems like Python or any other language, but it's pseudo code: `pop an item from S into v` can be translated into many instructions. – rendon Dec 02 '15 at 19:55
  • 1
    You pseudo-code is incorrect. You mark all `w` in the same iteration, so it __cannot__ be DFS; while @Matilda 's comment is also incorrect, he/she pointed out that your answer is probably incorrect, then a reasonable person will check one's answer. But you said "Nop. The key point here is data structure", – NeoZoom.lua Dec 31 '20 at 10:09
  • while DFS requires a stack, it doesn't mean that one use a stack in his code guarantee its code behave like DFS. – NeoZoom.lua Dec 31 '20 at 10:11
0
  1. The procedure will return success when an object is found.
  2. When an object is found, the S will contain nodes in the path [root, source). (the source itself is not included.)

procedure DFS(Graph, source, depth):
    StackInit(S)
    if source is goal then
        return success
    markVisited(source)
    S.push(null, source)              // S is a close-list
    while S is not empty then do
        c, p := S.pop()
        r := next(c, p)               // return the next sibling of c
        if r is null then
            continue
        S.push(r, p)
        if r is marked visited then   // that already marked means it cannot be goal
            continue
        if r is goal then
            return success
        markVisited(r)
        if equal(depth(r), depth) then // here is what OP wants.
            continue
        S.push(null, r)

return fail
NeoZoom.lua
  • 2,269
  • 4
  • 30
  • 64