Data Structures & AlgorithmsHardCodeforces Div 2 C · GATE CS
An undirected graph on 7 vertices has the edges U–Z, U–B, U–J, Z–G, Z–N, G–P, P–B, P–N, P–J, N–J. How many simple paths (no repeated vertex) go from U to J? Paths are counted as vertex sequences, so each route is counted once.
- A. 8
- B. 1
- C. 4
- D. 10
Correct answer: A. 8
A DFS that extends the path through unvisited neighbours enumerates every simple route, giving 8. Counting only the shortest routes gives 1, which is a much smaller set.
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, J, M, S, Z and the edges M–Z, Z–S, Z–J, Z–A, S–D, S–A, D–A. How many bridges does it…
- A flow network has source S and sink T, with directed arcs and capacities in brackets: S→E (4), S→T (9), C→D (8), C→T (3), B→E (6), B→T…
- A tree has the vertices A, E, G, I, K, L, N, O, W and the edges G–W, W–O, O–E, E–L, G–I, G–A, A–K, O–N. A centroid is a vertex whose…
- A tree has vertices A, B, F, H, I, K, M, Q, S and weighted edges (weights in brackets): Q–S (5), Q–M (4), Q–K (1), S–H (3), Q–A (7), K–B…
- A flow network has source S and sink T, with directed arcs and capacities in brackets: S→E (8), S→T (3), B→E (6), B→T (7), E→T (9), C→E…
- Floyd–Warshall runs on vertices 1 to 4 with these arcs u→v (weight): 1→3 (1), 1→4 (4), 2→1 (1), 2→3 (3), 3→1 (12), 3→2 (11), 3→4 (3), 4→1…