Advertisement
DeepRest

All paths from source to destination

Jun 23rd, 2021
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.33 KB | None | 0 0
  1. graph = [[1, 3], [2], [3], []]
  2.  
  3. dest = len(graph)-1
  4.  
  5. cache = {len(graph)-1: [[len(graph)-1]]}
  6. def paths(start):
  7. if start in cache.keys():
  8. return cache[start]
  9.  
  10. path = []
  11. for vertex in graph[start]:
  12. path+= [[start]+p for p in paths(vertex)]
  13.  
  14. cache[start] = path
  15. return path
  16.  
  17. print(paths(0))
  18.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement