Vous êtes sur la page 1sur 23

6.

006
Introduction to Algorithms

Lecture 2: Peak Finding


Prof. Erik Demaine
Today
• Peak finding (new problem)
– 1D algorithms
– 2D algorithms
• Divide & conquer (new technique)
Finding Water…
IN SPACE
• You are Geordi LaForge
• Trapped on alien mountain range
• Need to find a pool where water
accumulates
• Can teleport, but can’t see http://en.wikipedia.org/wiki/File:GeordiLaForge.jpg

photo by Erik Demaine Banff, Canada


Finding Water…
IN SPACE
• Problem: Find a local minimum or maximum
in a terrain by sampling

photo by Erik Demaine Banff, Canada


1D Peak Finding
• Given an array :
: 1 2 6 5 3 7 4
0 1 2 3 4 5 6
• is a peak if it is not smaller than its
neighbor(s):

where we imagine

• Goal: Find any peak


“Brute Force” Algorithm
• Test all elements for peakyness

for in :
if :
return

: 1 2 6 5 3 7 4
0 1 2 3 4 5 6
Algorithm 1½

– Global maximum is a local maximum

for in :
if :

return

: 1 2 6 5 3 7 4
0 1 2 3 4 5 6
Cleverer Idea
• Look at any element and
its neighbors &
– If peak: return
– Otherwise: locally rising on some side
• Must be a peak in that direction
• So can throw away rest of array,
leaving : or 1:

: 1 2 6 5 3 7 4
0 1 2 3 4 5 6
Where to Sample?
• Want to minimize the worst‐case remaining
elements in array
– Balance of length
with of length

– : middle element
– Reduce to

: 1 2 6 5 3 7 4
0 1 2 3 4 5 6
Algorithm

if :
return
elif :
return peak1d
elif
return peak1d
: 1 2 6 5 3 7 4
0 1 2 3 4 5 6
Divide & Conquer
• General design technique: • 1D peak:
1. Divide input into part(s) 1. One half
2. Conquer each part 2. Recurse
recursively 3. Return
3. Combine result(s) to solve
original problem
Divide & Conquer Analysis
• Recurrence for time
taken by problem size
1. Divide input into part(s):
divide cost
2. Conquer each part
recursively

3. Combine result(s) combine cost


to solve original problem
1D Peak Finding Analysis
• Divide problem into 1 problem of size
• Divide cost:
• Combine cost:
• Recurrence:
Solving Recurrence
2D Peak Finding
• Given matrix 9 3 5 2 4 9 8
of numbers 7 2 5 1 4 0 3
• Want an entry not 9 8 9 3 2 4 8
smaller than its (up to) 7 6 3 1 3 2 3
4 neighbors: 9 0 6 0 4 6 4
8 9 8 0 5 3 0
2 1 2 1 1 1 1
Divide & Conquer #0
• Looking at center 9 3 5 2 4 9 8
element doesn’t split 7 2 5 1 4 0 3
the problem into 9 8 9 3 2 4 8
pieces…
7 6 3 1 3 2 3
9 0 6 0 4 6 4
8 9 8 0 5 3 0
2 1 2 1 1 1 1
Divide & Conquer #½
• Consider max element 9 3 5 2 4 9 8
in each column 7 2 5 1 4 0 3
• 1D algorithm would 9 8 9 3 2 4 8
solve max array in 7 6 3 1 3 2 3
time 9 0 6 0 4 6 4
• But time to 8 9 8 0 5 3 0
compute max array 2 1 2 1 1 1 1

9 9 9 3 5 9 8
Divide & Conquer #1
• Look at center column 9 3 5 2 4 9 8
• Find global max within 7 2 5 1 4 0 3
• If peak: return it 9 8 9 3 2 4 8
• Else: 7 6 3 1 3 2 3
– Larger left/right neighbor 9 0 6 0 4 6 4
– Larger max in that column
8 9 8 0 5 3 0
– Recurse in left/right half
2 1 2 1 1 1 1
• Base case: 1 column
– Return global max within 9 9 9 3 5 9 8
Analysis #1
• time to find 9 3 5 2 4 9 8
max in column 7 2 5 1 4 0 3
• iterations 9 8 9 3 2 4 8
(like binary search) 7 6 3 1 3 2 3
• time total 9 0 6 0 4 6 4
8 9 8 0 5 3 0
2 1 2 1 1 1 1
• Can we do better?
Divide & Conquer #2
• Look at boundary, 0 0 0 0 0 0 0 0 0
center row, and center 0 9 3 5 2 4 9 8 0
column (window) 0 7 2 5 1 4 0 3 0
0 9 8 9 3 2 4 8 0
• Find global max within 0 7 6 3 1 3 2 3 0
• If it’s a peak: return it 0 9 0 6 0 4 6 4 0
• Else: 0 8 9 8 0 5 3 0 0
– Find larger neighbor 0 2 1 2 1 1 1 1 0
– Can’t be in window 0 0 0 0 0 0 0 0 0
– Recurse in quadrant,
including green boundary
Correctness
• Lemma: If you enter a 0 0 0 0 0 0 0 0 0
quadrant, it contains a 0 9 3 5 2 4 9 8 0
peak of the overall 0 7 2 5 1 4 0 3 0
array [climb up] 0 9 8 9 3 2 4 8 0
0 7 6 3 1 3 2 3 0
• Invariant: Maximum 0
0 9 0 6 0 4 6 4
element of window
0 8 9 8 0 5 3 0 0
never decreases as we
0 2 1 2 1 1 1 1 0
descend in recursion
0 0 0 0 0 0 0 0 0
• Theorem: Peak in
visited quadrant is also
peak in overall array
Analysis #2
0 0 0 0 0 0 0 0 0
• Reduce matrix to 0 9 3 5 2 4 9 8 0
submatrix in 0 7 2 5 1 4 0 3 0
0 9 8 9 3 2 4 8 0
time (|window|)
0 7 6 3 1 3 2 3 0
0 9 0 6 0 4 6 4 0
0 8 9 8 0 5 3 0 0
0 2 1 2 1 1 1 1 0
0 0 0 0 0 0 0 0 0
Divide & Conquer Wrapup
• Leads to surprisingly efficient algorithms
• Not terribly general, but still quite useful
• We’ll use it again in
– Module 4 (sorting)
– Module 8 (geometry)

http://en.wikipedia.org/wiki/File:CaesarTusculum.jpg

Vous aimerez peut-être aussi