MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / main

Function main

graphs/bidirectional_search.py:151–197  ·  view source on GitHub ↗

Run example of bidirectional search algorithm. Examples: >>> main() # doctest: +NORMALIZE_WHITESPACE Path from 0 to 11: [0, 1, 3, 7, 11] Path from 5 to 5: [5] Path from 0 to 3: None

()

Source from the content-addressed store, hash-verified

149
150
151def main() -> None:
152 """
153 Run example of bidirectional search algorithm.
154
155 Examples:
156 >>> main() # doctest: +NORMALIZE_WHITESPACE
157 Path from 0 to 11: [0, 1, 3, 7, 11]
158 Path from 5 to 5: [5]
159 Path from 0 to 3: None
160 """
161 # Example graph represented as an adjacency list
162 example_graph = {
163 0: [1, 2],
164 1: [0, 3, 4],
165 2: [0, 5, 6],
166 3: [1, 7],
167 4: [1, 8],
168 5: [2, 9],
169 6: [2, 10],
170 7: [3, 11],
171 8: [4, 11],
172 9: [5, 11],
173 10: [6, 11],
174 11: [7, 8, 9, 10],
175 }
176
177 # Test case 1: Path exists
178 start, goal = 0, 11
179 path = bidirectional_search(graph=example_graph, start=start, goal=goal)
180 print(f"Path from {start} to {goal}: {path}")
181
182 # Test case 2: Start and goal are the same
183 start, goal = 5, 5
184 path = bidirectional_search(graph=example_graph, start=start, goal=goal)
185 print(f"Path from {start} to {goal}: {path}")
186
187 # Test case 3: No path exists (disconnected graph)
188 disconnected_graph = {
189 0: [1, 2],
190 1: [0],
191 2: [0],
192 3: [4],
193 4: [3],
194 }
195 start, goal = 0, 3
196 path = bidirectional_search(graph=disconnected_graph, start=start, goal=goal)
197 print(f"Path from {start} to {goal}: {path}")
198
199
200if __name__ == "__main__":

Callers 1

Calls 1

bidirectional_searchFunction · 0.85

Tested by

no test coverage detected