
Assumption
All vertices are reachable from the given vertex. We use the following graph.

Instruction
- Construct a directed graph class with adjacency list representation.
- Use a list to record states of all vertices with True for visited and False for unvisited.
- Use a queue to store vertices to start in each inner loop.
- Start from one given vertex to reach all vertices reachable.
- When a vertex is reached, alter the corresponding element in the state list from False to True.
- When the queue is empty, all vertices are reached.
Code in Python 3.5
1 |
from collections import defaultdict, deque |
Output
1 |
the adjacency list of this graph: |




近期评论