Exception has occurred: UnboundLocalError local variable 'depth' refer

2 replies · 2K views · last reply
shivajikobardan
Messages
637
Reaction score
54
Homework Statement
iterative deepening depth first search
Relevant Equations
code given below
This code is for iterative deepening depth first search in python.

Python:
# Python dictionary to act as an adjacency list
graph = {
  '7' : ['19','21', '14'],
  '19': ['1', '12', '31'],
  '21': [],
  '14': ['23', '6'],
  '1' : [],
  '12': [],
  '31': [],
  '23': [],
  '6' : []
}
visited=[]
goal='31'
depth=0

depth_limit=2
def dls(visited, graph, node,depth_limit):
    if(node==goal):
            print("goal found")
            return True

   
    if(depth>=0):
        #to print path
        if node not in visited:
            visited.append(node)
       
       

        for neighbor in graph[node]:
            dls(visited, graph, neighbor,depth_limit-1)
       

    return Falsedef iddfs(visited,graph,node):
    while True:
        solution=dls(visited,graph,node,depth_limit)
        if(solution==goal):
            print("Success goal find at depth=",depth)
            print("Path=",visited)    
        depth=depth+1
 
       

print("Following is the Depth-First Search")
iddfs(visited, graph, '7')

There are various pseudocodes available for this problem. They are as follows-:
I did my best to understand and implement the code but I seem to have failed. And it is getting really confusing. Can you help me?

Slide titled Iterative deepening search showing pseudocode for function ITERATIVE-DEEPENING-SEARCH(problem), with a for loop over depth from 0 to infinity calling DEPTH-LIMITED-SEARCH until the result is not cutoff.

Slide titled Iterative Deepening Depth-first Search defining iterative deepening DFS and giving the Python-style pseudocode loop over depth_limit with depth_limited_search.


Slide titled Iterative deepening search with bullet Use DFS as a subroutine and numbered steps: 1. Check the root; 2. Do a DFS searching for a path of length 1; 3. If there is no path of length 1, do a DFS searching for a path of length 2; 4. If there is no path of length 2, do a DFS searching for a path of length 3...

Slide titled 05-46: Iterative Deepening DFS (IDS), showing IDS pseudocode: d = 0, while True, result = depth-limited-search(d), if result == goal return result, else d = d + 1.

Slide titled DLS pseudocode showing a stack-based depth-limited search: push initialState, loop while not done, pop a node, return it if goalTest succeeds, otherwise push its children if depth is under the limit, else return None.

Slide titled Depth-first Search (Non-recursive Version) showing pseudocode: open := new Stack, push_back root node, while not open.is_empty() pop_back n, return extract_path(n) if is_goal(n.state), else for each successor push_back make_node(n, a, s').

Pseudocode for a recursive function depth_limited_search(s, depth_limit): returns a goal path if is_goal(s), recurses over successors when depth_limit > 0, else returns none.

Slide titled iterative deepening depth-first search (iterative deepening DFS) with bullets on its idea, cost and a pseudocode box: for depth_limit in {0, 1, 2, ...}, solution := depth_limited_search(init(), depth_limit), if solution != none return solution. German: iterative Tiefensuche.

Slide titled Iterative Deepening Search (IDS) with pseudo-code for function ITERATIVE-DEEPENING-SEARCH(problem) that loops depth from 0 to infinity, calling DEPTH-LIMITED-SEARCH and returning failure at the end.

Pseudo-code for IDDFS: numbered lines 1 to 6 defining IDDFS(T), a loop for d = 0 to infinity, if DLS(T, d) return 1, else return 0.

Pseudocode for iterative deepening depth-first search: IDS(root, goal, depthLimit) loops depth from 0 to depthLimit calling DFS, and DFS(root, depth) recurses over root.children with depth minus 1.

Slide titled Proses IDS showing iterative deepening search level by level with a tree of nodes 1 to 16; node 11 is marked found at level 3, and levels 0 to 4 are labelled on the right.

Slide titled Iterative Deepening Search (IDS) showing a tree diagram of nodes A to K with node G highlighted in orange, levels 0 to 3 labelled on the right, and iteration lists below.

Slide titled Iterative Deepening Search showing a tree with nodes S, A, C, D, B, E, G, F, H, I across Levels 0 to 3, with iterations 01 to 04 and their depth limits and paths, ending at goal G.

Slide titled Algorithm (IDDFS) listing three numbered steps: set SEARCH_DEPTH to 0; conduct a depth-first search to SEARCH_DEPTH and return the solution path if found; otherwise increment SEARCH_DEPTH by 1 and go to step 2.
 
Last edited by a moderator:
  1. Post Python code with the starting tag CODE=Python
  2. In line 17 you have
    Python:
    def dls(visited, graph, node,depth_limit):
    Apart from the inconsistent spacing and the obscure name, do you not think that the value you are searching for should be an argument of the function?
  3. In lines 40-44 you are getting confused between depth, which you never initialise, and the global variable depth_limit, which I suggest you rename initial_depth_limit.
 
  • Like
Likes   Reactions: berkeman, shivajikobardan and jack action
pbuk said:
Post Python code with the starting tag CODE=Python
I edited the OP to add the Python tag value. Thanks