Solving UnboundLocalError for Python Iterative Deepening Depth First Search

Click For Summary
SUMMARY

The forum discussion addresses the UnboundLocalError encountered in a Python implementation of the Iterative Deepening Depth First Search (IDDFS) algorithm. The code provided uses a dictionary to represent a graph and attempts to find a goal node using depth-limited search. The error arises from the use of an undefined variable 'depth' within the iddfs function, which leads to confusion for the user trying to implement the algorithm. A definitive solution involves properly defining and managing the 'depth' variable within the scope of the iddfs function.

PREREQUISITES
  • Understanding of Python programming, specifically function definitions and scope.
  • Familiarity with graph theory concepts, particularly depth-first search algorithms.
  • Knowledge of recursion and how it applies to search algorithms.
  • Experience with debugging Python code and handling common errors like UnboundLocalError.
NEXT STEPS
  • Implement a local 'depth' variable within the iddfs function to track the current depth accurately.
  • Explore Python's built-in debugging tools to identify and resolve scope-related errors.
  • Study the differences between depth-limited search and iterative deepening search algorithms.
  • Review additional resources on graph traversal algorithms to enhance understanding of their applications.
USEFUL FOR

Python developers, algorithm enthusiasts, and students studying graph theory who are looking to implement and troubleshoot search algorithms effectively.

shivajikobardan
Messages
637
Reaction score
54
This code is for iterative deepening depth first search in python.

Code:
# 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?

https://lh3.googleusercontent.com/bCcAVC07nNgvg_rfn7GoSiplk5aMf8pjnKSak49QpnowIVbRu4gaemAgrwNgQJWctqw_lT1SqDneYntHVeWx04Jn8SKnn_svfRWVe5N267NeoO_rHZrMBa8OA4MlTYtDqZVLmDZv
https://lh6.googleusercontent.com/y0ALu9o9XvnLNTx0x-XvD02_vtgeURoMqfXM09vHIT50pbADwsQkqwSy55sUkLopSBKA_8L0b1xhJFcZgDgXyMKQAsy146aRIyaCGW-ofUtvwByjkUyGstnj9x0QJBITXyNRQdh6

https://lh5.googleusercontent.com/piadg7lpiYBSIlFzCkaRez9J1vSDkNU5nm0dOCBg4VtZwN9236Tn09V4-EJSHajc5tL64FNsrzhHJURHd6Y9lVefN0uhp8K5fzG7r3cHj6gV-BnzwOt2r5OW22AIFUZ36lSvy_qd
https://lh5.googleusercontent.com/3gsZwrM-eO_9PRtTPNmAWdD50E7lqwSzg81AJi8dFoisgZQA2ShmchSmGzY91O_2Gko6R-ed9R4XAw_cJ7egzBS6IF2aIaR_cQMWJq6SbVVd7bJDDYyRTLslBMzYF5gxe5z6qAMQ
https://lh4.googleusercontent.com/6rTwnJ9B5byKb_ygzpMMKwifdLUA4nQu4heJnRwVKOrxBOL3m-FoowraATMDnIM3vOzu6TgFPj8jYGQTb6sE7bDoWpfL27TC35nCGUfxjZ73Tu_Gm6iqHcx15JgTJFPyNvlWtXue

https://lh4.googleusercontent.com/zKLSsd5FTjVBbNpgUPZKfUiBmv9lIRKS28cFh5OIrV0KSaPziejmnufEWAMojmKcChsxsJ4v6o4wXGVwdptKd2eaIIpoKpCw0MO2YoN74quEG3nXGSGLA7KZuumSZSQ8pNxKxuXi
https://lh6.googleusercontent.com/P-enAU6E3tyZarmyp3mFlyts6h4gxUsK9RgwrsT18vzRymmvYKE1e4XXguiismIjS4PXMNyPyQDy8LwJ0MRlg4ssO3bK2FRpqNn_WAijet0HUZk2--YjO_5wHegsHA5WGl7NNGVG
https://lh3.googleusercontent.com/-CZvA8uUDNJ5T2a-WowcEAWj_5VEPQM20AV8s-GhkXckDN_rIOHuIfDoPa2Xlaur6fpc-iOJCK5Ie4H36IutVY60hNRyR6hIxqtpPBnyzP009UWOKfH80-XWFPx37hKoVry0Xbb-
https://lh6.googleusercontent.com/Iwi51k435c6g0HLrgvpSZM3RnoXFg5q9yrCEyPmhLMuoWAECXB2uDudITh8pcvouy_pc7Myn49thlWd4X_N2iGuQBXjIkKCMtEbjlxk8_jYDwfdvfgsfYOVVP88o-9FqM5mXkuqn
https://lh5.googleusercontent.com/PvBSa_J0ydYpDcrB6hZEy8vfomG7OrO-zECB64uFOn8lXp61Wl4H2ydGTquUu0EPSy5m95sGlGwXjKIB_ZkvfyIcGM4EoHitCnm_4NzygFWTE4RDXixnUZcLLzjy00FSExNjylzl
https://lh6.googleusercontent.com/9im-1q9yA18GyyRTNN6YR8KHdcSZeXjl97qhPpey1fT-CnjbPUTeHbzpNvp2BCusUXP3BZdNSJ_qIlv0I-1tr7mb7KsgHTDtebTxSmSzJWyQWI3IVBpZuZ1q6u7XPTMpMGqILJGd
https://lh6.googleusercontent.com/FDsphRKkKZnk9IrNDub0OM37r99PrQadZCtQ0ng57wW5AgmfeGDumw4U8cwQ8OxXYsamOutNeiE9VnMnOjAwv7L-LMt0D_HfXM1m_tJlzL8RumLcoEhUnbd-OH4UtwxVHaSbuBeU
https://lh6.googleusercontent.com/qQid6vyarXpvGZz_h3NDgi51xya-bApAMFLHJf_w232RJ_6ynadW22KTYGjMOJUr3FkVykrtIRrtGa-7dphrz8VfFqCgCoSbnNtfco7zU-8d-2wQquG_L_UE4A8G7wg0DIYq57i9
https://lh6.googleusercontent.com/VqD2_ogLHtT739zeBA1-xbc-eKFHOt5B00aLMfP64nvZDiyjncToZZaotOshLKMU4TndOGiH3c26A-yORC4pR3HONDFVp9-IJS6iAWQzDJjXJu4hh20Le53nAsSizUnxll92KbB8
https://lh3.googleusercontent.com/_5QbqLz94Nj3F7gUoTo7DwyNsY2TFdSWN3xEs04CjXfDDfRgmDPPz7VxOTpThu4ibQcllX53b7UaC_rpNPiof9z-sLDPD7CT8p44vgkjTs-hDnfupFfq10yGVXVRII89jcEndWZ1
 
Technology news on Phys.org
This function has an issue:
Code:
def 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

When you reference depth here in the last line, that causes the error. There is no input of depth or creation of a depth variable, so it doesn't exist inside the function and you can't increment it.
 

Similar threads

Replies
1
Views
1K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
1K
Replies
2
Views
2K