Data Structures & AlgorithmsHardProduct-company MCQ pattern
DFS is run on a directed graph with edges A→B, A→C, B→D, C→D, C→A, D→B, E→C, E→D, starting from A, taking vertices and neighbours in alphabetical order and restarting from the smallest unvisited vertex. How many back edges are found?
- A. 0
- B. 2
- C. 1
- D. 3
Correct answer: B. 2
Tree edges: A→B, B→D, A→C, then E starts a new tree. D→B (B is still on the stack) and C→A (A is still on the stack) are back edges, so 2; C→D, E→C and E→D are cross edges. Each back edge marks a cycle: B⇄D and A→C→A.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- Kahn's algorithm is run on the DAG with edges 5→2, 5→0, 4→0, 4→1, 2→3, 3→1, always removing the SMALLEST-numbered vertex of in-degree 0 (a…
- How many topological orderings does the DAG with edges A→B, A→C, B→D, C→D, D→E, B→F have?
- Prim's algorithm starts at A on the undirected graph with edges A–B (3), A–C (1), B–C (7), B–D (5), C–D (2), C–E (7), D–E (3), D–F (8)…
- An undirected graph on A, B, C, D has edges A–B, B–C, C–D, D–A of weight 1 and a diagonal A–C of weight 2. How many distinct minimum…
- Union-find on elements 0–7 uses union by size (the smaller tree's root points to the larger tree's root; on a tie the second root points…
- Seven distinct keys are stored in a max-heap in a 0-indexed array. Which statement is correct?