Skip to content

Problem 1. Prove that n3/1000100n2100n+3=Θ(n3)n^3 / 1000 - 100n^2 - 100n + 3 = \Theta(n^3).

Problem 2. Use the Master Theorem to solve the recurrence T(n)=3T(n/4)+nlognT(n) = 3T(n/4) + n \log n. If the Master Theorem does not apply, explain why.

Problem 3. Prove that log(n!)=Θ(nlogn)\log(n!) = \Theta(n \log n) using Stirling”s approximation: n!2πn(n/e)nn! \approx \sqrt{2\pi n}(n/e)^n.

Problem 4. Show the result of inserting the keys 15,5,20,10,25,3,7,3015, 5, 20, 10, 25, 3, 7, 30 into an initially empty AVL tree. Show all rotations.

Problem 5. Prove that deleting a node from a red-black tree with nn internal nodes takes O(logn)O(\log n) time.

Problem 6. Design a hash table for n=1000n = 1000 strings using chaining. Choose the table size mmA hash function, and compute the expected number of comparisons for a successful search.

Problem 7. A skip list uses p=1/4p = 1/4. What is the expected maximum level for n=10000n = 10000 elements? What is the expected time for search?

Problem 8. Prove that the union-by-rank heuristic alone (without path compression) gives O(logn)O(\log n) amortised time per Union-Find operation.

Problem 9. Prove that heapsort is not stable by giving a concrete counterexample.

Problem 10. Given an array of nn integers in the range [0,n21][0, n^2 - 1]Design an O(n)O(n) 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 Ω(nlogn)\Omega(n \log n) (not just the worst case).

Problem 12. Run Dijkstra’s algorithm on the following graph from source AA. Show the state of the priority queue after each extraction. Edge weights: A10BA \xrightarrow{10} B, A3CA \xrightarrow{3} C, C4BC \xrightarrow{4} B, C8DC \xrightarrow{8} D, C2EC \xrightarrow{2} E, B7DB \xrightarrow{7} D, E5DE \xrightarrow{5} D, D6BD \xrightarrow{6} B.

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: (A,B),(B,C),(C,A),(B,D),(D,E),(E,F),(F,D),(F,G)(A, B), (B, C), (C, A), (B, D), (D, E), (E, F), (F, D), (F, G). 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 nn types of coin denominations d1,d2,,dnd_1, d_2, \ldots, d_n and a target amount MM. Find the minimum number of coins needed to make exact change for MM (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 A1(2×10)A_1 (2 \times 10), A2(10×50)A_2 (10 \times 50), A3(50×20)A_3 (50 \times 20), A4(20×5)A_4 (20 \times 5), A5(5×80)A_5 (5 \times 80)Find the optimal parenthesisation using the matrix chain multiplication DP. Show the full DP table.

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 2/(n(n1))2 / (n(n-1))And hence that O(n2logn)O(n^2 \log n) repetitions suffice to find a minimum cut with high probability (Karger’s algorithm).

  • Confusing average and worst-case complexity. Quicksort: average O(nlogn)O(n \log n), worst case O(n2)O(n^2). 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: O(1)O(1) amortised append, O(n)O(n) worst case (when resizing).

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: log21000=10\lceil \log_2 1000 \rceil = 10 comparisons.

\blacksquare

Problem. A hash table of size 7 uses linear probing. Insert keys 10, 22, 31, 4, 15, 28, 17 with hash function h(k)=kmod7h(k) = k \bmod 7.

Solution. h(10)=3h(10)=3: slot 3. h(22)=1h(22)=1: slot 1. h(31)=3h(31)=3: collision, slot 4. h(4)=4h(4)=4: collision, slot 5. h(15)=1h(15)=1: collision, slot 2. h(28)=0h(28)=0: slot 0. h(17)=3h(17)=3: collision, slots 4, 5, 6.

\blacksquare

  • Sorting: merge sort O(nlogn)O(n \log n) guaranteed; quicksort O(nlogn)O(n \log n) average; heap sort O(nlogn)O(n \log n) in-place.
  • Searching: linear O(n)O(n), binary O(logn)O(\log n), hash O(1)O(1) average.
  • Data structures: arrays, linked lists, stacks, queues, trees, hash tables, heaps, graphs.
  • Amortised analysis: dynamic arrays O(1)O(1) amortised append; splay trees O(logn)O(\log n) amortised.

Problem 1. n3/1000100n2100n+3=Θ(n3)n^3/1000 - 100n^2 - 100n + 3 = \Theta(n^3). For n106n \ge 10^6: n3/1000100n2100n+3n3/1000100n2n3/2000n^3/1000 - 100n^2 - 100n + 3 \ge n^3/1000 - 100n^2 \ge n^3/2000 (for large nn). Also, n3/1000100n2100n+3n3/1000n^3/1000 - 100n^2 - 100n + 3 \le n^3/1000 for all nn. Thus c1=1/2000c_1 = 1/2000, c2=1/1000c_2 = 1/1000, n0=106n_0 = 10^6 suffice, establishing Θ(n3)\Theta(n^3).

Problem 2. T(n)=3T(n/4)+nlognT(n) = 3T(n/4) + n\log n. In Master Theorem form: a=3a = 3, b=4b = 4, f(n)=nlognf(n) = n\log n. logba=log430.792\log_b a = \log_4 3 \approx 0.792. Since f(n)=nlogn=Ω(n0.792+ε)f(n) = n\log n = \Omega(n^{0.792+\varepsilon}), case 3 applies if the regularity condition 3(n/4)log(n/4)cnlogn3(n/4)\log(n/4) \le c n\log n holds for some c<1c < 1. This holds for c=3/4c = 3/4 and sufficiently large nn. Thus T(n)=Θ(nlogn)T(n) = \Theta(n\log n).

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 2×\le 2 \times MST cost 2×\le 2 \times optimal TSP cost (since optimal TSP minus one edge is a spanning tree).

TopicSiteLink
[Algorithms and Data Structures (Advanced)]IBView
[Algorithms and Data Structures (Advanced)]UniversityView