Easy · O(V + E)
Graph Traversal: BFS and DFS quiz
Answer the questions in your own time, change a pick whenever you like, then hand in to see your score and why each answer is right. Nothing is saved or sent.
Questions (10)
- Viewing
- Answered
- Not answered
An undirected graph has 6 vertices, with these adjacency lists: 0: [1, 2], 1: [0, 3], 2: [0, 3, 5], 3: [1, 2, 4], 4: [3], 5: [2]. What does bfs(graph, 0) return?
Same graph (0: [1, 2], 1: [0, 3], 2: [0, 3, 5], 3: [1, 2, 4], 4: [3], 5: [2]). What does dfs(graph, 0) return?
On the same graph, what does dfs(graph, 5) return?
In an unweighted graph, which search visits vertices in order of their number of edges from start, and why?
In bfs, visited[v] = true is inside the neighbour loop. Suppose you instead mark u when it is taken out of the queue (and skip it if already marked). On the graph from question 1, starting at 0, what changes?
In dfs, imagine visited[v] = true were set when v is pushed, instead of when it is popped. On the graph from question 1, starting at 0, what would it return?
dfs pushes graph[u] from the last neighbour to the first. What would a loop that pushes from the first to the last return for the star 0: [1, 2, 3], 1: [0], 2: [0], 3: [0], starting at 0?
A graph has 8 vertices and these undirected edges: 0-1, 1-2, 2-0, 3-4, 4-5, 6-7. What does bfs(graph, 3) return, and how would you visit every vertex?
In the complete graph on 5 vertices (every pair joined, 10 edges, graph[u] lists the other vertices in increasing order), dfs(graph, 0) marks vertices when they are popped. What is the largest size the stack reaches?
A directed graph has these arcs: 0→1, 1→2, 3→1, 2→4, 4→3 (so graph[0] = [1], graph[1] = [2], graph[2] = [4], graph[3] = [1], graph[4] = [3]). What does bfs(graph, 2) return?
Progress
0 / 10 questions0%
10 questions left
Quiz details
- Total questions
- 10
- Answered
- 0
- Not answered
- 10
Take it further
- AnimationGraphs
Graph Traversal Animation
Queue or stack, watch the search choose: BFS and DFS on a glowing graph, side by side, ready for your lessons, slides and posts
Free
