Data Structures & AlgorithmsHardProduct company OA
How many distinct spanning trees does the graph with vertices A, B, C, D and edges A–B, B–C, C–D, D–A, A–C (a 4-cycle plus one diagonal) have?
- A. 10
- B. 4
- C. 8
- D. 16
Correct answer: C. 8
Choose 3 of the 5 edges: C(5, 3) = 10 subsets. Exactly two of them contain a triangle (A–B, B–C, A–C and A–C, C–D, D–A) and so are not trees: 10 − 2 = 8. Kirchhoff's matrix-tree theorem gives the same. 16 is Cayley's count for the complete graph K₄, which has one more edge.
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 recursive segment tree is built over an array of n = 6 elements (each internal node splits its range into two halves; leaves are single…
- In a Fenwick (binary indexed) tree of size 16 (1-based), the update at index 5 adds the value to i = 5 and then repeatedly does i += i &…
- Union–find with NO union by rank/size and NO path compression: union(a, b) sets parent[find(a)] = find(b). Starting with singletons 0–5…
- A connected undirected graph has vertex degrees A: 3, B: 4, C: 2, D: 3, E: 2. Does it have an Euler path (using every edge exactly once)…
- Houses in a row hold [3, 2, 7, 10, 1, 9]. No two ADJACENT houses may both be chosen. What is the maximum total? (dp[i] = max(dp[i − 1]…
- The set [3, 9, 7, 3, 11] is split into two subsets so that the absolute difference of their sums is as small as possible (subset-sum DP…