Data Structures & AlgorithmsVery hardProduct-company placement
A tree on vertices 1–11 has edges 2–1, 3–2, 4–3, 5–2, 6–3, 7–5, 8–7, 9–6, 10–9, 11–9. What is the size of a maximum independent set (no two chosen vertices adjacent)?
- A. 6
- B. 4
- C. 7
- D. 8
Correct answer: C. 7
Tree DP: in(u) = 1 + Σ out(child), out(u) = Σ max(in, out) of children; or greedily take every leaf and delete its parent. Both give 7. Taking the larger colour class of the bipartition (6) is not optimal here.
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
- Edit distance between BINARY and GRAPHICAL where insert and delete cost 1 each but a substitution costs 2: what is the minimum total cost?
- Keys 37, 84, 56, 34, 79, 18, 83, 69, 74 are inserted in that order into an empty AVL tree. What is the preorder traversal of the final tree?
- All suffixes of the string 'abab' are inserted into a character trie (a suffix trie). How many nodes does it have, including the root?
- How many subsets of {3, 5, 6, 7, 8, 10, 14} have sum exactly 24?
- A directed graph on A–H has edges A→B, A→C, A→G, B→E, E→G, F→A, F→B, F→C, F→E, H→B, H→D. How many strongly connected components does it…
- Keys 56, 16, 21, 93, 86, 85, 15 inserted in this order build a BST. How many of the 7! orderings of these same 7 keys, inserted into an…