Find path in a dict of edges, python

212 Views Asked by At

I have a dict, printing it looks in this shape: {(2, 2): (47, 42), (2, 37): (21, 116), (3, 159): (60, 189), ..................... (483, 284): (451, 297)} So I have key value where key---->value is an edge. I want to be able to build a function that finds a path between 2 points, if there is one, and returns only it (assuming there are many paths in the dict). So I tried doing something like this: paths variable is my dict, begin and end are 2 dimention int tuples

def findPath(paths, begin, end):
    cur = begin
    found = False
    path = [begin]
    while not found:
        if cur == end:
            path.append(end)
            found = True
        else:
            #I don't want to go on blind like this: cur = paths[cur]
            # because it may not lead to the end point
    return path            

More information - there are no circles in the paths dict

How can I implement this? does it need to use recursion?

0

There are 0 best solutions below