I am writing the following program in python:
def find_graph(graph, start, end, path=[]):
f=0
if start==end:
if start in graph:
return start
if not start in graph:
return None
if not end in graph.values():
return None
path.append(start)
for i in graph[start]:
if end in graph[start]:
path.append(end)
f=1
break
else:
for j in graph[start]:
if f==1:
break
else:
find_graph(graph, j, end, path)
return path
p=find_graph({'A': ['B','C'], 'B': ['C', 'D'], 'C': 'D', 'D': 'E'}, 'A', 'E')
print(p)
It isn't complete yet, but I just want to know, when it's in its last step, i.e., it does path.append('E'), then it does f=1 and break. Then it goes to the return path
line; next it should return this value, but instead it goes to find_graph(graph, j, end, path)
line in the else
condition. Why is this happening? I put the f
flag also, just so that it doesn't do this, but the moment it comes to return path
f
becomes 0, I don't know why. Please help.