Skip to content
The Lumenary

Each worksheet prints cleanly on A4 or Letter. Answer keys are on a separate page.

The Lumenary · Early undergraduate

Algorithm design and analysis

Name: ______________________

Date: ______________________

You will learn to

  • Design divide-and-conquer algorithms and analyse them with recurrences, proving that mergesort is correct and makes at most n⌈log⁡2n⌉n\lceil\log_2 n\rceil comparisons
  • Prove that every comparison sort needs at least log⁡2n!=Θ(nlog⁡n)\log_2 n! = \Theta(n \log n) comparisons in the worst case
  • Prove the correctness of greedy algorithms by exchange arguments — Kruskal's algorithm via the cut property, and Dijkstra's algorithm when all edge weights are non-negative — and recognise when greedy choice fails
  • Design dynamic-programming algorithms from an optimal-substructure recurrence, as for the longest common subsequence, and analyse their cost
  • Use breadth-first search for unweighted distances and choose an appropriate algorithm for a graph problem
  1. 1.

    Two sorted lists have lengths 55 and 77. What is the largest number of comparisons the merging procedure can need to merge them?

  2. 2.

    What is the decision-tree lower bound ⌈log⁡2n!⌉\lceil \log_2 n! \rceil on the worst-case number of comparisons needed to sort n=5n = 5 distinct items?

  3. 3.

    Mergesort's worst-case comparison count satisfies C(1)=0C(1) = 0 and C(n)=2C(n/2)+n−1C(n) = 2C(n/2) + n - 1 for nn a power of two. Compute C(8)C(8).

  4. 4.

    The network of towns in the Explore section has these cable costs: AB=4AB = 4, AC=1AC = 1, BC=2BC = 2, BD=5BD = 5, CD=8CD = 8, CE=10CE = 10, DE=3DE = 3. Run Kruskal's algorithm. What is the total cost of the minimum spanning tree?

  5. 5.

    Run Dijkstra's algorithm from ss on the directed graph with edges s→as \to a (weight 11), s→bs \to b (weight 22) and b→ab \to a (weight −2-2). What does it report for aa?

    • A.11 — which is wrong, since the path s→b→as \to b \to a has weight 00.
    • B.00, the correct distance.
    • C.−2-2
    • D.It detects the negative edge and stops with an error.
  6. 6.

    In the proof of Theorem 5 (Dijkstra), which step uses the hypothesis that all edge weights are non-negative?

    • A.δ(s,y)≤δ(s,u)\delta(s, y) \le \delta(s, u), because yy lies on a shortest path to uu.
    • B.d(y)≤d(x)+w(x,y)d(y) \le d(x) + w(x, y).
    • C.d(u)≤d(y)d(u) \le d(y).
    • D.δ(s,x)+w(x,y)=δ(s,y)\delta(s, x) + w(x, y) = \delta(s, y).
  7. 7.

    Suppose a currency has coins worth 11, 44 and 55. For which amount does the greedy method (always use the largest coin that fits) not use the fewest coins?

    • A.88
    • B.99
    • C.1010
    • D.77
  8. 8.

    What is the length of a longest common subsequence of AGGTAB and GXTXAYB?

  9. 9.

    Which specification correctly says that a Lean function sort : List ℕ → List ℕ is a sorting function? (Sorted l means each entry is ≤ the later ones; l₁ ~ l₂ means l₁ is a permutation of l₂.)

    • A.∀ l, Sorted (sort l) ∧ sort l ~ l
    • B.∀ l, Sorted (sort l)
    • C.∀ l, sort l ~ l
    • D.∀ l, Sorted (sort l) ∧ (sort l).length = l.length
  10. 10.

    Write lcs_length(x, y) that returns the length of a longest common subsequence of the strings x and y, by filling the dynamic-programming table of Theorem 6 in O(mn)O(mn) time.

    Computational — Practice

Code: Apache-2.0 · Content: CC BY-SA 4.0