Exception has occurred: UnboundLocalError local variable 'depth' refer

  • Context: Comp Sci 
  • Thread starter Thread starter shivajikobardan
  • Start date Start date
  • Tags Tags
    Depth Local Variable
Click For Summary
SUMMARY

The forum discussion addresses an UnboundLocalError in a Python implementation of an iterative deepening depth-first search (IDDFS) algorithm. The code provided fails due to the variable 'depth' not being initialized before use, leading to confusion between 'depth' and 'depth_limit'. Suggestions include passing the goal as a function argument and renaming 'depth_limit' to 'initial_depth_limit' for clarity. Proper initialization and clear variable naming are essential for resolving the error and improving code readability.

PREREQUISITES
  • Understanding of Python programming language
  • Familiarity with depth-first search algorithms
  • Knowledge of recursion in programming
  • Basic concepts of graph theory and data structures
NEXT STEPS
  • Implement error handling in Python functions
  • Learn about Python decorators for function enhancements
  • Explore advanced graph algorithms such as A* and Dijkstra's
  • Study Python's built-in data structures for efficient graph representation
USEFUL FOR

Software developers, particularly those working with algorithms and data structures in Python, as well as educators teaching graph theory concepts.

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?

Jn8SKnn_svfRWVe5N267NeoO_rHZrMBa8OA4MlTYtDqZVLmDZv.png

KQAsy146aRIyaCGW-ofUtvwByjkUyGstnj9x0QJBITXyNRQdh6.png


efN0uhp8K5fzG7r3cHj6gV-BnzwOt2r5OW22AIFUZ36lSvy_qd.png

S6IF2aIaR_cQMWJq6SbVVd7bJDDYyRTLslBMzYF5gxe5z6qAMQ.png

DoWpfL27TC35nCGUfxjZ73Tu_Gm6iqHcx15JgTJFPyNvlWtXue.png

eaIIpoKpCw0MO2YoN74quEG3nXGSGLA7KZuumSZSQ8pNxKxuXi.png

ssO3bK2FRpqNn_WAijet0HUZk2--YjO_5wHegsHA5WGl7NNGVG.png

60hNRyR6hIxqtpPBnyzP009UWOKfH80-XWFPx37hKoVry0Xbb-.png

uQBXjIkKCMtEbjlxk8_jYDwfdvfgsfYOVVP88o-9FqM5mXkuqn.png

IcGM4EoHitCnm_4NzygFWTE4RDXixnUZcLLzjy00FSExNjylzl.png

mb7KsgHTDtebTxSmSzJWyQWI3IVBpZuZ1q6u7XPTMpMGqILJGd.png

L-LMt0D_HfXM1m_tJlzL8RumLcoEhUnbd-OH4UtwxVHaSbuBeU.png

VfFqCgCoSbnNtfco7zU-8d-2wQquG_L_UE4A8G7wg0DIYq57i9.png

HONDFVp9-IJS6iAWQzDJjXJu4hh20Le53nAsSizUnxll92KbB8.png

z-sLDPD7CT8p44vgkjTs-hDnfupFfq10yGVXVRII89jcEndWZ1.png
 
Last edited by a moderator:
Physics news on Phys.org
  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
 

Similar threads

Replies
2
Views
2K
Replies
1
Views
2K
Replies
1
Views
1K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 7 ·
Replies
7
Views
4K