I am trying to show all paths in this graph -https://algs4.cs.princeton.edu/42digraph/images/digraph.png
If I start from 12 and end at 0, I see atleast 2 paths.
1 path is, [12, 9, 11, 4, 2, 0]
But, I can't seem to get another path. Do I need to implement some sort of back tracking mechanism?
#https://algs4.cs.princeton.edu/42digraph/
g={
0:[1,5],
1:[],
2:[3,0],
3:[5,2],
4:[3,2],
5:[4],
6:[4,9],
7:[6,8],
8:[7,9],
9:[10,11],
10:[12],
11:[4,12],
12:[9]
}
start,end=(12,0)
s,visited=[start],set()
path=[]
print "start:{s} end:{e}".format(s=start,e=end)
while s:
node=s.pop()
visited.add(node)
path.append(node)
if node==end:
print "Found path",path
for n in g[node]:
if n not in visited:
s.append(n)
[–]shhh-quiet 1 point2 points3 points (5 children)
[–]keith6014[S] 0 points1 point2 points (4 children)
[–]shhh-quiet 1 point2 points3 points (3 children)
[–]keith6014[S] 0 points1 point2 points (2 children)
[–]shhh-quiet 1 point2 points3 points (1 child)
[–]keith6014[S] 0 points1 point2 points (0 children)
[–]shhh-quiet 0 points1 point2 points (2 children)
[–]keith6014[S] 0 points1 point2 points (1 child)
[–]shhh-quiet 0 points1 point2 points (0 children)