440 likes | 718 Views
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.
E N D
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
Analysis • Every stop compared to every other stop • Could have to check every stop to find destination • Mathematically: • Given a set nodes of size n,number of comparisons will beon the n2 order of magnitude • O(n2) in notation • Eg. 100 stops could take ~10000 comparisons!
Optimizations Sorting
Analysis • We check every point, every time • Have not been to that point before • Not sure if in range of it yet as a result • Can we avoid this? • A point more than two jumps awaycan’t be within range (one jump) on the next try • Storing distance for later still requires checking it
Sorting • Must be able to avoid some checks • Sort based on distance from origin • Only compare to points within one jumpof the current point • As soon as one is outside this range,all the rest must be • One sort effective for all destinationsfrom a given origin
Sorting • Computer does not store picture! • Stores an array of descriptor objects • Containing X and Y coordinate • Containing link back to parent, if known X: 32; Y: 12; P: #1 X: -8; Y: 6; P: ? X: 0; Y: -10; P: ? X: 15; Y: 42; P: ?
Sorting • How long will sort take? • n elements to sort • Must find insertion point for each • O(log[n]) operation • Cut out half with each narrowing down • O(n log[n]) total • O(log[n]) vs. entire previous solution • Eg. What took it 10000 could take us ~100
Sorting • Now we store distance from origin as well • Elements sorted based on this Distance : 2;X: 32; Y: 12; P: #1 Distance : 3.4;X: -8; Y: 6; P: ? Distance : 5.86;X: 0; Y: -10; P: ? Distance: 0;X: 15; Y: 42; P: ?
Optimizations Reduce by one
Analysis • Still double-checking many elements • Can we avoid these entirely?
Arrays • Arrays are collections of objects • Could take finished objects out • Would leave a hole • Checking for holes takes just as long • Could shift contents to fill hole • Would have to read and manipulate many objects
Linked Lists • Arrays find next object byphysically grouping after current • Linked lists find next object by havingcurrent object conceptually point to it • Pointer uses memory, but is changed in O(1) 1 3 3 4 5 6 1 3 4 5 2 2
Review How things work now
Performance Competitiveness of new solution
Performance • Each node looked at far fewer times • How many depends largely on data set • Worst case scenario, nothing is eliminated • Meaning nothing was reachable(we were doomed from the start) • Best case, all eliminated first try, for O(n) search • Dense radial systems will be closer to best
Conclusion • O(n) search + O(n log[n]) sort = O(n log[n]) • log[n] the time of traditional solution • The bigger the data setthe more drastic the improvement • Requires less memory than many alternatives • Reduces memory where possible