Problem Set
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)
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)
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)
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)
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)
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
- 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
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
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
- 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.
Cross-References
| Topic | Site | Link |
|---|---|---|
| [Algorithms and Data Structures (Advanced)] | IB | View |
| [Algorithms and Data Structures (Advanced)] | University | View |