Prof. Erik Demainecourses.csail.mit.edu/6.006/spring11/lectures/lec02.pdf · 2011-02-03 · Lecture...

23
6.006 Introduction to Algorithms Lecture 2: Peak Finding Prof. Erik Demaine

Transcript of Prof. Erik Demainecourses.csail.mit.edu/6.006/spring11/lectures/lec02.pdf · 2011-02-03 · Lecture...

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

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

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

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

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

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

“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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Divide&ConquerWrapup• Leadstosurprisinglyefficientalgorithms• Notterriblygeneral,butstillquiteuseful• We’lluseitagainin

– Module4(sorting)– Module8(geometry)

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