Problem Set
7.1 Analysis (Problems 1—3)
Section titled “7.1 Analysis (Problems 1—3)”Problem 1. Prove that .
Problem 2. Use the Master Theorem to solve the recurrence . If the Master Theorem does not apply, explain why.
Problem 3. Prove that using Stirling”s approximation: .
7.2 Data Structures (Problems 4—8)
Section titled “7.2 Data Structures (Problems 4—8)”Problem 4. Show the result of inserting the keys into an initially empty AVL tree. Show all rotations.
Problem 5. Prove that deleting a node from a red-black tree with internal nodes takes time.
Problem 6. Design a hash table for strings using chaining. Choose the table size A hash function, and compute the expected number of comparisons for a successful search.
Problem 7. A skip list uses . What is the expected maximum level for elements? What is the expected time for search?
Problem 8. Prove that the union-by-rank heuristic alone (without path compression) gives amortised time per Union-Find operation.
7.3 Sorting (Problems 9—11)
Section titled “7.3 Sorting (Problems 9—11)”Problem 9. Prove that heapsort is not stable by giving a concrete counterexample.
Problem 10. Given an array of integers in the range Design an sorting algorithm using radix sort. Justify the choice of base and number of passes.
Problem 11. Prove that the best-case number of comparisons for comparison-based sorting is (not just the worst case).
7.4 Graph Algorithms (Problems 12—15)
Section titled “7.4 Graph Algorithms (Problems 12—15)”Problem 12. Run Dijkstra’s algorithm on the following graph from source . Show the state of the priority queue after each extraction. Edge weights: , , , , , , , .
Problem 13. Prove that if a graph has a negative-weight cycle reachable from the source, then Bellman-Ford will detect it.
Problem 14. Use the cut property to prove that the minimum spanning tree is unique when all edge weights are distinct.
Problem 15. Find the strongly connected components of the graph with edges: . Use Kosaraju’s algorithm and show all steps.
7.5 Dynamic Programming (Problems 16—17)
Section titled “7.5 Dynamic Programming (Problems 16—17)”Problem 16. You are given types of coin denominations and a target amount . Find the minimum number of coins needed to make exact change for (or report that it is impossible). Give a recurrence, prove correctness, and state the time and space complexity.
Problem 17. Given a sequence of matrices , , , , Find the optimal parenthesisation using the matrix chain multiplication DP. Show the full DP table.
7.6 Advanced Topics (Problems 18—20)
Section titled “7.6 Advanced Topics (Problems 18—20)”Problem 18. Prove that CLIQUE is NP-complete by reducing from 3-SAT.
Problem 19. Design a 2-approximation algorithm for the metric TSP using the MST shortcutting technique. Prove the approximation ratio.
Problem 20. A randomised algorithm for MINIMUM CUT works by repeatedly contracting random edges until two vertices remain. Prove that the probability that any specific minimum cut survives is at least And hence that repetitions suffice to find a minimum cut with high probability (Karger’s algorithm).
Common Pitfalls
Section titled “Common Pitfalls”- Confusing average and worst-case complexity. Quicksort: average , worst case . Fix: Always state which case; worst case is the guaranteed upper bound.
- Wrong BST deletion. Deleting a node with two children requires finding the in-order successor (or predecessor), not removing the node. Fix: Replace the node with its in-order successor, then delete the successor from its original position.
- Confusing amortised and worst-case analysis. Amortised: average cost per operation over a sequence. Worst-case: maximum cost of a single operation. Fix: Dynamic array: amortised append, worst case (when resizing).
Worked Examples
Section titled “Worked Examples”Example 1: Binary search
Section titled “Example 1: Binary search”Problem. In a sorted array of 1000 elements, how many comparisons does binary search need in the worst case?
Solution. Binary search eliminates half the remaining elements each step. Worst case: comparisons.
Example 2: Hash table collision
Section titled “Example 2: Hash table collision”Problem. A hash table of size 7 uses linear probing. Insert keys 10, 22, 31, 4, 15, 28, 17 with hash function .
Solution. : slot 3. : slot 1. : collision, slot 4. : collision, slot 5. : collision, slot 2. : slot 0. : collision, slots 4, 5, 6.
Summary
Section titled “Summary”- Sorting: merge sort guaranteed; quicksort average; heap sort in-place.
- Searching: linear , binary , hash average.
- Data structures: arrays, linked lists, stacks, queues, trees, hash tables, heaps, graphs.
- Amortised analysis: dynamic arrays amortised append; splay trees amortised.
Solutions to Selected Problems
Section titled “Solutions to Selected Problems”Problem 1. . For : (for large ). Also, for all . Thus , , suffice, establishing .
Problem 2. . In Master Theorem form: , , . . Since , case 3 applies if the regularity condition holds for some . This holds for and sufficiently large . Thus .
Problem 4. Insert into AVL tree: 15, 5, 20, 10, 25, 3, 7, 30.
- Insert 15: root.
- Insert 5: left child of 15.
- Insert 20: right child of 15.
- Insert 10: right child of 5. Balance factor of 15 is 1 - 2 = -1 (right-heavy). No rotation.
- Insert 25: right child of 20. Balance factor of 15 is 1 - 3 = -2. Right-Right case: rotate 15 left. New root: 20.
- Insert 3: left child of 5. Balance factor of 20 is 2 - 2 = 0. OK.
- Insert 7: right child of 5. Balance factor of 5 is 1 - 2 = -1. Then 20 has factor 3 - 2 = 1. Right-Left case at 5: rotate 10 right, then rotate left. Final AVL tree balanced.
Problem 12. Dijkstra from A: PQ = {A:0}. Extract A, relax: B=10, C=3. PQ = {C:3, B:10}. Extract C, relax: B=min(10,3+4)=7, D=3+8=11, E=3+2=5. PQ = {E:5, B:7, D:11}. Extract E, relax: D=min(11,5+5)=10. PQ = {B:7, D:10}. Extract B, relax: D=min(10,7+7)=10. PQ = {D:10}. Extract D. Final distances: A=0, C=3, E=5, B=7, D=10.
Problem 19. Metric TSP 2-approximation: (1) Compute MST of the complete graph with metric distances. (2) Double every edge to get an Eulerian graph. (3) Find an Eulerian tour. (4) Shortcut by skipping repeated vertices. Cost MST cost optimal TSP cost (since optimal TSP minus one edge is a spanning tree).
Cross-References
Section titled “Cross-References”| Topic | Site | Link |
|---|---|---|
| [Algorithms and Data Structures (Advanced)] | IB | View |
| [Algorithms and Data Structures (Advanced)] | University | View |