Prof. Erik Demaine - courses.csail.mit.edu · 2011-05-05 · Today • Computational geometry •...

24
6.006 Introduction to Algorithms Lecture 24: Geometry Prof. Erik Demaine

Transcript of Prof. Erik Demaine - courses.csail.mit.edu · 2011-05-05 · Today • Computational geometry •...

Page 1: Prof. Erik Demaine - courses.csail.mit.edu · 2011-05-05 · Today • Computational geometry • Line‐segment intersection – Sweep‐line technique • Closest pair of points

6.006IntroductiontoAlgorithms

Lecture24:GeometryProf.ErikDemaine

Page 2: Prof. Erik Demaine - courses.csail.mit.edu · 2011-05-05 · Today • Computational geometry • Line‐segment intersection – Sweep‐line technique • Closest pair of points

Today• Computationalgeometry• Line‐segmentintersection

– Sweep‐linetechnique• Closestpairofpoints

– Divide&conquerstrikesback!

Page 3: Prof. Erik Demaine - courses.csail.mit.edu · 2011-05-05 · Today • Computational geometry • Line‐segment intersection – Sweep‐line technique • Closest pair of points

Motivation:CollisionDetectionphotobyfotios lindiakos

http://www.flickr.com/photos/fotios_lindiakos/342596118/

Page 4: Prof. Erik Demaine - courses.csail.mit.edu · 2011-05-05 · Today • Computational geometry • Line‐segment intersection – Sweep‐line technique • Closest pair of points

Motivation:CollisionDetectionphotobyfotios lindiakos

http://www.flickr.com/photos/fotios_lindiakos/342596118/

“GTA4Carmageddon!”bydot12321http://www.youtube.com/watch?v=4-620xx7yTo

Page 5: Prof. Erik Demaine - courses.csail.mit.edu · 2011-05-05 · Today • Computational geometry • Line‐segment intersection – Sweep‐line technique • Closest pair of points

Line‐SegmentIntersection• Input: linesegmentsin2D• Goal: Findthe intersections

Page 6: Prof. Erik Demaine - courses.csail.mit.edu · 2011-05-05 · Today • Computational geometry • Line‐segment intersection – Sweep‐line technique • Closest pair of points

ObviousAlgorithmforeverypair oflinesegments:checkforintersection

Page 7: Prof. Erik Demaine - courses.csail.mit.edu · 2011-05-05 · Today • Computational geometry • Line‐segment intersection – Sweep‐line technique • Closest pair of points

Sweep‐LineTechnique• Idea: Sweepaverticallinefromlefttoright• Maintainintersectionoflinewithinput

Page 8: Prof. Erik Demaine - courses.csail.mit.edu · 2011-05-05 · Today • Computational geometry • Line‐segment intersection – Sweep‐line technique • Closest pair of points

Sweep‐LineIntersections

Page 9: Prof. Erik Demaine - courses.csail.mit.edu · 2011-05-05 · Today • Computational geometry • Line‐segment intersection – Sweep‐line technique • Closest pair of points

Sweep‐LineEvents• Discretizecontinuousmotionofsweepline• Eventswhenintersectionchanges

– Segmentendpoints – Intersections

Page 10: Prof. Erik Demaine - courses.csail.mit.edu · 2011-05-05 · Today • Computational geometry • Line‐segment intersection – Sweep‐line technique • Closest pair of points

Sweep‐LineAlgorithmSketch

• Maintainsweep‐lineintersection• Maintainpriorityqueue of(possible)eventtimes ( coordinatesofsweepline)

• Untilqueueisempty:– Deleteminimumeventtime frompriorityqueue– Updatesweep‐lineintersectionfrom to– Updatepossibleeventtimesinpriorityqueue

“Discrete‐eventsimulation”

Page 11: Prof. Erik Demaine - courses.csail.mit.edu · 2011-05-05 · Today • Computational geometry • Line‐segment intersection – Sweep‐line technique • Closest pair of points

IntersectionDataStructure• Balancedbinarysearchtree(e.g.,AVLtree)tostoresorted ( ) orderofintersections

Page 12: Prof. Erik Demaine - courses.csail.mit.edu · 2011-05-05 · Today • Computational geometry • Line‐segment intersection – Sweep‐line technique • Closest pair of points

EndpointEvents• Foreachlinesegment :

– At :insert (binarysearchforneighborsthen)– At :delete

Page 13: Prof. Erik Demaine - courses.csail.mit.edu · 2011-05-05 · Today • Computational geometry • Line‐segment intersection – Sweep‐line technique • Closest pair of points

IntersectionEvents?• Howtoknowwhenhaveintersectionevents?• Thisisthewholeproblem!

Page 14: Prof. Erik Demaine - courses.csail.mit.edu · 2011-05-05 · Today • Computational geometry • Line‐segment intersection – Sweep‐line technique • Closest pair of points

IntersectionEvents• Computewhenneighboringsegmentswouldintersect,ifnothingchangesmeanwhile

• Ifsuchaneventisnext,thenitreallyhappens

Page 15: Prof. Erik Demaine - courses.csail.mit.edu · 2011-05-05 · Today • Computational geometry • Line‐segment intersection – Sweep‐line technique • Closest pair of points

while isnotempty:

if is :insert into (binarysearchingwith )

elif is :delete from

elif is :reportintersectionbetween andswapcontentsofnodesfor and in

foreachnode changedorneighboringchangedin :deleteall eventsinvolving fromadd & to

Sweep‐LineAlgorithm

Page 16: Prof. Erik Demaine - courses.csail.mit.edu · 2011-05-05 · Today • Computational geometry • Line‐segment intersection – Sweep‐line technique • Closest pair of points

Sweep‐LineAnalysis• Runningtime=• Numberofendpointevents=• Numberofmeetevents=number ofintersections=sizeofoutput

• Runningtime=• Outputsensitivealgorithm:runningtimedependsonsizeofoutput

• Bestalgorithmrunsin time

Page 17: Prof. Erik Demaine - courses.csail.mit.edu · 2011-05-05 · Today • Computational geometry • Line‐segment intersection – Sweep‐line technique • Closest pair of points

ClosestPairofPoints• Input: pointsin2D• Goal: findtwoclosestpoints

Page 18: Prof. Erik Demaine - courses.csail.mit.edu · 2011-05-05 · Today • Computational geometry • Line‐segment intersection – Sweep‐line technique • Closest pair of points

ObviousAlgorithmmin(distance

foreverypair ofpoints)

Page 19: Prof. Erik Demaine - courses.csail.mit.edu · 2011-05-05 · Today • Computational geometry • Line‐segment intersection – Sweep‐line technique • Closest pair of points

Divide‐and‐ConquerIdea• Initiallysortpointsby coordinate• Splitpointsintolefthalfandrighthalf• Recurseoneachhalf:findclosestpair• returnmin{closestpairineachhalf,

closestpairbetweentwohalves}

Page 20: Prof. Erik Demaine - courses.csail.mit.edu · 2011-05-05 · Today • Computational geometry • Line‐segment intersection – Sweep‐line technique • Closest pair of points

ClosestPairBetweenTwoHalves?

• Let =min{closestpairineachhalf}• Onlyinterestedinpairswithdistance• Restricttowindowofwidth aroundmiddle

Page 21: Prof. Erik Demaine - courses.csail.mit.edu · 2011-05-05 · Today • Computational geometry • Line‐segment intersection – Sweep‐line technique • Closest pair of points

ClosestPairBetweenTwoHalves

• Foreachleftpoint,interestedinpointsonrightwithindistance

• Pointsonrightsidecan’tbewithin ofeachother

• Soatmostthreerightpointstoconsiderforeachleftpoint– Dittoforeachrightpoint

• Cancomputein timebymergingtwosortedarrays

Page 22: Prof. Erik Demaine - courses.csail.mit.edu · 2011-05-05 · Today • Computational geometry • Line‐segment intersection – Sweep‐line technique • Closest pair of points

Divide‐and‐Conquerpresortpointsbydef :

sort bysort bymergeandfindclosestpairbetweentwolistsreturnmin{ ,closestdistancefrommerge}

Page 23: Prof. Erik Demaine - courses.csail.mit.edu · 2011-05-05 · Today • Computational geometry • Line‐segment intersection – Sweep‐line technique • Closest pair of points

FasterDivide‐and‐Conquer[Shamos &Hoey 1975]

presortpointsby and ,andcrosslinkpointsdef :

maptoypoints &findclosestpairbetweenlistsreturnmin{ ,closestdistancefrommerge}

Page 24: Prof. Erik Demaine - courses.csail.mit.edu · 2011-05-05 · Today • Computational geometry • Line‐segment intersection – Sweep‐line technique • Closest pair of points

OtherComputationalGeometryProblems

• Convexhull• Voronoi diagram• Triangulation• Pointlocation• Rangesearching• Motionplanning• …

6.850:GeometricComputing