This code implements a simple unweighted, undirected graph data structure in Python. It also includes iterators for breadth-first search (BFS) and depth-first search (DFS) traversals.
The project consists of three main components:
Graph
: A class that represents a graph. The graph is stored as an adjacency list, where each node maps to a list of its neighbors.BfsIterator
andDfsIterator
classes that allow for iterating over the graph in a Breadth-First Search (BFS) and Depth-First Search (DFS) manner respectively.
- Add and remove nodes from the graph.
- Add and remove edges between nodes in the graph.
- Get the neighbors of a given node.
- Perform a breadth-first search (BFS) or depth-first search (DFS) on the graph.
g = Graph()
g.add_node('A')
g.add_node('B')
bfs_iterator = g.bfs('A')
for node in bfs_iterator:
print(node)
dfs_iterator = g.dfs('A')
for node in dfs_iterator:
print(node)