Data Structures & AlgorithmsHardProduct company OA
DFS with white/grey/black colours detects a cycle in the directed graph with adjacency lists 1: [2, 3], 2: [4], 3: [4], 4: [5], 5: [3], starting at 1 and visiting neighbours in order. Which edge is the FIRST one found to point to a grey vertex (a back edge)?
- A. 3 → 4
- B. 5 → 3
- C. 4 → 5
- D. 1 → 3
Correct answer: A. 3 → 4
DFS goes 1 → 2 → 4 → 5; from 5 the edge 5 → 3 leads to WHITE 3, so 3 is explored (grey path now 1, 2, 4, 5, 3). From 3 the edge 3 → 4 hits 4, which is still grey → back edge 3 → 4, and the cycle is 4 → 5 → 3 → 4. Expecting 5 → 3 assumes 3 was visited first via 1 → 3.
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
- Keys 1 to 7 are inserted in increasing order into an empty AVL tree, then 4 is deleted (a node with two children is replaced by its…
- What is the size of a minimum vertex cover (a smallest set of vertices touching every edge) of the tree with edges 1–2, 1–3, 2–4, 2–5…
- In the tree with edges 1–2, 1–3, 2–4, 2–5, 3–6, 5–7, the rerooting technique computes S(v) = sum of distances from v to every other node…
- Each node of this binary tree holds a value; how many DOWNWARD paths (starting at any node, moving only to children, ending at any node)…
- Weighted undirected graph: A–B 1, B–C 2, C–D 3, D–A 4, A–C 5, B–D 6. The MST is unique. What is the weight of the SECOND-best spanning…
- Word ladder: change one letter at a time, every intermediate word must be in the dictionary {cold, cord, card, ward, warm, word, worm…