Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
1. Data Structures and Algorithms – COMS21103
Single Source Shortest Paths
Priority Queues and Dijkstra’s Algorithm
Benjamin Sach
2. In today’s lectures we’ll be discussing the single source shortest paths problem
In particular we’ll be interested in Dijkstra’s Algorithm
in a weighted, directed graph. . .
which is based on an abstract data structure called a priority queue
The shortest path from MVB to Temple Meads
(according to Google Maps)
. . . which can be efficiently implemented as a binary heap
3. In today’s lectures we’ll be discussing the single source shortest paths problem
In particular we’ll be interested in Dijkstra’s Algorithm
in a weighted, directed graph. . .
which is based on an abstract data structure called a priority queue
The shortest path from MVB to Temple Meads
(according to Google Maps)
Vertices are junctions
Edges are roads
Edge weights are in miles
Directed edges
are one-way streets
. . . which can be efficiently implemented as a binary heap
6. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
Q
7. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
8. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
INSERT(x, k) - inserts x with x.key = k
9. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
10. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
11. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
A priority queue:
x
x.key
x
x.key5
Bob
5
Bob
8
Chris
8
Chris
3
Alice
33
Alice
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
12. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
A priority queue:
x
x.key
x
x.key5
Bob
5
Bob
8
Chris
8
Chris
3
Alice
33
Alice
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
INSERT(Dawn, 4)
13. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
A priority queue:
x
x.key
x
x.key5
Bob
5
Bob
8
Chris
8
Chris
4
Dawn
4
Dawn
3
Alice
33
Alice
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
INSERT(Dawn, 4)
14. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
A priority queue:
x
x.key
x
x.key5
Bob
5
Bob
8
Chris
8
Chris
4
Dawn
4
Dawn
3
Alice
33
Alice
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
15. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
A priority queue:
x
x.key
x
x.key5
Bob
5
Bob
8
Chris
8
Chris
4
Dawn
4
Dawn
3
Alice
33
Alice
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
EXTRACTMIN()
16. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
A priority queue:
x
x.key
x
x.key5
Bob
5
Bob
8
Chris
8
Chris
4
Dawn
4
Dawn
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
EXTRACTMIN()
17. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
A priority queue:
x
x.key
x
x.key5
Bob
5
Bob
8
Chris
8
Chris
4
Dawn
4
Dawn
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
18. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
A priority queue:
x
x.key
x
x.key5
Bob
5
Bob
8
Chris
8
Chris
4
Dawn
4
Dawn
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
INSERT(Emma, 6)
19. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
A priority queue:
x
x.key
x
x.key5
Bob
5
Bob
8
Chris
8
Chris
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
INSERT(Emma, 6)
20. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
A priority queue:
x
x.key
x
x.key5
Bob
5
Bob
8
Chris
8
Chris
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
21. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
A priority queue:
x
x.key
x
x.key5
Bob
5
Bob
8
Chris
8
Chris
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
EXTRACTMIN()
22. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
A priority queue:
x
x.key
x
x.key5
Bob
5
Bob
8
Chris
8
Chris
3
Alice
35
Emma
3
Alice
36
Emma
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
EXTRACTMIN()
23. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
A priority queue:
x
x.key
x
x.key5
Bob
5
Bob
8
Chris
8
Chris
3
Alice
35
Emma
3
Alice
36
Emma
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
24. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
A priority queue:
x
x.key
x
x.key5
Bob
5
Bob
8
Chris
8
Chris
3
Alice
35
Emma
3
Alice
36
Emma
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
DECREASEKEY(Bob, 2)
25. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
A priority queue:
x
x.key
x
x.key8
Chris
8
Chris
5
Bob
2
Bob
3
Alice
35
Emma
3
Alice
36
Emma
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
DECREASEKEY(Bob, 2)
26. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
A priority queue:
x
x.key
x
x.key8
Chris
8
Chris
5
Bob
2
Bob
3
Alice
35
Emma
3
Alice
36
Emma
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
27. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
A priority queue:
x
x.key
x
x.key8
Chris
8
Chris
5
Bob
2
Bob
3
Alice
35
Emma
3
Alice
36
Emma
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
INSERT(Alice, 3)
28. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
A priority queue:
x
x.key
x
x.key8
Chris
8
Chris
3
Alice
33
Alice
5
Bob
2
Bob
3
Alice
35
Emma
3
Alice
36
Emma
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
INSERT(Alice, 3)
29. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
A priority queue:
x
x.key
x
x.key8
Chris
8
Chris
3
Alice
33
Alice
5
Bob
2
Bob
3
Alice
35
Emma
3
Alice
36
Emma
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
30. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
A priority queue:
x
x.key
x
x.key8
Chris
8
Chris
3
Alice
33
Alice
5
Bob
2
Bob
3
Alice
35
Emma
3
Alice
36
Emma
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
EXTRACTMIN()
31. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
A priority queue:
x
x.key
x
x.key8
Chris
8
Chris
3
Alice
33
Alice
3
Alice
35
Emma
3
Alice
36
Emma
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
EXTRACTMIN()
32. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
A priority queue:
x
x.key
x
x.key8
Chris
8
Chris
3
Alice
33
Alice
3
Alice
35
Emma
3
Alice
36
Emma
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
33. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
A priority queue:
x
x.key
x
x.key8
Chris
8
Chris
3
Alice
33
Alice
3
Alice
35
Emma
3
Alice
36
Emma
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
DECREASEKEY(Chris, 4)
34. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
A priority queue:
x
x.key
x
x.key3
Alice
33
Alice
3
Alice
35
Emma
3
Alice
36
Emma
8
Chris
4
Chris
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
DECREASEKEY(Chris, 4)
35. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
A priority queue:
x
x.key
x
x.key3
Alice
33
Alice
3
Alice
35
Emma
3
Alice
36
Emma
8
Chris
4
Chris
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
36. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
A priority queue:
x
x.key
x
x.key3
Alice
33
Alice
3
Alice
35
Emma
3
Alice
36
Emma
8
Chris
4
Chris
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
EXTRACTMIN()
37. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
A priority queue:
x
x.key
x
x.key3
Alice
35
Emma
3
Alice
36
Emma
8
Chris
4
Chris
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
EXTRACTMIN()
38. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
A priority queue:
x
x.key
x
x.key3
Alice
35
Emma
3
Alice
36
Emma
8
Chris
4
Chris
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
39. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
A priority queue:
x
x.key
x
x.key3
Alice
35
Emma
3
Alice
36
Emma
8
Chris
4
Chris
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
EXTRACTMIN()
40. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
A priority queue:
x
x.key
x
x.key3
Alice
35
Emma
3
Alice
36
Emma
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
EXTRACTMIN()
41. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
A priority queue:
x
x.key
x
x.key3
Alice
35
Emma
3
Alice
36
Emma
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
42. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
A priority queue:
x
x.key
x
x.key3
Alice
35
Emma
3
Alice
36
Emma
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
EXTRACTMIN()
43. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
A priority queue:
x
x.key
x
x.key
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
EXTRACTMIN()
44. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
A priority queue:
x
x.key
x
x.key
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
45. Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
46. Using a Linked List as a Priority Queue
There are many ways in which we could implement a priority queue. . .
but they aren’t all efficient
Let n denote the number of elements in the queue
- our goal is to implement a queue with operations which scale well as n grows
47. Using a Linked List as a Priority Queue
There are many ways in which we could implement a priority queue. . .
but they aren’t all efficient
Let n denote the number of elements in the queue
- our goal is to implement a queue with operations which scale well as n grows
48. Using a Linked List as a Priority Queue
We could implement a Priority Queue using an unsorted linked list:
There are many ways in which we could implement a priority queue. . .
but they aren’t all efficient
Let n denote the number of elements in the queue
- our goal is to implement a queue with operations which scale well as n grows
49. Using a Linked List as a Priority Queue
5
Bob
5
Bob x
x.key
x
x.key
We could implement a Priority Queue using an unsorted linked list:
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
3
Alice
33
Alice
There are many ways in which we could implement a priority queue. . .
but they aren’t all efficient
Let n denote the number of elements in the queue
- our goal is to implement a queue with operations which scale well as n grows
50. Using a Linked List as a Priority Queue
5
Bob
5
Bob x
x.key
x
x.key
We could implement a Priority Queue using an unsorted linked list:
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
3
Alice
33
Alice
INSERT is very efficient,
- add the new item to the head of the list in O(1) time
There are many ways in which we could implement a priority queue. . .
but they aren’t all efficient
Let n denote the number of elements in the queue
- our goal is to implement a queue with operations which scale well as n grows
51. Using a Linked List as a Priority Queue
5
Bob
5
Bob x
x.key
x
x.key
We could implement a Priority Queue using an unsorted linked list:
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
3
Alice
33
Alice
INSERT is very efficient,
- add the new item to the head of the list in O(1) time
8
Chris
7
Chris
There are many ways in which we could implement a priority queue. . .
but they aren’t all efficient
Let n denote the number of elements in the queue
- our goal is to implement a queue with operations which scale well as n grows
52. Using a Linked List as a Priority Queue
5
Bob
5
Bob x
x.key
x
x.key
We could implement a Priority Queue using an unsorted linked list:
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
3
Alice
33
Alice
INSERT is very efficient,
- add the new item to the head of the list in O(1) time
8
Chris
7
Chris
EXTRACTMIN and DECREASEKEY are very inefficient, they take O(n) time
- we have to look through the entire linked list to find an item
(in the worst case)
There are many ways in which we could implement a priority queue. . .
but they aren’t all efficient
4
Dawn
4
Dawn
Let n denote the number of elements in the queue
- our goal is to implement a queue with operations which scale well as n grows
53. Using a Linked List as a Priority Queue
5
Bob
5
Bob x
x.key
x
x.key
We could implement a Priority Queue using an unsorted linked list:
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
3
Alice
33
Alice
INSERT is very efficient,
- add the new item to the head of the list in O(1) time
8
Chris
7
Chris
EXTRACTMIN and DECREASEKEY are very inefficient, they take O(n) time
- we have to look through the entire linked list to find an item
(in the worst case)
There are many ways in which we could implement a priority queue. . .
but they aren’t all efficient
4
Dawn
4
Dawn
Let n denote the number of elements in the queue
- our goal is to implement a queue with operations which scale well as n grows
54. Using a Linked List as a Priority Queue
5
Bob
5
Bob x
x.key
x
x.key
We could implement a Priority Queue using an unsorted linked list:
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
3
Alice
33
Alice
INSERT is very efficient,
- add the new item to the head of the list in O(1) time
8
Chris
7
Chris
EXTRACTMIN and DECREASEKEY are very inefficient, they take O(n) time
- we have to look through the entire linked list to find an item
(in the worst case)
There are many ways in which we could implement a priority queue. . .
but they aren’t all efficient
4
Dawn
4
Dawn
Let n denote the number of elements in the queue
- our goal is to implement a queue with operations which scale well as n grows
55. Using a Linked List as a Priority Queue
5
Bob
5
Bob x
x.key
x
x.key
We could implement a Priority Queue using an unsorted linked list:
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
3
Alice
33
Alice
INSERT is very efficient,
- add the new item to the head of the list in O(1) time
8
Chris
7
Chris
EXTRACTMIN and DECREASEKEY are very inefficient, they take O(n) time
- we have to look through the entire linked list to find an item
(in the worst case)
There are many ways in which we could implement a priority queue. . .
but they aren’t all efficient
4
Dawn
4
Dawn
Let n denote the number of elements in the queue
- our goal is to implement a queue with operations which scale well as n grows
56. Using a Linked List as a Priority Queue
5
Bob
5
Bob x
x.key
x
x.key
We could implement a Priority Queue using an unsorted linked list:
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
3
Alice
33
Alice
INSERT is very efficient,
- add the new item to the head of the list in O(1) time
8
Chris
7
Chris
EXTRACTMIN and DECREASEKEY are very inefficient, they take O(n) time
- we have to look through the entire linked list to find an item
(in the worst case)
There are many ways in which we could implement a priority queue. . .
but they aren’t all efficient
4
Dawn
4
Dawn
Let n denote the number of elements in the queue
- our goal is to implement a queue with operations which scale well as n grows
57. Using a Linked List as a Priority Queue
5
Bob
5
Bob x
x.key
x
x.key
We could implement a Priority Queue using an unsorted linked list:
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
3
Alice
33
Alice
INSERT is very efficient,
- add the new item to the head of the list in O(1) time
8
Chris
7
Chris
EXTRACTMIN and DECREASEKEY are very inefficient, they take O(n) time
- we have to look through the entire linked list to find an item
(in the worst case)
There are many ways in which we could implement a priority queue. . .
but they aren’t all efficient
4
Dawn
4
Dawn
Let n denote the number of elements in the queue
- our goal is to implement a queue with operations which scale well as n grows
58. Using a Linked List as a Priority Queue
5
Bob
5
Bob x
x.key
x
x.key
We could implement a Priority Queue using an unsorted linked list:
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
3
Alice
33
Alice
INSERT is very efficient,
- add the new item to the head of the list in O(1) time
8
Chris
7
Chris
EXTRACTMIN and DECREASEKEY are very inefficient, they take O(n) time
- we have to look through the entire linked list to find an item
(in the worst case)
There are many ways in which we could implement a priority queue. . .
but they aren’t all efficient
4
Dawn
4
Dawn
Let n denote the number of elements in the queue
- our goal is to implement a queue with operations which scale well as n grows
59. Using a Linked List as a Priority Queue
5
Bob
5
Bob
4
Dawn
4
Dawn x
x.key
x
x.key
We could implement a Priority Queue using a sorted linked list:
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
3
Alice
33
Alice
There are many ways in which we could implement a priority queue. . .
but they aren’t all efficient
Let n denote the number of elements in the queue
- our goal is to implement a queue with operations which scale well as n grows
Instead,
5
Bob
5
Bob
4
Dawn
4
Dawn
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
60. Using a Linked List as a Priority Queue
5
Bob
5
Bob
4
Dawn
4
Dawn x
x.key
x
x.key
We could implement a Priority Queue using a sorted linked list:
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
3
Alice
33
Alice
EXTRACTMIN is very efficient,
- remove the head of the list in O(1) time
There are many ways in which we could implement a priority queue. . .
but they aren’t all efficient
Let n denote the number of elements in the queue
- our goal is to implement a queue with operations which scale well as n grows
Instead,
5
Bob
5
Bob
4
Dawn
4
Dawn
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
61. Using a Linked List as a Priority Queue
5
Bob
5
Bob
4
Dawn
4
Dawn x
x.key
x
x.key
We could implement a Priority Queue using a sorted linked list:
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
EXTRACTMIN is very efficient,
- remove the head of the list in O(1) time
There are many ways in which we could implement a priority queue. . .
but they aren’t all efficient
Let n denote the number of elements in the queue
- our goal is to implement a queue with operations which scale well as n grows
Instead,
62. Using a Linked List as a Priority Queue
5
Bob
5
Bob
4
Dawn
4
Dawn x
x.key
x
x.key
We could implement a Priority Queue using a sorted linked list:
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
EXTRACTMIN is very efficient,
- remove the head of the list in O(1) time
INSERT and DECREASEKEY are very inefficient, they take O(n) time
- we have to look through the entire linked list
(in the worst case)
There are many ways in which we could implement a priority queue. . .
but they aren’t all efficient
Let n denote the number of elements in the queue
- our goal is to implement a queue with operations which scale well as n grows
Instead,
63. Using a Linked List as a Priority Queue
5
Bob
5
Bob
4
Dawn
4
Dawn x
x.key
x
x.key
We could implement a Priority Queue using a sorted linked list:
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
EXTRACTMIN is very efficient,
- remove the head of the list in O(1) time
8
Chris
7
Chris
INSERT and DECREASEKEY are very inefficient, they take O(n) time
- we have to look through the entire linked list
(in the worst case)
There are many ways in which we could implement a priority queue. . .
but they aren’t all efficient
Let n denote the number of elements in the queue
- our goal is to implement a queue with operations which scale well as n grows
Instead,
64. Using a Linked List as a Priority Queue
5
Bob
5
Bob
4
Dawn
4
Dawn x
x.key
x
x.key
We could implement a Priority Queue using a sorted linked list:
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
EXTRACTMIN is very efficient,
- remove the head of the list in O(1) time
8
Chris
7
Chris
INSERT and DECREASEKEY are very inefficient, they take O(n) time
- we have to look through the entire linked list
(in the worst case)
There are many ways in which we could implement a priority queue. . .
but they aren’t all efficient
Let n denote the number of elements in the queue
- our goal is to implement a queue with operations which scale well as n grows
Instead,
65. Using a Linked List as a Priority Queue
5
Bob
5
Bob
4
Dawn
4
Dawn x
x.key
x
x.key
We could implement a Priority Queue using a sorted linked list:
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
EXTRACTMIN is very efficient,
- remove the head of the list in O(1) time
8
Chris
7
Chris
INSERT and DECREASEKEY are very inefficient, they take O(n) time
- we have to look through the entire linked list
(in the worst case)
There are many ways in which we could implement a priority queue. . .
but they aren’t all efficient
Let n denote the number of elements in the queue
- our goal is to implement a queue with operations which scale well as n grows
Instead,
66. Using a Linked List as a Priority Queue
5
Bob
5
Bob
4
Dawn
4
Dawn x
x.key
x
x.key
We could implement a Priority Queue using a sorted linked list:
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
EXTRACTMIN is very efficient,
- remove the head of the list in O(1) time
8
Chris
7
Chris
INSERT and DECREASEKEY are very inefficient, they take O(n) time
- we have to look through the entire linked list
(in the worst case)
There are many ways in which we could implement a priority queue. . .
but they aren’t all efficient
Let n denote the number of elements in the queue
- our goal is to implement a queue with operations which scale well as n grows
Instead,
67. Using a Linked List as a Priority Queue
5
Bob
5
Bob
4
Dawn
4
Dawn x
x.key
x
x.key
We could implement a Priority Queue using a sorted linked list:
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
EXTRACTMIN is very efficient,
- remove the head of the list in O(1) time
8
Chris
7
Chris
INSERT and DECREASEKEY are very inefficient, they take O(n) time
- we have to look through the entire linked list
(in the worst case)
There are many ways in which we could implement a priority queue. . .
but they aren’t all efficient
Let n denote the number of elements in the queue
- our goal is to implement a queue with operations which scale well as n grows
Instead,
68. Using a Linked List as a Priority Queue
5
Bob
5
Bob
4
Dawn
4
Dawn x
x.key
x
x.key
We could implement a Priority Queue using a sorted linked list:
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
EXTRACTMIN is very efficient,
- remove the head of the list in O(1) time
8
Chris
7
Chris
INSERT and DECREASEKEY are very inefficient, they take O(n) time
- we have to look through the entire linked list
(in the worst case)
There are many ways in which we could implement a priority queue. . .
but they aren’t all efficient
Let n denote the number of elements in the queue
- our goal is to implement a queue with operations which scale well as n grows
Instead,
69. Binary Heaps
A binary heap is an ‘almost complete’ binary tree, where every level is full. . .
except (possibly) the lowest, which is filled from left to right
key2
2 3
5 3 4 6
7 6 5 4 9
70. Binary Heaps
A binary heap is an ‘almost complete’ binary tree, where every level is full. . .
except (possibly) the lowest, which is filled from left to right
key2
2 3
5 3 4 6
7 6 5 4 9
filling order
71. Binary Heaps
A binary heap is an ‘almost complete’ binary tree, where every level is full. . .
except (possibly) the lowest, which is filled from left to right
key
the height is
O(log n)
n is the number of elements in the Heap
2
2 3
5 3 4 6
7 6 5 4 9
filling order
72. Binary Heaps
A binary heap is an ‘almost complete’ binary tree, where every level is full. . .
except (possibly) the lowest, which is filled from left to right
key
the height is
O(log n)
n is the number of elements in the Heap
2
2 3
5 3 4 6
7 6 5 4 9
73. Binary Heaps
A binary heap is an ‘almost complete’ binary tree, where every level is full. . .
except (possibly) the lowest, which is filled from left to right
key
the height is
O(log n)
n is the number of elements in the Heap
2
2 3
5 3 4 6
7 6 5 4 9
Heap Property Any element has a key less than or equal to the keys of its children.
74. Binary Heaps
A binary heap is an ‘almost complete’ binary tree, where every level is full. . .
except (possibly) the lowest, which is filled from left to right
key
the height is
O(log n)
n is the number of elements in the Heap
2
2 3
5 3 4 6
7 6 5 4 9
Heap Property Any element has a key less than or equal to the keys of its children.
75. Binary Heaps
A binary heap is an ‘almost complete’ binary tree, where every level is full. . .
except (possibly) the lowest, which is filled from left to right
key
the height is
O(log n)
n is the number of elements in the Heap
2
2 3
5 3 4 6
7 6 5 4 9
Heap Property Any element has a key less than or equal to the keys of its children.
76. Binary Heaps
A binary heap is an ‘almost complete’ binary tree, where every level is full. . .
except (possibly) the lowest, which is filled from left to right
key
the height is
O(log n)
n is the number of elements in the Heap
2
2 3
5 3 4 6
7 6 5 4 9
Heap Property Any element has a key less than or equal to the keys of its children.
77. Binary Heaps
A binary heap is an ‘almost complete’ binary tree, where every level is full. . .
except (possibly) the lowest, which is filled from left to right
key
the height is
O(log n)
n is the number of elements in the Heap
2
2 3
5 3 4 6
7 6 5 4 9
Heap Property Any element has a key less than or equal to the keys of its children.
A binary heap can be efficiently stored implicitly as an array A:
78. Binary Heaps
A binary heap is an ‘almost complete’ binary tree, where every level is full. . .
except (possibly) the lowest, which is filled from left to right
key
the height is
O(log n)
n is the number of elements in the Heap
2
2 3
5 3 4 6
7 6 5 4 9
Heap Property Any element has a key less than or equal to the keys of its children.
A binary heap can be efficiently stored implicitly as an array A:
2 2 3 5 3 4 6 7 6 5 4 9
79. Binary Heaps
A binary heap is an ‘almost complete’ binary tree, where every level is full. . .
except (possibly) the lowest, which is filled from left to right
key
the height is
O(log n)
n is the number of elements in the Heap
2
2 3
5 3 4 6
7 6 5 4 9
Heap Property Any element has a key less than or equal to the keys of its children.
A binary heap can be efficiently stored implicitly as an array A:
2 2 3 5 3 4 6 7 6 5 4 9
80. Binary Heaps
A binary heap is an ‘almost complete’ binary tree, where every level is full. . .
except (possibly) the lowest, which is filled from left to right
key
the height is
O(log n)
n is the number of elements in the Heap
2
2 3
5 3 4 6
7 6 5 4 9
Heap Property Any element has a key less than or equal to the keys of its children.
A binary heap can be efficiently stored implicitly as an array A:
2 2 3 5 3 4 6 7 6 5 4 9
81. Binary Heaps
A binary heap is an ‘almost complete’ binary tree, where every level is full. . .
except (possibly) the lowest, which is filled from left to right
key
the height is
O(log n)
n is the number of elements in the Heap
2
2 3
5 3 4 6
7 6 5 4 9
Heap Property Any element has a key less than or equal to the keys of its children.
A binary heap can be efficiently stored implicitly as an array A:
2 2 3 5 3 4 6 7 6 5 4 9
82. Binary Heaps
A binary heap is an ‘almost complete’ binary tree, where every level is full. . .
except (possibly) the lowest, which is filled from left to right
key
the height is
O(log n)
n is the number of elements in the Heap
2
2 3
5 3 4 6
7 6 5 4 9
Heap Property Any element has a key less than or equal to the keys of its children.
A binary heap can be efficiently stored implicitly as an array A:
2 2 3 5 3 4 6 7 6 5 4 9
83. Binary Heaps
A binary heap is an ‘almost complete’ binary tree, where every level is full. . .
except (possibly) the lowest, which is filled from left to right
key
the height is
O(log n)
n is the number of elements in the Heap
2
2 3
5 3 4 6
7 6 5 4 9
Heap Property Any element has a key less than or equal to the keys of its children.
A binary heap can be efficiently stored implicitly as an array A:
2 2 3 5 3 4 6 7 6 5 4 9
84. Binary Heaps
A binary heap is an ‘almost complete’ binary tree, where every level is full. . .
except (possibly) the lowest, which is filled from left to right
key
the height is
O(log n)
n is the number of elements in the Heap
2
2 3
5 3 4 6
7 6 5 4 9
Heap Property Any element has a key less than or equal to the keys of its children.
A binary heap can be efficiently stored implicitly as an array A:
2 2 3 5 3 4 6 7 6 5 4 9
Moving around using: PARENT(i) = i/2 LEFT(i) = 2i RIGHT(i) = 2i + 1
85. Binary Heaps
A binary heap is an ‘almost complete’ binary tree, where every level is full. . .
except (possibly) the lowest, which is filled from left to right
key
the height is
O(log n)
n is the number of elements in the Heap
2
2 3
5 3 4 6
7 6 5 4 9
Heap Property Any element has a key less than or equal to the keys of its children.
A binary heap can be efficiently stored implicitly as an array A:
2 2 3 5 3 4 6 7 6 5 4 9
Moving around using: PARENT(i) = i/2 LEFT(i) = 2i RIGHT(i) = 2i + 1
7
7
86. Binary Heaps
A binary heap is an ‘almost complete’ binary tree, where every level is full. . .
except (possibly) the lowest, which is filled from left to right
key
the height is
O(log n)
n is the number of elements in the Heap
2
2 3
5 3 4 6
7 6 5 4 9
Heap Property Any element has a key less than or equal to the keys of its children.
A binary heap can be efficiently stored implicitly as an array A:
2 2 3 5 3 4 6 7 6 5 4 9
Moving around using: PARENT(i) = i/2 LEFT(i) = 2i RIGHT(i) = 2i + 1
5
5
87. Using a Binary Heap as a Priority Queue
We will now see how to use a Binary Heap to implement the required operations:
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
Each in O(log n) time per operation
88. Using a Binary Heap as a Priority Queue
We will now see how to use a Binary Heap to implement the required operations:
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
Each in O(log n) time per operation
Assumption we can find the location of any element x in the Heap in O(1) time
89. Using a Binary Heap as a Priority Queue
We will now see how to use a Binary Heap to implement the required operations:
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
Each in O(log n) time per operation
Assumption we can find the location of any element x in the Heap in O(1) time
This is a little fiddly. . . we’ll come back to it at the end of the lecture
90. DECREASEKEY with a Binary Heap
key2
2 3
5 3 4 6
7 6 5 4 9
Step 1: Find element x
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
Step 2: Check that k x.key, otherwise raise an error
Step 3: Set x.key = k
Step 4: While x.key is smaller than its parent’s:
swap x with its parent
(stop if x becomes the root)
91. DECREASEKEY with a Binary Heap
key2
2 3
5 3 4 6
7 6 5 4 9
Step 1: Find element x
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
Step 2: Check that k x.key, otherwise raise an error
Step 3: Set x.key = k
Step 4: While x.key is smaller than its parent’s:
swap x with its parent
(stop if x becomes the root)
element E9
DECREASEKEY(E, 2)
9
92. DECREASEKEY with a Binary Heap
key2
2 3
5 3 4 6
7 6 5 4 9
Step 1: Find element x
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
Step 2: Check that k x.key, otherwise raise an error
Step 3: Set x.key = k
Step 4: While x.key is smaller than its parent’s:
swap x with its parent
(stop if x becomes the root)
element E9
DECREASEKEY(E, 2)
9
93. DECREASEKEY with a Binary Heap
key2
2 3
5 3 4 6
7 6 5 4 9
Step 1: Find element x
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
Step 2: Check that k x.key, otherwise raise an error
Step 3: Set x.key = k
Step 4: While x.key is smaller than its parent’s:
swap x with its parent
(stop if x becomes the root)
element E
DECREASEKEY(E, 2)
2
94. DECREASEKEY with a Binary Heap
key2
2 3
5 3 4 6
7 6 5 4 9
Step 1: Find element x
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
Step 2: Check that k x.key, otherwise raise an error
Step 3: Set x.key = k
Step 4: While x.key is smaller than its parent’s:
swap x with its parent
(stop if x becomes the root)
DECREASEKEY(E, 2)
2
4 element E
95. DECREASEKEY with a Binary Heap
key2
2 3
5 3 4 6
7 6 5 4 9
Step 1: Find element x
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
Step 2: Check that k x.key, otherwise raise an error
Step 3: Set x.key = k
Step 4: While x.key is smaller than its parent’s:
swap x with its parent
(stop if x becomes the root)
DECREASEKEY(E, 2)
element E4
2
3
96. DECREASEKEY with a Binary Heap
key2
2 3
5 3 4 6
7 6 5 4 9
Step 1: Find element x
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
Step 2: Check that k x.key, otherwise raise an error
Step 3: Set x.key = k
Step 4: While x.key is smaller than its parent’s:
swap x with its parent
(stop if x becomes the root)
DECREASEKEY(E, 2)
element E4
2
3It’s a heap again!
97. DECREASEKEY with a Binary Heap
key2
2 3
5 3 4 6
7 6 5 4 9
Step 1: Find element x
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
Step 2: Check that k x.key, otherwise raise an error
Step 3: Set x.key = k
Step 4: While x.key is smaller than its parent’s:
swap x with its parent
(stop if x becomes the root)
element E4
2
3
98. DECREASEKEY with a Binary Heap
key2
2 3
5 3 4 6
7 6 5 4 9
Step 1: Find element x
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
Step 2: Check that k x.key, otherwise raise an error
Step 3: Set x.key = k
Step 4: While x.key is smaller than its parent’s:
swap x with its parent
(stop if x becomes the root)
element E4
2
3
O(1) time
99. DECREASEKEY with a Binary Heap
key2
2 3
5 3 4 6
7 6 5 4 9
Step 1: Find element x
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
Step 2: Check that k x.key, otherwise raise an error
Step 3: Set x.key = k
Step 4: While x.key is smaller than its parent’s:
swap x with its parent
(stop if x becomes the root)
element E4
2
3
O(1) time
100. DECREASEKEY with a Binary Heap
key2
2 3
5 3 4 6
7 6 5 4 9
Step 1: Find element x
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
Step 2: Check that k x.key, otherwise raise an error
Step 3: Set x.key = k
Step 4: While x.key is smaller than its parent’s:
swap x with its parent
(stop if x becomes the root)
element E4
2
3
O(1) time
O(1) time
101. DECREASEKEY with a Binary Heap
key2
2 3
5 3 4 6
7 6 5 4 9
Step 1: Find element x
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
Step 2: Check that k x.key, otherwise raise an error
Step 3: Set x.key = k
Step 4: While x.key is smaller than its parent’s:
swap x with its parent
(stop if x becomes the root)
element E4
2
3
O(1) time
O(1) time
Each swap takes
O(1) time
102. DECREASEKEY with a Binary Heap
key2
2 3
5 3 4 6
7 6 5 4 9
Step 1: Find element x
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
Step 2: Check that k x.key, otherwise raise an error
Step 3: Set x.key = k
Step 4: While x.key is smaller than its parent’s:
swap x with its parent
(stop if x becomes the root)
element E4
2
3
O(1) time
O(1) time
Each swap takes
O(1) time
the height is
O(log n)
103. DECREASEKEY with a Binary Heap
key2
2 3
5 3 4 6
7 6 5 4 9
Step 1: Find element x
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
Step 2: Check that k x.key, otherwise raise an error
Step 3: Set x.key = k
Step 4: While x.key is smaller than its parent’s:
swap x with its parent
(stop if x becomes the root)
element E4
2
3
O(1) time
O(1) time
Each swap takes
O(1) time
The height of the tree is O(log n) so there are O(log n) swaps
the height is
O(log n)
104. DECREASEKEY with a Binary Heap
key2
2 3
5 3 4 6
7 6 5 4 9
Step 1: Find element x
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
Step 2: Check that k x.key, otherwise raise an error
Step 3: Set x.key = k
Step 4: While x.key is smaller than its parent’s:
swap x with its parent
(stop if x becomes the root)
element E4
2
3
the height is
O(log n)
105. DECREASEKEY with a Binary Heap
key2
2 3
5 3 4 6
7 6 5 4 9
Step 1: Find element x
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
Step 2: Check that k x.key, otherwise raise an error
Step 3: Set x.key = k
Step 4: While x.key is smaller than its parent’s:
swap x with its parent
(stop if x becomes the root)
element E4
2
3
Overall this takes O(log n) time
the height is
O(log n)
106. INSERT with a Binary Heap
key2
2 3
5 3 4 6
7 6 5 4 9
filling order
INSERT(x, k) - inserts x with x.key = k
Step 1: Put element x in the next free slot
Step 2: Run DECREASEKEY(x,k).
107. INSERT with a Binary Heap
key2
2 3
5 3 4 6
7 6 5 4 9
filling order
INSERT(x, k) - inserts x with x.key = k
Step 1: Put element x in the next free slot
Step 2: Run DECREASEKEY(x,k).
INSERT(F, 1)
108. INSERT with a Binary Heap
key2
2 3
5 3 4 6
7 6 5 4 9
INSERT(x, k) - inserts x with x.key = k
Step 1: Put element x in the next free slot
Step 2: Run DECREASEKEY(x,k).
1
filling order
element F
INSERT(F, 1)
109. INSERT with a Binary Heap
key2
2 3
5 3 4 6
7 6 5 4 9
INSERT(x, k) - inserts x with x.key = k
Step 1: Put element x in the next free slot
Step 2: Run DECREASEKEY(x,k).
filling order
element F
INSERT(F, 1)
1
4
110. INSERT with a Binary Heap
key2
2 3
5 3 4 6
7 6 5 4 9
INSERT(x, k) - inserts x with x.key = k
Step 1: Put element x in the next free slot
Step 2: Run DECREASEKEY(x,k).
filling order
element F
INSERT(F, 1) 1
4
3
111. INSERT with a Binary Heap
key2
2 3
5 3 4 6
7 6 5 4 9
INSERT(x, k) - inserts x with x.key = k
Step 1: Put element x in the next free slot
Step 2: Run DECREASEKEY(x,k).
filling order
element F
INSERT(F, 1) 2
1
4
3
112. INSERT with a Binary Heap
key2
2 3
5 3 4 6
7 6 5 4 9
INSERT(x, k) - inserts x with x.key = k
Step 1: Put element x in the next free slot
Step 2: Run DECREASEKEY(x,k).
filling order
element F
INSERT(F, 1) 2
1
4
3
O(1) time
113. INSERT with a Binary Heap
key2
2 3
5 3 4 6
7 6 5 4 9
INSERT(x, k) - inserts x with x.key = k
Step 1: Put element x in the next free slot
Step 2: Run DECREASEKEY(x,k).
filling order
element F
INSERT(F, 1) 2
1
4
3
O(1) time
O(log n) time
114. INSERT with a Binary Heap
key2
2 3
5 3 4 6
7 6 5 4 9
INSERT(x, k) - inserts x with x.key = k
Step 1: Put element x in the next free slot
Step 2: Run DECREASEKEY(x,k).
filling order
element F
INSERT(F, 1) 2
1
4
3
O(1) time
O(log n) time
Overall this takes O(log n) time
115. INSERT with a Binary Heap
key2
2 3
5 3 4 6
7 6 5 4 9
INSERT(x, k) - inserts x with x.key = k
Step 1: Put element x in the next free slot
Step 2: Run DECREASEKEY(x,k).
filling order
element F
2
1
4
3
O(1) time
O(log n) time
Overall this takes O(log n) time
116. EXTRACTMIN with a Binary Heap
2 3
5 3 4 6
7 6 5 4 9
Step 1: Extract the element at the root
by definition, it is the minimum
2
Step 2: Move the rightmost element in the bottom level to the root
Step 3: While y.key is larger than one of its children’s:
swap y with the child with the smaller key
(stop if y becomes a leaf)
EXTRACTMIN() - removes and returns the element with the smallest key
(call this element y)
117. EXTRACTMIN with a Binary Heap
2 3
5 3 4 6
7 6 5 4 9
Step 1: Extract the element at the root
by definition, it is the minimum
Step 2: Move the rightmost element in the bottom level to the root
Step 3: While y.key is larger than one of its children’s:
swap y with the child with the smaller key
(stop if y becomes a leaf)
EXTRACTMIN() - removes and returns the element with the smallest key
(call this element y)
118. EXTRACTMIN with a Binary Heap
2 3
5 3 4 6
7 6 5 4 9
Step 1: Extract the element at the root
by definition, it is the minimum
Step 2: Move the rightmost element in the bottom level to the root
9
Step 3: While y.key is larger than one of its children’s:
swap y with the child with the smaller key
(stop if y becomes a leaf)
EXTRACTMIN() - removes and returns the element with the smallest key
(call this element y)
119. EXTRACTMIN with a Binary Heap
2 3
5 3 4 6
7 6 5 4
Step 1: Extract the element at the root
by definition, it is the minimum
Step 2: Move the rightmost element in the bottom level to the root
9
Step 3: While y.key is larger than one of its children’s:
swap y with the child with the smaller key
(stop if y becomes a leaf)
EXTRACTMIN() - removes and returns the element with the smallest key
(call this element y)
120. EXTRACTMIN with a Binary Heap
2 3
5 3 4 6
7 6 5 4
Step 1: Extract the element at the root
by definition, it is the minimum
Step 2: Move the rightmost element in the bottom level to the root
9
Step 3: While y.key is larger than one of its children’s:
swap y with the child with the smaller key
(stop if y becomes a leaf)
EXTRACTMIN() - removes and returns the element with the smallest key
(call this element y)
121. EXTRACTMIN with a Binary Heap
2 3
5 3 4 6
7 6 5 4
Step 1: Extract the element at the root
by definition, it is the minimum
Step 2: Move the rightmost element in the bottom level to the root
Step 3: While y.key is larger than one of its children’s:
swap y with the child with the smaller key
(stop if y becomes a leaf)
EXTRACTMIN() - removes and returns the element with the smallest key
2
9
(call this element y)
122. EXTRACTMIN with a Binary Heap
2 3
5 3 4 6
7 6 5 4
Step 1: Extract the element at the root
by definition, it is the minimum
Step 2: Move the rightmost element in the bottom level to the root
Step 3: While y.key is larger than one of its children’s:
swap y with the child with the smaller key
(stop if y becomes a leaf)
EXTRACTMIN() - removes and returns the element with the smallest key
3
2
9
(call this element y)
123. EXTRACTMIN with a Binary Heap
2 3
5 3 4 6
7 6 5 4
Step 1: Extract the element at the root
by definition, it is the minimum
Step 2: Move the rightmost element in the bottom level to the root
Step 3: While y.key is larger than one of its children’s:
swap y with the child with the smaller key
(stop if y becomes a leaf)
EXTRACTMIN() - removes and returns the element with the smallest key
4
3
2
9
(call this element y)
124. EXTRACTMIN with a Binary Heap
2 3
5 3 4 6
7 6 5 4
Step 1: Extract the element at the root
by definition, it is the minimum
Step 2: Move the rightmost element in the bottom level to the root
Step 3: While y.key is larger than one of its children’s:
swap y with the child with the smaller key
(stop if y becomes a leaf)
EXTRACTMIN() - removes and returns the element with the smallest key
4
3
2
9
O(1) time
(call this element y)
125. EXTRACTMIN with a Binary Heap
2 3
5 3 4 6
7 6 5 4
Step 1: Extract the element at the root
by definition, it is the minimum
Step 2: Move the rightmost element in the bottom level to the root
Step 3: While y.key is larger than one of its children’s:
swap y with the child with the smaller key
(stop if y becomes a leaf)
EXTRACTMIN() - removes and returns the element with the smallest key
4
3
2
9
O(1) time
(call this element y)
126. EXTRACTMIN with a Binary Heap
2 3
5 3 4 6
7 6 5 4
Step 1: Extract the element at the root
by definition, it is the minimum
Step 2: Move the rightmost element in the bottom level to the root
Step 3: While y.key is larger than one of its children’s:
swap y with the child with the smaller key
(stop if y becomes a leaf)
EXTRACTMIN() - removes and returns the element with the smallest key
4
3
2
9
O(1) time
Each swap takes
O(1) time
(call this element y)
127. EXTRACTMIN with a Binary Heap
2 3
5 3 4 6
7 6 5 4
Step 1: Extract the element at the root
by definition, it is the minimum
Step 2: Move the rightmost element in the bottom level to the root
Step 3: While y.key is larger than one of its children’s:
swap y with the child with the smaller key
(stop if y becomes a leaf)
EXTRACTMIN() - removes and returns the element with the smallest key
4
3
2
9
O(1) time
Each swap takes
O(1) time
The height of the tree is O(log n) so there are O(log n) swaps (again)
(call this element y)
128. EXTRACTMIN with a Binary Heap
2 3
5 3 4 6
7 6 5 4
Step 1: Extract the element at the root
by definition, it is the minimum
Step 2: Move the rightmost element in the bottom level to the root
Step 3: While y.key is larger than one of its children’s:
swap y with the child with the smaller key
(stop if y becomes a leaf)
EXTRACTMIN() - removes and returns the element with the smallest key
4
3
2
9
(call this element y)
129. EXTRACTMIN with a Binary Heap
2 3
5 3 4 6
7 6 5 4
Step 1: Extract the element at the root
by definition, it is the minimum
Step 2: Move the rightmost element in the bottom level to the root
Step 3: While y.key is larger than one of its children’s:
swap y with the child with the smaller key
(stop if y becomes a leaf)
EXTRACTMIN() - removes and returns the element with the smallest key
4
3
2
9
Overall this takes O(log n) time
(call this element y)
130. Priority queue Summary
We have seen three different priority queue implementations
each supporting the following operations:
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
Sorted Linked List
Binary Heap
Unsorted Linked List
INSERT DECREASEKEY EXTRACTMIN
O(log n) O(log n)
O(1) O(n) O(n)
O(n) O(n) O(1)
O(log n)
131. Priority queue Summary
We have seen three different priority queue implementations
each supporting the following operations:
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
Sorted Linked List
Binary Heap
Unsorted Linked List
INSERT DECREASEKEY EXTRACTMIN
O(log n) O(log n)
O(1) O(n) O(n)
O(n) O(n) O(1)
O(log n)
What happened to that assumption?
132. Priority queue Summary
We have seen three different priority queue implementations
each supporting the following operations:
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
Sorted Linked List
Binary Heap
Unsorted Linked List
INSERT DECREASEKEY EXTRACTMIN
O(log n) O(log n)
O(1) O(n) O(n)
O(n) O(n) O(1)
O(log n)
Assumption we can find the location of any element x in the Heap in O(1) time
What happened to that assumption?
134. That pesky assumption. . .
Old Assumption we can find the location of any element x in the Heap in O(1) time
135. That pesky assumption. . .
Old Assumption we can find the location of any element x in the Heap in O(1) time
Each element x has an associated value called its key - x.key
Previously we said that. . .
136. That pesky assumption. . .
Old Assumption we can find the location of any element x in the Heap in O(1) time
Each element x has an associated value called its key - x.key
Previously we said that. . .
Each element x also has an associated (unique) positive integer ID - x.ID N
New (more reasonable) Assumption
137. That pesky assumption. . .
Old Assumption we can find the location of any element x in the Heap in O(1) time
ID
37
21 12
5 41 27 31
47 16 35 4 19
ID
Each element x has an associated value called its key - x.key
Previously we said that. . .
Each element x also has an associated (unique) positive integer ID - x.ID N
New (more reasonable) Assumption
138. That pesky assumption. . .
Old Assumption we can find the location of any element x in the Heap in O(1) time
ID
37
21 12
5 41 27 31
47 16 35 4 19
ID
Each element x has an associated value called its key - x.key
Previously we said that. . .
Each element x also has an associated (unique) positive integer ID - x.ID N
New (more reasonable) Assumption
Lookup table L:
N
139. That pesky assumption. . .
Old Assumption we can find the location of any element x in the Heap in O(1) time
ID
37
21 12
5 41 27 31
47 16 35 4 19
ID
Each element x has an associated value called its key - x.key
Previously we said that. . .
Each element x also has an associated (unique) positive integer ID - x.ID N
New (more reasonable) Assumption
Lookup table L:
N
L[i] stores a pointer
to the location of x
with x.ID = i
140. That pesky assumption. . .
Old Assumption we can find the location of any element x in the Heap in O(1) time
ID
37
21 12
5 41 27 31
47 16 35 4 19
ID
Each element x has an associated value called its key - x.key
Previously we said that. . .
Each element x also has an associated (unique) positive integer ID - x.ID N
New (more reasonable) Assumption
Lookup table L:
N
L[i] stores a pointer
to the location of x
with x.ID = i
L[41]
141. That pesky assumption. . .
Old Assumption we can find the location of any element x in the Heap in O(1) time
ID
37
21 12
5 41 27 31
47 16 35 4 19
ID
Each element x has an associated value called its key - x.key
Previously we said that. . .
Each element x also has an associated (unique) positive integer ID - x.ID N
New (more reasonable) Assumption
Lookup table L:
N
L[i] stores a pointer
to the location of x
with x.ID = i
L[41]
Whenever we move an element
we update L in O(1) time
142. That pesky assumption. . .
Old Assumption we can find the location of any element x in the Heap in O(1) time
ID
37
21 12
5 41 27 31
47 16 35 4 19
ID
Each element x has an associated value called its key - x.key
Previously we said that. . .
Each element x also has an associated (unique) positive integer ID - x.ID N
New (more reasonable) Assumption
Lookup table L:
N
L[i] stores a pointer
to the location of x
with x.ID = i
L[41]
Finding element x takes O(1) time as required
Whenever we move an element
we update L in O(1) time
143. Priority queue Summary
We have seen three different priority queue implementations
each supporting the following operations:
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
Sorted Linked List
Binary Heap
Unsorted Linked List
INSERT DECREASEKEY EXTRACTMIN
O(log n) O(log n)
O(1) O(n) O(n)
O(n) O(n) O(1)
O(log n)
SPACE
O(n)
O(n)
O(N)
144. Priority queue Summary
We have seen three different priority queue implementations
each supporting the following operations:
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
Sorted Linked List
Binary Heap
Unsorted Linked List
INSERT DECREASEKEY EXTRACTMIN
O(log n) O(log n)
O(1) O(n) O(n)
O(n) O(n) O(1)
O(log n)
SPACE
O(n)
O(n)
O(N)
Spoiler: for the shortest path problem, N = O(n)
145. Priority queue Summary
We have seen three different priority queue implementations
each supporting the following operations:
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
Sorted Linked List
Binary Heap
Unsorted Linked List
INSERT DECREASEKEY EXTRACTMIN
O(log n) O(log n)
O(1) O(n) O(n)
O(n) O(n) O(1)
O(log n)
SPACE
O(n)
O(n)
O(N)
146. Priority queue Summary
We have seen three different priority queue implementations
each supporting the following operations:
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
Sorted Linked List
Binary Heap
Unsorted Linked List
INSERT DECREASEKEY EXTRACTMIN
O(log n) O(log n)
O(1) O(n) O(n)
O(n) O(n) O(1)
O(log n)
Is this the best possible?
SPACE
O(n)
O(n)
O(N)
147. Priority queue Summary
We have seen three different priority queue implementations
each supporting the following operations:
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
Sorted Linked List
Binary Heap
Unsorted Linked List
INSERT DECREASEKEY EXTRACTMIN
O(log n) O(log n)
O(1) O(n) O(n)
O(n) O(n) O(1)
O(log n)
Is this the best possible? actually, no :)
SPACE
O(n)
O(n)
O(N)
148. Priority queue Summary
We have seen three different priority queue implementations
each supporting the following operations:
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
Sorted Linked List
Binary Heap
Unsorted Linked List
INSERT DECREASEKEY EXTRACTMIN
O(log n) O(log n)
O(1) O(n) O(n)
O(n) O(n) O(1)
O(log n)
Is this the best possible? actually, no :)
Fibonacci Heap O(1) O(1) O(log n)
SPACE
O(n)
O(n)
O(N)
O(n)
149. Priority queue Summary
We have seen three different priority queue implementations
each supporting the following operations:
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
Sorted Linked List
Binary Heap
Unsorted Linked List
INSERT DECREASEKEY EXTRACTMIN
O(log n) O(log n)
O(1) O(n) O(n)
O(n) O(n) O(1)
O(log n)
Is this the best possible? actually, no :)
. . . but Fibonacci Heaps are complicated, amortised and have large hidden constants
Fibonacci Heap O(1) O(1) O(log n)
SPACE
O(n)
O(n)
O(N)
O(n)
150. One more thing. . .
Take an array of elements of length n
A
n
INSERT every element into a priority queue:
EXTRACTMIN from the priority queue n times
and put the elements in A in the order they come out
A
n
what is A ?
PRIORITY
QUEUE
151. One more thing. . .
Take an array of elements of length n
A
n
INSERT every element into a priority queue:
EXTRACTMIN from the priority queue n times
and put the elements in A in the order they come out
A
n
what is A ?
PRIORITY
QUEUE
it’s A in sorted order
152. One more thing. . .
Take an array of elements of length n
A
n
INSERT every element into a priority queue:
EXTRACTMIN from the priority queue n times
and put the elements in A in the order they come out
A
n
what is A ?
PRIORITY
QUEUE
it’s A in sorted order
If you implement the priority
queue as a Binary Heap
You can use this to sort in
O(n log n) time
153. HeapSort
Take an array of elements of length n
A
n
INSERT every element into a priority queue:
EXTRACTMIN from the priority queue n times
and put the elements in A in the order they come out
A
n
what is A ?
PRIORITY
QUEUE
it’s A in sorted order
If you implement the priority
queue as a Binary Heap
You can use this to sort in
O(n log n) time
156. In today’s lectures we’ll be discussing the single source shortest paths problem
In particular we’ll be interested in Dijkstra’s Algorithm
in a weighted, directed graph. . .
which is based on an abstract data structure called a priority queue
The shortest path from MVB to Temple Meads
(according to Google Maps)
. . . which can be efficiently implemented as a binary heap
157. In today’s lectures we’ll be discussing the single source shortest paths problem
In particular we’ll be interested in Dijkstra’s Algorithm
in a weighted, directed graph. . .
which is based on an abstract data structure called a priority queue
The shortest path from MVB to Temple Meads
(according to Google Maps)
Vertices are junctions
Edges are roads
Edge weights are in miles
Directed edges
are one-way streets
. . . which can be efficiently implemented as a binary heap
158. Post-lunch Priority Queue refresher
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
A priority queue supports the following operations:
Q
EXTRACTMIN() - removes and returns the element with the smallest key
INSERT(x, k) - inserts x with x.key = k
DECREASEKEY(x, k) - decreases the value of x.key to k
where k < x.key
(ties are broken arbitrarily)
159. Single source shortest paths
in a weighted, directed graph. . .
A
B E
D
C F
G
21
2
1
4 2 4
5
1
2
1
Dijkstra’s Algorithm solves the single source shortest paths problem
160. Single source shortest paths
in a weighted, directed graph. . .
A
B E
D
C F
G
21
2
1
4 2 4
5
1
2
1
Dijkstra’s Algorithm solves the single source shortest paths problem
It finds the shortest path from
a given source vertex
to every other vertex
161. Single source shortest paths
in a weighted, directed graph. . .
A
B E
D
C F
G
21
2
1
4 2 4
5
1
2
1
Dijkstra’s Algorithm solves the single source shortest paths problem
It finds the shortest path from
a given source vertex
The weights have to be non-negative
to every other vertex
162. Single source shortest paths
in a weighted, directed graph. . .
A
B E
D
C F
G
21
2
1
4 2 4
5
1
2
1
Dijkstra’s Algorithm solves the single source shortest paths problem
It finds the shortest path from
a given source vertex
The weights have to be non-negative
to every other vertex
The graph is stored as an Adjacency List
163. Single source shortest paths
in a weighted, directed graph. . .
A
B E
D
C F
G
21
2
1
4 2 4
5
1
2
1
Dijkstra’s Algorithm solves the single source shortest paths problem
It finds the shortest path from
a given source vertex
The weights have to be non-negative
to every other vertex
The time complexity will depend on
how efficient the priority queue used is
The graph is stored as an Adjacency List
164. Single source shortest paths
in a weighted, directed graph. . .
A
B E
D
C F
G
21
2
1
4 2 4
5
1
2
1
Dijkstra’s Algorithm solves the single source shortest paths problem
It finds the shortest path from
a given source vertex
The weights have to be non-negative
to every other vertex
The time complexity will depend on
how efficient the priority queue used is
Remember from Monday’s lecture that in unweighted, directed graphs,
Breadth First Search solves this problem in O(|V | + |E|) time
|V | is the number of vertices and |E| is the number of edges
The graph is stored as an Adjacency List
165. Dijkstra’s algorithm
DIJKSTRA(s)
We assume that we have a priority queue, supporting
for each vertex v, dist(v) is the distance between s and v
Claim when Dijkstra’s algorithm terminates,
INSERT,DECREASEKEY and EXTRACTMIN
weight(u, v) is the weight of
the edge from u to v
(u, v) ∈ E iff there is an
edge from u to v
dist(v) is the length of the best
path between s and v, found so far
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
166. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist:
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
A
1
B
167. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
1
B
∞
168. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
∞
169. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
∞
new path to B = 0 + 1 = 1
170. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1 ∞
new path to B = 0 + 1 = 1
171. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1 ∞
new path to B = 0 + 1 = 1
this is called relaxing edge (u, v)
172. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1 ∞
this is called relaxing edge (u, v)
173. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1 ∞
this is called relaxing edge (u, v)
174. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1
settled vertices
∞
this is called relaxing edge (u, v)
175. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1
settled vertices
∞
this is called relaxing edge (u, v)
176. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1
settled vertices
∞
new path to C = 1 + 2 = 3
this is called relaxing edge (u, v)
177. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1
settled vertices
∞
new path to C = 1 + 2 = 3
3
this is called relaxing edge (u, v)
178. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1
settled vertices
∞3
new path to D = 1 + 4 = 5
this is called relaxing edge (u, v)
179. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1
settled vertices
∞ 53
new path to D = 1 + 4 = 5
this is called relaxing edge (u, v)
180. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1 ∞ 53
this is called relaxing edge (u, v)
181. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1 ∞ 533
C
this is called relaxing edge (u, v)
182. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1 ∞ 533
C
this is called relaxing edge (u, v)
183. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1 ∞ 533
C
new path to D = 3 + 1 = 4
this is called relaxing edge (u, v)
184. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1 ∞ 533
C
new path to D = 3 + 1 = 4
4
this is called relaxing edge (u, v)
185. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1 ∞ 533
C
4
new path to G = 3 + 5 = 8
this is called relaxing edge (u, v)
186. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1 ∞ 533
C
4 8
new path to G = 3 + 5 = 8
this is called relaxing edge (u, v)
187. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1 ∞ 533
C
4 8
this is called relaxing edge (u, v)
188. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1 ∞ 533
C
4 8
D
4
this is called relaxing edge (u, v)
189. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1 ∞ 533
C
4 8
D
4
this is called relaxing edge (u, v)
190. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1 ∞ 533
C
4 8
D
4
new path to A = 0 + 2 = 2
this is called relaxing edge (u, v)
191. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1 ∞ 533
C
4 8
D
4
new path to E = 4 + 2 = 6
this is called relaxing edge (u, v)
192. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1 ∞ 533
C
4 8
D
4
new path to E = 4 + 2 = 6
6
this is called relaxing edge (u, v)
193. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1 ∞ 533
C
4 8
D
4 6
new path to F = 4 + 2 = 6
this is called relaxing edge (u, v)
194. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1 ∞ 533
C
4 8
D
4 6 6
new path to F = 4 + 2 = 6
this is called relaxing edge (u, v)
195. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1 ∞ 533
C
4 8
D
4 6 6
this is called relaxing edge (u, v)
196. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1 ∞ 533
C
4 8
D
4 6 6
E
6
this is called relaxing edge (u, v)
197. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1 ∞ 533
C
4 8
D
4 6 6
E
6
this is called relaxing edge (u, v)
198. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1 ∞ 533
C
4 8
D
4 6 6
E
6
new path to B = 6 + 1 = 7
this is called relaxing edge (u, v)
199. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1 ∞ 533
C
4 8
D
4 6 6
E
6
new path to G = 6 + 4 = 10
this is called relaxing edge (u, v)
200. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1 ∞ 533
C
4 8
D
4 6 6
E
6
this is called relaxing edge (u, v)
201. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1 ∞ 533
C
4 8
D
4 6 6
E
6
F
6
this is called relaxing edge (u, v)
202. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1 ∞ 533
C
4 8
D
4 6 6
E
6
F
6
this is called relaxing edge (u, v)
203. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1 ∞ 533
C
4 8
D
4 6 6
E
6
F
6
new path to G = 6 + 1 = 7
this is called relaxing edge (u, v)
204. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1 ∞ 533
C
4 8
D
4 6 6
E
6
F
6 7
new path to G = 6 + 1 = 7
this is called relaxing edge (u, v)
205. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1 ∞ 533
C
4 8
D
4 6 6
E
6
F
6 7
this is called relaxing edge (u, v)
206. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1 ∞ 533
C
4 8
D
4 6 6
E
6
F
6 7
G
7
this is called relaxing edge (u, v)
207. E
D
C F
G
2
2
1
4 2 4
5
1
2
1
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
We’re going to simulate
DIJKSTRA(A)
i.e. s = A
A B C D E F G
dist: ∞
at all times, for each vertex v,
v.key = dist(v)
dist(v) is the length of the shortest
path between s and v, found so far
∞ ∞ ∞ ∞ ∞0
A
0
A
1
B
1
B
1 ∞ 533
C
4 8
D
4 6 6
E
6
F
6 7
G
7
shortest paths from s = A:
this is called relaxing edge (u, v)
208. Proof of Correctness
for each vertex v, dist(v) = δ(s, v)Claim when Dijkstra’s algorithm terminates,
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
where δ(s, v) is the true distance between s and v
209. Proof of Correctness
for each vertex v, dist(v) = δ(s, v)Claim when Dijkstra’s algorithm terminates,
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
where δ(s, v) is the true distance between s and v
Observation At all times, dist(v) is the length of some path from s to v
(unless dist(v) = ∞)
Therefore, for each vertex v, δ(s, v) dist(v)
210. Proof of Correctness
for each vertex v, dist(v) = δ(s, v)Claim when Dijkstra’s algorithm terminates,
For all v, set dist(v) = ∞
set dist(s) = 0
For each v, Do Insert(v, dist(v))
While the queue is not empty
u = ExtractMin()
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
DecreaseKey(v, dist(v))
DIJKSTRA(s)
where δ(s, v) is the true distance between s and v