Data Structures & AlgorithmsVery hardICPC regional · Product Company SDE
Problem: Greedy graph colouring assigns colours in vertex order 0, 1, 2, 3, 4, 5, giving each vertex the smallest colour (starting at 0) not used by an already-coloured neighbour. The graph has edges 0-2, 0-3, 1-2, 1-4, 2-3, 3-5, 4-5. How many colours does greedy colouring use?
- A. 3
- B. 4
- C. 2
- D. 5
Correct answer: A. 3
Greedy colouring is order-dependent: each vertex takes the least colour its earlier neighbours do not use. Here the triangle 0-2-3 forces three colours, so greedy happens to match the chromatic number.
More Coding Interview Problems questions
- Problem: In the undirected graph with edges 0-1, 0-2, 1-2, 1-3, 2-3, 3-4, how many simple paths (no repeated vertex) go from vertex 0 to…
- Problem: An undirected forest (a graph with no cycles) has 9 vertices and 6 edges. How many connected components does it have?
- Problem: A rooted tree (root 1) has parent pointers: 2→1, 3→1, 4→2, 5→2, 6→4, 7→6, 8→3, 9→8. How many nodes lie on the path between node 7…
- Problem: For the complete binary tree with 10 nodes stored in heap order (node i has children 2i and 2i+1, root is node 1), what is the…
- Problem: Using the parent array par[i] for nodes 0–9 with par = [−1, 0, 0, 1, 1, 3, 3, 5, 6, 8], what is the 3rd ancestor of node 9 (the…
- Problem: Count the indices i of the string "abacaba" for which the suffix s[i:] is a palindrome.