prof. erik demainecourses.csail.mit.edu/6.006/spring11/lectures/lec02.pdf · 2011-02-03 · lecture...
TRANSCRIPT
![Page 1: Prof. Erik Demainecourses.csail.mit.edu/6.006/spring11/lectures/lec02.pdf · 2011-02-03 · Lecture 2: Peak Finding Prof. Erik Demaine. Today • Peak finding (new problem) – 1D](https://reader030.vdocument.in/reader030/viewer/2022040215/5ed835c90fa3e705ec0e0a63/html5/thumbnails/1.jpg)
6.006IntroductiontoAlgorithms
Lecture2:PeakFindingProf.ErikDemaine
![Page 2: Prof. Erik Demainecourses.csail.mit.edu/6.006/spring11/lectures/lec02.pdf · 2011-02-03 · Lecture 2: Peak Finding Prof. Erik Demaine. Today • Peak finding (new problem) – 1D](https://reader030.vdocument.in/reader030/viewer/2022040215/5ed835c90fa3e705ec0e0a63/html5/thumbnails/2.jpg)
Today• Peakfinding(newproblem)
– 1Dalgorithms– 2Dalgorithms
• Divide&conquer(newtechnique)
![Page 3: Prof. Erik Demainecourses.csail.mit.edu/6.006/spring11/lectures/lec02.pdf · 2011-02-03 · Lecture 2: Peak Finding Prof. Erik Demaine. Today • Peak finding (new problem) – 1D](https://reader030.vdocument.in/reader030/viewer/2022040215/5ed835c90fa3e705ec0e0a63/html5/thumbnails/3.jpg)
FindingWater…IN SPACE
• YouareGeordi LaForge• Trappedonalienmountainrange• Needtofindapoolwherewateraccumulates
• Canteleport,butcan’tsee http://en.wikipedia.org/wiki/File:GeordiLaForge.jpg
photo by Erik Demaine Banff, Canada
![Page 4: Prof. Erik Demainecourses.csail.mit.edu/6.006/spring11/lectures/lec02.pdf · 2011-02-03 · Lecture 2: Peak Finding Prof. Erik Demaine. Today • Peak finding (new problem) – 1D](https://reader030.vdocument.in/reader030/viewer/2022040215/5ed835c90fa3e705ec0e0a63/html5/thumbnails/4.jpg)
FindingWater…IN SPACE
• Problem: Findalocalminimumormaximuminaterrainbysampling
photo by Erik Demaine Banff, Canada
![Page 5: Prof. Erik Demainecourses.csail.mit.edu/6.006/spring11/lectures/lec02.pdf · 2011-02-03 · Lecture 2: Peak Finding Prof. Erik Demaine. Today • Peak finding (new problem) – 1D](https://reader030.vdocument.in/reader030/viewer/2022040215/5ed835c90fa3e705ec0e0a63/html5/thumbnails/5.jpg)
1DPeakFinding• Givenanarray :
• isapeak ifitisnotsmallerthanitsneighbor(s):
whereweimagine
• Goal: Findany peak
1 2 6 5 3 7 40 1 2 3 4 5 6
:
![Page 6: Prof. Erik Demainecourses.csail.mit.edu/6.006/spring11/lectures/lec02.pdf · 2011-02-03 · Lecture 2: Peak Finding Prof. Erik Demaine. Today • Peak finding (new problem) – 1D](https://reader030.vdocument.in/reader030/viewer/2022040215/5ed835c90fa3e705ec0e0a63/html5/thumbnails/6.jpg)
“BruteForce”Algorithm• Testallelementsforpeakyness
for in :if :
return
1 2 6 5 3 7 40 1 2 3 4 5 6
:
![Page 7: Prof. Erik Demainecourses.csail.mit.edu/6.006/spring11/lectures/lec02.pdf · 2011-02-03 · Lecture 2: Peak Finding Prof. Erik Demaine. Today • Peak finding (new problem) – 1D](https://reader030.vdocument.in/reader030/viewer/2022040215/5ed835c90fa3e705ec0e0a63/html5/thumbnails/7.jpg)
Algorithm1½•
– Globalmaximumisa localmaximum
for in :if :
return
1 2 6 5 3 7 40 1 2 3 4 5 6
:
![Page 8: Prof. Erik Demainecourses.csail.mit.edu/6.006/spring11/lectures/lec02.pdf · 2011-02-03 · Lecture 2: Peak Finding Prof. Erik Demaine. Today • Peak finding (new problem) – 1D](https://reader030.vdocument.in/reader030/viewer/2022040215/5ed835c90fa3e705ec0e0a63/html5/thumbnails/8.jpg)
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
:
![Page 9: Prof. Erik Demainecourses.csail.mit.edu/6.006/spring11/lectures/lec02.pdf · 2011-02-03 · Lecture 2: Peak Finding Prof. Erik Demaine. Today • Peak finding (new problem) – 1D](https://reader030.vdocument.in/reader030/viewer/2022040215/5ed835c90fa3e705ec0e0a63/html5/thumbnails/9.jpg)
WheretoSample?• Wanttominimizetheworst‐caseremainingelementsinarray– Balance oflengthwith oflength
–– :middleelement– Reduce to
1 2 6 5 3 7 40 1 2 3 4 5 6
:
![Page 10: Prof. Erik Demainecourses.csail.mit.edu/6.006/spring11/lectures/lec02.pdf · 2011-02-03 · Lecture 2: Peak Finding Prof. Erik Demaine. Today • Peak finding (new problem) – 1D](https://reader030.vdocument.in/reader030/viewer/2022040215/5ed835c90fa3e705ec0e0a63/html5/thumbnails/10.jpg)
Algorithm
if :return
elif :return peak1d
elifreturn peak1d
1 2 6 5 3 7 40 1 2 3 4 5 6
:
![Page 11: Prof. Erik Demainecourses.csail.mit.edu/6.006/spring11/lectures/lec02.pdf · 2011-02-03 · Lecture 2: Peak Finding Prof. Erik Demaine. Today • Peak finding (new problem) – 1D](https://reader030.vdocument.in/reader030/viewer/2022040215/5ed835c90fa3e705ec0e0a63/html5/thumbnails/11.jpg)
Divide&Conquer• Generaldesigntechnique:1. Divideinputintopart(s)2. Conquereachpart
recursively3. Combineresult(s)tosolve
originalproblem
• 1Dpeak:1. Onehalf2. Recurse3. Return
![Page 12: Prof. Erik Demainecourses.csail.mit.edu/6.006/spring11/lectures/lec02.pdf · 2011-02-03 · Lecture 2: Peak Finding Prof. Erik Demaine. Today • Peak finding (new problem) – 1D](https://reader030.vdocument.in/reader030/viewer/2022040215/5ed835c90fa3e705ec0e0a63/html5/thumbnails/12.jpg)
Divide&ConquerAnalysis• Recurrence fortimetakenbyproblemsize
1. Divideinputintopart(s):
2. Conquereachpartrecursively
3. Combineresult(s)tosolveoriginalproblem
combinecost
dividecost
![Page 13: Prof. Erik Demainecourses.csail.mit.edu/6.006/spring11/lectures/lec02.pdf · 2011-02-03 · Lecture 2: Peak Finding Prof. Erik Demaine. Today • Peak finding (new problem) – 1D](https://reader030.vdocument.in/reader030/viewer/2022040215/5ed835c90fa3e705ec0e0a63/html5/thumbnails/13.jpg)
1DPeakFindingAnalysis• Divide probleminto1problemofsize
• Dividecost:• Combinecost:• Recurrence:
![Page 14: Prof. Erik Demainecourses.csail.mit.edu/6.006/spring11/lectures/lec02.pdf · 2011-02-03 · Lecture 2: Peak Finding Prof. Erik Demaine. Today • Peak finding (new problem) – 1D](https://reader030.vdocument.in/reader030/viewer/2022040215/5ed835c90fa3e705ec0e0a63/html5/thumbnails/14.jpg)
SolvingRecurrence
![Page 15: Prof. Erik Demainecourses.csail.mit.edu/6.006/spring11/lectures/lec02.pdf · 2011-02-03 · Lecture 2: Peak Finding Prof. Erik Demaine. Today • Peak finding (new problem) – 1D](https://reader030.vdocument.in/reader030/viewer/2022040215/5ed835c90fa3e705ec0e0a63/html5/thumbnails/15.jpg)
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
![Page 16: Prof. Erik Demainecourses.csail.mit.edu/6.006/spring11/lectures/lec02.pdf · 2011-02-03 · Lecture 2: Peak Finding Prof. Erik Demaine. Today • Peak finding (new problem) – 1D](https://reader030.vdocument.in/reader030/viewer/2022040215/5ed835c90fa3e705ec0e0a63/html5/thumbnails/16.jpg)
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
![Page 17: Prof. Erik Demainecourses.csail.mit.edu/6.006/spring11/lectures/lec02.pdf · 2011-02-03 · Lecture 2: Peak Finding Prof. Erik Demaine. Today • Peak finding (new problem) – 1D](https://reader030.vdocument.in/reader030/viewer/2022040215/5ed835c90fa3e705ec0e0a63/html5/thumbnails/17.jpg)
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
![Page 18: Prof. Erik Demainecourses.csail.mit.edu/6.006/spring11/lectures/lec02.pdf · 2011-02-03 · Lecture 2: Peak Finding Prof. Erik Demaine. Today • Peak finding (new problem) – 1D](https://reader030.vdocument.in/reader030/viewer/2022040215/5ed835c90fa3e705ec0e0a63/html5/thumbnails/18.jpg)
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
![Page 19: Prof. Erik Demainecourses.csail.mit.edu/6.006/spring11/lectures/lec02.pdf · 2011-02-03 · Lecture 2: Peak Finding Prof. Erik Demaine. Today • Peak finding (new problem) – 1D](https://reader030.vdocument.in/reader030/viewer/2022040215/5ed835c90fa3e705ec0e0a63/html5/thumbnails/19.jpg)
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
![Page 20: Prof. Erik Demainecourses.csail.mit.edu/6.006/spring11/lectures/lec02.pdf · 2011-02-03 · Lecture 2: Peak Finding Prof. Erik Demaine. Today • Peak finding (new problem) – 1D](https://reader030.vdocument.in/reader030/viewer/2022040215/5ed835c90fa3e705ec0e0a63/html5/thumbnails/20.jpg)
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
![Page 21: Prof. Erik Demainecourses.csail.mit.edu/6.006/spring11/lectures/lec02.pdf · 2011-02-03 · Lecture 2: Peak Finding Prof. Erik Demaine. Today • Peak finding (new problem) – 1D](https://reader030.vdocument.in/reader030/viewer/2022040215/5ed835c90fa3e705ec0e0a63/html5/thumbnails/21.jpg)
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
![Page 22: Prof. Erik Demainecourses.csail.mit.edu/6.006/spring11/lectures/lec02.pdf · 2011-02-03 · Lecture 2: Peak Finding Prof. Erik Demaine. Today • Peak finding (new problem) – 1D](https://reader030.vdocument.in/reader030/viewer/2022040215/5ed835c90fa3e705ec0e0a63/html5/thumbnails/22.jpg)
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
![Page 23: Prof. Erik Demainecourses.csail.mit.edu/6.006/spring11/lectures/lec02.pdf · 2011-02-03 · Lecture 2: Peak Finding Prof. Erik Demaine. Today • Peak finding (new problem) – 1D](https://reader030.vdocument.in/reader030/viewer/2022040215/5ed835c90fa3e705ec0e0a63/html5/thumbnails/23.jpg)
Divide&ConquerWrapup• Leadstosurprisinglyefficientalgorithms• Notterriblygeneral,butstillquiteuseful• We’lluseitagainin
– Module4(sorting)– Module8(geometry)
http://en.wikipedia.org/wiki/File:CaesarTusculum.jpg