Data Structures & AlgorithmsModerateCodeforces Div 2 C · GATE CS
The directed acyclic graph has the vertices D, E, J, L, N, Q, S, Z and the arcs D→Q, Z→N, N→Q, Q→S, D→J, E→S, L→Q, E→Q, D→N, E→N, J→Q, J→L, J→S, D→S, L→S, Z→S, E→L. How many distinct directed paths go from D to S? A path is a sequence of consecutive arcs; a single arc counts as a path.
- A. 7
- B. 5
- C. 8
- D. 4
Correct answer: A. 7
Count with ways[x] = sum of ways[y] over arcs y→x, starting at ways[D] = 1 and processing vertices in a topological order. This gives 7 paths; using only paths of at most two arcs would give 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
- A connected undirected graph has the vertices A, D, E, F, H, J, M, Z and the edges J–Z, J–D, J–F, J–M, Z–F, Z–H, Z–E, D–A, D–F, A–E, F–E…
- On an 8×8 chessboard, rows and columns are numbered 0 to 7, and squares are written (row, column). A knight moves in the usual L-shape…
- An undirected graph has the vertices C, D, E, L, M, S, U and the edges U–E, U–S, U–C, D–L, D–C, D–M, L–S, L–M, E–S, E–M, C–M. What is the…
- An undirected graph has the vertices D, E, H, M, O, P, U, V and the edges H–O, H–M, H–D, H–V, O–E, O–V, O–P, M–U, M–V, E–D, E–U, D–U, D–V…
- An undirected connected graph on the vertices B, H, O, P, S has the edges H–S, O–B, S–P, H–P, S–B, B–P, H–B. How many spanning trees does…
- A tree has vertices A, C, G, J, N, Q, X, Y and weighted edges (weights in brackets): Y–Q (2), Y–X (5), Y–J (2), J–C (2), C–A (8), X–G (7)…