b. 2
c. 3
d. 4
#
Section 28.7 Depth–First Search (DSF)
11. Suppose a graph is created in the following code. Using the dfs algorithm in the text, what is the output for the
path from 4 to 0?
Integer[] vertices = {0, 1, 2, 3, 4};
int[][] edges = {
{0, 1}, {0, 2},
{1, 0}, {1, 2}, {1, 3}, {1, 4},
{2, 0}, {2, 1}, {2, 3},
{3, 1}, {3, 2}, {3, 4},
{4, 1}, {4, 3}
};
Graph<Integer> graph1 = new UnweightedGraph<>(vertices, edges);
UnweigtedGraph<Integer>.Tree dfs = graph1.dfs(0);
System.out.println(dfs.getPath(4));
a. [4, 3, 2, 0]
b. [4, 3, 1, 0]
c. [4, 1, 0]
d. [4, 3, 2, 1, 0]
e. [4, 1, 2, 0]
#
9. The search of a graph first visits a vertex, then it recursively visits all the vertices adjacent to that vertex.
a. depth–first
b. breadth-first
Key:a See Listing 28.8.
#
10. The time complexity of the DFS algorithm is O(|E| + |V|).
a. true
b. false
#
Section 28.9 Breadth-First Search
14. Suppose a graph is created in the following code. Using the bfs algorithm in the text, what is the output for the
path from 4 to 0?
Integer[] vertices = {0, 1, 2, 3, 4};
int[][] edges = {
{0, 1}, {0, 2},
{1, 0}, {1, 2}, {1, 3}, {1, 4},
{2, 0}, {2, 1}, {2, 3},