Algorithm Problem Solving (APS):
Divide-and-Conquer
Niema Moshiri
UC San Diego SPIS 2021
What is an algorithm?
Goals of Algorithm Problem Solving (APS)
Goals of Algorithm Problem Solving (APS)
Goals of Algorithm Problem Solving (APS)
Goals of Algorithm Problem Solving (APS)
Goals of Algorithm Problem Solving (APS)
Example: The Largest Integer Problem
Example: The Largest Integer Problem
Example: The Largest Integer Problem
7 | 25 | 0 | 42 | -9 |
Example: The Largest Integer Problem
7 | 25 | 0 | 42 | -9 |
Example: The Largest Integer Problem
7 | 25 | 0 | 42 | -9 |
Example: The Largest Integer Problem
7 | 2 | 0 | 4 | -9 | 5 | 1 | -4 | 3 | 8 | -2 | -7 | ... | -1 | -8 | 6 | -3 | -6 | 9 | -5 | 2 |
Easier Example: The Peanut Butter & Jelly Problem
Easier Example: The Peanut Butter & Jelly Problem
Easier Example: The Peanut Butter & Jelly Problem
Easier Example: The Peanut Butter & Jelly Problem
Let’s solve the problem!
Easier Example: The Peanut Butter & Jelly Problem
What is Algorithm Problem Solving (APS)?
What is Algorithm Problem Solving (APS)?
What is Algorithm Problem Solving (APS)?
What is Algorithm Problem Solving (APS)?
What is Algorithm Problem Solving (APS)?
APS → Algorithm → Program
Example: The Largest Integer Problem
Let’s solve the problem!
Example: The Largest Integer Problem
Algorithm largest_number(ints):
x ← negative infinity
For every integer y in ints:
if y > x:
x ← y
Return x
Example: The Largest Integer Problem
Example: The Largest Integer Problem
Example: The Largest Integer Problem
Example: The Largest Integer Problem
Recursion
Recursion
Recursion
Recursion
Example: Counting People Recursively
Algorithm num_people(person):
If person is at the front of the line:
Return 1
Else:
neighbor ← the person in front of person
Return num_people(neighbor) + 1
Divide-and-Conquer Algorithms
Divide-and-Conquer Algorithms
Divide-and-Conquer Algorithms
Divide-and-Conquer Algorithms
Divide-and-Conquer Algorithms
A Protocol for Solving Problems
A Protocol for Solving Problems
A Protocol for Solving Problems
A Protocol for Solving Problems
A Protocol for Solving Problems
A Protocol for Solving Problems
A Protocol for Solving Problems
A Protocol for Solving Problems
Example: The Largest Integer Problem
Let’s solve the problem!
Example: The Largest Integer Problem
largest_integer(ints, start, end)
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
a | b | c | d | e | f | g | h |
Example: The Largest Integer Problem
largest_integer(ints, 0 , 7 )
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
a | b | c | d | e | f | g | h |
Example: The Largest Integer Problem
largest_integer(ints, 0 , 3 )
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
a | b | c | d | e | f | g | h |
Example: The Largest Integer Problem
largest_integer(ints, 0 , 1 )
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
a | b | c | d | e | f | g | h |
Example: The Largest Integer Problem
largest_integer(ints, 0 , 0 )
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
a | b | c | d | e | f | g | h |
Example: The Largest Integer Problem
largest_integer(ints, 0 , 0 )
a
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
a | b | c | d | e | f | g | h |
Example: The Largest Integer Problem
largest_integer(ints, 1 , 1 )
b
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
a | b | c | d | e | f | g | h |
Example: The Largest Integer Problem
largest_integer(ints, 0 , 1 )
i = max(a,b)
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
i | c | d | e | f | g | h | |
Example: The Largest Integer Problem
largest_integer(ints, 2 , 3 )
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
i | c | d | e | f | g | h | |
Example: The Largest Integer Problem
largest_integer(ints, 2 , 2 )
c
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
i | c | d | e | f | g | h | |
Example: The Largest Integer Problem
largest_integer(ints, 3 , 3 )
d
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
i | c | d | e | f | g | h | |
Example: The Largest Integer Problem
largest_integer(ints, 2 , 3 )
j = max(c,d)
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
i | j | e | f | g | h | ||
Example: The Largest Integer Problem
largest_integer(ints, 0 , 3 )
k = max(i,j)
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
k | e | f | g | h | |||
Example: The Largest Integer Problem
largest_integer(ints, 4 , 5 )
l = max(e,f)
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
k | l | g | h | ||||
Example: The Largest Integer Problem
largest_integer(ints, 6 , 7 )
m = max(g,h)
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
k | l | m | |||||
Example: The Largest Integer Problem
largest_integer(ints, 4 , 7 )
n = max(l,m)
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
k | n | ||||||
Example: The Largest Integer Problem
largest_integer(ints, 0 , 7 )
n = max(k,n)
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
n | |||||||
Example: The Largest Integer Problem
Algorithm largest_number(ints, start, end):
If start equals end:
Return ints[start]
Else:
mid ← floor((start + end) / 2)
left ← largest_number(ints, start, mid)
right ← largest_number(ints, mid+1, end)
Return max(left, right)