1212"""
1313
1414from collections import deque
15- from typing import Dict , List , Optional
15+ from typing import Optional
1616
1717
1818def bidirectional_search (
19- graph : Dict [int , List [int ]], start : int , goal : int
20- ) -> Optional [ List [ int ]] :
19+ graph : dict [int , list [int ]], start : int , goal : int
20+ ) -> list [ int ] | None :
2121 """
2222 Perform bidirectional search on a graph to find the shortest path.
2323
@@ -67,8 +67,8 @@ def bidirectional_search(
6767
6868 # Initialize forward and backward search dictionaries
6969 # Each maps a node to its parent in the search
70- forward_parents = {start : None }
71- backward_parents = {goal : None }
70+ forward_parents : dict [ int , int | None ] = {start : None }
71+ backward_parents : dict [ int , int | None ] = {goal : None }
7272
7373 # Initialize forward and backward search queues
7474 forward_queue = deque ([start ])
@@ -110,19 +110,19 @@ def bidirectional_search(
110110 return None
111111
112112 # Construct path from start to intersection
113- forward_path = []
114- current = intersection
115- while current is not None :
116- forward_path .append (current )
117- current = forward_parents [current ]
113+ forward_path : list [ int ] = []
114+ current_forward : int | None = intersection
115+ while current_forward is not None :
116+ forward_path .append (current_forward )
117+ current_forward = forward_parents [current_forward ]
118118 forward_path .reverse ()
119119
120120 # Construct path from intersection to goal
121- backward_path = []
122- current = backward_parents [intersection ]
123- while current is not None :
124- backward_path .append (current )
125- current = backward_parents [current ]
121+ backward_path : list [ int ] = []
122+ current_backward : int | None = backward_parents [intersection ]
123+ while current_backward is not None :
124+ backward_path .append (current_backward )
125+ current_backward = backward_parents [current_backward ]
126126
127127 # Return the complete path
128128 return forward_path + backward_path
@@ -131,7 +131,7 @@ def bidirectional_search(
131131def main () -> None :
132132 """
133133 Run example of bidirectional search algorithm.
134-
134+
135135 Examples:
136136 >>> main() # doctest: +NORMALIZE_WHITESPACE
137137 Path from 0 to 11: [0, 1, 3, 7, 11]
0 commit comments