Download Lecture 7a

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Lecture: Priority Queue
Questions
• Is array a data structure?
• What is a data structure?
No! Why?
It is a standard part of algorithm
• What data structures are implemented by
array?
Stack, Queue, List, Heap, Max-heap, Min-heap,
…
• Priority queue (max --, min --).
Contents
• Recall: Heap, a data structure
Min-heap
(a) Min-Heapify procedure
(b) Building a min-heap
• Min-Priority Queue
• Implementation of Dijkstra’s Algorithm
Heap
A Data Structure Heap
• A heap is an array object that can be
viewed as a nearly complete binary tree.
1
6
2
3
5
3
4
2
5
4
6
5
3
2
4
1
6
1
All levels except last
level are complete.
Tied with three procedures for finding
Parent, finding left child, and finding
Right child.
Parent (i )
return i / 2;
Left (i )
return 2i;
Right (i )
return 2i  1;
Min-Heap
Min-Heap
In a max - heap, every node i other than the
root satisfies the following property :
A[Parent (i )]  A[i ].
Min-Heapify
• Min-Heapify(A,i) is a subroutine.
• Input: When it is called, two subtrees
rooted at Left(i) and Right(i) are minheaps, but A[i] may not satisfy the minheap property.
• Output: Min-Heapify(A,i) makes the
subtree rooted at A[i] become a min-heap
by letting A[i] “float down”.
4
14
4
12
14
7
8
11
12
7
8
11
14
8
2
7
14
1
O(log n) time
Building a Min-Heap
Build - Min - Heap ( A)
heap - size[ A]  length[ A];
for i  length[ A] / 2 downto 1
do Min - Heapify ( A, i );
e.g., 4, 1, 3, 2, 16, 9, 10, 14, 8, 7.
4
1
2
14
3
16
8
7
9
10
4
1
2
14
3
7
8
16
9
10
4
1
2
14
3
7
8
16
9
10
4
1
2
14
3
7
8
16
9
10
4
1
2
14
3
7
8
16
9
10
1
4
2
14
3
7
8
16
9
10
1
2
4
14
3
7
8
9
10
16
O(n) time
Priority Queue
Priority Queue
• A priority queue is a data structure for
maintaining a set of elements, each with an
associated value, called a key.
• A min-priority queue supports the following
operations: Minimum(S), Extract-Min(S),
Increase-Key(S,x,k), Insert(S,x).
• Min-Heap can be used for implementing
min-priority queue.
Minimum( S ) returns the element of S with
the smallest key.
Heap - Minimum ( A)
return A[1];
Extract - Min ( S ) removes and returns the
element of S with the smallest key.
Heap - Extract - Min ( A)
if heap - size[ A]  1
the n error " heap underflow" ;
max  A[1];
A[1]  A[heap - size[ A]];
heap - size[ A]  heap - size[ A]  1;
Min - Heapify( A,1);
return max;
O(log n) time
Input: 4, 1, 3, 2, 16, 9, 10, 14, 8, 7.
Build a min-heap
1
2
4
14
3
7
8
9
10
16
1, 2, 3, 4, 7, 9, 10, 14, 8, 16.
Min  1
16
2
4
14
3
7
9
10
8
16, 2, 3, 4, 7, 9, 10, 14, 8.
Min  1
2
16
4
14
3
7
9
10
8
2, 16, 3, 4, 7, 9, 10, 14, 8.
Min  1
2
4
7
16
14
3
9
10
8
2, 4, 3, 16, 7, 9, 10, 14, 8.
Min  1
2
4
8
14
3
7
9
10
16
2, 4, 3, 8, 7, 9, 10, 14, 16.
Decrease - Key( S , x, k ) decreases the value of element
x' s key to the new value k , which is assumed to be at
most as large as x' s current key value.
Heap - decrease - Key( A, i, key)
if key  A[i ]
the n error " new key is larger tha n current key" ;
A[i ]  key;
while i  1 and A[Parent (i )]  A[i ]
do exchange A[i ]  A[Parent (i )]
and i  Parent (i );
O(log n) time
Heap - Increase - Key( A, 9, 1)
1
2
4
14
3
7
8
9
10
16
1, 2, 3, 4, 7, 9, 10, 14, 8, 16.
Heap - Increase - Key( A, 9, 1)
1
2
4
14
3
7
1
9
10
16
1, 2, 3, 4, 7, 9, 10, 14, 1, 16.
Heap - Increase - Key( A, 9, 1)
1
2
1
14
3
7
4
9
10
16
1, 2, 3, 1, 7, 9, 10, 14, 4, 16.
Heap - Increase - Key( A, 9, 1)
1
1
2
14
3
7
4
9
10
16
1, 1, 3, 2, 7, 9, 10, 14, 4, 16.
Insert( S , x.key) inserts the element x into set S .
Insert ( A, key)
array - size[ A]  array - size[ A]  1;
A[array - size[ A]]  ;
Decrease - Key( A, array - size[ A], key);
O(log n) time
Insert( A, 2) with heap
1
3
4
14
6
7
8
9
10
16
1, 3, 6, 4, 7, 9, 10, 14, 8, 16.
Decrease - Key( A, 11, 2) with heap
1
3
4
14
6
9
7
8
16
10
+∞
1, 3, 6, 4, 7, 9, 10, 14, 8, 16, +∞.
Decrease - Key( A, 11, 2)
1
3
4
14
6
9
7
8
16
10
2
1, 3, 6, 4, 7, 9, 10, 14, 8, 16, 2.
Decrease - Key( A, 11, 2)
1
2
4
14
6
9
3
8
16
10
7
1, 2, 6, 4, 3, 9, 10, 14, 8, 16, 7.
Implementation of Dijkstra’s
Algorithm
Dijkstra’s Algorithm
Define d (u )  min
{d * (v)  c(v, u )} for u  T .

vN ( u )  S
Initially, S  {s}, T  V  S .
In each iteration, find u  T : d (u )  min d ( w).
wT
S  S  {u}
T  T  {u}
update d ( w) for w  T ;
Stop if T  .
Implementations
• With min-priority queue, Dijkstra algorithm can be
implemented in time O (( m  n) lg n).
• With Fibonacci heap, Dijkstra algorithm can be implemented
in time O(m  n lg n).
• With Radix heap, Dijkstra algorithm can be implemented
in time O(m  n lg c).
Related documents