Docsity

CS 161 — Divide and Conquer, Exams of Nursing

Toggle sidebar
bg1
CS 161 — Divide and Conquer
Table of Contents
1. The Divide-and-Conquer Recipe
2. Recurrences
3. Recursion Trees
4. Master Theorem
5. MergeSort
6. Integer Multiplication
7. Selection and Median
8. Divide-and-Conquer versus DP
9. Common Mistakes
10. Official CS 161 References
The Divide-and-Conquer Recipe
A divide-and-conquer algorithm has three steps:
1. Divide the input into smaller subproblems.
2. Conquer each subproblem recursively.
3. Combine the subproblem solutions into a solution for the original problem.stanford-
cs161.github+1
Stanford’s slides summarize the idea as breaking a big problem into smaller, easier subproblems,
then solving those recursively.stanford-cs161.github+1
Base case
Every recursive algorithm needs a base case: an input small enough to solve directly.
For sorting, the base case is usually an array of size 0 or 1, which is already sorted.stanford-
cs161.github+1
General structure
text
DIVIDE-AND-CONQUER(input):
if input is small enough:
solve directly
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download CS 161 — Divide and Conquer and more Exams Nursing in PDF only on Docsity!

CS 161 — Divide and Conquer

Table of Contents

  1. The Divide-and-Conquer Recipe
  2. Recurrences
  3. Recursion Trees
  4. Master Theorem
  5. MergeSort
  6. Integer Multiplication
  7. Selection and Median
  8. Divide-and-Conquer versus DP
  9. Common Mistakes
  10. Official CS 161 References

The Divide-and-Conquer Recipe

A divide-and-conquer algorithm has three steps:

  1. Divide the input into smaller subproblems.
  2. Conquer each subproblem recursively.
  3. Combine the subproblem solutions into a solution for the original problem.stanford- cs161.github+

Stanford’s slides summarize the idea as breaking a big problem into smaller, easier subproblems, then solving those recursively.stanford-cs161.github+

Base case

Every recursive algorithm needs a base case: an input small enough to solve directly.

For sorting, the base case is usually an array of size 0 or 1, which is already sorted.stanford- cs161.github+

General structure

text DIVIDE-AND-CONQUER(input): if input is small enough: solve directly

return solution

divide input into subproblems recursively solve each subproblem combine subproblem solutions

return solution

Recurrences

A recurrence relation describes the running time of a problem of size nnn in terms of the running time of smaller subproblems.stanford-cs161.github

For a divide-and-conquer algorithm that creates aaa subproblems of size n/bn/bn/b, and spends O(nd)O(n^d)O(nd) time dividing and combining, the recurrence is:

T(n)=aT(n/b)+O(nd)T(n) = aT(n/b) + O(n^d)T(n)=aT(n/b)+O(nd)

where:

 a≥1a \ge 1a≥1 is the number of recursive subproblems  b>1b > 1b>1 is the factor by which the input shrinks  d≥0d \ge 0d≥0 is the exponent in the divide/combine coststanford-cs161.github+

Example

MergeSort splits an array into two halves, recursively sorts each half, and merges them in linear time:

T(n)=2T(n/2)+O(n)T(n) = 2T(n/2) + O(n)T(n)=2T(n/2)+O(n)

stanford-cs161.github+

Recursion Trees

A recursion tree shows the work done at each level of recursion.

For:

T(n)=2T(n/2)+nT(n) = 2T(n/2) + nT(n)=2T(n/2)+n

 Level 0: one problem of size nnn, with nnn combine work  Level 1: two problems of size n/2n/2n/2, with 2(n/2)=n2(n/2) = n2(n/2)=n combine work

Examples

Recurrence aaa bbb ddd Comparison Solution

T(n)=2T(n/2)+nT(n) = 2T(n/2) + nT(n)=2T(n/2)+n 2 2 1 2=22 = 2 2 = 2 Θ(nlogn)^ \Theta(n^ \log n)Θ(nlogn)

T(n)=4T(n/2)+nT(n) = 4T(n/2) + nT(n)=4T(n/2)+n 4 2 1 4>24 > 2 4 > 2 Θ(n2)\Theta(n^2)Θ(n2)

T(n)=2T(n/2)+n2T(n) = 2T(n/2) + n^2T(n)=2T(n/2)+n 2 2 2 2<42 < 4 2 < 4 Θ(n2)\Theta(n^2)Θ(n2)

T(n)=8T(n/2)+n2T(n) = 8T(n/2) + n^2T(n)=8T(n/2)+n 8 2 2 8>48 > 4 8 > 4 Θ(n3)\Theta(n^3)Θ(n3)

Stanford’s notes use MergeSort as the primary example: a=2a = 2a=2, b=2b = 2b=2, d=1d = 1d=1, so a=bda = b^da=bd, giving O(nlogn)O(n \log n)O(nlogn).stanford-cs161.github+

MergeSort

MergeSort is the canonical divide-and-conquer sorting algorithm.stanford-cs161.github+

Algorithm

  1. If the array has size 1, return it.
  2. Split it into two halves.
  3. Recursively sort each half.
  4. Merge the two sorted halves.stanford-cs161.github+

text MERGE-SORT(A): if length(A) <= 1: return A

L = MERGE-SORT(first half of A) R = MERGE-SORT(second half of A)

return MERGE(L, R)

Correctness

Base case: An array of size 1 is sorted.  Inductive step: If both halves are sorted correctly, MERGE produces a sorted array containing all original elements.

Therefore, MergeSort correctly sorts any array.

Running time

T(n)=2T(n/2)+O(n)T(n) = 2T(n/2) + O(n)T(n)=2T(n/2)+O(n)

Using the Master Theorem:

T(n)=O(nlogn)T(n) = O(n \log n)T(n)=O(nlogn)

Stanford’s notes explicitly derive this result.stanford-cs161.github+

Properties

 Stable, if equal elements from the left half are merged first  Worst-case time: Θ(nlogn) \Theta(n \log n)Θ(nlogn)  Extra space: Θ(n)\Theta(n)Θ(n)

Integer Multiplication

Stanford introduces divide-and-conquer through multiplying two nnn-digit integers.

Naive approach

Given two nnn-digit numbers xxx and yyy, split them into halves:

x=10n/2a+bx = 10^{n/2}a + bx=10n/2a+b y=10n/2c+dy = 10^{n/2}c + dy=10n/2c+d

Then:

xy=10nac+10n/2(ad+bc)+bdxy = 10^n ac + 10^{n/2}(ad + bc) + bdxy=10nac+10n/2(ad+bc)+bd

This creates four subproblems:

ac,ad,bc,bdac, \quad ad, \quad bc, \quad bdac,ad,bc,bd

The recurrence is:

T(n)=4T(n/2)+O(n)T(n) = 4T(n/2) + O(n)T(n)=4T(n/2)+O(n)

By the Master Theorem:

T(n)=Θ(n2)T(n) = \Theta(n^2)T(n)=Θ(n2)

Deterministic median-of-medians

The deterministic linear-time selection algorithm uses groups of five elements:

  1. Divide the array into groups of 5.
  2. Find the median of each group.
  3. Recursively find the median of those medians.
  4. Use that value as a pivot.
  5. Recurse into the side containing the desired rank.

This guarantees a constant-fraction reduction in problem size and yields worst-case linear time:

Θ(n)\Theta(n)Θ(n)

Stanford’s Lecture 4 notes discuss median selection and recurrence-based analysis for this algorithm.stanford-cs161.github

Divide-and-Conquer versus DP

Feature Divide and conquer Dynamic programming

Subproblems Usually disjoint Often overlapping

Combination Combine solutions directly Store and reuse subproblem solutions

Recurrence aT(n/b)+f(n)aT(n/b) + f(n)aT(n/b)+f(n) Often depends on multiple prior states

Typical analysis Recursion trees and Master Theorem Table size and work per state

Example MergeSort Longest common subsequence

Divide-and-conquer works especially well when subproblems are independent. When the same subproblems recur, dynamic programming is usually more appropriate.stanford-cs161.github+

Common Mistakes

Mistake 1: Forgetting the combine cost

A recurrence must include both recursive work and divide/combine work.

For MergeSort:

T(n)=2T(n/2)+O(n)T(n) = 2T(n/2) + O(n)T(n)=2T(n/2)+O(n)

not merely:

T(n)=2T(n/2)T(n) = 2T(n/2)T(n)=2T(n/2)

stanford-cs161.github+

Mistake 2: Misidentifying aaa, bbb, and ddd

For:

T(n)=4T(n/2)+nT(n) = 4T(n/2) + nT(n)=4T(n/2)+n

the parameters are:

a=4,b=2,d=1a = 4, \qquad b = 2, \qquad d = 1a=4,b=2,d=

not a=2a = 2a=2.stanford-cs161.github+

Mistake 3: Applying the Master Theorem to unequal

subproblems

The standard Master Theorem assumes all subproblems have the same size. If subproblem sizes differ, use a recursion tree or substitution instead.stanford-cs161.github+

Mistake 4: Confusing recursion depth with total runtime

For MergeSort, the recursion depth is Θ(logn) \Theta(\log n)Θ(logn), but each level does Θ(n)\Theta(n)Θ(n) work. Therefore, total runtime is:

Θ(nlogn) \Theta(n \log n)Θ(nlogn)

not merely Θ(logn) \Theta(\log n)Θ(logn).stanford-cs161.github+

Mistake 5: Assuming divide-and-conquer always improves

runtime

A divide-and-conquer algorithm can be slower than a simple iterative algorithm if the divide/combine cost is too high or the recursion creates too many subproblems. Always derive and compare the recurrence.

Official CS 161 References