Prof. Erik Demainecourses.csail.mit.edu/6.006/spring11/lectures/lec02.pdf · 2011-02-03 · Lecture...
Transcript of Prof. Erik Demainecourses.csail.mit.edu/6.006/spring11/lectures/lec02.pdf · 2011-02-03 · Lecture...
6.006IntroductiontoAlgorithms
Lecture2:PeakFindingProf.ErikDemaine
Today• Peakfinding(newproblem)
– 1Dalgorithms– 2Dalgorithms
• Divide&conquer(newtechnique)
FindingWater…IN SPACE
• YouareGeordi LaForge• Trappedonalienmountainrange• Needtofindapoolwherewateraccumulates
• Canteleport,butcan’tsee http://en.wikipedia.org/wiki/File:GeordiLaForge.jpg
photo by Erik Demaine Banff, Canada
FindingWater…IN SPACE
• Problem: Findalocalminimumormaximuminaterrainbysampling
photo by Erik Demaine Banff, Canada
1DPeakFinding• Givenanarray :
• isapeak ifitisnotsmallerthanitsneighbor(s):
whereweimagine
• Goal: Findany peak
1 2 6 5 3 7 40 1 2 3 4 5 6
:
“BruteForce”Algorithm• Testallelementsforpeakyness
for in :if :
return
1 2 6 5 3 7 40 1 2 3 4 5 6
:
Algorithm1½•
– Globalmaximumisa localmaximum
for in :if :
return
1 2 6 5 3 7 40 1 2 3 4 5 6
:
ClevererIdea• Lookatanyelement anditsneighbors &– Ifpeak:return– Otherwise:locallyrisingonsomeside
• Mustbeapeakinthatdirection• Socanthrowawayrestofarray,leaving : or 1:
1 2 6 5 3 7 40 1 2 3 4 5 6
:
WheretoSample?• Wanttominimizetheworst‐caseremainingelementsinarray– Balance oflengthwith oflength
–– :middleelement– Reduce to
1 2 6 5 3 7 40 1 2 3 4 5 6
:
Algorithm
if :return
elif :return peak1d
elifreturn peak1d
1 2 6 5 3 7 40 1 2 3 4 5 6
:
Divide&Conquer• Generaldesigntechnique:1. Divideinputintopart(s)2. Conquereachpart
recursively3. Combineresult(s)tosolve
originalproblem
• 1Dpeak:1. Onehalf2. Recurse3. Return
Divide&ConquerAnalysis• Recurrence fortimetakenbyproblemsize
1. Divideinputintopart(s):
2. Conquereachpartrecursively
3. Combineresult(s)tosolveoriginalproblem
combinecost
dividecost
1DPeakFindingAnalysis• Divide probleminto1problemofsize
• Dividecost:• Combinecost:• Recurrence:
SolvingRecurrence
2DPeakFinding• Given matrixofnumbers
• Wantanentrynotsmallerthanits(upto)4neighbors:
2 1 2 1 1 1 18 9 8 0 5 3 09 0 6 0 4 6 47 6 3 1 3 2 39 8 9 3 2 4 87 2 5 1 4 0 39 3 5 2 4 9 8
Divide&Conquer#0• Lookingatcenterelementdoesn’tsplittheproblemintopieces…
2 1 2 1 1 1 18 9 8 0 5 3 09 0 6 0 4 6 47 6 3 1 3 2 39 8 9 3 2 4 87 2 5 1 4 0 39 3 5 2 4 9 8
Divide&Conquer#½• Considermaxelementineachcolumn
• 1Dalgorithmwouldsolvemaxarrayin
time• But timetocomputemaxarray 2 1 2 1 1 1 1
8 9 8 0 5 3 09 0 6 0 4 6 47 6 3 1 3 2 39 8 9 3 2 4 87 2 5 1 4 0 39 3 5 2 4 9 8
9 9 9 3 5 9 8
Divide&Conquer#1• Lookatcentercolumn• Findglobalmaxwithin• Ifpeak:returnit• Else:
– Largerleft/rightneighbor– Largermaxinthatcolumn– Recurseinleft/righthalf
• Basecase: 1column– Returnglobalmaxwithin
2 1 2 1 1 1 18 9 8 0 5 3 09 0 6 0 4 6 47 6 3 1 3 2 39 8 9 3 2 4 87 2 5 1 4 0 39 3 5 2 4 9 8
9 9 9 3 5 9 8
Analysis#1• timetofindmaxincolumn
• iterations(likebinarysearch)
• timetotal
• Canwedobetter?2 1 2 1 1 1 18 9 8 0 5 3 09 0 6 0 4 6 47 6 3 1 3 2 39 8 9 3 2 4 87 2 5 1 4 0 39 3 5 2 4 9 8
Divide&Conquer#2• Lookatboundary,centerrow,andcentercolumn(window)
• Findglobalmaxwithin• Ifit’sapeak:returnit• Else:
– Findlargerneighbor– Can’tbeinwindow– Recurseinquadrant,includinggreenboundary
22 11 22 11 11 11 1188 99 88 00 55 33 0099 00 66 00 44 66 4477 66 33 11 33 22 3399 88 99 33 22 44 8877 22 55 11 44 00 3399 33 55 22 44 99 88
00 00 00 00 00 00 00
000000000000000000
00 00 00 00 00 00 00 000000000000000000
Correctness• Lemma: Ifyouenteraquadrant,itcontainsapeakoftheoverallarray[climbup]
• Invariant: Maximumelementofwindowneverdecreasesaswedescendinrecursion
• Theorem: Peakinvisitedquadrantisalsopeakinoverallarray
22 11 22 11 11 11 1188 99 88 00 55 33 0099 00 66 00 44 66 4477 66 33 11 33 22 3399 88 99 33 22 44 8877 22 55 11 44 00 3399 33 55 22 44 99 88
00 00 00 00 00 00 00
000000000000000000
00 00 00 00 00 00 00 000000000000000000
Analysis#2• Reduce matrixto
submatrixintime(|window|)
22 11 22 11 11 11 1188 99 88 00 55 33 0099 00 66 00 44 66 4477 66 33 11 33 22 3399 88 99 33 22 44 8877 22 55 11 44 00 3399 33 55 22 44 99 88
00 00 00 00 00 00 00
000000000000000000
00 00 00 00 00 00 00 000000000000000000
Divide&ConquerWrapup• Leadstosurprisinglyefficientalgorithms• Notterriblygeneral,butstillquiteuseful• We’lluseitagainin
– Module4(sorting)– Module8(geometry)
http://en.wikipedia.org/wiki/File:CaesarTusculum.jpg