Download presentation
Presentation is loading. Please wait.
Published byRandall Reeves Modified over 9 years ago
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 ) 12 3 4 5 6 7
4
Graph Adjacency list o Space complexity: O(|V|+|E|) 12 3 4 5 6 7 345/ 6/ 1/ 15/ 147/ 2/ 5/ 1 2 3 4 5 6 7
5
Single-source shortest path What is the optimal path 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” 12 3 4 5 6 7 udist[u] 11 2∞ 30 42 52 6∞ 73
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 12 3 4 5 6 7 udist[u] 1∞ 2∞ 30 4∞ 5∞ 6∞ 7∞ Source s
8
Dry run 2: Q = [ s ] 12 3 4 5 6 7 udist[u] 1∞ 2∞ 30 4∞ 5∞ 6∞ 7∞ 3 1 4 5 7 Q
9
Dry run 3: Dequeue 12 3 4 5 6 7 udist[u] 1∞ 2∞ 30 4∞ 5∞ 6∞ 7∞ 3 1 4 5 7 Q u = 3
10
Dry run 4: Find neighbors 12 3 4 5 6 7 udist[u] 1∞ 2∞ 30 4∞ 5∞ 6∞ 7∞ 3 1 4 5 7 Q u = 3
11
Dry run 5: Update distance and enqueue 12 3 4 5 6 7 udist[u] 11 2∞ 30 4∞ 5∞ 6∞ 7∞ 3 1 4 5 7 Q u = 3 dist[3] + 1 = 0 + 1 = 1
12
Dry run 6: Dequeue 12 3 4 5 6 7 udist[u] 11 2∞ 30 4∞ 5∞ 6∞ 7∞ 3 1 4 5 7 Q u = 1
13
Dry run 7: Find neighbors 12 3 4 5 6 7 udist[u] 11 2∞ 30 4∞ 5∞ 6∞ 7∞ 3 1 4 5 7 Q u = 1
14
Dry run 8: Update distance and enqueue 12 3 4 5 6 7 udist[u] 11 2∞ 30 42 52 6∞ 7∞ 3 1 4 5 7 Q u = 1 dist[1] + 1 = 1 + 1 = 2
15
Dry run 9: Dequeue 12 3 4 5 6 7 udist[u] 11 2∞ 30 42 52 6∞ 7∞ 3 1 4 5 7 Q u = 4
16
Dry run 10: Find neighbors 12 3 4 5 6 7 udist[u] 11 2∞ 30 42 52 6∞ 7∞ 3 1 4 5 7 Q u = 4
17
Dry run 11: 12 3 4 5 6 7 udist[u] 11 2∞ 30 42 52 6∞ 7∞ 3 1 4 5 7 Q u = 4
18
Dry run 12: Dequeue 12 3 4 5 6 7 udist[u] 11 2∞ 30 42 52 6∞ 7∞ 3 1 4 5 7 Q u = 5
19
Dry run 13: Find neighbors 12 3 4 5 6 7 udist[u] 11 2∞ 30 42 52 6∞ 7∞ 3 1 4 5 7 Q u = 5
20
Dry run 14: 12 3 4 5 6 7 udist[u] 11 2∞ 30 42 52 6∞ 73 3 1 4 5 7 Q u = 5 dist[5] + 1 = 2 + 1 = 3
21
Dry run 15: Dequeue 12 3 4 5 6 7 udist[u] 11 2∞ 30 42 52 6∞ 73 3 1 4 5 7 Q u = 7
22
Dry run 16: Find neighbors 12 3 4 5 6 7 udist[u] 11 2∞ 30 42 52 6∞ 73 3 1 4 5 7 Q u = 7
23
Dry run 17: 12 3 4 5 6 7 udist[u] 11 2∞ 30 42 52 6∞ 73 3 1 4 5 7 Q u = 7
24
Dry run 18: Q is now empty! 12 3 4 5 6 7 udist[u] 11 2∞ 30 42 52 6∞ 73 3 1 4 5 7 Q
25
Dry run 19: Final result 12 3 4 5 6 7 udist[u] 11 2∞ 30 42 52 6∞ 73 3 1 4 5 7 Q
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 12 3 4 5 6 7 udist[u] 13 2∞ 30 46 55 6∞ 78 3 2 1 4 3 2 345/ 1 (3, 3) (4, 4) (5, 2)
28
Dijkstra’s 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 Q contain 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 12 3 4 5 6 7 3 2 1 4 3 2 udist[u] 1∞ 2∞ 30 4∞ 5∞ 6∞ 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. udist[u] 1∞ 2∞ 30 4∞ 5∞ 6∞ 7∞ 1 2 3 4 5 6 7 Q 12 3 4 5 6 7 3 2 1 4 3 2
31
Dry run 3: Find minimum udist[u] 1∞ 2∞ 30 4∞ 5∞ 6∞ 7∞ 1 2 3 4 5 6 7 Q 12 3 4 5 6 7 3 2 1 4 3 2 u = 3
32
Dry run 4: Delete u from Q udist[u] 1∞ 2∞ 30 4∞ 5∞ 6∞ 7∞ 1 2 3 4 5 6 7 Q 12 3 4 5 6 7 3 2 1 4 3 2 u = 3
33
Dry run 5: Relaxation udist[u] 13 2∞ 30 4∞ 5∞ 6∞ 7∞ 1 2 3 4 5 6 7 Q 12 3 4 5 6 7 3 2 1 4 3 2 u = 3 dist[3] + l(3, 1) = 0 + 3 = 3
34
Dry run 6: Find minimum udist[u] 13 2∞ 30 4∞ 5∞ 6∞ 7∞ 1 2 3 4 5 6 7 Q 12 3 4 5 6 7 3 2 1 4 3 2 u = 1
35
Dry run 7: Delete u from Q udist[u] 13 2∞ 30 4∞ 5∞ 6∞ 7∞ 1 2 3 4 5 6 7 Q 12 3 4 5 6 7 3 2 1 4 3 2 u = 1
36
Dry run 8: Relaxation udist[u] 13 2∞ 30 47 55 6∞ 7∞ 1 2 3 4 5 6 7 Q 12 3 4 5 6 7 3 2 1 4 3 2 u = 1 dist[1] + l(1, 4) = 3 + 4 = 7 dist[1] + l(1, 5) = 3 + 2 = 5
37
Dry run 9: Find minimum udist[u] 13 2∞ 30 47 55 6∞ 7∞ 1 2 3 4 5 6 7 Q 12 3 4 5 6 7 3 2 1 4 3 2 u = 5
38
Dry run 10: Delete u from Q udist[u] 13 2∞ 30 47 55 6∞ 7∞ 1 2 3 4 5 6 7 Q u = 5 12 3 4 5 6 7 3 2 1 4 3 2
39
Dry run 11: Relaxation udist[u] 13 2∞ 30 46 55 6∞ 78 1 2 3 4 5 6 7 Q u = 5 dist[5] + l(5, 4) = 5 + 1 = 6 dist[5] + l(5, 7) = 5 + 3 = 8 12 3 4 5 6 7 3 2 1 4 3 2
40
Dry run 12: Find minimum 1 2 3 4 5 6 7 Q 12 3 4 5 6 7 3 2 1 4 3 2 u = 4 udist[u] 13 2∞ 30 46 55 6∞ 78
41
Dry run 13: 1 2 3 4 5 6 7 Q u = 4 udist[u] 13 2∞ 30 46 55 6∞ 78 12 3 4 5 6 7 3 2 1 4 3 2
42
Dry run 14: 1 2 3 4 5 6 7 Q u = 7 12 3 4 5 6 7 3 2 1 4 3 2 udist[u] 13 2∞ 30 46 55 6∞ 78
43
Dry run 15: 1 2 3 4 5 6 7 Q u = 2 12 3 4 5 6 7 3 2 1 4 3 2 udist[u] 13 2∞ 30 46 55 6∞ 78 dist[2] + l(2, 6) = ∞ + 2 = ∞
44
Dry run 16: 1 2 3 4 5 6 71 2 3 4 5 6 7 Q u = 6 12 3 4 5 6 7 3 2 1 4 3 2 udist[u] 13 2∞ 30 46 55 6∞ 78
45
Dry run 17: Q is now empty! 1 2 3 4 5 6 71 2 3 4 5 6 7 Q 12 3 4 5 6 7 3 2 1 4 3 2 udist[u] 13 2∞ 30 46 55 6∞ 78
46
Dry run 18: Final result 1 2 3 4 5 6 71 2 3 4 5 6 7 Q 12 3 4 5 6 7 3 2 1 4 3 2 udist[u] 13 2∞ 30 46 55 6∞ 78
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. o parent’s value ≤ children’s values (3, 0) (1, ∞)(2, ∞) (4, ∞) (6, ∞) (5, ∞)(7, ∞)
49
Priority queue Delete (3, 0) (3, 0) (1, ∞)(2, ∞) (4, ∞) (6, ∞) (5, ∞)(7, ∞)
50
Priority queue Delete (3, 0) o Heap property NOT violated; OK! (1, ∞)(2, ∞) (4, ∞) (6, ∞) (5, ∞) (7, ∞)
51
Priority queue Update (1, ∞) → (1, 3) o dist[ 3 ] + l (3, 1) = 0 + 3 = 3 o Heap property violated; DecreaseKey triggered (1, 3)(2, ∞) (4, ∞) (6, ∞) (5, ∞) (7, ∞)
52
Priority queue Update (1, ∞) → (1, 3) o Heap property NOT violated; OK! (7, ∞)(2, ∞) (4, ∞) (6, ∞) (5, ∞) (1, 3)
53
End Questions
54
End o I now know the distances dist[u], but what are the shortest paths? o How do we locate the heap entries quickly?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.