Graph Traversal
Graph Traversal MCQs
๐ Graph Traversals โ GATE-Level MCQs (Algorithms)
Q1. Breadth-First Search (BFS) uses which data structure?
A) StackโB) QueueโC) Priority QueueโD) Deque
โ
Answer: B
Explanation: BFS explores neighbors level by level using a queue.
Q2. Depth-First Search (DFS) uses which data structure?
A) QueueโB) StackโC) HeapโD) Deque
โ
Answer: B
Explanation: DFS explores depth-wise using recursion or explicit stack.
Q3. Time complexity of BFS using adjacency list is:
A) O(V + E)โB) O(Vยฒ)โC) O(E log V)โD) O(V log E)
โ
Answer: A
Explanation: Each vertex and edge visited once โ O(V + E).
Q4. DFS time complexity for adjacency matrix representation is:
A) O(Vยฒ)โB) O(V + E)โC) O(E log V)โD) O(V log E)
โ
Answer: A
Explanation: Checking adjacency for each vertex costs O(Vยฒ).
Q5. In BFS, the shortest path in an unweighted graph is found because:
A) It explores by depth
B) It explores by increasing distance
C) It uses stack
D) It backtracks frequently
โ
Answer: B
Explanation: BFS explores nodes in order of non-decreasing distance from the source.
Q6. DFS tree of a directed acyclic graph (DAG) is always:
A) A forestโB) A single treeโC) A cycleโD) Complete graph
โ
Answer: A
Explanation: DAG may be disconnected โ DFS gives a forest.
Q7. Which traversal can be used to detect cycles in a directed graph?
A) DFSโB) BFSโC) BothโD) None
โ
Answer: A
Explanation: Back edges in DFS indicate cycles.
Q8. BFS of a disconnected undirected graph:
A) Visits only connected componentโB) Visits all nodes
C) Always failsโD) Requires DFS
โ
Answer: A
Explanation: BFS from one node covers only its connected component.
Q9. Space complexity of BFS is:
A) O(V)โB) O(E)โC) O(V + E)โD) O(Vยฒ)
โ
Answer: A
Explanation: Queue can hold up to all vertices at a level.
Q10. Which traversal is best to find connected components in undirected graph?
A) DFSโB) BFSโC) BothโD) None
โ
Answer: C
Explanation: Either DFS or BFS can identify connected components.
Q11. DFS on a graph with V vertices and E edges requires how much space?
A) O(V)โB) O(V + E)โC) O(E)โD) O(Vยฒ)
โ
Answer: A
Explanation: Stack depth โค V.
Q12. BFS tree of an undirected graph is unique if:
A) Graph is connected
B) Graph is tree
C) Graph is complete
D) Start node is isolated
โ
Answer: B
Explanation: Tree structure ensures unique traversal.
Q13. DFS is particularly useful for:
A) Finding shortest paths
B) Topological sorting
C) Minimum spanning tree
D) Dijkstraโs algorithm
โ
Answer: B
Explanation: Topological sorting uses post-order DFS.
Q14. In BFS, the level of a vertex equals:
A) Its distance from source
B) Its degree
C) Its finish time
D) Its weight
โ
Answer: A
Explanation: BFS visits nodes level-wise โ level = distance.
Q15. DFS on a tree will visit each node:
A) OnceโB) TwiceโC) nยฒ timesโD) Randomly
โ
Answer: A
Explanation: Each node visited exactly once.
Q16. Edge classification in DFS includes all except:
A) Tree edgeโB) Back edgeโC) Cross edgeโD) Symmetric edge
โ
Answer: D
Explanation: โSymmetric edgeโ not part of DFS classification.
Q17. BFS cannot be used directly for:
A) Shortest path (unweighted)โB) Detecting bipartite graphโC) Detecting cycles in directed graphโD) Level-order traversal
โ
Answer: C
Explanation: DFS is better for directed cycle detection.
Q18. A directed acyclic graph (DAG) will have:
A) Back edgesโB) Tree edges onlyโC) Forward and cross edgesโD) No edges
โ
Answer: C
Explanation: DAG has no back edges but may have forward/cross edges.
Q19. In BFS, if a vertex is discovered at level k, then all its neighbors are at level:
A) k + 1โB) kโC) โค kโD) โฅ k + 1
โ
Answer: A
Explanation: BFS explores one level deeper next.
Q20. DFS can detect cycles because:
A) Uses stackโB) Uses queue
C) Back edges appear only in cyclic paths
D) Uses adjacency matrix
โ
Answer: C
Explanation: Back edges imply cycles.
Q21. Topological sorting is possible only if:
A) Graph is connectedโB) Graph is DAGโC) Graph has cyclesโD) Graph is tree
โ
Answer: B
Explanation: Topological sort valid only for DAGs.
Q22. BFS traversal of a tree and level-order traversal:
A) Are different
B) Are identical
C) May differ depending on weights
D) Depend on recursion
โ
Answer: B
Explanation: Both visit nodes level by level.
Q23. DFS traversal can be implemented using:
A) RecursionโB) Explicit stackโC) BothโD) None
โ
Answer: C
Explanation: Both recursive and iterative versions possible.
Q24. BFS on an adjacency matrix requires:
A) O(Vยฒ)โB) O(V + E)โC) O(V log V)โD) O(E log V)
โ
Answer: A
Explanation: Checking adjacency row takes O(V) per vertex.
Q25. For connected undirected graph, number of BFS trees = ?
A) Depends on start vertex
B) OneโC) Equal to number of vertices
D) Infinite
โ
Answer: C
Explanation: One BFS tree per source vertex.
Q26. Finish time in DFS is used in:
A) Kruskalโs algorithmโB) Dijkstraโs algorithmโC) Topological sortโD) Primโs algorithm
โ
Answer: C
Explanation: Sorting vertices by finish time gives topological order.
Q27. BFS fails to detect cycles because:
A) It ignores back edges
B) It marks visited nodes early
C) It explores in breadth-first manner
D) All of these
โ
Answer: D
Explanation: BFS doesnโt maintain recursion info โ misses back edges.
Q28. Time complexity to find connected components using DFS:
A) O(V + E)โB) O(Vยฒ)โC) O(V log V)โD) O(Eยฒ)
โ
Answer: A
Explanation: Every vertex & edge processed once.
Q29. DFS pre-order and post-order can differ because:
A) They depend on edge order
B) They are identical
C) Graph is directed
D) Graph is cyclic
โ
Answer: A
Explanation: The order of adjacency traversal changes visit order.
Q30. In BFS, if two vertices are at same level, then:
A) Equal distance from sourceโB) Different distanceโC) One is ancestor of otherโD) Undefined
โ
Answer: A
Explanation: Level represents distance in unweighted graph.
(Skipping repetitive intros โ continuing compactly but detailed)
Q31. DFS traversal of a disconnected graph gives: A) One treeโB) ForestโC) CycleโD) Line
โ
Answer: B โ Each component โ separate DFS tree.
Q32. BFS traversal order depends on: A) Edge weightsโB) Queue orderโC) Graph shape onlyโD) Vertex colors
โ
Answer: B โ FIFO order affects visitation.
Q33. DFS can be used to find: A) Strongly connected componentsโB) Minimum pathโC) MSTโD) Degree of vertex
โ
Answer: A โ Kosarajuโs algorithm uses DFS twice.
Q34. BFS can detect bipartite graph by: A) Level parity coloringโB) StackโC) Edge weight checkโD) Sorting edges
โ
Answer: A โ Alternate colors on levels.
Q35. DFS is tail-recursive when: A) Graph is treeโB) No branchingโC) Graph cyclicโD) Weighted
โ
Answer: B โ Linear chain โ tail recursion.
Q36. For DAG, topological sort count equals: A) 1โB) V!โC) May varyโD) 2
โ
Answer: C โ Depends on DAG structure.
Q37. Cycle detection in undirected graph uses: A) Parent checkโB) Queue sizeโC) Degree checkโD) BFS only
โ
Answer: A โ Edge to visited โ parent โ cycle.
Q38. BFS can be used to find shortest path in: A) Weighted graphsโB) Unweighted graphsโC) Directed acyclic graphsโD) Negative weighted graphs
โ
Answer: B โ BFS works only on unweighted.
Q39. In DFS, if no back edges exist โ graph is: A) AcyclicโB) BipartiteโC) WeightedโD) Disconnected
โ
Answer: A โ No back edge โ acyclic.
Q40. BFS queue length is maximum when: A) Graph completeโB) Graph treeโC) Graph chainโD) Star graph
โ
Answer: D โ Center connected to all leaves.
Q41. DFS recursion depth for binary tree = height โ O(log n).
โ
Answer: A
Explanation: For balanced tree.
Q42. Number of edges in BFS tree = Vโ1 (if connected).
โ
Answer: A
Explanation: Spanning tree has Vโ1 edges.
Q43. BFS complexity on dense graph = O(Vยฒ).
โ
Answer: A
Explanation: EโVยฒ.
Q44. DFS visited array avoids: revisiting nodes โ prevents infinite recursion.
โ
Answer: A
Q45. BFS can compute shortest distance table โ O(V + E).
โ
Answer: A
Q46. In DAG, forward edges exist but not back edges.
โ
Answer: A
Q47. DFS discovery time < finish time always true.
โ
Answer: A
Q48. Cross edges occur in DFS when: different branches connect.
โ
Answer: A
Q49. BFS traversal on binary tree โ Level order traversal.
โ
Answer: A
Q50. DFS traversal on binary tree โ Preorder traversal if process before recursion.
โ
Answer: A
Graph Traversal MCQs
Q51. In a DFS traversal of a graph with 10 vertices and no back edges, the graph must be:
A) CyclicโB) DirectedโC) A forestโD) Strongly connected
โ
Answer: C
Explanation: Absence of back edges indicates acyclic structure โ forest.
Q52. BFS traversal of a graph is equivalent to:
A) Level order traversal of treeโB) PreorderโC) InorderโD) Postorder
โ
Answer: A
Explanation: BFS explores level by level like level-order traversal.
Q53. DFS traversal can be implemented using:
A) QueueโB) StackโC) Priority queueโD) Linked list only
โ
Answer: B
Explanation: DFS uses a stack (explicit or recursion).
Q54. BFS is preferable over DFS when:
A) Searching for shortest pathโB) Searching deep pathsโC) Detecting cyclesโD) Finding articulation points
โ
Answer: A
Explanation: BFS finds shortest path in unweighted graphs.
Q55. Time complexity of DFS for adjacency list representation = ?
A) O(V)โB) O(V + E)โC) O(Vยฒ)โD) O(E log V)
โ
Answer: B
Explanation: Each vertex and edge visited once.
Q56. In DFS traversal, a back edge indicates:
A) CycleโB) Self loop onlyโC) BridgeโD) Bipartiteness
โ
Answer: A
Explanation: Back edge to ancestor implies cycle.
Q57. BFS traversal is not suitable for:
A) Shortest path in unweighted graphsโB) Shortest path in weighted graphsโC) Level order searchโD) Connectivity testing
โ
Answer: B
Explanation: Weighted graphs need Dijkstra, not BFS.
Q58. Number of BFS trees in disconnected graph = ?
A) 1โB) Equal to connected componentsโC) Equal to edgesโD) Equal to vertices
โ
Answer: B
Explanation: Each component produces its own BFS tree.
Q59. A directed acyclic graph (DAG) has how many possible topological orderings?
A) Always 1โB) May have multipleโC) NoneโD) Equal to vertices
โ
Answer: B
Explanation: Different valid orders possible depending on dependencies.
Q60. DFS in undirected graph marks an edge as tree edge if:
A) It leads to unvisited vertexโB) Leads to parentโC) Leads to ancestorโD) Leads to visited vertex
โ
Answer: A
Explanation: First visit to vertex creates tree edge.
Q61. DFS traversal of disconnected graph requires:
A) One DFSโB) Separate DFS for each componentโC) BFS traversal firstโD) Cycle removal
โ
Answer: B
Explanation: Each disconnected component must be visited separately.
Q62. Edge classification is applicable in:
A) Directed graphs onlyโB) Undirected graphs onlyโC) TreesโD) Bipartite graphs
โ
Answer: A
Explanation: DFS edge classification (tree, back, forward, cross) applies only to directed graphs.
Q63. The color method in DFS uses three colors for marking states as:
A) White, Gray, BlackโB) Red, Green, BlueโC) 0, 1, 2โD) A, B, C
โ
Answer: A
Explanation: White = unvisited, Gray = visiting, Black = visited.
Q64. BFS queue can hold at most how many vertices at a time (worst case)?
A) O(V)โB) O(E)โC) O(Vยฒ)โD) O(log V)
โ
Answer: A
Explanation: Entire level may be enqueued.
Q65. Which traversal uses implicit recursion stack?
A) BFSโB) DFSโC) DijkstraโD) Bellman-Ford
โ
Answer: B
Explanation: DFS recursion uses call stack.
Q66. Topological sorting is possible only when graph is:
A) AcyclicโB) CyclicโC) WeightedโD) Strongly connected
โ
Answer: A
Explanation: Cycles prevent topological order.
Q67. Detecting cycle in directed graph can be done by:
A) BFS onlyโB) DFS onlyโC) DijkstraโD) Primโs algorithm
โ
Answer: B
Explanation: DFS with recursion stack detects cycles.
Q68. A BFS traversal of a connected undirected graph with n vertices and n edges will have:
A) Exactly one cycleโB) No cycleโC) Two cyclesโD) Multiple cycles
โ
Answer: A
Explanation: n vertices and n edges โ one cycle.
Q69. DFS can be used to compute finishing times used in:
A) Topological sortโB) BFSโC) DijkstraโD) Kruskal
โ
Answer: A
Explanation: DFS finishing times define topological order.
Q70. The space complexity of BFS = ?
A) O(V)โB) O(V + E)โC) O(E)โD) O(log V)
โ
Answer: A
Explanation: Queue can store O(V) vertices.
Q71. DFS tree of an undirected graph has 20 vertices and 19 edges. Graph is:
A) Connected and acyclicโB) DisconnectedโC) CyclicโD) Complete
โ
Answer: A
Explanation: n-1 edges โ tree โ connected, acyclic.
Q72. The postorder numbering in DFS is mainly used to:
A) Detect cyclesโB) Classify edgesโC) Compute SCCsโD) Sort edges
โ
Answer: C
Explanation: Kosarajuโs algorithm uses postorder for SCCs.
Q73. In DFS traversal, discovery time and finishing time differ by:
A) ConstantโB) Proportional to degreeโC) 2 ร subtree sizeโD) None
โ
Answer: C
Explanation: Each vertex adds two timestamps (entry, exit).
Q74. Number of edges explored in BFS = ?
A) EโB) 2EโC) VโD) V + E
โ
Answer: A
Explanation: Each edge considered once per direction.
Q75. BFS can be used to detect bipartite graphs by:
A) Level coloringโB) DFSโC) Counting edgesโD) Sorting degrees
โ
Answer: A
Explanation: Alternate coloring levels ensures no same-color adjacency.
Q76. DFS traversal order depends on:
A) Adjacency list orderโB) Vertex degreeโC) Graph typeโD) None
โ
Answer: A
Explanation: Different adjacency orders โ different DFS trees.
Q77. In BFS, when is a node marked as visited?
A) When dequeuedโB) When enqueuedโC) When processedโD) After traversal
โ
Answer: B
Explanation: Marking on enqueue avoids duplication.
Q78. Which traversal can be used to find strongly connected components?
A) BFSโB) DFSโC) DijkstraโD) Kruskal
โ
Answer: B
Explanation: DFS-based algorithms (Kosaraju, Tarjan).
Q79. BFS traversal of tree starting from root visits nodes in:
A) PreorderโB) InorderโC) Level orderโD) Postorder
โ
Answer: C
Explanation: BFS = level order traversal.
Q80. For an undirected connected graph, DFS traversal gives a spanning:
A) TreeโB) ForestโC) DAGโD) Cycle
โ
Answer: A
Explanation: DFS on connected graph yields a spanning tree.
Q81. BFS traversal can be applied to:
A) DirectedโB) UndirectedโC) BothโD) None
โ
Answer: C
Explanation: BFS works on any graph type.
Q82. Recursive DFS uses which data structure internally?
A) StackโB) QueueโC) Priority QueueโD) Deque
โ
Answer: A
Explanation: Recursion uses call stack.
Q83. Topological sort of DAG using DFS has time complexity:
A) O(V + E)โB) O(Vยฒ)โC) O(E log V)โD) O(VE)
โ
Answer: A
Explanation: Each vertex and edge visited once.
Q84. BFS is not recursive because:
A) Uses queueโB) Uses stackโC) Cannot detect cyclesโD) Complex logic
โ
Answer: A
Explanation: Queue prevents recursion.
Q85. BFS traversal produces shortest paths when:
A) Edge weights equalโB) Edge weights positiveโC) Edge weights negativeโD) Graph cyclic
โ
Answer: A
Explanation: Equal weight edges โ BFS gives shortest paths.
Q86. DFS traversal can be used to detect bridges using:
A) Discovery and low timesโB) Level numbersโC) Edge weightsโD) In-degrees
โ
Answer: A
Explanation: Tarjanโs algorithm uses low values for bridges.
Q87. BFS traversal can fail to terminate if:
A) No visited array usedโB) Recursion usedโC) Stack overflowโD) Edges missing
โ
Answer: A
Explanation: Without marking, nodes re-enqueued infinitely.
Q88. DFS traversal number of recursive calls = ?
A) VโB) EโC) V + EโD) 2V
โ
Answer: A
Explanation: One call per vertex.
Q89. In directed acyclic graph, the reverse postorder of DFS = ?
A) Topological orderโB) BFS orderโC) RandomโD) Level order
โ
Answer: A
Explanation: Reverse of finishing time gives topo order.
Q90. BFS traversal tree edges correspond to:
A) First discovery of verticesโB) Back edgesโC) Cross edgesโD) Forward edges
โ
Answer: A
Explanation: Each first visit creates tree edge.
Q91. DFS traversal of complete graph with n vertices visits:
A) All vertices onceโB) Multiple visitsโC) NoneโD) Random subset
โ
Answer: A
Explanation: DFS ensures each vertex visited exactly once.
Q92. If DFS of graph with 6 vertices finishes with vertex 1 first, it means:
A) 1 has no outgoing edgesโB) 1 is sourceโC) 1 is sinkโD) 1 has max degree
โ
Answer: C
Explanation: Finishes first โ no outgoing edges.
Q93. BFS traversal detects cycle when:
A) Edge connects two visited vertices in same levelโB) Edge connects to unvisited vertexโC) Self-loopโD) Forward edge
โ
Answer: A
Explanation: Same-level visited vertex indicates cycle.
Q94. For an undirected tree with n vertices, BFS traverses how many edges?
A) n โ 1โB) nโC) 2nโD) log n
โ
Answer: A
Explanation: Tree has nโ1 edges.
Q95. DFS traversal of disconnected graph produces:
A) ForestโB) Single treeโC) Cyclic subgraphโD) None
โ
Answer: A
Explanation: Each component โ one DFS tree.
Q96. BFS traversal uses which visiting pattern?
A) FIFOโB) LIFOโC) RandomโD) Priority
โ
Answer: A
Explanation: Queue โ FIFO.
Q97. DFS traversal uses which visiting pattern?
A) LIFOโB) FIFOโC) RandomโD) Round robin
โ
Answer: A
Explanation: Stack โ LIFO.
Q98. BFS traversal starting from vertex u can reach vertex v iff:
A) v is in same componentโB) v has higher degreeโC) v is isolatedโD) v has lower degree
โ
Answer: A
Explanation: BFS explores connected component of start vertex.
Q99. Topological sort exists iff graph has:
A) No cycleโB) Equal vertices and edgesโC) One sinkโD) Equal in/out degrees
โ
Answer: A
Explanation: DAG โ topological order possible.
Q100. DFS traversal of a tree visits every vertex exactly:
A) OnceโB) TwiceโC) ThriceโD) Four times
โ
Answer: A
Explanation: Each vertex entered once and exited once.
