Professional Documents
Culture Documents
COMP2230 Introduction To Algorithmics: A/Prof Ljiljana Brankovic
COMP2230 Introduction To Algorithmics: A/Prof Ljiljana Brankovic
Lecture Overview
Priority Queues, Heaps Text, Chapter 3, Section 3.5 Disjoint sets Text, Chapter 3, Section 3.6 Binary Search Text, Chapter 4, Section 4.1 Next week: Searching Text, Chapter 4, Section 4.2 4.5
Priority Queues
A priority queue is an abstract data type that allows:
insertion of items with specified priorities, and deletion of an item with the highest priority
389, 12 and 7 into initially empty priority queue. After deleting and item, the priority queue contains 20,12,7 and after inserting 67 it contains 20,12,7,67.
(1)
(n)
Deletion
TOTAL
Heaps
A heap is a binary tree where all levels except possibly the last level are completely filled in; the nodes in the last level are at the left. LEVEL
0 1
Binary Maxheaps
In a binary maxheap a value of each node is greater or equal to the values of its children.
104
LEVEL 1 3
24
2
71
0 1 7
8
4
66 27
6
23
8
5 32
9
25
10
18
11
22
12 3
5 8 32 9 25 10 18 11 22 12
104 1
71 2
24 3
66 4
27 5
23 6
8 7
71 66 22 32 25 27 18 23 24 8 32 5 22 25 66
71 24 27 18 23 8
The array v represents a heap structure indexed from 1 to n. The left and right subtrees of node i are heaps. After siftdown (v, i , n) is called, the subtree rooted at i is a heap.
Input Parameter: v,t, n Output Parameters: v siftdown( v,i, n) { temp = v[ i] // 2 * i = n tests for a left child while (2 * i = n) { child = 2 * i // if there is a right child and it is // bigger than the left child, move child if (child < n && v[child + 1] > v [child]) child = child + 1 // move child up? if (v [child] > temp) v[i] = v[ child] else break // exit while loop i = child } // insert original v[ i] in correct spot v[i] = temp }
Complexity: (lg n)
Complexity?
Complexity?
T (n ) i2 h i = 2 h
i =1 i =1
i < 2h 2 = 2lg n 2 2 lg n 2 = 2 n i 2
Therefore T(n)=O(n).
2
i =1
i
i
<2
total
1/2 1/22 1/22 1/23 1/23 1/23 . . . . . . . . . . . . 1/2h 1/2h 1/2h ... 1/2h <1 <1/2 <1/22 ... <1/2h-1
TOTAL
<2
Indirect Heaps
key
66 312 1 12 2 312 3 25 4 8 5 109 6 7 7 18 8
into
66 109 2 8 1 7 5 3 6 4
outof
18 8 7 25 3 1 6 8 5 7 4 2
12
Input Parameters: i,newval Output Parameters: None increase( i,newval ) { key[i ] = newval // p is the parent index in the heap structure // c is the child index in the heap structure c = into[ i] p = c /2 while (p = 1) { if (key[outof [p]] = newval) break // exit while loop // move value at p down to c outof [c] = outof[ p] into[ outof[c]] = c // move p and c up c = p p = c /2 } // put newval in heap structure at index c outof [c] = i into[ i] = c }
We consider nonempty pairwise disjoint sets, where each set has an element marked as a representative element of the set. Sets X and Y are disjoint iff X Y= Example: The sets {1} {2,4,3} {5,6,7} Are nonempty pairwise disjoint sets. The mark element is each set is represented in bold & underlined.
Disjoint sets
Disjoint sets
The operations: makeset(i) constructs the set {i} findset(i) returns the marked member of the set to which i belongs union(i,j) replaces the set containing I and the set containing j with their union.
Example:
makeset(1) makeset(2) makeset(3) makeset(4) makeset(5) makeset(6) makeset(7) We have {1}, {2},{3},{4}, {5},{6},{7} union(2,4) union(2,3) union(5,6) union(6,7) We have {1} {2,4,3} {5,6,7} findset(2) returns 4.
We represent the disjoint-set abstract data type as an arbitrary tree with the marked element as a root. Note that such a tree is not necessarily binary. Example: Sets {1} {2,4,3} {5,6,7} may be represented as follows (note that there is more than one way to represent these sets):
1 4 6 5 2 3 7
Disjoint sets
Disjoint sets
We then use an array parent to represent such a tree; parent[i] is the parent of i in the corresponding tree, unless i is the root, in which case the parent[i] is i. We can also use a single array to represent a collection of such trees (forest). Example: Sets {1} {2,4,3} {5,6,7} represented as
1 4 6 5 2 3 7
4 4 4
6 6 5
Input Parameter: i Output Parameters: None findset1( i) { while (i != parent[i]) i = parent[i] return i }
Q: What is a height of a tree constructed by previous algorithms? A: At most n-1, where n is the number of elements in the set. It is more desirable for a tree representing a disjoint set to have a smaller height as then algorithms findset and union would be more efficient. The following algorithms guarantee that the height of a tree with n nodes is at most lg n.
Input Parameter: i Output Parameters: None findset2( i) { while (i != parent[i]) i = parent[i] return i }
1 8 6 4
2 3
6 4
The following algorithms incorporate path compression. Note that the algorithms use rank instead of height. As the path compression can potentially decrease the height if the tree, this parameter does not necessarily denote the height of the tree. It rather denotes an upper bound on its height, which we call rank.
Binary Search
Binary search is a very efficient algorithm for searching in a sorted array. In fact, it is an optimal algorithm no other algorithm that uses only comparisons is faster than binary search.
Find 11. i=0; j=12; k=(i+j)/2=6; 22>11 -> j=k-1=5 i=0; j=5; k =(i+j)/2=2; 7<11 -> i=k+1=3
Find 11. i=0; j=12; k=(i+j)/2=6; 22>11 -> j=k-1=5 i=0; j=5; k =(i+j)/2=2; 7<11 -> i=k+1=3 i=3; j=5; k =(i+j)/2=4; 13>11 -> j=k-1=3
1 4
2 7
10 11 12
11 13 17 22 23 24 35 36 38 43
k=(i+j)/2=6; k =(i+j)/2=2; k =(i+j)/2=4; k =(i+j)/2=3; 22>11 -> 7<11 -> 13>11 -> 11=11 j=k-1=5 i=k+1=3 j=k-1=3
Find 11.
j=12; j=5; j=5; j=3;
Correct or not?
bsearch( L,i,j, key) { while (i j) { k = (i + j)/2 if (key == L[k ]) return k if (key < L[k]) j = k else i = k } return -1
Correct or not?
bsearch( L,i,j, key) { if (i>j) return -1 k = (i + j)/2 if (key == L[k ]) return k flag = bsearch(L,i,k-1,key) if (flag == -1 ) return bsearch(L,k+1,j,key) else return flag }
Correct or not?
bsearch( L,i,j, key) { if (i>j) return -1 k = (i + j)/2 if (key == L[k ]) return k if (key <L[k]) return bsearch(L,i,k,key) else return bsearch(L,k+1,j,key) }
Interpolation search
Binary search compares a search key with the middle value of the sorted array. Interpolation search takes into account the values of the search key and the smallest and largest element in the array, and estimates where the search key would be if it is in the array. Interpolation search assumes that values in the array increase linearly with the index.
Interpolation search
a(j) key
a(i) i x j
Interpolation search
The worst case complexity for interpolation search is (n). However, in an array with random keys, the averagecase complexity is (lg lg n).
Hashing
Hashing distributes the values of the array evenly among elements of the hash array. This is done by computing a hash function. For example, if the elements of the array are integers, the hash function can be h(K) = K mod m. The hash values will be integers between 0 and m-1 , inclusive.
Hashing
Let n be the size of the original array, and m the size of the hash array. Whenever m < n we will certainly have collisions, that is, two or more elements of the original array being hashed into the same cell of the hash array.
Hashing
One way to deal with collisions is to have a linked list for each cell of the hash array that gets more than one element of the original array. Then for a good hash function, that is, the function that distributes the values evenly among elements of the hash array, the average number of comparisons for a successful search will be 1+n/(2m). The drawback of hashing is the extra space required for the hash array.