Solving UnboundLocalError for Python Iterative Deepening Depth First Search

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 2K views
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
 
Physics 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.