1 / 44

Algorithms

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.

cachez
Download Presentation

Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Algorithms Analysis and Design

  2. The Problem The trip with the fewest stops

  3. 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

  4. Example

  5. Breadth First Search

  6. Breadth First Search

  7. Breadth First Search

  8. Breadth First Search

  9. Breadth First Search

  10. Breadth First Search

  11. Breadth First Search

  12. 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!

  13. Optimizations Sorting

  14. 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

  15. 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

  16. 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: ?

  17. 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

  18. 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: ?

  19. Universe

  20. Search Area

  21. Search Area

  22. Search Area

  23. Search Area

  24. Search Area

  25. Search Area

  26. Analysis

  27. Analysis

  28. Analysis

  29. Optimizations Reduce by one

  30. Analysis • Still double-checking many elements • Can we avoid these entirely?

  31. 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

  32. 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

  33. Review How things work now

  34. Review

  35. Review

  36. Review

  37. Review

  38. Review

  39. Review

  40. Result

  41. Performance Competitiveness of new solution

  42. 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

  43. Universe

  44. 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

More Related