Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Python Exception has occurred: UnboundLocalError local variable 'depth' referenced before assignment

shivajikobardan

Legendary Coder
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 False


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
 
        

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?

bCcAVC07nNgvg_rfn7GoSiplk5aMf8pjnKSak49QpnowIVbRu4gaemAgrwNgQJWctqw_lT1SqDneYntHVeWx04Jn8SKnn_svfRWVe5N267NeoO_rHZrMBa8OA4MlTYtDqZVLmDZv

y0ALu9o9XvnLNTx0x-XvD02_vtgeURoMqfXM09vHIT50pbAD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lXp61Wl4H2ydGTquUu0EPSy5m95sGlGwXjKIB_ZkvfyIcGM4EoHitCnm_4NzygFWTE4RDXixnUZcLLzjy00FSExNjylzl

9im-1q9yA18GyyRTNN6YR8KHdcSZeXjl97qhPpey1fT-CnjbPUTeHbzpNvp2BCusUXP3BZdNSJ_qIlv0I-1tr7mb7KsgHTDtebTxSmSzJWyQWI3IVBpZuZ1q6u7XPTMpMGqILJGd

FDsphRKkKZnk9IrNDub0OM37r99PrQadZCtQ0ng57wW5AgmfeGDumw4U8cwQ8OxXYsamOutNeiE9VnMnOjAwv7L-LMt0D_HfXM1m_tJlzL8RumLcoEhUnbd-OH4UtwxVHaSbuBeU

qQid6vyarXpvGZz_h3NDgi51xya-bApAMFLHJf_w232RJ_6ynadW22KTYGjMOJUr3FkVykrtIRrtGa-7dphrz8VfFqCgCoSbnNtfco7zU-8d-2wQquG_L_UE4A8G7wg0DIYq57i9

VqD2_ogLHtT739zeBA1-xbc-eKFHOt5B00aLMfP64nvZDiyjncToZZaotOshLKMU4TndOGiH3c26A-yORC4pR3HONDFVp9-IJS6iAWQzDJjXJu4hh20Le53nAsSizUnxll92KbB8

_5QbqLz94Nj3F7gUoTo7DwyNsY2TFdSWN3xEs04CjXfDDfRgmDPPz7VxOTpThu4ibQcllX53b7UaC_rpNPiof9z-sLDPD7CT8p44vgkjTs-hDnfupFfq10yGVXVRII89jcEndWZ1
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom