forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_path.py
More file actions
123 lines (104 loc) · 3.05 KB
/
find_path.py
File metadata and controls
123 lines (104 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
"""
Find Paths in a Graph
Provides functions to find a single path, all paths, or the shortest path
between two nodes using recursion and backtracking.
Complexity:
Time: O(V!) worst case (exponential backtracking)
Space: O(V) per recursion stack
"""
from __future__ import annotations
from typing import Any
def find_path(
graph: dict[Any, list[Any]],
start: Any,
end: Any,
path: list[Any] | None = None,
) -> list[Any] | None:
"""Find a path between *start* and *end* using backtracking.
Args:
graph: Adjacency list.
start: Source node.
end: Target node.
path: Accumulated path (internal use).
Returns:
A list representing the path, or None if no path exists.
Examples:
>>> find_path({'A': ['B'], 'B': ['C'], 'C': []}, 'A', 'C')
['A', 'B', 'C']
"""
if path is None:
path = []
path = path + [start]
if start == end:
return path
if start not in graph:
return None
for node in graph[start]:
if node not in path:
newpath = find_path(graph, node, end, path)
return newpath
return None
def find_all_path(
graph: dict[Any, list[Any]],
start: Any,
end: Any,
path: list[Any] | None = None,
) -> list[list[Any]]:
"""Find all paths between *start* and *end*.
Args:
graph: Adjacency list.
start: Source node.
end: Target node.
path: Accumulated path (internal use).
Returns:
A list of all paths, where each path is a list of nodes.
Examples:
>>> find_all_path({'A': ['B', 'C'], 'B': ['C'], 'C': []}, 'A', 'C')
[['A', 'B', 'C'], ['A', 'C']]
"""
if path is None:
path = []
path = path + [start]
if start == end:
return [path]
if start not in graph:
return []
paths: list[list[Any]] = []
for node in graph[start]:
if node not in path:
newpaths = find_all_path(graph, node, end, path)
for newpath in newpaths:
paths.append(newpath)
return paths
def find_shortest_path(
graph: dict[Any, list[Any]],
start: Any,
end: Any,
path: list[Any] | None = None,
) -> list[Any] | None:
"""Find the shortest path between *start* and *end*.
Args:
graph: Adjacency list.
start: Source node.
end: Target node.
path: Accumulated path (internal use).
Returns:
The shortest path as a list of nodes, or None if unreachable.
Examples:
>>> find_shortest_path({'A': ['B', 'C'], 'B': ['C'], 'C': []}, 'A', 'C')
['A', 'C']
"""
if path is None:
path = []
path = path + [start]
if start == end:
return path
if start not in graph:
return None
shortest: list[Any] | None = None
for node in graph[start]:
if node not in path:
newpath = find_shortest_path(graph, node, end, path)
if newpath and (not shortest or len(newpath) < len(shortest)):
shortest = newpath
return shortest