Solving UnboundLocalError for Python Iterative Deepening Depth First Search

In summary, the conversation discussed the implementation of iterative deepening depth first search in Python using a dictionary as an adjacency list. The code also includes a function for depth-limited search and a function for iterative deepening depth first search. However, there seems to be an issue with the depth variable in the code.
  • #1
shivajikobardan
674
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
  • #2
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.
 

FAQ: Solving UnboundLocalError for Python Iterative Deepening Depth First Search

What is an UnboundLocalError in Python?

An UnboundLocalError in Python is an error that occurs when a local variable is referenced before it has been assigned a value. This usually happens when the variable is defined within a function and is referenced before it has been assigned a value within that function.

What is Iterative Deepening Depth First Search in Python?

Iterative Deepening Depth First Search in Python is a search algorithm used to traverse a tree or graph data structure. It starts at the root node and explores as far as possible along each branch before backtracking and exploring the next branch. It repeats this process with increasing depth until the desired node is found.

How does Python's Iterative Deepening Depth First Search work?

Python's Iterative Deepening Depth First Search works by maintaining a stack of nodes to be explored. It starts at the root node and adds its children to the stack. It then explores the first child and adds its children to the stack. This process continues until the desired node is found or the stack is empty. If the node is not found, the search starts again with a deeper level of exploration.

What causes an UnboundLocalError in Python's Iterative Deepening Depth First Search?

An UnboundLocalError in Python's Iterative Deepening Depth First Search can occur when the variable being referenced is defined within a function and is not assigned a value before being referenced. This can happen if the variable is used before being defined, or if it is defined within a nested function without being declared as nonlocal.

How can an UnboundLocalError be solved in Python's Iterative Deepening Depth First Search?

An UnboundLocalError in Python's Iterative Deepening Depth First Search can be solved by declaring the variable as nonlocal if it is defined within a nested function. If the variable is used before being defined, it can be assigned a default value before being referenced. Additionally, using the "global" keyword can also solve this error if the variable is defined outside of the function.

Back
Top