Data Structures & AlgorithmsModerateGATE CS · ICPC regional
The directed acyclic graph has the vertices C, E, J, K, L, M, Q, Z and the arcs Q→C, Q→M, C→J, L→J, L→K, Q→K, E→J, M→C, M→L, M→E, Q→E, L→C. How many distinct directed paths go from Q to J? A path is a sequence of consecutive arcs; a single arc counts as a path.
- A. 2
- B. 4
- C. 6
- D. 3
Correct answer: C. 6
Count with ways[x] = sum of ways[y] over arcs y→x, starting at ways[Q] = 1 and processing vertices in a topological order. This gives 6 paths; using only paths of at most two arcs would give 2.
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
- Starting from the integer 4, one operation either adds 1, subtracts 1, or doubles the current value. Values may be any non-negative…
- A directed graph has the vertices A, M, O, V and the arcs O→M, O→A, M→O, M→A, A→O, A→V, V→O, V→A. Count the directed Hamiltonian paths…
- An undirected graph has the vertices B, D, E, F, I, M, X and the edges M–E, M–X, M–D, M–B, M–F, E–X, E–D, E–I, X–D, X–B, D–F, B–I, F–I…
- A directed weighted graph has the arcs u→v (weight): Y→K (14), Y→Q (6), K→L (4), K→T (5), L→Q (8), D→Y (3), D→Q (11), D→T (3), Q→Y (12)…
- Each cell of a grid with 3 rows and 4 columns has an arrow: R = right, L = left, U = up, D = down. UURD DLRU LRUU From a cell you may move…
- A grid has 3 rows and 4 columns with these values (top to bottom): 4179 9341 9892 A path moves between side-adjacent cells (up, down, left…