Data Structures & AlgorithmsVery hardGATE CS · ICPC regional
The directed acyclic graph has the vertices D, F, G, I, J, O, Z and the arcs J→D, I→G, G→F, I→Z, I→D, F→D, G→D. How many distinct directed paths go from I to D? A path is a sequence of consecutive arcs; a single arc counts as a path.
- A. 5
- B. 2
- C. 4
- D. 3
Correct answer: D. 3
Count with ways[x] = sum of ways[y] over arcs y→x, starting at ways[I] = 1 and processing vertices in a topological order. This gives 3 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
- A connected undirected graph has the vertices D, E, O, P, W and weighted edges (weights in brackets): D–P (3), E–P (19), D–E (8), P–O (5)…
- A bipartite graph has left side P1, P2, P3 and right side Q1, Q2, Q3, Q4, Q5, with edges P3–Q1, P1–Q2, P3–Q2, P3–Q4, P1–Q5, P2–Q4, P1–Q1…
- A grid has 4 rows and 3 columns with these values (top to bottom): 944 176 815 649 A path moves between side-adjacent cells (up, down…
- A tree has vertices with values in brackets: R[-3], G[6], I[7], U[-4], H[3], W[1], P[9], O[-3]. The edges are R–G, G–I, I–U, R–H, R–W…
- An undirected graph has the vertices A, G, Q, Z and the edges Z–A, Z–G, Q–A, Q–G. In how many ways can each vertex be given one of 3…
- A directed acyclic graph has the vertices E, N, O, S, U, V, Z and the edges Z→S, N→O, U→V, U→N, U→O, E→V, E→S. A topological order lists…