1.204 Lecture 8 Data structures: heaps Priority Queues or Heaps

advertisement
1.204 Lecture 8
Data structures: heaps
Priority Queues or Heaps
a
Top
• Highest priority element at top
• “Partial sort”
• All enter at bottom, leave at top
b
c
d
e
Bottom
Applications:
1. Simulations: event list
2. Search, decision trees
3 Mi
3.
Minimum
i
spanning
i tree
t
4. Shortest path (label setting)
5. And many others…
Complexity:
1. Insertion, deletion: O(lg n)
1
Min Heap Modeled as Binary Tree
c
d
e
q
f
t
v
a
Min Heap Modeled as Binary Tree
c
d
a
e
e
q
f
t
v
a
2
Min Heap Modeled as Binary Tree
c
a
d
ad e
e
q
f
t
v
a
Min Heap Modeled as Binary Tree
a
c
ac d
ad e
e
q
f
t
v
a
3
Heap: constructors
public class Heap {
// Max heap: largest element at top
private Comparable[] data;
private int size;
;
// Actual number of elements in heap
p
private int capacity;
private static final int DEFAULT_CAPACITY= 30;
public Heap(int capacity) {
data = new Comparable[capacity];
this.capacity= capacity;
}
publi
blic Heap()
() {
{
this(DEFAULT_CAPACITY);
}
public Heap(Comparable[] c) {
data= c;
heapify(data);
capacity= size= data.length;
}
(Max) Heap insertion
(a)
35
40
40
80
(b)
80
40
80
80
40
40
(c)
90
90
80
35
45
40
40
90
(d)
(e)
90
90
35
80
50
80
50
45
40
35
80
35
35
45
40
(f)
90
90
40
45
35
70
80
50
80
40
70
45
35
50
(g)
Figure by MIT OpenCourseWare.
4
Heap: insert()
public void insert(Comparable item) {
if (size == 0) { // Empty heap, first element being added
size=
i
1;
1
data[0]= item;
} else {
if (size == data.length)
grow();
int i = size++; // Increase no of elements
while (i > 0 && (data[(i-1)/2].compareTo(item) < 0)) {
data[ ] = data[(i-1)/2];
data[i]
data[(
)/ ]; // Move
o e parent
pa e t item
te down
do
i = (i-1)/2;
// Go up one level in heap
}
data[i] = item; // Drop item into correct place in heap
}
}
// See download for grow() code
(Max) Heap deletion
90
80
40
70
45
35
80
50
40
80
50
40
70
45
35
35
50
35
80
50
40
50
70
45
80
90
50
45
35
80
40
70
45
70
70
50
40
50
35
35
70
35
45
5
Heap: delete()
public Comparable delete() throws NoSuchElementException {
if (size == 0)
throw new NoSuchElementException();
Comparable retValue = data[0];
// Top removed and returned
// Put last element at top (element 0) and bubble it down
Comparable item = data[0] = data[--size];
int j = 1;
// Look at right and left children of top node
while (j < size) {
// Compare left and right child and let j be larger child
if ((j+1 < size) && (data[j].compareTo(data[j + 1]) < 0))
j++;
if (item.compareTo(data[j]) > 0) break;
// Position for item found
data[(j-1) / 2] = data[j];
// Else put in parent node
j = 2*j+1;
// Move down to next level of heap
}
data[(j-1) / 2] = item;
return retValue;
// Drop last element in place
}
(Max) Heapify
100
100
119
171
118
112
151
132
171
118
112
(a)
(b)
100
171
171
119
151
119
119
151
112
118
(c)
132
100
132
151
112
118
132
(d)
Figure by MIT OpenCourseWare.
6
Heap: heapify()
private static void heapify(Comparable[] c) {
Comparable item;
i
int
size=
i
c.length;
l
h
for (int i= size/2 - 1; i >= 0; i--) { // Start at mid-tree node
int j= 2*i + 1;
// Left child
item= c[i];
while (j < size) {
// While loop same as delete()
// Compare left and right child and let j be larger child
if ((j+1 < size) && (c[j].compareTo(c[j + 1]) < 0))
j++;
if (item.compareTo(c[j])
te co pa e o(c[j]) > 0)
break;
// Position for item found
c[(j-1) / 2] = c[j]; // Else put child data in parent node
j = 2*j+1;
// Move down to next level of heap
}
c[(j-1) / 2] = item;
// Drop last element into correct place
}
}
(Max) Heapsort
90
50
80
40
70
45
35
80
50
40
70
45
35
70
45
35
80
90
40
70
45
35
45
80
80
50
90
40
35
70
80
40
35
70
90
90
45
45
90
35
50
40
35
70
40
45
50
50
45
40
50
90
70
50
40
80
80
90
50
…
35
70
80
90
7
Heap: heapsort()
public static Comparable[] heapsort(Comparable[] c) {
heapify(c);
Comparable item;
int size= c.length;
for (int i= size-1; i > 0; i--) {
Comparable t= c[i]; // Swap top element with ith element
c[i]=
[i] c[0];
[0]
c[0]= t;
int j= 1;
// Left child
item= c[0];
while (j < i) {
// Compare left and right child and let j be larger child
if ((j+1 < i) && (c[j].compareTo(c[j + 1]) < 0))
j++;
if (item.compareTo(c[j])
(i
( [j]) > 0)
)
break;
// Position for item found
c[(j-1) / 2] = c[j]; // Else put data in parent node
j = 2*j+1;
// Move down to next level of heap
}
c[(j-1) / 2] = item;
// Drop element into correct place
}
return c;
}
Heap: example
public static void main(String[] args) { // Max heap
System.out.println("Heap");
Heap h= new Heap(10);
h.insert("b");
h.insert("d");
h.insert("f");
h.insert("a");
h.insert("c");
h.insert("e");
h.insert("g");
h insert("h");
h.insert(
h );
h.insert("i");
String top = null;
while (h.getSize() > 0) {
top= (String) h.delete();
System.out.println(" "+ top);
}
8
Heap: example, p.2
System.out.println("\nHeapify");
String[]
i [] s= {"a",
{" " "b",
"b" "c",
" " "d",
"d" "e",
" " "f",
"f" "g",
" " "h",
"h"
"i", "j", "k", "l", "m"};
Heap h2= new Heap(s);
while (!h2.isEmpty()) {
top= (String) h2.delete();
System.out.println(" "+ top);
}
System.out.println("\nHeapsort");
String[] s2= {"z", "b", "x", "d", "y", "f", "w", "h",
"v", "j", "u", "l", "t"};
String[] answer2= (String[]) Heap.heapsort(s2);
for (String ss : answer2)
System.out.println(" "+ss);
System.out.println();
}
} // Download also has MinHeap class; Heap is max heap
Heap performance
• Heap insert
– Maximum number of operations= number of levels in tree =
O(lg n), where n is number of nodes
• Heap delete
– Same as insert
• Heapsort
– First execute heapify (analyzed below)
– Number of operations= number of nodes * adjustments/node,
which are O(lg n) deletions
– Thus heapsort is O(n lg n)
• Similar to quicksort, but quicksort tends to be twice as fast
9
Heapify performance
n= number of nodes in heap
K= levels in heap
(0 is bottom, K is top)
K= floor(lg n) +1
(n+1)/(2k+1) nodes in each level
Heapify moves a node at level
k a maximum of k steps
The total number of steps=
(number of nodes at each
level) * (maximum moves
for that level)
lg n
∑2
k =0
∞
∑ kx
k
=
k =0
k =0
k
2( k +1)
lg n
1
≅ n∑ k ( ) k
2
k =0
x
for | x |< 1
(1 − x) 2
CLRS, page 1061, (A.8)
Thus
lg n
1
∑ k(2)
k =0
50
k=1
35
70
( k +1)
∗ k = n∑
k
≤
1/ 2
=2
(1 − 1 / 2) 2
k=2
45
40
lg n
n
80
90
k= 0
lg n
1
n ∑ k ( ) k = O ( 2 n ) = O ( n)
2
k =0
10
MIT OpenCourseWare
http://ocw.mit.edu
1.204 Computer Algorithms in Systems Engineering
Spring 2010
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.
Download