Data Structures & AlgorithmsModerateGATE CS · ICPC regional
A directed acyclic graph has the vertices C, D, G, J, O, S, T and the directed edges D→J, G→C, G→O, S→O, C→J, D→O, O→J, D→T, C→O, S→C, O→T, C→T, S→D. How many edges does a longest directed path in this graph contain?
- A. 2
- B. 3
- C. 4
- D. 6
Correct answer: B. 3
Take dp[x] = 1 + max(dp[y]) over arcs y→x, with dp = 0 for sources; the largest dp value is 3 edges. Following the first listed out-arc at each step gives only 2, and counting vertices instead of edges gives 4.
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
- An undirected graph has the vertices B, C, F, L, O, P, Q, R, V, Z and the edges Q–L, Q–Z, Q–V, B–O, B–L, B–Z, B–R, B–P, O–Z, O–F, O–V…
- A grid has 6 rows and 4 columns (top to bottom; '.' open, '#' blocked): .... #... .... #... ..#. ..#. You may only move right or down, and…
- A bipartite graph has left side P1, P2, P3 and right side Q1, Q2, Q3, Q4, Q5, with edges P1–Q5, P1–Q3, P3–Q4, P1–Q2, P2–Q1, P2–Q4, P3–Q3…
- A directed graph has the vertices E, G, Q, Z and the arcs E→Q, E→Z, G→E, G→Q, G→Z, Q→G, Q→Z, Z→E. Count the directed Hamiltonian paths…
- A grid has 4 rows and 3 columns with these values (top to bottom): 121 725 748 239 A path moves between side-adjacent cells (up, down…
- A tree has vertices with weights in brackets: A[9], J[13], P[6], Q[8], M[2], V[3], Y[6], R[8]. The edges are A–J, J–P, A–Q, Q–M, P–V, Q–Y…