algorithms

44
Algorithms Analysis and Design

Upload: cachez

Post on 16-Feb-2016

100 views

Category:

Documents


0 download

DESCRIPTION

Algorithms. Analysis and Design. The Problem. The trip with the fewest stops. The Problem. Given arbitrary collection of possible stops Get from stop A to B with fewest possible intermediate stops Limit to distance between stops Practical applications Fuel station locations Mass transit. - PowerPoint PPT Presentation

TRANSCRIPT

Algorithms

AlgorithmsAnalysis and DesignThe ProblemThe trip with the fewest stopsThe ProblemGiven arbitrary collection of possible stopsGet from stop A to B with fewest possible intermediate stopsLimit to distance between stopsPractical applicationsFuel station locationsMass transitExampleBreadth First SearchBreadth First SearchBreadth First SearchBreadth First SearchBreadth First SearchBreadth First SearchBreadth First SearchAnalysisEvery stop compared to every other stopCould have to check every stop to find destinationMathematically:Given a set nodes of size n,number of comparisons will beon the n2 order of magnitudeO(n2) in notationEg. 100 stops could take ~10000 comparisons!OptimizationsSortingAnalysisWe check every point, every timeHave not been to that point beforeNot sure if in range of it yet as a resultCan we avoid this?A point more than two jumps awaycant be within range (one jump) on the next tryStoring distance for later still requires checking itSortingMust be able to avoid some checksSort based on distance from originOnly compare to points within one jumpof the current pointAs soon as one is outside this range,all the rest must beOne sort effective for all destinationsfrom a given originSortingComputer does not store picture!Stores an array of descriptor objectsContaining X and Y coordinateContaining link back to parent, if knownX: 15; Y: 42; P: ?X: 32; Y: 12; P: #1X: -8; Y: 6; P: ?X: 0; Y: -10; P: ?SortingHow long will sort take?n elements to sortMust find insertion point for eachO(log[n]) operationCut out half with each narrowing down

O(n log[n]) totalO(log[n]) vs. entire previous solutionEg. What took it 10000 could take us ~100SortingNow we store distance from origin as wellElements sorted based on thisDistance: 0;X: 15; Y: 42; P: ?Distance : 2;X: 32; Y: 12; P: #1Distance : 3.4;X: -8; Y: 6; P: ?Distance : 5.86;X: 0; Y: -10; P: ?UniverseSearch AreaSearch AreaSearch AreaSearch AreaSearch AreaSearch AreaAnalysisAnalysisAnalysisOptimizationsReduce by oneAnalysisStill double-checking many elementsCan we avoid these entirely?ArraysArrays are collections of objectsCould take finished objects outWould leave a holeChecking for holes takes just as longCould shift contents to fill holeWould have to read and manipulate many objectsLinked ListsArrays find next object byphysically grouping after currentLinked lists find next object by havingcurrent object conceptually point to itPointer uses memory, but is changed in O(1)132456123453ReviewHow things work nowReviewReviewReviewReviewReviewReviewResultPerformanceCompetitiveness of new solutionPerformanceEach node looked at far fewer timesHow many depends largely on data setWorst case scenario, nothing is eliminatedMeaning nothing was reachable(we were doomed from the start)Best case, all eliminated first try, for O(n) searchDense radial systems will be closer to bestUniverseConclusionO(n) search + O(n log[n]) sort = O(n log[n])log[n] the time of traditional solutionThe bigger the data setthe more drastic the improvementRequires less memory than many alternativesReduces memory where possible