Data Structures & AlgorithmsHardTCS Digital · Infosys pattern
DFS-based topological sort on the DAG with adjacency lists a: [b, c], b: [d], c: [d, e], d: [f], e: [f], f: [] starts at a, visits neighbours in list order, and outputs vertices in REVERSE finishing order. What order is produced?
- A. a, b, c, d, e, f
- B. a, b, d, f, c, e
- C. f, d, b, e, c, a
- D. a, c, e, b, d, f
Correct answer: D. a, c, e, b, d, f
Finishing order: f (via a→b→d→f), d, b, then from c: d is done, e → f done, so e, then c, then a: f, d, b, e, c, a. Reversed: a, c, e, b, d, f. 'a, b, d, f, c, e' is the DFS DISCOVERY order, which is not a valid topological order here (c → d is violated); f, d, b, e, c, a is the finishing order itself, not reversed.
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
- 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]…
- 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…