Analysis of Single-Source Shortest Path Algorithms Tutorial

csci 3160 n.w
1 / 54
Embed
Share

"Explore graph concepts, breadth-first search, and Dijkstra's algorithm. Learn to find optimal paths, utilize adjacency lists, and understand the dry run process. Enhance your algorithm design skills with step-by-step explanations and visual aids."

  • Algorithms
  • Shortest Path
  • Graph Concepts
  • Dijkstra
  • Tutorial

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. CSCI 3160 Design and Analysis of Algorithms Tutorial 2 Chengyu Lin

  2. Outline Graph Concepts Single-source shortest path problem Breadth-first search for unweighted graphs Dijkstra s algorithm for non-negative weights

  3. Graph G = (V, E) Simple graph: unweighted, undirected graph, containing no loops and multiple edges o |E| = O(|V|2) 1 2 4 3 5 6 7

  4. Graph Adjacency list 1 3 4 5 / 2 6 / 3 1 / 4 1 5 / 1 2 5 1 4 7 / 4 3 6 2 / 5 7 5 / 6 o Space complexity: O(|V|+|E|) 7

  5. Single-source shortest path What is the optimalpath from one vertex to another? o Suppose each edge is of unit length o Store the minimum distances in an array dist[] o Example: if source vertex s= 3 1 2 u dist[u] 1 1 4 3 2 5 3 0 6 4 2 5 2 7 6 7 3

  6. Breadth-first search (BFS) Intuition: find the neighbors of the neighbors Additional structure: a queue Q Pseudocode: o Initialize: dist[s] = 0; dist[u]= for all other vertices u o Q = [s] o While Q is not empty Dequeue the top element u of Q For all neighbors v of u, if dist[v]= , o Put v in Q o Set dist[v] = dist[u] + 1

  7. Dry run 1: Initialize u dist[u] 1 Source s 2 3 0 1 2 4 5 4 3 6 5 7 6 7

  8. Dry run 2: Q = [s] Q 3 1 4 5 7 u dist[u] 1 2 3 0 1 2 4 5 4 3 6 5 7 6 7

  9. Dry run 3: Dequeue Q 3 1 4 5 7 u dist[u] 1 u = 3 2 3 0 1 2 4 5 4 3 6 5 7 6 7

  10. Dry run 4: Find neighbors Q 3 1 4 5 7 u dist[u] 1 u = 3 2 3 0 1 2 4 5 4 3 6 5 7 6 7

  11. Dry run 5: Update distance and enqueue Q 3 1 4 5 7 u dist[u] 1 1 u = 3 2 3 0 1 2 4 dist[3] + 1 = 0 + 1 = 1 5 4 3 6 5 7 6 7

  12. Dry run 6: Dequeue Q 3 1 4 5 7 u dist[u] 1 1 u = 1 2 3 0 1 2 4 5 4 3 6 5 7 6 7

  13. Dry run 7: Find neighbors Q 3 1 4 5 7 u dist[u] 1 1 u = 1 2 3 0 1 2 4 5 4 3 6 5 7 6 7

  14. Dry run 8: Update distance and enqueue Q 3 1 4 5 7 u dist[u] 1 1 u = 1 2 3 0 1 2 4 2 dist[1] + 1 = 1 + 1 = 2 5 2 4 3 6 5 7 6 7

  15. Dry run 9: Dequeue Q 3 1 4 5 7 u dist[u] 1 1 u = 4 2 3 0 1 2 4 2 5 2 4 3 6 5 7 6 7

  16. Dry run 10: Find neighbors Q 3 1 4 5 7 u dist[u] 1 1 u = 4 2 3 0 1 2 4 2 5 2 4 3 6 5 7 6 7

  17. Dry run 11: Q 3 1 4 5 7 u dist[u] 1 1 u = 4 2 3 0 1 2 4 2 5 2 4 3 6 5 7 6 7

  18. Dry run 12: Dequeue Q 3 1 4 5 7 u dist[u] 1 1 u = 5 2 3 0 1 2 4 2 5 2 4 3 6 5 7 6 7

  19. Dry run 13: Find neighbors Q 3 1 4 5 7 u dist[u] 1 1 u = 5 2 3 0 1 2 4 2 5 2 4 3 6 5 7 6 7

  20. Dry run 14: Q 3 1 4 5 7 u dist[u] 1 1 u = 5 2 3 0 1 2 4 2 dist[5] + 1 = 2 + 1 = 3 5 2 4 3 6 5 7 3 6 7

  21. Dry run 15: Dequeue Q 3 1 4 5 7 u dist[u] 1 1 u = 7 2 3 0 1 2 4 2 5 2 4 3 6 5 7 3 6 7

  22. Dry run 16: Find neighbors Q 3 1 4 5 7 u dist[u] 1 1 u = 7 2 3 0 1 2 4 2 5 2 4 3 6 5 7 3 6 7

  23. Dry run 17: Q 3 1 4 5 7 u dist[u] 1 1 u = 7 2 3 0 1 2 4 2 5 2 4 3 6 5 7 3 6 7

  24. Dry run 18: Q is now empty! Q 3 1 4 5 7 u dist[u] 1 1 2 3 0 1 2 4 2 5 2 4 3 6 5 7 3 6 7

  25. Dry run 19: Final result Q 3 1 4 5 7 u dist[u] 1 1 2 3 0 1 2 4 2 5 2 4 3 6 5 7 3 6 7

  26. Analysis Correctness: by induction o Vertices are processed in ascending order of distance from s o Subpaths of shortest paths are shortest paths Size of Q = O(|V|) o Each vertex is enqueued at most once Time complexity = O(|V|+|E|) o Initialization: O(|V|) operations o Each edge is considered O(1) times o Enqueue/dequeue: O(1) operations

  27. Weighted graphs Suppose now that each edge has its non- negative length l(u, v) o Need something more than BFS 1 3 4 5 / u dist[u] (3, 3) (4, 4) (5, 2) 1 3 1 2 2 4 3 3 0 2 2 4 4 6 3 1 5 5 5 6 3 6 7 8 7

  28. Dijkstras Algorithm Intuition: Identify those vertices whose distances are tight Additional structure: a priority queue Q Pseudocode: o Initialize: dist[s] = 0, and dist[u] = for all other u o Let Qcontain all vertices o while Q is not empty find a vertex u in Q with dist[u] being the minimum delete u from Q for each neighbor v o if dist[v] > dist[u] + l(u,v), set dist[v] = dist[u] + l(u,v)

  29. Dry run 1: Initialize Source s u dist[u] 1 1 2 2 4 3 3 0 2 2 4 4 3 1 5 5 6 3 6 7 7

  30. Dry run 2: Let Q contain all vertices o Let us not care about what a priority queue is for the time being. 1 2 3 4 5 6 7 Q u dist[u] 1 1 2 2 4 3 3 0 2 2 4 4 3 1 5 5 6 3 6 7 7

  31. Dry run 3: Find minimum Q 1 2 3 4 5 6 7 u dist[u] u = 3 1 1 2 2 4 3 3 0 2 2 4 4 3 1 5 5 6 3 6 7 7

  32. Dry run 4: Delete u from Q Q 1 2 3 4 5 6 7 u dist[u] u = 3 1 1 2 2 4 3 3 0 2 2 4 4 3 1 5 5 6 3 6 7 7

  33. Dry run 5: Relaxation Q 1 2 3 4 5 6 7 u dist[u] u = 3 1 3 1 2 2 dist[3] + l(3, 1) = 0 + 3 = 3 4 3 3 0 2 2 4 4 3 1 5 5 6 3 6 7 7

  34. Dry run 6: Find minimum Q 1 2 3 4 5 6 7 u dist[u] u = 1 1 3 1 2 2 4 3 3 0 2 2 4 4 3 1 5 5 6 3 6 7 7

  35. Dry run 7: Delete u from Q Q 1 2 3 4 5 6 7 u dist[u] u = 1 1 3 1 2 2 4 3 3 0 2 2 4 4 3 1 5 5 6 3 6 7 7

  36. Dry run 8: Relaxation Q 1 2 3 4 5 6 7 u dist[u] u = 1 1 3 1 2 2 dist[1] + l(1, 4) = 3 + 4 = 7 4 3 3 0 2 dist[1] + l(1, 5) = 3 + 2 = 5 2 4 4 7 3 1 5 5 5 6 3 6 7 7

  37. Dry run 9: Find minimum Q 1 2 3 4 5 6 7 u dist[u] u = 5 1 3 1 2 2 4 3 3 0 2 2 4 4 7 3 1 5 5 5 6 3 6 7 7

  38. Dry run 10: Delete u from Q Q 1 2 3 4 5 6 7 u dist[u] u = 5 1 3 1 2 2 4 3 3 0 2 2 4 4 7 3 1 5 5 5 6 3 6 7 7

  39. Dry run 11: Relaxation Q 1 2 3 4 5 6 7 u dist[u] u = 5 1 3 1 2 2 dist[5] + l(5, 4) = 5 + 1 = 6 4 3 3 0 2 dist[5] + l(5, 7) = 5 + 3 = 8 2 4 4 6 3 1 5 5 5 6 3 6 7 8 7

  40. Dry run 12: Find minimum Q 1 2 3 4 5 6 7 u dist[u] u = 4 1 3 1 2 2 4 3 3 0 2 2 4 4 6 3 1 5 5 5 6 3 6 7 8 7

  41. Dry run 13: Q 1 2 3 4 5 6 7 u dist[u] u = 4 1 3 1 2 2 4 3 3 0 2 2 4 4 6 3 1 5 5 5 6 3 6 7 8 7

  42. Dry run 14: Q 1 2 3 4 5 6 7 u dist[u] u = 7 1 3 1 2 2 4 3 3 0 2 2 4 4 6 3 1 5 5 5 6 3 6 7 8 7

  43. Dry run 15: Q 1 2 3 4 5 6 7 u dist[u] u = 2 1 3 1 2 2 dist[2] + l(2, 6) = + 2 = 4 3 3 0 2 2 4 4 6 3 1 5 5 5 6 3 6 7 8 7

  44. Dry run 16: Q 1 2 3 4 5 6 7 u dist[u] u = 6 1 3 1 2 2 4 3 3 0 2 2 4 4 6 3 1 5 5 5 6 3 6 7 8 7

  45. Dry run 17: Q is now empty! Q 1 2 3 4 5 6 7 u dist[u] 1 3 1 2 2 4 3 3 0 2 2 4 4 6 3 1 5 5 5 6 3 6 7 8 7

  46. Dry run 18: Final result Q 1 2 3 4 5 6 7 u dist[u] 1 3 1 2 2 4 3 3 0 2 2 4 4 6 3 1 5 5 5 6 3 6 7 8 7

  47. Analysis Correctness: same as before Size of Q = O(|V|) A naive implementation has time complexity O(|V|2) o A vertex is removed from Q in each iteration of the while loop o Finding a minimum: O(|V|) operations o Deletion / relaxation: O(1) operations We can achieve O(|V|log|V|+|E|) with a binary heap o Finding a minimum: O(1) operations o Deletion: O(log|V|) operations, to maintain the heap property that parent s value children s values

  48. Priority queue It could be implemented using a heap. (3, 0) (1, ) (2, ) (6, ) (4, ) (5, ) (7, ) o parent s value children s values

  49. Priority queue Delete (3, 0) (3, 0) (1, ) (2, ) (6, ) (4, ) (5, ) (7, )

  50. Priority queue Delete (3, 0) (7, ) (1, ) (2, ) (6, ) (4, ) (5, ) o Heap property NOT violated; OK!

Related


More Related Content