SlideShare a Scribd company logo
1 of 276
Download to read offline
Data Structures and Algorithms – COMS21103
Single Source Shortest Paths
Priority Queues and Dijkstra’s Algorithm
Benjamin Sach
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
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
Part one
Priority Queues
Part one
Priority Queues
(you can forget all about graphs for the whole of part one)
Priority Queues
A priority queue, stores a set of distinct elements
Each element x has an associated value called its key - x.key
Q
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
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
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
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)
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)
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)
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)
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)
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()
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()
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)
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)
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)
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)
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()
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()
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)
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)
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)
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)
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)
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)
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)
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()
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()
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)
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)
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)
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)
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()
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()
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)
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()
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()
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)
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()
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()
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)
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)
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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,
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,
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,
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,
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,
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,
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,
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,
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
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
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
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
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.
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.
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.
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.
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:
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
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
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
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
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
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
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
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
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
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
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
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
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 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
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
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
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
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
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!
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
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
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
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
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
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)
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)
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)
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)
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 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)
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)
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
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
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
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
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
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
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
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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?
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?
That pesky assumption. . .
That pesky assumption. . .
Old Assumption we can find the location of any element x in the Heap in O(1) time
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. . .
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
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
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
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
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]
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
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
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)
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)
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)
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)
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)
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)
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)
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
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
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
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
End of part one
Part two
Dijkstra’s Algorithm
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
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
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)
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
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
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
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
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
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
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))
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
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
∞
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
∞
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
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
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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
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)
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
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm

More Related Content

Similar to Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm

Similar to Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm (20)

Data Structures In Scala
Data Structures In ScalaData Structures In Scala
Data Structures In Scala
 
Data structures in scala
Data structures in scalaData structures in scala
Data structures in scala
 
Priority queues
Priority queuesPriority queues
Priority queues
 
03-data-structures.pdf
03-data-structures.pdf03-data-structures.pdf
03-data-structures.pdf
 
Queue
QueueQueue
Queue
 
Queue
QueueQueue
Queue
 
4. Queues in Data Structure
4. Queues in Data Structure4. Queues in Data Structure
4. Queues in Data Structure
 
Soft Heaps
Soft HeapsSoft Heaps
Soft Heaps
 
purrr.pdf
purrr.pdfpurrr.pdf
purrr.pdf
 
dictionary14 ppt FINAL.pptx
dictionary14 ppt FINAL.pptxdictionary14 ppt FINAL.pptx
dictionary14 ppt FINAL.pptx
 
Address calculation-sort
Address calculation-sortAddress calculation-sort
Address calculation-sort
 
Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii  dfs u-2 linklist,stack,queueBsc cs ii  dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queue
 
lecture 11
lecture 11lecture 11
lecture 11
 
Hashing Algorithm
Hashing AlgorithmHashing Algorithm
Hashing Algorithm
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queue
 
Ch03_stacks_and_queues.ppt
Ch03_stacks_and_queues.pptCh03_stacks_and_queues.ppt
Ch03_stacks_and_queues.ppt
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
 
Queues.ppt
Queues.pptQueues.ppt
Queues.ppt
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
 
2.DS Array
2.DS Array2.DS Array
2.DS Array
 

More from Benjamin Sach

Approximation Algorithms Part Four: APTAS
Approximation Algorithms Part Four: APTASApproximation Algorithms Part Four: APTAS
Approximation Algorithms Part Four: APTASBenjamin Sach
 
Approximation Algorithms Part Three: (F)PTAS
Approximation Algorithms Part Three: (F)PTASApproximation Algorithms Part Three: (F)PTAS
Approximation Algorithms Part Three: (F)PTASBenjamin Sach
 
Approximation Algorithms Part Two: More Constant factor approximations
Approximation Algorithms Part Two: More Constant factor approximationsApproximation Algorithms Part Two: More Constant factor approximations
Approximation Algorithms Part Two: More Constant factor approximationsBenjamin Sach
 
Approximation Algorithms Part One: Constant factor approximations
Approximation Algorithms Part One: Constant factor approximationsApproximation Algorithms Part One: Constant factor approximations
Approximation Algorithms Part One: Constant factor approximationsBenjamin Sach
 
Orthogonal Range Searching
Orthogonal Range SearchingOrthogonal Range Searching
Orthogonal Range SearchingBenjamin Sach
 
Pattern Matching Part Two: k-mismatches
Pattern Matching Part Two: k-mismatchesPattern Matching Part Two: k-mismatches
Pattern Matching Part Two: k-mismatchesBenjamin Sach
 
Pattern Matching Part Three: Hamming Distance
Pattern Matching Part Three: Hamming DistancePattern Matching Part Three: Hamming Distance
Pattern Matching Part Three: Hamming DistanceBenjamin Sach
 
Lowest Common Ancestor
Lowest Common AncestorLowest Common Ancestor
Lowest Common AncestorBenjamin Sach
 
Range Minimum Queries
Range Minimum QueriesRange Minimum Queries
Range Minimum QueriesBenjamin Sach
 
Pattern Matching Part Two: Suffix Arrays
Pattern Matching Part Two: Suffix ArraysPattern Matching Part Two: Suffix Arrays
Pattern Matching Part Two: Suffix ArraysBenjamin Sach
 
Pattern Matching Part One: Suffix Trees
Pattern Matching Part One: Suffix TreesPattern Matching Part One: Suffix Trees
Pattern Matching Part One: Suffix TreesBenjamin Sach
 
Hashing Part Two: Cuckoo Hashing
Hashing Part Two: Cuckoo HashingHashing Part Two: Cuckoo Hashing
Hashing Part Two: Cuckoo HashingBenjamin Sach
 
Hashing Part Two: Static Perfect Hashing
Hashing Part Two: Static Perfect HashingHashing Part Two: Static Perfect Hashing
Hashing Part Two: Static Perfect HashingBenjamin Sach
 
Minimum Spanning Trees (via Disjoint Sets)
Minimum Spanning Trees (via Disjoint Sets)Minimum Spanning Trees (via Disjoint Sets)
Minimum Spanning Trees (via Disjoint Sets)Benjamin Sach
 
Depth First Search and Breadth First Search
Depth First Search and Breadth First SearchDepth First Search and Breadth First Search
Depth First Search and Breadth First SearchBenjamin Sach
 

More from Benjamin Sach (20)

Approximation Algorithms Part Four: APTAS
Approximation Algorithms Part Four: APTASApproximation Algorithms Part Four: APTAS
Approximation Algorithms Part Four: APTAS
 
Approximation Algorithms Part Three: (F)PTAS
Approximation Algorithms Part Three: (F)PTASApproximation Algorithms Part Three: (F)PTAS
Approximation Algorithms Part Three: (F)PTAS
 
Approximation Algorithms Part Two: More Constant factor approximations
Approximation Algorithms Part Two: More Constant factor approximationsApproximation Algorithms Part Two: More Constant factor approximations
Approximation Algorithms Part Two: More Constant factor approximations
 
Approximation Algorithms Part One: Constant factor approximations
Approximation Algorithms Part One: Constant factor approximationsApproximation Algorithms Part One: Constant factor approximations
Approximation Algorithms Part One: Constant factor approximations
 
van Emde Boas trees
van Emde Boas treesvan Emde Boas trees
van Emde Boas trees
 
Orthogonal Range Searching
Orthogonal Range SearchingOrthogonal Range Searching
Orthogonal Range Searching
 
Pattern Matching Part Two: k-mismatches
Pattern Matching Part Two: k-mismatchesPattern Matching Part Two: k-mismatches
Pattern Matching Part Two: k-mismatches
 
Pattern Matching Part Three: Hamming Distance
Pattern Matching Part Three: Hamming DistancePattern Matching Part Three: Hamming Distance
Pattern Matching Part Three: Hamming Distance
 
Lowest Common Ancestor
Lowest Common AncestorLowest Common Ancestor
Lowest Common Ancestor
 
Range Minimum Queries
Range Minimum QueriesRange Minimum Queries
Range Minimum Queries
 
Pattern Matching Part Two: Suffix Arrays
Pattern Matching Part Two: Suffix ArraysPattern Matching Part Two: Suffix Arrays
Pattern Matching Part Two: Suffix Arrays
 
Pattern Matching Part One: Suffix Trees
Pattern Matching Part One: Suffix TreesPattern Matching Part One: Suffix Trees
Pattern Matching Part One: Suffix Trees
 
Hashing Part Two: Cuckoo Hashing
Hashing Part Two: Cuckoo HashingHashing Part Two: Cuckoo Hashing
Hashing Part Two: Cuckoo Hashing
 
Hashing Part Two: Static Perfect Hashing
Hashing Part Two: Static Perfect HashingHashing Part Two: Static Perfect Hashing
Hashing Part Two: Static Perfect Hashing
 
Hashing Part One
Hashing Part OneHashing Part One
Hashing Part One
 
Probability Recap
Probability RecapProbability Recap
Probability Recap
 
Bloom Filters
Bloom FiltersBloom Filters
Bloom Filters
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
Minimum Spanning Trees (via Disjoint Sets)
Minimum Spanning Trees (via Disjoint Sets)Minimum Spanning Trees (via Disjoint Sets)
Minimum Spanning Trees (via Disjoint Sets)
 
Depth First Search and Breadth First Search
Depth First Search and Breadth First SearchDepth First Search and Breadth First Search
Depth First Search and Breadth First Search
 

Recently uploaded

Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 

Recently uploaded (20)

Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 

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
  • 5. Part one Priority Queues (you can forget all about graphs for the whole of part one)
  • 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
  • 154. End of part one
  • 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