Advertisement
Alex-Flexer

Untitled

May 6th, 2023
5
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. graph = {1: [2, 3], 2: [1, 3, 6], 3: [2, 4, 1, 5], 4: [3, 5], 5: [4, 3, 6], 6: []}
  2.  
  3. temp_path = []
  4. all_paths = []
  5.  
  6. def get_all_pathes(a, b, graph):
  7. if b in graph[a]:
  8. all_paths.append(temp_path + [a, b])
  9. else:
  10. for v in graph[a]:
  11. if v == b:
  12. all_paths.append(temp_path + [a, b])
  13. break
  14. elif v not in temp_path:
  15. temp_path.append(a)
  16. get_all_pathes(v, b, graph)
  17. del temp_path[-1]
  18. return None
  19.  
  20.  
  21. def shortest_path(a, b, graph):
  22. get_all_pathes(a, b, graph)
  23. shortest_path_ = sorted(all_paths, key=len)[0]
  24. shortest_path_formated = '-'.join(map(str, shortest_path_))
  25. print(f'Самый короткий путь: {shortest_path_formated}. Сложность: {len(shortest_path_)}')
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement