9.5 Exercises¶
9.5.1 Concept Review¶
1. Represent the Same Graph in Two Ways¶
An undirected graph has four vertices, A, B, C, D, and the edges
A-B, A-C, B-C, C-D.
- Write its adjacency list.
- Fill in its adjacency matrix using only 0s and 1s.
- To determine whether
AandDare directly connected, which graph representation requires checking only one stored entry? - If a graph has many vertices but few edges, which representation usually uses less space?
Answer
-
The adjacency list is:
-
The adjacency matrix is:
A B C D A 0 1 1 0 B 1 0 1 0 C 1 1 0 1 D 0 0 1 0 -
In an adjacency matrix, you can directly check row
A, columnD, making it well suited to determining whether any two vertices are directly connected. -
When a graph has many vertices but few edges, an adjacency list records only the edges that actually exist. It usually uses less space than an adjacency matrix, which reserves a position for every pair of vertices.
2. Breadth-First and Depth-First Traversal Orders¶
An undirected graph has vertices A, B, C, D, E and edges
A-B, A-C, B-D, C-D, D-E.
Start at A. Whenever there are several unvisited adjacent vertices, choose them in alphabetical order:
- Write the visit order of breadth-first traversal (BFS).
- Write the visit order of recursive depth-first traversal (DFS).
- Why must both traversals record which vertices have already been visited?
Answer
-
The BFS visit order is
A, B, C, D, E. It first visits B and C, which are one edge away from A, and then visits the more distant D and E. -
The DFS visit order is
A, B, D, C, E. It repeatedly enters an unvisited adjacent vertex, first followingA → B → D → C. When C has no new adjacent vertex, it returns to D and then visits E. -
The graph contains a cycle, such as
A-B-D-C-A. Without recording visited vertices, a traversal could repeatedly visit the same vertices around the cycle and fail to terminate normally.
3. Can One BFS Visit the Entire Graph?¶
An undirected graph has vertices A, B, C, D, E, F and only the edges
A-B, B-C, D-E.
- Which vertices can one BFS starting at A visit?
- Based on Question 1, has this BFS visited every vertex in the graph? Why or why not?
- Suppose you scan all vertices in alphabetical order and start a new BFS whenever you reach an unvisited vertex. What is the starting vertex of each BFS? Into how many mutually disconnected parts (connected components) is the graph divided?
Answer
-
Starting from A, the traversal can visit only
A, B, C. -
It has not visited every vertex.
D, Eform another connected part, while F is an isolated vertex. None of them has a path to A, so they cannot be reached from A. -
The three BFS traversals start at
A, D, F, and visit{A, B, C},{D, E}, and{F}, respectively. Therefore, the graph has 3 connected components.
9.5.2 Programming Exercises¶
1. Determine Whether a Path Exists in an Undirected Graph¶
You are given an undirected graph with \(n\) vertices numbered from \(0\) to \(n-1\). Each entry [u, v] in the array edges represents an undirected edge between vertices u and v.
You are also given a starting vertex source and a destination vertex destination. First build an adjacency list from edges, then use BFS or DFS
to determine whether a path exists from source to destination. Return true if one exists and false otherwise.
The graph may contain cycles and may be disconnected.
Hints
- Add every undirected edge in both directions
- The graph may contain cycles, so you must record which vertices have already been visited
- Starting from source, return true if you encounter destination; if the traversal ends without reaching it, return false