Python Solving UnboundLocalError for Python Iterative Deepening Depth First Search

AI Thread Summary
The discussion centers around an implementation of iterative deepening depth-first search (IDDFS) in Python, using a graph represented as a dictionary. The code attempts to find a goal node ('31') starting from a given node ('7') while adhering to a specified depth limit. Key issues highlighted include confusion regarding the implementation and a specific error related to the depth variable, which is not properly defined or incremented within the IDDFS function. The importance of correctly managing the depth variable is emphasized, as it is crucial for the algorithm's operation. Additionally, there are references to various pseudocode resources that may aid in understanding the algorithm better. Overall, the thread reflects challenges in coding and debugging the IDDFS algorithm 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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top