J. Carlos Gomez
This project implements a breadth-first search (BFS) algorithm for directed graphs using NetworkX. The Graph class loads a graph from an adjacency list file and provides a bfs method for traversal and shortest-path search.
The bfs(start, end=None) method supports two modes:
-
Traversal Mode (end is None)
Returns a list of nodes in the order they are visited using breadth-first traversal starting from the given start node. -
Pathfinding Mode (end is provided)
Returns the shortest path from the start node to the end node as a list of nodes.
If no path exists, the function returnsNone.
The implementation handles several edge cases:
- Empty graphs return an empty list for traversal and
Nonefor pathfinding. - A missing end node returns
None. - A missing start node raises a
ValueError. - Disconnected graphs return
Nonewhen no path exists.
Unit tests are included to verify:
- Correct BFS traversal order
- Correct shortest path computation
- Handling of edge cases such as empty graphs and missing nodes
- Exception handling for invalid inputs