Skip to content

Commit 28a122b

Browse files
Update bidirectional_search.py
1 parent 2791920 commit 28a122b

1 file changed

Lines changed: 10 additions & 10 deletions

File tree

graphs/bidirectional_search.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ def expand_search(
3838
return None
3939

4040

41+
def construct_path(current: int | None, parents: dict[int, int | None]) -> list[int]:
42+
path: list[int] = []
43+
while current is not None:
44+
path.append(current)
45+
current = parents[current]
46+
return path
47+
48+
4149
def bidirectional_search(
4250
graph: dict[int, list[int]], start: int, goal: int
4351
) -> list[int] | None:
@@ -126,19 +134,11 @@ def bidirectional_search(
126134
return None
127135

128136
# Construct path from start to intersection
129-
forward_path: list[int] = []
130-
current_forward: int | None = intersection
131-
while current_forward is not None:
132-
forward_path.append(current_forward)
133-
current_forward = forward_parents[current_forward]
137+
forward_path: list[int] = construct_path(current=intersection, parents=forward_parents)
134138
forward_path.reverse()
135139

136140
# Construct path from intersection to goal
137-
backward_path: list[int] = []
138-
current_backward: int | None = backward_parents[intersection]
139-
while current_backward is not None:
140-
backward_path.append(current_backward)
141-
current_backward = backward_parents[current_backward]
141+
backward_path: list[int] = construct_path(current=backward_parents[intersection], parents=backward_parents)
142142

143143
# Return the complete path
144144
return forward_path + backward_path

0 commit comments

Comments
 (0)