Upload
randall-reeves
View
263
Download
1
Tags:
Embed Size (px)
Citation preview
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)• Simple graph: unweighted, undirected graph,
containing no loops and multiple edgeso |E| = O(|V|2)
1 2
3 45
6
7
Graph• Adjacency list
o Space complexity: O(|V|+|E|)
1 2
3 45
6
7
3 4 5 /
6 /
1 /
1 5 /
1 4 7 /
2 /
5 /
1
2
3
4
5
6
7
Single-source shortest path
• What is the optimal path from one vertex to another?o Suppose each edge is of unit lengtho Store the minimum distances in an array dist[]o Example: if source vertex s = “3”
1 2
3 45
6
7
u dist[u]
1 1
2 ∞
3 0
4 2
5 2
6 ∞
7 3
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 Qo Set dist[v] = dist[u] + 1
Dry run• 5: Update distance and enqueue
1 2
3 45
6
7
u dist[u]
1 1
2 ∞
3 0
4 ∞
5 ∞
6 ∞
7 ∞
3 1 4 5 7Q
u = 3
dist[3] + 1 = 0 + 1 = 1
Dry run• 8: Update distance and enqueue
1 2
3 45
6
7
u dist[u]
1 1
2 ∞
3 0
4 2
5 2
6 ∞
7 ∞
3 1 4 5 7Q
u = 1
dist[1] + 1 = 1 + 1 = 2
Dry run• 14:
1 2
3 45
6
7
u dist[u]
1 1
2 ∞
3 0
4 2
5 2
6 ∞
7 3
3 1 4 5 7Q
u = 5
dist[5] + 1 = 2 + 1 = 3
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|) operationso Each edge is considered O(1) timeso Enqueue/dequeue: O(1) operations
Weighted graphs• Suppose now that each edge has its non-
negative length l(u, v)o Need something more than BFS
1 2
3 45
6
7
u dist[u]
1 3
2 ∞
3 0
4 6
5 5
6 ∞
7 8
32
1
4
3
2
3 4 5 /1
(3, 3) (4, 4) (5, 2)
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 verticeso 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)
Dry run• 2: Let Q contain all vertices
o Let us not care about what a priority queue is for the time being.
u dist[u]1 ∞2 ∞3 04 ∞5 ∞6 ∞7 ∞
1 2 3 4 5 6 7Q
1 2
3 45
6
7
32
1
4
3
2
Dry run• 3: Find minimum
u dist[u]1 ∞2 ∞3 04 ∞5 ∞6 ∞7 ∞
1 2 3 4 5 6 7Q
1 2
3 45
6
7
32
1
4
3
2
u = 3
Dry run• 4: Delete u from Q
u dist[u]1 ∞2 ∞3 04 ∞5 ∞6 ∞7 ∞
1 2 3 4 5 6 7Q
1 2
3 45
6
7
32
1
4
3
2
u = 3
Dry run• 5: Relaxation
u dist[u]1 32 ∞3 04 ∞5 ∞6 ∞7 ∞
1 2 3 4 5 6 7Q
1 2
3 45
6
7
32
1
4
3
2
u = 3
dist[3] + l(3, 1) = 0 + 3 = 3
Dry run• 6: Find minimum
u dist[u]1 32 ∞3 04 ∞5 ∞6 ∞7 ∞
1 2 3 4 5 6 7Q
1 2
3 45
6
7
32
1
4
3
2
u = 1
Dry run• 7: Delete u from Q
u dist[u]1 32 ∞3 04 ∞5 ∞6 ∞7 ∞
1 2 3 4 5 6 7Q
1 2
3 45
6
7
32
1
4
3
2
u = 1
Dry run• 8: Relaxation
u dist[u]1 32 ∞3 04 75 56 ∞7 ∞
1 2 3 4 5 6 7Q
1 2
3 45
6
7
32
1
4
3
2
u = 1
dist[1] + l(1, 4) = 3 + 4 = 7
dist[1] + l(1, 5) = 3 + 2 = 5
Dry run• 9: Find minimum
u dist[u]1 32 ∞3 04 75 56 ∞7 ∞
1 2 3 4 5 6 7Q
1 2
3 45
6
7
32
1
4
3
2
u = 5
Dry run• 10: Delete u from Q
u dist[u]1 32 ∞3 04 75 56 ∞7 ∞
1 2 3 4 5 6 7Qu = 5
1 2
3 45
6
7
32
1
4
3
2
Dry run• 11: Relaxation
u dist[u]1 32 ∞3 04 65 56 ∞7 8
1 2 3 4 5 6 7Qu = 5
dist[5] + l(5, 4) = 5 + 1 = 6
dist[5] + l(5, 7) = 5 + 3 = 8
1 2
3 45
6
7
32
1
4
3
2
Dry run• 12: Find minimum
1 2 3 4 5 6 7Q
1 2
3 45
6
7
32
1
4
3
2
u = 4u dist[u]
1 3
2 ∞
3 0
4 6
5 5
6 ∞
7 8
Dry run• 15:
1 2 3 4 5 6 7Qu = 2
1 2
3 45
6
7
32
1
4
3
2
u dist[u]
1 3
2 ∞
3 0
4 6
5 5
6 ∞
7 8
dist[2] + l(2, 6) = ∞ + 2 = ∞
Dry run• 17: Q is now empty!
1 2 3 4 5 6 7Q
1 2
3 45
6
7
32
1
4
3
2
u dist[u]
1 3
2 ∞
3 0
4 6
5 5
6 ∞
7 8
Dry run• 18: Final result
1 2 3 4 5 6 7Q
1 2
3 45
6
7
32
1
4
3
2
u dist[u]
1 3
2 ∞
3 0
4 6
5 5
6 ∞
7 8
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|) operationso Deletion / relaxation: O(1) operations
• We can achieve O(|V|log|V|+|E|) with a binary heapo Finding a minimum: O(1) operationso Deletion: O(log|V|) operations, to maintain the heap
property that parent’s value ≤ children’s values
Priority queue• It could be implemented using a heap.
o parent’s value ≤ children’s values
(3, 0)
(1, ∞)
(2, ∞)
(4, ∞)
(6, ∞)
(5, ∞)
(7, ∞)
Priority queue• Delete (3, 0)
o Heap property NOT violated; OK!
(1, ∞)
(2, ∞)
(4, ∞)
(6, ∞)
(5, ∞)
(7, ∞)
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, ∞)
Priority queue• Update (1, ∞) → (1, 3)
o Heap property NOT violated; OK!
(7, ∞)
(2, ∞)
(4, ∞)
(6, ∞)
(5, ∞)
(1, 3)