1 / 54

CSCI 3160 Design and Analysis of Algorithms Tutorial 2

CSCI 3160 Design and Analysis of Algorithms Tutorial 2. Yang LIU. Outline. Graph Concepts Single-source shortest path problem Breadth-first search – for unweighted graphs Dijkstra’s algorithm – for non-negative weights. Graph. G = (V, E)

virgo
Download Presentation

CSCI 3160 Design and Analysis of Algorithms Tutorial 2

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. CSCI 3160 Design and Analysis of AlgorithmsTutorial 2 Yang LIU

  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 • |E| = O(|V|2) 1 2 4 3 5 6 7

  4. Graph • Adjacency list • Space complexity: O(|V|+|E|) 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 7

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

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

  7. Dry run • 1: Initialize Source s 1 2 4 3 5 6 7

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

  9. Dry run • 3: Dequeue Q 3 1 4 5 7 u = 3 1 2 4 3 5 6 7

  10. Dry run • 4: Find neighbors Q 3 1 4 5 7 u = 3 1 2 4 3 5 6 7

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

  12. Dry run • 6: Dequeue Q 3 1 4 5 7 u = 1 1 2 4 3 5 6 7

  13. Dry run • 7: Find neighbors Q 3 1 4 5 7 u = 1 1 2 4 3 5 6 7

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

  15. Dry run • 9: Dequeue Q 3 1 4 5 7 u = 4 1 2 4 3 5 6 7

  16. Dry run • 10: Find neighbors Q 3 1 4 5 7 u = 4 1 2 4 3 5 6 7

  17. Dry run • 11: Q 3 1 4 5 7 u = 4 1 2 4 3 5 6 7

  18. Dry run • 12: Dequeue Q 3 1 45 7 u = 5 1 2 4 3 5 6 7

  19. Dry run • 13: Find neighbors Q 3 1 45 7 u = 5 1 2 4 3 5 6 7

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

  21. Dry run • 15: Dequeue Q 3 1 45 7 u = 7 1 2 4 3 5 6 7

  22. Dry run • 16: Find neighbors Q 3 1 45 7 u = 7 1 2 4 3 5 6 7

  23. Dry run • 17: Q 3 1 45 7 u = 7 1 2 4 3 5 6 7

  24. Dry run • 18: Q is now empty! Q 3 1 45 7 1 2 4 3 5 6 7

  25. Dry run • 19: Final result Q 3 1 45 7 1 2 4 3 5 6 7

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

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

  28. Dijkstra’s Algorithm • Intuition: find the nearest neighbors of the nearest neighbors • Additional structure: a priority queue Q • Pseudocode: • Initialize: • dist[s] = 0, and dist[u] = ∞ for all other u • Let Qcontain all vertices • 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 • if dist[v] > dist[u] + l(u,v), set dist[v] = dist[u] + l(u,v)

  29. Dry run • 1: Initialize Source s 1 2 4 3 2 2 4 3 1 5 6 3 7

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

  31. Dry run • 3: Find minimum Q 1 2 3 4 5 6 7 u = 3 1 2 4 3 2 2 4 3 1 5 6 3 7

  32. Dry run • 4: Delete u from Q Q 1 2 3 4 5 6 7 u = 3 1 2 4 3 2 2 4 3 1 5 6 3 7

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

  34. Dry run • 6: Find minimum Q 1 2 3 4 5 6 7 u = 1 1 2 4 3 2 2 4 3 1 5 6 3 7

  35. Dry run • 7: Delete u from Q Q 1 2 3 4 5 6 7 u = 1 1 2 4 3 2 2 4 3 1 5 6 3 7

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

  37. Dry run • 9: Find minimum Q 1 2 3 4 5 6 7 u = 5 1 2 4 3 2 2 4 3 1 5 6 3 7

  38. Dry run • 10: Delete u from Q Q 1 2 3 4 5 6 7 u = 5 1 2 4 3 2 2 4 3 1 5 6 3 7

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

  40. Dry run • 12: Find minimum Q 1 2 34 5 6 7 u = 4 1 2 4 3 2 2 4 3 1 5 6 3 7

  41. Dry run • 13: Q 1 2 345 6 7 u = 4 1 2 4 3 2 2 4 3 1 5 6 3 7

  42. Dry run • 14: Q 1 2 345 6 7 u = 7 1 2 4 3 2 2 4 3 1 5 6 3 7

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

  44. Dry run • 16: Q 1234567 u = 6 1 2 4 3 2 2 4 3 1 5 6 3 7

  45. Dry run • 17: Q is now empty! Q 1234567 1 2 4 3 2 2 4 3 1 5 6 3 7

  46. Dry run • 18: Final result Q 1234567 1 2 4 3 2 2 4 3 1 5 6 3 7

  47. Analysis • Correctness: same as before • Size of Q = O(|V|) • A naive implementation has time complexity O(|V|2) • A vertex is removed from Q in each iteration of the while loop • Finding a minimum: O(|V|) operations • Deletion / relaxation: O(1) operations • We can achieve O(|V|log|V|+|E|) with a binary heap • Finding a minimum: O(1) operations • 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. • parent’s value ≤ children’s values (3, 0) (1, ∞) (2, ∞) (6, ∞) (4, ∞) (5, ∞) (7, ∞)

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

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

More Related