Title: Heaps
1Heaps
2Motivation
- Dijkstras algorithm for single source shortest
path - Prims algorithm for minimum spanning trees
3Motivation
- Want to find the shortest route from New York to
San Francisco - Model the road-map with a graph
4A Graph G(V,E)
V is a set of vertices
E is a set of edges (pairs of vertices)
5Model driving distances by weights on the edges
V is a set of vertices
E is a set of edges (pairs of vertices)
6Source and destination
V is a set of vertices
E is a set of edges (pairs of vertices)
7Dijkstras algorithm
- Assume all weights are non-negative
- Finds the shortest path from some fixed vertex s
to every other vertex
8Example
s4
8
15
s3
2
10
s
s2
3
4
5
1
s1
9Example
s4
8
Maintain an upper bound d(v) on the shortest path
to v
8
8
15
s3
2
10
s
s2
3
4
8
0
5
1
s1
8
10s4
8
Maintain an upper bound d(v) on the shortest path
to v
8
8
15
s3
2
A node is either scanned (in S) or labeled (in Q)
10
s
s2
3
4
8
5
0
1
s1
Initially S ? and Q V
8
11Initially S ? and Q V
s4
8
Pick a vertex v in Q with minimum d(v) and add it
to S
8
8
15
s3
2
10
s
s2
3
4
8
5
0
1
s1
8
12Initially S ? and Q V
s4
8
Pick a vertex v in Q with minimum d(v) and add it
to S
8
8
15
s3
2
w
10
s
s2
3
4
15
8
v
5
0
1
s1
8
For every edge (v,w) where w in Q relax(v,w)
13Relax(v,w) If d(v) w(v,w) lt d(w) then d(w) ?
d(v) w(v,w) p(w) ? v
s4
8
8
8
15
s3
2
w
10
s
s2
3
4
15
8
v
5
0
1
s1
8
For every edge (v,w) where w in Q relax(v,w)
14S s
s4
8
8
8
15
s3
2
10
Relax(s,s4)
s
s2
3
4
8
5
0
1
s1
8
15S s
s4
15
8
8
15
s3
2
10
Relax(s,s4)
s
s2
3
4
8
5
0
1
s1
8
16S s
s4
15
8
8
15
s3
2
10
Relax(s,s4)
s
s2
3
4
Relax(s,s3)
8
5
0
1
s1
8
17S s
s4
15
10
8
15
s3
2
10
Relax(s,s4)
s
s2
3
4
Relax(s,s3)
8
5
0
1
s1
8
18S s
s4
15
10
8
15
s3
2
10
Relax(s,s4)
s
s2
3
4
Relax(s,s3)
8
5
0
1
s1
Relax(s,s1)
8
19S s
s4
15
10
8
15
s3
2
10
Relax(s,s4)
s
s2
3
4
Relax(s,s3)
8
5
0
1
s1
Relax(s,s1)
5
20S s
s4
15
Pick a vertex v in Q with minimum d(v) and add it
to S
10
8
15
s3
2
10
s
s2
3
4
8
5
0
1
s1
5
21S s,s1
s4
15
Pick a vertex v in Q with minimum d(v) and add it
to S
10
8
15
s3
2
10
s
s2
3
4
8
5
0
1
s1
5
22S s,s1
s4
15
Pick a vertex v in Q with minimum d(v) and add it
to S
10
8
15
s3
2
10
Relax(s1,s3)
s
s2
3
4
8
5
0
1
s1
5
23S s,s1
s4
15
Pick a vertex v in Q with minimum d(v) and add it
to S
9
8
15
s3
2
10
Relax(s1,s3)
s
s2
3
4
8
5
0
1
s1
5
24S s,s1
s4
15
Pick a vertex v in Q with minimum d(v) and add it
to S
9
8
15
s3
2
10
Relax(s1,s3)
s
s2
3
4
Relax(s1,s2)
8
5
0
1
s1
5
25S s,s1
s4
15
Pick a vertex v in Q with minimum d(v) and add it
to S
9
8
15
s3
2
10
Relax(s1,s3)
s
s2
3
4
Relax(s1,s2)
6
5
0
1
s1
5
26S s,s1
s4
15
Pick a vertex v in Q with minimum d(v) and add it
to S
9
8
15
s3
2
10
s
s2
3
4
6
5
0
1
s1
5
27S s,s1,s2
s4
15
Pick a vertex v in Q with minimum d(v) and add it
to S
9
8
15
s3
2
10
s
s2
3
4
6
5
0
1
s1
5
28S s,s1,s2
s4
15
Pick a vertex v in Q with minimum d(v) and add it
to S
9
8
15
s3
2
10
Relax(s2,s3)
s
s2
3
4
6
5
0
1
s1
5
29S s,s1,s2
s4
15
Pick a vertex v in Q with minimum d(v) and add it
to S
8
8
15
s3
2
10
Relax(s2,s3)
s
s2
3
4
6
5
0
1
s1
5
30S s,s1,s2
s4
15
Pick a vertex v in Q with minimum d(v) and add it
to S
8
8
15
s3
2
10
s
s2
3
4
6
5
0
1
s1
5
31S s,s1,s2,s3
s4
15
Pick a vertex v in Q with minimum d(v) and add it
to S
8
8
15
s3
2
10
s
s2
3
4
6
5
0
1
s1
5
32S s,s1,s2,s3,s4
s4
15
Pick a vertex v in Q with minimum d(v) and add it
to S
8
8
15
s3
2
10
s
s2
3
4
6
5
0
1
s1
5
33S s,s1,s2,s3,s4
s4
15
When Q ? then the d() values are the distances
from s
8
8
15
s3
2
10
The p function gives the shortest path tree
s
s2
3
4
6
5
0
1
s1
5
34S s,s1,s2,s3,s4
s4
15
When Q ? then the d() values are the distances
from s
8
8
15
s3
2
10
The p function gives the shortest path tree
s
s2
3
4
6
5
0
1
s1
5
35Implementation of Dijkstras algorithm
- We need to find efficiently the vertex with
minimum d() in Q - We need to update d() values of vertices in Q
36Required ADT
- Maintain items with keys subject to
- Insert(x,Q)
- min(Q)
- Deletemin(Q)
- Decrease-key(x,Q,?)
37Required ADT
- Insert(x,Q)
- min(Q)
- Deletemin(Q)
- Decrease-key(x,Q,?) Can simulate by
- Delete(x,Q), insert(x-?,Q)
38How many times we do these operations ?
- Insert(x,Q)
- min(Q)
- Deletemin(Q)
- Decrease-key(x,Q,?) Can simulate by
- Delete(x,Q), insert(x-?,Q)
n V
n
n
m E
39Do we know an algorithm for this ADT ?
- Insert(x,Q)
- min(Q)
- Deletemin(Q)
- Decrease-key(x,Q,?) Can simulate by
- Delete(x,Q), insert(x-?,Q)
Yes! Use binary search trees, we had insert and
delete and also min in the dictionary data type
40Heap
- Heap is
- A complete binary tree
- The items at the children of v are greater than
the one at v
7
23
17
19
26
29
24
33
65
79
40
Heap-ordered tree
41Array Representation
- Representing a heap with an array
- Get the elements from top to bottom, from left to
right
7
23
17
19
26
29
24
33
65
79
40
7
23
17
24
29
26
19
65
33
40
79
Q
42Array Representation
- Left(i) 2i1
- Right(i) 2i2
- Parent(i) ?(i-1)/2?
7
23
17
19
26
29
24
33
65
79
40
7
23
17
24
29
26
19
65
33
40
79
Q
43Find the minimum
7
23
17
19
26
29
24
33
65
79
40
7
23
17
24
29
26
19
65
33
40
79
Q
44Delete the minimum
7
23
17
19
26
29
24
33
65
79
40
7
23
17
24
29
26
19
65
33
40
79
Q
45Delete the minimum
23
17
19
26
29
24
33
65
79
40
23
17
24
29
26
19
65
33
40
79
Q
46Delete the minimum
79
23
17
19
26
29
24
33
65
40
79
23
17
24
29
26
19
65
33
40
Q
47Delete the minimum
17
23
79
19
26
29
24
33
65
40
17
23
79
24
29
26
19
65
33
40
Q
48Delete the minimum
17
23
19
79
26
29
24
33
65
40
17
23
19
24
29
26
79
65
33
40
Q
49Delete the minimum
Q0 ? Qsize(Q)-1
size(Q) ? size(Q)-1
Heapify-down(Q,0)
17
23
19
79
26
29
24
33
65
40
17
23
19
24
29
26
79
65
33
40
Q
50Heapify-down(Q,i)
- Heapify-down(Q, i)
- l ? left(i)
- r ? right(i)
- smallest ? i
- if l lt size(Q) and Ql lt Qsmallest
- then smallest ? l
- if r lt size(Q) and Qr lt Qsmallest
- then smallest ? r
- if smallest gt i then Qi ? Qsmallest
- Heapify-down(Q,
smallest)
51Inserting an item
Insert(15,Q)
17
23
19
79
26
29
24
33
65
40
17
23
19
24
29
26
79
65
33
40
Q
52Inserting an item
Insert(15,Q)
Qsize(Q) ? 15
Heapify-up(Q,size(Q))
size(Q) ? size(Q) 1
17
23
19
79
26
29
24
33
65
40
15
17
23
19
24
29
26
79
65
33
40
15
Q
53Inserting an item
17
23
19
79
26
15
24
33
65
40
29
17
23
19
24
15
26
79
65
33
40
29
Q
54Inserting an item
17
15
19
79
26
23
24
33
65
40
29
17
15
19
24
23
26
79
65
33
40
29
Q
55Inserting an item
15
17
19
79
26
23
24
33
65
40
29
15
17
19
24
23
26
79
65
33
40
29
Q
56Heapify-up
- Heapify-up(Q, i)
- while i gt 0 and Qi lt Qparent(i)
- do Qi ? Qparent(i)
- i ? parent(i)
15
17
19
79
26
23
24
33
65
40
29
15
17
19
24
23
26
79
65
33
40
29
Q
57Other operations
- Decrease-key(x,Q,?)
- Delete(x,Q)
- Can implement them easily using heapify
58Heapsort (Williams, Floyd, 1964)
- Put the elements in an array
- Make the array into a heap
- Do a deletemin and put the deleted element at the
last position of the array
59Put the elements in the heap
79
65
26
29
15
19
24
33
23
7
40
79
65
26
24
19
15
29
23
33
40
7
Q
60Make the elements into a heap
79
65
26
29
15
19
24
33
23
7
40
79
65
26
24
19
15
29
23
33
40
7
Q
61Make the elements into a heap
Heapify-down(Q,4)
79
65
26
29
15
19
24
33
23
7
40
79
65
26
24
19
15
29
23
33
40
7
Q
62Heapify-down(Q,4)
79
65
26
29
15
7
24
33
23
19
40
79
65
26
24
7
15
29
23
33
40
19
Q
63Heapify-down(Q,3)
79
65
26
29
15
7
24
33
23
19
40
79
65
26
24
7
15
29
23
33
40
19
Q
64Heapify-down(Q,3)
79
65
26
29
15
7
23
33
24
19
40
79
65
26
23
7
15
29
24
33
40
19
Q
65Heapify-down(Q,2)
79
65
26
29
15
7
23
33
24
19
40
79
65
26
23
7
15
29
24
33
40
19
Q
66Heapify-down(Q,2)
79
65
15
29
26
7
23
33
24
19
40
79
65
15
23
7
26
29
24
33
40
19
Q
67Heapify-down(Q,1)
79
65
15
29
26
7
23
33
24
19
40
79
65
15
23
7
26
29
24
33
40
19
Q
68Heapify-down(Q,1)
79
7
15
29
26
65
23
33
24
19
40
79
7
15
23
65
26
29
24
33
40
19
Q
69Heapify-down(Q,1)
79
7
15
29
26
19
23
33
24
65
40
79
7
15
23
19
26
29
24
33
40
65
Q
70Heapify-down(Q,0)
79
7
15
29
26
19
23
33
24
65
40
79
7
15
23
19
26
29
24
33
40
65
Q
71Heapify-down(Q,0)
7
79
15
29
26
19
23
33
24
65
40
7
79
15
23
19
26
29
24
33
40
65
Q
72Heapify-down(Q,0)
7
19
15
29
26
79
23
33
24
65
40
7
19
15
23
79
26
29
24
33
40
65
Q
73Heapify-down(Q,0)
7
19
15
29
26
40
23
33
24
65
79
7
19
15
23
40
26
29
24
33
79
65
Q
74How much time does it take to build the heap this
way ?
7
23
15
26
19
29
24
33
79
65
40
We have at most n/2 nodes heapified at height 1
We have at most n/4 nodes heapified at height 2
We have at most n/8 nodes heapified at height 3
75Summary
- We can build the heap in linear time
- We still have to deletemin the elements one by
one in order to sort that will take O(nlog(n))