Upload
eden-best
View
12
Download
0
Embed Size (px)
DESCRIPTION
Heaps. Section 6.4, Pg. 309 (Section 9.1). T. How many times is this edge visited?. Motivation: Prim’s Algorithm. Starting node. 3 times. Problem : You keep comparing the same edges over and over. Solution : Use priority queues containing the edges. Priority Queues. Typical example:. - PowerPoint PPT Presentation
Citation preview
Heaps
Section 6.4, Pg. 309 (Section 9.1)
T
Motivation: Prim’s Algorithm
Problem: You keep comparing the same edges over and over.Solution: Use priority queues containing the edges
Starting node
3 times
How many times is this edge visited?
Priority Queues•Typical example:
Operations supported in a priority queue:
•Insert a new element•Extract/Delete of the element with the highest priority•The priority is a number
printing in a Unix/Linux environment. Printing jobs have different priorities.
These priorities override the FIFO policy of the queues
Implementing Priority Queues: min-HEAPS
A min-Heap is a binary tree T such that:• The priority of the element in any node is less or equal than the priority of the elements in its children nodes•T is complete
We assume that each element has a key, K, and other information, I. K is the priority of I.
Heapgreater
Note: The algorithms for Heaps are the same as for min-Heaps. Just invert the comparisons.
(non) Examples
5
8 10
16 12
5
8 9
16 12 10
5
8 9
16 12 109 56
10 > 9! Tree is not complete
Insert a New Element
Idea: insert the new element as the last leaf and re-adjust the tree
5
8 9
16 12
18 20 22 44
10
13
56
Insert 7
Insert a New Element (II)
Step 1: add it as the last leaf
5
8 9
16 12
18 20 22 44
10
13
56
7
Insert a New Element (III)
Steps 2, 3 ,… : swap until key is in the right place
5
8 9
16 12
18 20 22 44 1013
567
Insert a New Element (IV)
Steps 2, 3 ,… : swap until it finds its place
5
8
916 12
18 20 22 44 1013
56
7
Complexity is O(log2 n)
Extract/Delete the Element with the Lowest Priority
Idea: the root has always the lowest priority. Then delete it and replace it with the last child. Re-adjust the tree.
5
8 9
16 12
18 20 22 44
10
13
56
Extract/Step 1: returns 5
Extract/Delete the Element with the Lowest Priority (II)
Step 2: Put the last key as the new root.
13
8 9
16 12
18 20 22 44
10 56
Extract/Delete the Element with the Lowest Priority (III)
Steps 3, 4, … : Swap until key is in the correct place. Always swap with node that has the lowest priority.
13
8
9
16 12
18 20 22 44
10 56
Extract/Delete the Element with the Lowest Priority (IV)
Steps 3, 4, … : Swap until key is in the correct place. Always swap with node that has the lowest priority.
13
8
9
16
12
18 20 22 44
10 56
Complexity is O(log2 n)
Complete Trees can be Represented in Arrays
5
8 9
16 12
18 20 22 44
10
13
56
Corresponding array: 5 8 9 16 12 10 56 18 20 22 44 13
0 1 2 …
Operations in Array Representation of Complete Trees
Assume that the first index in the array is 0 and that the number of keys is n
isLeaf(i):
LeftChild(i):
rightChild(i):
Parent(i):
Homework
2i + 1
2i + 2
Homework
root(i): i = 0
The Fringe (neighbors of T) are maintained in a priority list (min-Heap), which ensures O((|E|+|V|)log2|E|)
T
Using Priority Queues in Prim’s Algorithm
Maintaining the Fringe is the crucial aspect of Djisktra’s Algorithm for computing shortest path between two points (next class)