Professional Documents
Culture Documents
Divide-and-Conquer Count, Radix & Bucket • Heap Sort, Merge sort: O(nlgn)
A 2 5 3 0 2 3 0 3 Cnew 2 2 4 7 7 8
• Decrease C[A[i]]
A 2by5one
3 0 2 3 0 3 B 0 3 3 B 0 2 3 3
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
Cnew 1 2 4 5 7 8 Cnew 1 2 3 5 7 8
Cnew 2 2 4 7 7 8
7 8 9
1
21-Feb-20
2. B
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
3. for j ← 1 to n O(n)
B 0 0 2 3 3 B 0 0 2 3 3 3 5 3. for j ← 1 to n
4. do C[A[ j ]] ← C[A[ j ]] + 1
0 1 2 3 4 5 0 1 2 3 4 5
4. do C[A[ j ]] ← C[A[ j ]] + 1
C 0 2 3 5 7 8 C 0 2 3 4 7 7 5. C[i] contains the number of elements equal to i
5. C[i] contains the number of elements equal to O(r)
i 6. for i ← 1 to r
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
6. for i ← 1 to r 7. do C[ i ] ← C[ i ] + C[i -1]
B 0 0 2 3 3 3 B 0 0 2 2 3 3 3 5 8. C[i] contains the number of elements ≤ i
7. do C[ i ] ← C[ i ] + C[i -1] O(n)
for j ← n downto 1
0 1 2 3 4 5
– Assuming d=O(1) and k=O(n), running time is O(n) • Output: elements A[i] sorted
16 17 18
2
21-Feb-20
3 .39 2
1 .12 .17 / 1 .12 .17 /
.26 .21 .23 /
2 .21 .23 .26 / 2 .21 .23 .26 /
4 .26 3 .39 /
3 .39 / 3 .39 /
5 .72 4 / Distribute Sort within each
Into buckets 4 / 4 /
6 .94 5 / bucket
7 .21 6 .68 / 5 / 5 / Concatenate the lists from
6 .68 / 6 .68 / 0 to n – 1 together, in order
8 .12 7 .78 .72 /
Analysis of Bucket Sort Radix Sort as a Bucket Sort Special Types of Trees
Heap
3
21-Feb-20
children 1. n = length[A] 4
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r • Can be written in terms of the height of the heap, 2. for i ← n/2 downto 1
2
1
3
3
4 5 6 7
4
21-Feb-20
Example: A 4 1 3 2 16 9 10 14 8 7 Running Time of BUILD MAX HEAP Running Time of BUILD MAX HEAP
4 4
1 1
4
proportional to the height of the node i in the tree
1. n = length[A]
h
T (n) ni hi 2i h i O(n)
h
2 3 2 3 2 3
i =2 i =1
Running time: O(nlgn)
1 1 1
h2 = 1 i=2 22
4 4 16 • This is not an asymptotically tight upper bound
2 3 2 3 2 3
1 10 16 10 14 10 h3 = 0 i = 3 (lgn) 23
4 5 6 7 4 5 6 7 4 5 6 7
14 16 9 3 14 7 9 3 8 7 9 3
8 9 10 8 9 10 8 9 10 hi = h – i height of the heap rooted at level i
2 8 7 2 8 1 2 4 1 ni = 2i number of nodes at level i
5
21-Feb-20
Goal:
Goal: 16 1
– Extract the largest element of the heap (i.e., return the max
– Return the largest element of the heap value and also remove that element from the heap
14 10 max = 16 14 10
Idea:
Running time: O(1) 8 7 9 3 8 7 9 3
Alg: HEAP-MAXIMUM(A) – Exchange the root element with the last 2 4 1 2 4
1. return A[1] – Decrease the size of the heap by 1 element Heap size decreased with 1
Heap A: – Call MAX-HEAPIFY on the new root, on a heap of size n-1
14
Heap A: Root is the largest element
Call MAX-HEAPIFY(A, 1, n-1)
8 10
4 7 9 3
2 1
Heap-Maximum(A) returns 7
• Goal:
Alg: HEAP-EXTRACT-MAX(A, n) 16 16
– Increases the key of an element i in the heap
1. if n < 1 • Idea: 14 10 14 10
2 4 1 7 2 4 1 7
6
21-Feb-20
8 4 6 9 2 1 3 1 2 8 4 6 9 3
– The data to be sorted is all stored in the – Repeatedly pass through the array i= 1 j i= 3 j
computer’s main memory. – Swaps adjacent elements that are out of order 8 4 6 9 1 2 3 1 2 3 8 4 6 9
i
• External Sort 1 2 3 n i= 1 j i= 4 j
8 4 1 6 9 2 3 1 2 3 4 6 8 9
• In Place Sort i= 1 j i= 6 j
• Disadvantage:
n
(n i)
i= 1 j
= (n) + (c2 + c2 + c4)
i 1
– Running time depends only slightly on the amount
n n
n(n 1) n 2 n
n
where (n i ) n i n 2
of order in the file
i 1 i 1 i 1 2 2 2
Thus,T(n) = (n2)
7
21-Feb-20
c2 n
1 2 6 9 4 3 8 1 2 3 4 6 8 9
for j ← 1 to n - 1 do smallest ← j
c3 n-1
1 2 3 9 4 6 8 1 2 3 4 6 8 9 do smallest ← j
n2/2 for i ← j + 1 to n c4 nj11 (n j 1)
for i ← j + 1 to n comparisons
do if A[i] < A[smallest] do if A[i] < A[smallest] c5 nj11 (n j )
n
then smallest ← i exchanges then smallest ← i c6
n 1
j 1
(n j )
• Property. After ith iteration, a[0] through a[i] • Property. After ith iteration, a[0] through a[i]
contain first i+1 elements in ascending order. contain first i+1 elements in ascending order.
• Property. After ith iteration, a[0] through a[i] • Property. After ith iteration, a[0] through a[i] • Property. After ith iteration, a[0] through a[i]
contain first i+1 elements in ascending order. contain first i+1 elements in ascending order. contain first i+1 elements in ascending order.
8
21-Feb-20
• Property. After ith iteration, a[0] through a[i] • Property. After ith iteration, a[0] through a[i] • Property. After ith iteration, a[0] through a[i]
contain first i+1 elements in ascending order. contain first i+1 elements in ascending order. contain first i+1 elements in ascending order.
• Property. After ith iteration, a[0] through a[i] • Property. After ith iteration, a[0] through a[i] • Property. After ith iteration, a[0] through a[i]
contain first i+1 elements in ascending order. contain first i+1 elements in ascending order. contain first i+1 elements in ascending order.
• Property. After ith iteration, a[0] through a[i] • Property. After ith iteration, a[0] through a[i] • Property. After ith iteration, a[0] through a[i]
contain first i+1 elements in ascending order. contain first i+1 elements in ascending order. contain first i+1 elements in ascending order.
9
21-Feb-20
• Property. After ith iteration, a[0] through a[i] • Property. After ith iteration, a[0] through a[i] • Property. After ith iteration, a[0] through a[i]
contain first i+1 elements in ascending order. contain first i+1 elements in ascending order. contain first i+1 elements in ascending order.
• Property. After ith iteration, a[0] through a[i] • Property. After ith iteration, a[0] through a[i] • Property. After ith iteration, a[0] through a[i]
contain first i+1 elements in ascending order. contain first i+1 elements in ascending order. contain first i+1 elements in ascending order.
• Property. After ith iteration, a[0] through a[i] • Property. After ith iteration, a[0] through a[i] • Property. After ith iteration, a[0] through a[i]
contain first i+1 elements in ascending order. contain first i+1 elements in ascending order. contain first i+1 elements in ascending order.
10
21-Feb-20
• Property. After ith iteration, a[0] through a[i] • Property. After ith iteration, a[0] through a[i] • Property. After ith iteration, a[0] through a[i]
contain first i+1 elements in ascending order. contain first i+1 elements in ascending order. contain first i+1 elements in ascending order.
Backward Substitution: Example (1) Backward Substitution: Example (2) Backward Substitution: Example (3)
• Give a solution to • If we continue to do that we get • We want to get rid of the recursive term
T(n)= T(n-1) + 2n T(n) = T(n-2) + 2(n-1) + 2n T(n) = T(n-i) + 2n(i-1) – i2 + i + 2n
where T(1)=5 T(n) = T(n-3) + 2(n-2) + 2(n-1) + 2n • To do that, we need to know at what iteration we reach our
based case, i.e. for what value of i can we use the initial
• We begin by unfolding the recursion by a simple substitution T(n) = T(n-4) + 2(n-3) + 2(n-2) + 2(n-1) + 2n condition T(1)=5?
of the function values ….. • We get the base case when n-i=1 or i=n-1
• We observe that T(n) = T(n-i) + j=0i-1 2(n - j) function’s value at the ith iteration • Substituting in the equation above we get
T(n-1) = T((n-1) -1) + 2(n-1) = T(n-2) + 2 (n-1) • Solving the sum we get T(n) = 5 + 2n(n-1-1) – (n-1)2 + (n-1) + 2n
T(n) = T(n-i) + 2n(i-1) – 2(i-1)(i-1+1)/2 + 2n
T(n) = 5 + 2n(n-2) – (n2-2n+1) + (n-1) + 2n = n2 + n + 3
• Substituting into the original equation T(n) = T(n-i) + 2n(i-1) – i2 + i + 2n
T(n)=T(n-2)+2(n-1)+2n
11
21-Feb-20
12
21-Feb-20
13
21-Feb-20
8 3 2 9 7 1 5 4
1 2 3 4 5 7 8 9
14
21-Feb-20
A[i]p A[i]>p
15
21-Feb-20
140
• S is a set of n points Pi=(xi, yi) in the plane Finding the closest pair of points
• For simplicity, n is a power of two Running time of the algorithm is described by - Find the closest pair of points in a set of points. The set consists of points in two
dimension plane
• Without loss of generality, we assume points - Given a set P of N points, find p, q P, such that the distance d(p, q) is minimum
are ordered by their x coordinates T(n) = 2T(n/2) + f(n), where f(n) O(n)
• Discussion: How to apply divide-and-conquer?
By the Master Theorem (with a = 2, b = 2, d = 1)
T(n) O(n log n) - Application:
- traffic control systems: A system for controlling air or sea traffic might need to know
which two vehicles are too close in order to detect potential collisions.
- computational geometry
16
21-Feb-20
2D Closest Pair
Example 2 2D Closest Pair
Problem d d
d = minimum (dLmin, dRmi n)
Finding the closest pair of points Find the closest pair in a strip of width 2d d For each point p on the left of the
dividing line, we have to compare to
p the distances to the points in the
Example: d= min(12, 21) blue rectangle.
- Divide: Sort the points by x-coordinate; draw vertical line
d
to have roughly n/2 points on each side Note there must be no point inside
the blue rectangle, because d =
- Conquer: Find closest pair in each side recursively minimum (dLmin, dRmin). Thus, for
- Combine: Find closest pair with one point in each side d each point p, we only have to consider
6 points – 6 red circles on the blue
- Return: best of three solutions S1 S2 rectangles.
Dividing So, we need at most 6*n/2 distance
Line comparisons
17
21-Feb-20
Strassen’s matrix multiplication Strassen’s matrix multiplication The convex hull problem
C00 C01 A00 A01 B00 B01 C00 C01 A00 A01 B00 B01 concave polygon: convex polygon:
C * C *
10 C11 A10 A11 B10 B11 10 C11 A10 A11 B10 B11
M 1 M 4 M 5 M 7 M3 M5 M 1 M 4 M 5 M 7 M3 M5
M2 M4 M 1 M 3 M 2 M 6 M2 M4 M 1 M 3 M 2 M 6
Topological Sort
Divide-and-conquer for convex hull • Directed graph G.
Input : A set S of planar points Step 4: Apply the merging procedure to merge • Rule: if there is an edge u v, then u must come
Output : A convex hull for S Hull(SL) and Hull(SR) together to form a convex before v.
Step 1: If S contains no more than five points, use hull. • Ex: A B
exhaustive searching to find the convex hull and G
return.
Time complexity: F
Step 2: Find a median line perpendicular to the X- C
axis which divides S into S L and SR ; SL lies to the T(n) = 2T(n/2) + O(n) I H
left of SR . A B
= O(n log n) E D
Step 3: Recursively construct convex hulls for S L and G
SR. Denote these convex hulls by Hull(SL) and F
Hull(SR) respectively. C
I H
5 -160 5 -161 E D 162
18
21-Feb-20
– Called a DAG for short Initialize In-Degree map: O(|V| + |E|) to a simpler/more convenient instance of the same problem. We call
• General problem: Initialize Queue with In-Degree 0 vertices: O(|V|) it instance simplification.
– Given a DAG input G with weights on edges Dequeue and output vertex: to a different representation of the same instance. We call it
– Find shortest paths from source A to every other vertex |V| vertices, each takes only O(1) to dequeue and output: representation change.
O(|V|)
Reduce In-Degree of all vertices adjacent to a vertex and to a different problem for which an algorithm is already available.
We call it problem reduction.
Enqueue any In-Degree 0 vertices:
O(|E|)
Runtime = O(|V| + |E|) Linear!
169
170 Figure: Transform-and-conquer strategy. 171
19
21-Feb-20
A
Example 3: Searching Problem Binary Trees
B E
Binary Tree: A binary tree is a finite set of elements
Problem: Search for a given K in A[0..n-1] that is either empty or is partitioned into three disjoint
subsets:
C D F
Presorting-based algorithm: - The first subset contains a single element called the
Stage 1 Sort the array by an efficient sorting algorithm. ROOT of the tree. Left subtree Right subtree
Stage 2 Apply binary search. - The other two subsets are themselves binary trees,
called the LEFT and RIGHT subtrees of the original BINARY TREE
Efficiency: tree.
T(n) = Tsort(n) + Tsearch(n) = Θ(nlog n) + Θ(log n) = Θ(nlog n)
NODE : Each element of a binary tree is called a node
of the tree.
subtrees.
20
21-Feb-20
BINARY SEARCH TREE Draw the BSTs of height 2, 3, 4, 5, 6 on the following set of keys:
Binary Search Tree (Binary Sorted Tree) : Example BSTs: { 1, 4, 5, 10, 16, 17, 21 } 16 17
16 21
Suppose T is a binary tree. Then T is called a binary search 10 10 17
tree if each node of the tree has the following property : 4 17 10
4 21
the value of each node is greater than every value in the left 5 2 1 5 16 21 4
subtree of that node and is less than every value in the right 1 5
HEIGHT 2
1 5
subtree of N. 3 HEIGHT 3
3 7 HEIGHT 4
17 21 1
7 16 21 21 17
Let x be a node in a binary search tree . 4
2 4 8 17 16
If y is a node in the left subtree of x,then info(y)<info(x). 10 5
5 16
If y is a node in the right subtree of x , then 8 5 10 10
info(x)<=info(y) or 10 or
x 4 5 16
4
4 4 17
1
1 5 1 21183
All<x All≥x 181 182
HEIGHT 6
HEIGHT 5
Operations of BSTs: Insert Insert Operations: 1st of 3 steps Insert Operations: 2nd of 3 steps
1) The function begins at the root node and compares item 32 2) Considering 35 to be the root of its own subtree, we compare
with the root value 25. Since 32 > 25, we traverse the right
Adds an element x to the tree so that the binary search item 32 with 35 and traverse the left subtree of 35.
subtree and look at node 35.
tree property continues to hold
25
The basic algorithm
parent 25
20 35 parent
06 33 53 97 06 33 53 97
12 32 40
(c) 13 25 43 64 84 99 13 25 43 64 84 99
Step 3: Insert 32 as left child
of parent 35
187 188 189
21
21-Feb-20
14 52 < 72 72 14 52 < 72 72 14 72
Go left. Go left.
06 33 53 97 06 33 53 97 06 33 53 97
52 < 53
Go left.
13 25 43 64 84 99 13 25 43 64 84 99 13 25 43 64 84 99
06 33 53 97 06 33 53 97 06 33 53 97
13 25 43 64 84 99 13 25 43 64 84 99 13 25 43 64 84 99
22
21-Feb-20
Search for Key 43. Search for Key 43. Search for Key 43.
51 51 51
14 72 14 72 14 72
43 > 14
Go right.
06 33 53 97 06 33 53 97 06 33 53 97
43 > 33 43 > 33
Go right. Go right.
13 25 43 64 84 99 13 25 43 64 84 99 13 25 43 64 84 99
14 72 14 72 14 72
06 33 53 97 06 33 53 97 06 33 53 97
13 25 43 64 84 99 13 25 43 64 84 99 13 25 43 64 84 99
43 = 43
FOUND
14 52 < 72 72 14 52 < 72 72 14 72
Go left. Go left.
06 33 53 97 06 33 53 97 06 33 52 < 53 53 97
Go left.
13 25 43 64 84 99 13 25 43 64 84 99 13 25 43 64 84 99
23
21-Feb-20
06 33 53 97
52 < 53 70
70
Go left. KEY
35 75 35 90
13 25 43 64 84 99
20 65 90 20 65
No more tree here.
NOT FOUND 50
208
50 209 210
211
Figure: (a) AVL tree. (b) Binary search tree that is not an AVL tree. The number212 213
24
21-Feb-20
25
21-Feb-20
5 5
0 -1
0
6 6
5
0
Figure: Construction of an AVL tree for the list 5, 6, 8, 3, 2, 4, 7 Figure: Construction of an AVL tree for the list 5, 6, 8, 3, 2, 4, 7 Figure: Construction of an AVL tree for the list 5, 6, 8, 3, 2, 4, 7
by successive insertions. by successive insertions. by successive insertions.
229 230 231
2
Figure: Construction of an AVL tree for the list 5, 6, 8, 3, 2, 4, 7 by successive
insertions. The parenthesized number of a rotation’s abbreviation
Figure: Construction of an AVL tree for the list 5, 6, 8, 3, 2, 4, 7 Figure: Construction of an AVL tree for the list 5, 6, 8, 3, 2, 4, 7
indicates the root of the tree being reorganized.
by successive insertions. by successive insertions.
232 233 234
26
21-Feb-20
Figure: Construction of an AVL tree for the list 5, 6, 8, 3, 2, 4, 7 by successive Figure: Construction of an AVL tree for the list 5, 6, 8, 3, 2, 4, 7 by successive Figure: Construction of an AVL tree for the list 5, 6, 8, 3, 2, 4, 7 by successive
insertions. The parenthesized number of a rotation’s abbreviation insertions. insertions. The parenthesized number of a rotation’s abbreviation
indicates the root of the tree being reorganized. 235 236 indicates the root of the tree being reorganized. 237
2-3 TREES
2-3 TREES 2-3 TREES In 2-3 trees, all the leaves must be on the same level. i.e., a 2-3 tree is
Another idea of balancing a search tree is to allow more than one key in always perfectly height-balanced: the length of a path from the root of
the same node ( Ex: 2-3 trees). A 3-node contains two ordered keys K1 and K2 (K1 < K2)
the tree to a leaf must be the same for every leaf.
and has three children.
2-3 trees was introduced by the U. S. computer scientist John Hopcroft in The leftmost child serves as the root of a subtree with
Searching in a 2-3 Tree:
1970. keys less than K1 Searching for an element always starts from the root.
The middle child serves as the root of a subtree with If the root is a 2-node, then the searching is similar to the way we search
A 2-3 tree is a tree that can have nodes of two kinds: 2-nodes and 3-nodes. keys between K1 and K2. in a binary search tree: We either stop if search key K is equal to the
root’s key or continue the search in the left subtree if K is less than the
The rightmost child serves as the root of a subtree with
A 2-node contains a single key K and has two children: the left child serves root’s key or continue the search in the right subtree if K is larger than
keys greater than K2. the root’s key.
as the root of a subtree whose keys are less than K and the right child
serves as the root of a subtree whose keys are greater than K. 3 - node If the root is a 3-node, then K can be compared with two elements in the
2 - node root. If it doesn’t match with root elements, then we need to search
K1 , K2 either left subtree, or middle subtree or right subtree. If K is less than the
K first element in the root, continue searching in the left subtree. If K is in
between the two elements of root, continue searching in the middle
subtree. If K is larger than the second element of the root, continue
searching in the right subtree.
<K >K 241
< K1 (K1 , K2 ) > K2 242 243
27
21-Feb-20
An example of a 2-3 tree construction is given below: An example of a 2-3 tree construction is given below:
Creating a 2-3 Tree
If the 2-3 tree is empty, insert a new key at the root level. Except the Construct 2-3 tree for the list 9, 5, 8,3, 2, 4 and 7 Construct 2-3 tree for the list 9, 5, 8,3, 2, 4 and 7
first node, rest of the nodes are always inserted in the leaf. Since a node in a 2-3 tree cannot contain more than 2 elements, we move
By performing a search for the key to be inserted, appropriate leaf the middle node to the parent position, left element 5 towards left of the
node is found. Step1: Insert the element 9 into the empty tree as shown below: parent and right element 9 towards right of the parent as shown below:
If the leaf is a 2-node, we insert K there as either the first or the
second key, depending on whether K is smaller or larger then the
9 8
node’s old key.
If the leaf is a 3-node, we split the leaf into two parts: the smallest of Step2: Element to be inserted is 5. Since there is only one element in the 5 9
the three keys (two old ones and the new key) is put in the first leaf,
root node, the element 5 is inserted along with 9 as shown below:
the largest key is put in the second leaf, while the middle key is
promoted to the old leaf’s parent. 5, 9 Step4: Element to be inserted is 3. Since 3 is less than 8, it can be inserted
If the tree’s root itself is the leaf node, a new root is created to accept towards left of 8, but along with node 5 as shown below:
the middle key.
Step3: Element to be inserted is 8. Since 8 is greater than 5 and less than
If the promotion of a middle element to its parent leads to a 3-node, 8
it may be required to split along the chain of leaf’s ancestor. 9, it is inserted between 5 and 9 as shown below:
By repeating this procedure, a 2-3 tree can be constructed.
244
5, 8, 9 245
3, 5 9 246
An example of a 2-3 tree construction is given below: An example of a 2-3 tree construction is given below: An example of a 2-3 tree construction is given below:
Construct 2-3 tree for the list 9, 5, 8,3, 2, 4 and 7 Construct 2-3 tree for the list 9, 5, 8,3, 2, 4 and 7 Construct 2-3 tree for the list 9, 5, 8,3, 2, 4 and 7
But, if a node has two elements i.e., 3, 8 (except the leaf i.e., 2, 5), it Step 7: Element to be inserted is 7. Since, 7 is between 3 and 8, it can be
should have 3 children: left child containing all elements less than 3,
Step5: Element to be inserted is 2. Since 2 is less than 8, it can be inserted inserted in the middle node along with 4, 5 as shown below:
middle child containing all elements between 3 and 8, and the right child
towards left of 8, but along with nodes 3 and 5 as shown below: containing the elements larger than 8. So, the above tree can be modified
as shown below: 3, 8
8 3, 8
2 4, 5, 7 9
2, 3, 5 9 2 5 9
Since a node cannot have more than 2 elements, move 5 to the parent
Step 6: Element to be inserted is 4. Since, 4 is between 3 and 8, it can be position and the resulting tree is shown below:
But, a node cannot have more than 2 elements. So, move the middle
element 3 to its parent position as shown below: inserted in the middle node along with 5 as shown below:
3, 5, 8
3, 8
3, 8
2 4 7 9
2 4, 5 9
2, 5 9 247 248 249
An example of a 2-3 tree construction is given below: Efficiency of 2-3 Trees Efficiency of 2-3 Trees
Therefore, for any 2-3 tree of height h with n nodes, we get the
Construct 2-3 tree for the list 9, 5, 8,3, 2, 4 and 7 inequality:
Since a 2-3 tree is always height balanced, the efficiency of any
operation depends only on the height of the tree. n ≥ 20 + 21 + 22 + . . . + 2h = 2h+1 - 1
n + 1 ≥ 2h+1
Since, there are 3 elements namely 3, 5, 8 in the node, it is required to To find the time complexity, it is required to find the lower bound as
well as the upper bound. So, taking log on both sides we get,
move the middle element to the root. Elements 2 and 4 can be the
log2 (n + 1) ≥ h + 1
children of node 3 and the elements 7 and 9 can be the children of node 8
as shown below: h ≤ log2 (n+1) - 1
To find the lower bound for 2-3 tree: Consider the 2-3 tree with each
node exhibiting the property of 2-nodes. To find the upper bound for 2-3 tree: Consider the 2-3 tree with
5 each node exhibiting the property of 3-nodes with each node having
Level 0 Number of nodes at level 0 = 1 = 2 0
maximum of two elements as shown below:
K 1, K 2 Level 0 : 2 * 3 0
Level 1
3 8 Number of nodes at level 1 = 2 = 2 1
Level 1 : 2 * 31
K1, K2 K 1, K 2 K1, K2
Level 2
Number of nodes at level 2 = 4 = 2 2
2 4 7 9
| | | | Level 2 : 2 * 32
Level i Number of nodes at level i = 2 i
| | | | K1, K2 K , K K , K K , K
| |1 2 |1 2 1| 2 K|1, K 2 K 1| , K 2 K 1|, K 2 K 1|, K 2 K|1, K 2
250 251 252
| | | | | | | | | Level i : 2 * 3 i
28
21-Feb-20
4 2 1 figure.
29
21-Feb-20
9 HEAPSORT HEAPSORT
This sorting algorithm was discovered by J. W. Williams. Stage 1 (Heap construction) Stage 2 (Maximum deletions)
8 6 2 9 7 6 5 8 9 6 8 2 5 7
This is a two-stage algorithm that works as follows: 2 9 8 6 5 7 7 6 8 2 5 9
2 5 1 Stage 1 (Heap construction): Construct a heap for a given 2 9 8 6 5 7 8 6 7 2 5
Step1 Step3 array. 9 2 8 6 5 7 5 6 7 2 8
1 Step2 1 8
Stage 2 (Maximum deletions): Apply the root-deletion 9 6 8 2 5 7 7 6 5 2
8 6 5 6 operation n-1 times to the remaining heap. 2 6 5 7
8 6
6 2 5
2 2 As a result, the array elements are eliminated in decreasing 5 2 6
2 5 9 5 1
order.
5 2
Figure: Deleting root’s key from heap. The key to be deleted is swapped with the last 2 5
But since under the array implementation of heaps, an
key, after which the smaller tree is “heapified” by exchanging the new key at 2
element being deleted is placed last, the resulting array will
its root with the larger key at its children until the parental dominance Figure: Sorting the array 2, 9, 7, 6, 5, 8 by heapsort.
265 be exactly the original array sorted in ascending order. 266 267
requirement is satisfied.
5 7 2 2 2
9 8 2 8 6 8
6 7 6 5 6 5 5 6 5 6
6 5 7 6 5 7 2 5 7
2 8 9 2 7 8 7 8 9 7 8 9
8 9 9
FIGURE: Bottom-up construction of a heap for the list 2, 9, 7, 6, 5, 8.
Heapify the list 5, 6, 7, 2 HEAP: Exchange 7 and 2 Heapify the list 2, 6, 5
After “Heapification”, the list becomes 9, 6, 8, 2, 5, 7. 268 269 Sorted list 2, 5, 6, 7, 8, 9 270
30
21-Feb-20
Analysis of Heapsort using bottom-up approach Analysis of Heapsort using bottom-up approach
The heap construction stage of the algorithm is in O(n). So, the total time complexity T(n) is given by the inequality:
T(n) ≤ 2log2 (n-1) + 2log2 (n-2) + 2log2 (n-3) + . . . + 2log2 1
Heapsort
n-1 n-1
Goal:
Now, it is required to analyze only the second stage.
• Observe that after exchanging the root with (n-1)th item, we have to T(n) ≤ 2 ∑ log2 i ≤ 2 ∑ log2 (n-1) which indicates that log2 (n-1) Sort an array using heap representations
reconstruct the heap for (n-1) elements which depends only on height i=1 i=1 appears (n-1) times.
of the tree.
Idea:
Therefore, the time complexity for the second stage = 2n log2 n ≈ n log2 n.
• So, time complexity to reconstruct the heap for (n-1) elements Build a max-heap from the array
= 2 log2 (n-1) So, the time complexity of heap sort =time complexity of stage 1 + time
• After exchanging 0th item with n-2 item, time complexity to complexity of stage 2 Swap the root (the maximum element) with
reconstruct the heap = 2 log2 (n-2)
= O(n) + O(n log2 n) the last element in the array
• After exchanging 0th item with n-3 item, time complexity to
reconstruct the heap = 2 log2 (n-3) = O(max{ n, n log2 n })
... ... = O(n log2 n) “Discard” this last node by decreasing the
After exchanging 0th item with 1st item, time complexity to Therefore, the time complexity of heapsort = O(nlog 2 n)
• heap size
reconstruct the heap = 2 log2 1
271 272
Call MAX-HEAPIFY on the new root
Repeat this process until only one node
HEAP-INCREASE-KEY O(lgn)
Running time: O(nlgn) --- HEAP-MAXIMUM O(1)
• Using balanced trees (2-3, 2-3-4, red-black, and AVL trees) we can • A hash function tells us where to place an item in array called a
implement table operations (retrieval, insertion and deletion) hash table. This method is know as hashing.
efficiently. O(logN) • A hash function maps a search key into an integer between 0 and n-1.
– We can have different hash functions.
• Can we find a data structure so that we can perform these table
– Ex. h(x) = x mod n if x is an integer
operations better than balanced search trees? O(1) – The hash function is designed for the search keys depending on the data types of
YES HASH TABLES these search keys (int, string, ...)
• In hash tables, we have an array (index: 0..n-1) and an address • Collisions occur when the hash function maps more than one item into
Hash Function the same array.
calculator (hash function) which maps a search key into an array
index between 0 and n-1. – We have to resolve these collisions using certain mechanism.
• A perfect hash function maps each search key into a unique location
of the hash table.
– A perfect hash function is possible if we know all the search keys in advance.
Hash Table – In practice (we do not know all the search keys), a hash function can map more than
one key into the same location (collision).
21-Feb-20 CS202 - Fundamental Structures of Computer Science II 277 21-Feb-20 CS202 - Fundamental Structures of Computer Science II 278 21-Feb-20 CS202 - Fundamental Structures of Computer Science II 279
31
21-Feb-20
• We can design different hash functions. • If the search keys are big integers (Ex. nine-digit numbers), we can • Folding: Selecting all digits and add them
• But a good hash function should: select certain digits and combine to create the address.
– be easy and fast to compute, h(033475678) = 0 + 3+ 3 + 4 +7 + 5 + 6 +7 + 8 = 43
– place items evenly throughout the hash table. h(033475678) = 37 selecting 2nd and 5th digits (table size is 100)
• We will consider only hash functions operate on integers. h(023455678) = 25 0 h(nine-digit search key) 81
– If the key is not an integer, we map it into an integer first, and apply the hash
function.
• Digit-Selection is not a good hash function because it does not place • We can select a group of digits and we can add these groups too.
• The hash table size should be prime. items evenly throughout the hash table.
– By selecting the table size as a prime number, we may place items evenly
throughout the hash table, and we may reduce the number of collisions.
21-Feb-20 CS202 - Fundamental Structures of Computer Science II 280 21-Feb-20 CS202 - Fundamental Structures of Computer Science II 281 21-Feb-20 CS202 - Fundamental Structures of Computer Science II 282
Hash Functions
Hash Functions – Modula Arithmetic Collision Resolution
Converting Character String into An Integer
• Modula arithmetic provides a simple and effective hash function. • If our search keys are strings, first we have to convert the string into an • There are two general approaches to collision resolution in hash
– We will use modula arithmetic as our hash function in the rest of our discussions. integer, and apply a hash function which is designed to operate on tables:
integers to this integer value to compute the address. 1. Open Addressing – Each entry holds one item
h(x) = x mod tableSize 2. Chaining – Each entry can hold more than item
• We can use ASCII codes of characters in the conversion. • Buckets – hold certain number of items
– Consider the string “NOTE”, assign 1 (00001) to ‘A’, ....
• The table size should be prime.
– N is 14 (01110), O is 15 (01111), T is 20 (10100), E is 5 ((00101)
– Some prime numbers: 7,11, 13, ..., 101, ...
– Concatenate four binary numbers to get a new binary number
– 011100111111010000101 474,757
– apply x mod tableSize
21-Feb-20 CS202 - Fundamental Structures of Computer Science II 283 21-Feb-20 CS202 - Fundamental Structures of Computer Science II 284 21-Feb-20 CS202 - Fundamental Structures of Computer Science II 285
• During an attempt to insert a new item into a table, if the hash function • In linear probing, we search the hash table sequentially starting from
indicates a location in the hash table that is already occupied, we probe the original hash location.
for some other empty (or open) location in which to place the item.The – If a location is occupied, we check the next location
sequence of locations that we examine is called the probe sequence. – We wrap around from the last table location to the first table location if necessary.
If a scheme which uses this approach we say that
it uses open addressing
21-Feb-20 CS202 - Fundamental Structures of Computer Science II 286 21-Feb-20 CS202 - Fundamental Structures of Computer Science II 287 21-Feb-20 CS202 - Fundamental Structures of Computer Science II 288
32
21-Feb-20
Linear Probing - Example Linear Probing – Clustering Problem Open Addressing – Quadratic Probing
• One of the problems with linear probing is that table items tend to • Primary clustering problem can be almost eliminated if we use
• Example:
0 9 cluster together in the hash table. quadratic probing scheme.
– Table Size is 11 (0..10) – This means that the table contains groups of consecutively occupied locations.
1 • In quadratic probing,
– Hash Function: h(x) = x mod 11 • This phenomenon is called primary clustering. – We start from the original hash location i
2 2
– Insert keys: 3 13 • Clusters can get close to one another, and merge into a larger cluster. – If a location is occupied, we check the locations i+12 , i+22 , i+32 , i+42
• 20 mod 11 = 9 ...
4 25 – Thus, the one part of the table might be quite dense, even though another part has
• 30 mod 11 = 8 relatively few items. – We wrap around from the last table location to the first table location if necessary.
5 24
• 2 mod 11 = 2 • Primary clustering causes long probe searches and therefore decreases
6
• 13 mod 11 = 2 2+1=3 the overall efficiency.
• 25 mod 11 = 3 3+1=4 7
• 24 mod 11 = 2 2+1, 2+2, 2+3=5 8 30
• 10 mod 11 = 10 9 20
• 9 mod 11 = 9 9+1, 9+2 mod 11 =0 10 10
21-Feb-20 CS202 - Fundamental Structures of Computer Science II 289 21-Feb-20 CS202 - Fundamental Structures of Computer Science II 290 21-Feb-20 CS202 - Fundamental Structures of Computer Science II 291
Quadratic Probing - Example Open Addressing – Double Hashing Double Hashing - Example
21-Feb-20 CS202 - Fundamental Structures of Computer Science II 292 21-Feb-20 CS202 - Fundamental Structures of Computer Science II 293 21-Feb-20 CS202 - Fundamental Structures of Computer Science II 294
• In open addressing, to find an item with a given key: • Another way to resolve collisions is to change the structure of the hash
– We probe the locations (same as insertion) until we find the desired table.
item or we reach to an empty location. • In open-addressing, each location of the hash table holds only one
• Deletions in open addressing cause complications: item.
– We CANNOT simply delete an item from the hash table because • We can define a hash table so that each location is itself an array called
this new empty (deleted locations) cause to stop prematurely bucket, we can store the items which hash into this location in this
(incorrectly) indicating a failure during a retrieval. array.
– Problem: What will be the size of the bucket?
– Solution: We have to have three kinds of locations in a hash table:
Occupied, Empty, Deleted. • A better approach is to design the hash table as an array of linked lists,
– A deleted location will be treated as an occupied location during this collision resolution method is known as separate-chaining.
retrieval and insertion. • In separate-chaining , each entry (of the hash table) is a pointer to a
linked list (the chain) of the items that the hash function has mapped
into that location.
21-Feb-20 CS202 - Fundamental Structures of Computer Science II 295 21-Feb-20 CS202 - Fundamental Structures of Computer Science II 296 21-Feb-20 CS202 - Fundamental Structures of Computer Science II 297
33
21-Feb-20
• An analysis of the average-case efficiency of hashing involves the • What is the average number of probes for a successful
• For linear probing, the approximate average number of comparisons
load factor , which is the ration of the current number of items in search and an unsuccessful search for this hash table? 0 9
(probes) that a search requires as follows:
the table to the table size. – Hash Function: h(x) = x mod 11 1
= (current number of items) / tableSize 1 1 Successful Search: 2 2
1 – 20: 9 -- 30: 8 -- 2 : 2 -- 13: 2, 3 -- 25: 3,4
2 1
for a successful search
3 13
– 24: 2,3,4,5 -- 10: 10 -- 9: 9,10, 0
• The load factor measures how full a hash table is. Avg. Probe for SS = (1+1+1+2+2+4+1+3)/8=15/8 4 25
– The hash table should be so filled too much to get better performance from the 1 1 Unsuccessful Search: 5 24
1
2 (1 ) 2
hashing.
for an unsuccessful search – We assume that the hash function uniformly 6
• Unsuccessful searches generally require more time than successful distributes the keys. 7
searches. – 0: 0,1 -- 1: 1 -- 2: 2,3,4,5,6 -- 3: 3,4,5,6
• As load factor increases, the number of collisions increases – 4: 4,5,6 -- 5: 5,6 -- 6: 6 -- 7: 7 -- 8: 8,9,10,0,1
8 30
• In average case analyses, we assume that the hash function uniformly causing increased search times. 9 20
– 9: 9,10,0,1 -- 10: 10,0,1
distributes the keys in the hash table. • To maintain efficiency, it is important to prevent the hash table Avg. Probe for US = 10 10
from filling up. (2+1+5+4+3+2+1+1+5+4+3)/11=31/11
21-Feb-20 CS202 - Fundamental Structures of Computer Science II 298 21-Feb-20 CS202 - Fundamental Structures of Computer Science II 299 21-Feb-20 CS202 - Fundamental Structures of Computer Science II 300
1 1 log e (1 )
(log e 1 ) for a successful search
1 for a successful search
2
1
1
for an unsuccessful search
for an unsuccessful search
21-Feb-20 CS202 - Fundamental Structures of Computer Science II 301 21-Feb-20 CS202 - Fundamental Structures of Computer Science II 302 21-Feb-20 CS202 - Fundamental Structures of Computer Science II 303
What Constitutes a Good Hash Function Hash Table versus Search Trees Data with Multiple Organizations
• A hash function should be easy and fast to compute. • In the most of operations, the hash table performs better than search • Several independent data structure do not support all operations
• A hash function should scatter the data evenly throughout the hash trees. efficiently.
table. • But, the traversing the data in the hash table in a sorted order is very • We may need multiple organizations for data to get efficient
– How well does the hash function scatter random data? difficult. implementations for all operations.
– How well does the hash function scatter non-random data? – For similar operations, the hash table will not be good choice. – One organization will be used for certain operations, the other organizations will be
• Two general principles : – Ex. Finding all the items in a certain range. used for other operations.
21-Feb-20 CS202 - Fundamental Structures of Computer Science II 304 21-Feb-20 CS202 - Fundamental Structures of Computer Science II 305 21-Feb-20 CS202 - Fundamental Structures of Computer Science II 306
34
21-Feb-20
Data with Multiple Organizations (cont.) Data with Multiple Organizations (cont.)
ANALYSIS AND DESIGN OF
ALGORITHMS
UNIT-II
CHAPTER 4:
DIVIDE –AND- CONQUER
309
21-Feb-20 CS202 - Fundamental Structures of Computer Science II 307 21-Feb-20 CS202 - Fundamental Structures of Computer Science II 308
310 311
Figure: Divide – and – Conquer Technique. 312
Divide-and-Conquer technique is ideally suited for parallel Assuming that size n is a power of b, we get the following Θ(nd) if a < b d
computations, in which each subproblem can be solved recurrence for the running time T (n):
simultaneously by its own processor. Later all solutions can T (n) = aT (n/b) + f (n) ………….. (1) T(n) Є Θ(nd log n) if a = b d
be merged to get solution to original problem. Thus, the (General Divide-and-Conquer Recurrence)
execution speed of a program which is based on this Θ(nlogba) if a > b d
where f (n) is a function that accounts for the time spent on
technique can be improved significantly. dividing the problem into smaller ones and on combining
their solutions.
313 314 315
35
21-Feb-20
Divide – and – Conquer : Example: 4, 3, 2, 1 Analysis of the algorithm to find the sum of all the elements
Example1: Consider the problem of computing the sum of of the array using divide-and-Conquer technique
10
n numbers stored in an array. 4, 3 + 2, 1
7 3 Since the problem instance is divided into two parts, the recurrence
ALGORITHM Sum(A[0… n-1], low, high) 4 +
3 2 1 relation for this algorithm is:
+
//Determines sum of n numbers stored in an array A
//using divide-and-conquer technique recursively. A, 0, 3 A(n) = 0 if n = 1
//Input: An array A[0… n-1], low and high initialized to 0 and n-1 A(n/2) + A(n/2) + 1 otherwise
10
// respectively. A, 0, 1
A, 2, 3 No. of additions No. of additions to add the sum of
//Output: Sum of all the elements in the array.
7
if low = high return A[low] 3 on left part of array on right part of array left and right part
else return n2
3, 4 1, 2
36
21-Feb-20
Mergesort Mergesort
Mergesort ALGORITHM Merge(B[0 . . . p-1], C[0 . . . q-1], A[0 . . . p + q - 1]) #include<stdio.h>
ALGORITHM Mergesort(A[0…n-1]) //Merges two sorted arrays into one sorted array #include<conio.h>
//Input: Arrays B[0 . . . p-1] and C[0 . . . q-1] both sorted void mergesort(int *,int,int);
//Sorts array A[0…n-1] by recursive mergesort void merge(int *,int,int,int);
//Input: An array A[0…n-1] of orderable elements //Output: Sorted array A[0 . . . p + q - 1] of the elements of B & C void main()
i ← 0, j ← 0, k ← 0 { int i,a[10],n;
//Output: Array A[0…n-1] sorted in nondecreasing order
clrscr();
while i < p and j < q do
if n > 1 printf("enter n value\n");
if B[i] ≤ C[j] scanf("%d",&n);
copy A[0 . . . n/2 - 1] to B[0 . . . n/2 - 1]
A[k] ← B[i]; i ← i + 1
copy A[ n/2 . . . n - 1] to C[0 . . . n/2 – 1] printf("enter values\n");
else
Mergesort(B[0 . . . n/2 – 1]) for(i=0;i<n;i++)
A[k] ← C[j]; j ← j + 1 scanf("%d",&a[i]);
Mergesort(C[0 . . . n/2 – 1]) k←k+1
Merge(B, C, A) if i = p
mergesort(a,0,n-1);
printf("\nSorted array\n");
copy C[j . . . q – 1] to A[k . . . p + q – 1] for(i=0;i<n;i++)
else printf("%d\t",a[i]);
325 326 getch(); 327
copy B[i . . . p – 1] to A[k . . . p + q – 1]
}
7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 7 29 4 2 4 7 9 3 8 6 1 1 3 8 6 7 29 4 2 4 7 9 3 8 6 1 1 3 8 6
7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6 7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6 722 7 9 4 4 9 3 8 3 8 6 1 1 6
77 22 99 44 33 88 66 11 77 22 99 44 33 88 66 11 77 22 99 44 33 88 66 11
331 332 333
37
21-Feb-20
77 22 99 44 33 88 66 11 77 22 99 44 33 88 66 11 77 22 99 44 33 88 66 11
334 335 336
77 22 99 44 33 88 66 11 77 22 99 44 33 88 66 11 77 22 99 44 33 88 66 11
337 338 339
38
21-Feb-20
Partition Procedure 1. While A[i] <= A[pivot] 1. While A[i] <= A[pivot]
ALGORITHM Partition(A[l..r]) ++i ++i
//Partitions subarray by using its first element as a pivot
//Input: subarray A[l..r] of A[0..n - 1], defined by its left and right
indices l and r (l< r)
//Output: A partition of A[l..r], with the split position returned as this
function’s value
p A[l]
i l; j r + 1
repeat
repeat i i + 1 until A[i] ≥ p
repeat j j -1 until A[j ] ≤ p Pivot_index = 0 40 20 10 80 60 50 7 30 100 pivot_index = 0 40 20 10 80 60 50 7 30 100
swap(A[i], A[j ])
[0] [1] [2] [3] [4] [5] [6] [7] [8] [0] [1] [2] [3] [4] [5] [6] [7] [8]
until i ≥ j
swap(A[i], A[j ]) //undo last swap when i ≥ j
swap(A[l], A[j ]) i j i j
return j 349 350 351
39
21-Feb-20
1. While A[i] <= A[pivot] 1. While A[i] <= A[pivot] 1. While A[i] <= A[pivot]
++i ++i ++i
2. While A[j] > A[pivot] 2. While A[j] > A[pivot]
--j --j
i j i j i j
352 353 354
1. While A[i] <= A[pivot] 1. While A[i] <= A[pivot] 1. While A[i] <= A[pivot]
++i ++i ++i
2. While A[j] > A[pivot] 2. While A[j] > A[pivot] 2. While A[j] > A[pivot]
--j --j --j
3. If i < j 3. If i < j 3. If i < j
Swap A[i] and A[j] Swap A[i] and A[j] Swap A[i] and A[j]
4. While j > i goto step 1
i j i j i j
355 356 357
1. While A[i] <= A[pivot] 1. While A[i] <= A[pivot] 1. While A[i] <= A[pivot]
++i ++i ++i
2. While A[j] > A[pivot] 2. While A[j] > A[pivot] 2. While A[j] > A[pivot]
--j --j --j
3. If i < j 3. If i < j 3. If i < j
Swap A[i] and A[j] Swap A[i] and A[j] Swap A[i] and A[j]
4. While j > i goto step 1 4. While j > i goto step 1 4. While j > i goto step 1
i j i j i j
358 359 360
40
21-Feb-20
1. While A[i] <= A[pivot] 1. While A[i] <= A[pivot] 1. While A[i] <= A[pivot]
++i ++i ++i
2. While A[j] > A[pivot] 2. While A[j] > A[pivot] 2. While A[j] > A[pivot]
--j --j --j
3. If i < j 3. If i < j 3. If i < j
Swap A[i] and A[j] Swap A[i] and A[j] Swap A[i] and A[j]
4. While j > i goto step 1 4. While j > i goto step 1 4. While j > i goto step 1
i j i j i j
361 362 363
1. While A[i] <= A[pivot] 1. While A[i] <= A[pivot] 1. While A[i] <= A[pivot]
++i ++i ++i
2. While A[j] > A[pivot] 2. While A[j] > A[pivot] 2. While A[j] > A[pivot]
--j --j --j
3. If i < j 3. If i < j 3. If i < j
Swap A[i] and A[j] Swap A[i] and A[j] Swap A[i] and A[j]
4. While j > i goto step 1 4. While j > i goto step 1 4. While j > i goto step 1
i j i j i j
364 365 366
1. While A[i] <= A[pivot] 1. While A[i] <= A[pivot] 1. While A[i] <= A[pivot]
++i ++i ++i
2. While A[j] > A[pivot] 2. While A[j] > A[pivot] 2. While A[j] > A[pivot]
--j --j --j
3. If i < j 3. If i < j 3. If i < j
Swap A[i] and A[j] Swap A[i] and A[j] Swap A[i] and A[j]
4. While j > i goto step 1 4. While j > i goto step 1 4. While j > i goto step 1
i j i j j i
367 368 369
41
21-Feb-20
j i j i j i
370 371 372
pivot_index = 4 7 20 10 30 40 50 60 80 100 < = A[pivot] > A[pivot] < = A[pivot] > A[pivot]
j i
373 374 375
42
21-Feb-20
1. While A[i] <= A[pivot] 1. While A[i] <= A[pivot] 1. While A[i] <= A[pivot]
++i ++i ++i
2. While A[j] > A[pivot] 2. While A[j] > A[pivot] 2. While A[j] > A[pivot]
--j --j --j
3. If i < j 3. If i < j 3. If i < j
Swap A[i] and A[j] Swap A[i] and A[j] Swap A[i] and A[j]
4. While j > i goto step 1 4. While j > i goto step 1 4. While j > i goto step 1
5. Swap A[j] and A[pivot] 5. Swap A[j] and A[pivot] 5. Swap A[j] and A[pivot]
j i j j i
i 382 383 384
1. While A[i] <= A[pivot] 1. While A[i] <= A[pivot] 1. While A[i] <= A[pivot]
++i ++i ++i
2. While A[j] > A[pivot] 2. While A[j] > A[pivot] 2. While A[j] > A[pivot]
--j --j --j
3. If i < j 3. If i < j 3. If i < j
Swap A[i] and A[j] Swap A[i] and A[j] Swap A[i] and A[j]
4. While j > i goto step 1 4. While j > i goto step 1 4. While j > i goto step 1
5. Swap A[j] and A[pivot] 5. Swap A[j] and A[pivot] 5. Swap A[j] and A[pivot]
j i j i j i
385 386 387
43
21-Feb-20
1. While A[i] <= A[pivot] Quick Sort Worst case analysis Quick Sort Average case analysis
++i Let Cavg (n) be the average number of key comparisons made by
In the worst case, all the splits will be skewed to the extreme: one of
2. While A[j] > A[pivot] the two subarrays will be empty, while the size of the other will be quicksort on a randomly ordered array of size n.
--j just one less than the size of the subarray being partitioned. This
3. If i < j situation arises in particular, for increasing arrays(already Sorted). Cavg(0) = 0, Cavg(1) = 0.
Swap A[i] and A[j] If A[0 . . . n-1] is a strictly increasing array and we use A[0] as the
pivot, the left-to-right scan will stop on A[1] while the right-to-left After the split QUICKSORT calls itself to sort two subarrays, the
4. While j > i goto step 1 average comparisons to sort an array A[0 . . s-1] is Cavg(s) and the
5. Swap A[j] and A[pivot] scan will go all the way to reach A[0], indicating the split at position
average comparisons to sort an array A[s+1 . . n-1] is Cavg(n-1-s).
0: j i
A[0] A[1] ... A[n-1] Assuming that the partition split can happen in each position s
(0 ≤ s ≤ n-1) with the same probability 1/n, we get the following
So, after making n + 1 comparisons to get to this partition and recurrence relation:
exchanging the pivot A[0] with itself, the algorithm will find itself n-1
pivot_index = 0 2 4 10 12 13 50 57 63 100
with the strictly increasing array A[1 . . . n-1]. This sorting continues Cavg(n) = 1/n ∑ [(n + 1) + Cavg(s) + Cavg(n-1-s)] for n > 1
[0] [1] [2] [3] [4] [5] [6] [7] [8] untill the last one A[n-2] . . . n-1] has been processed. The total s=0
number of key comparisons made will be equal to
After solving the above recurrence, we get the solution,
Cworst(n) = (n + 1) + n + . . . + 3 = (n +1) (n +2) - 3 Є Θ(n2 ).
2 Cavg(n) ≈ 2n ln n ≈ 1.38n log2 n
<= data[pivot] > data[pivot] 388 389 390
It works by comparing a search key K with the array’s Example: low high
middle element A[m]. Maintain array of Items.
Store in sorted order.
Use binary search to FIND Item with Key K= 33. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
If they match, the algorithm stops; otherwise, the same
operation is repeated recursively for the first half of the Value 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
array if K <A[m] and for the second half if K >A[m]: Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Value 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 if Key K is in array,
then it has index
low=0
between low and
high.
high=n-1=15-1=14
44
21-Feb-20
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Value 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 Value 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
Value 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Value 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 Value 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
Value 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Value 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 Value 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Value 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
high Since 33 < 43, we can
mid Compute mid position
reduce search
and check if matching high Matching Key found.
interval.
Key is in that position. Return index 4.
45
21-Feb-20
46
21-Feb-20
415 417
47
21-Feb-20
Insertion Sort
Variable – Size – Decrease: Decrease – by – one technique is applied to sort an array A[0 . . . n-1].
We assume that the smaller problem of sorting the array A[0 . . . n-2]
Consider the problem of exponentiation: Compute an In this variation, the size reduction pattern varies from has already been solved to give us a sorted array of size n – 1:
one iteration of an algorithm to another. A[0] ≤ . . . ≤ A[n – 2].
Brute Force: an = a * a * a * a * . . . * a All we need is to find an appropriate position for A[n – 1] among sorted
Euclid’s algorithm for computing the GCD provides a elements and insert it there.
good example of such a situation:
Divide and conquer: an = an/2 * an/2 m if n = 0 There are three reasonable alternatives for doing this:
• First, we can scan the sorted subarray from left to right until the
GCD(m, n) = GCD(n, m mod n) otherwise first element greater than or equal to A[n – 1] is encountered and
then insert A[n – 1] right before that element.
Decrease by one: an = an-1 * a
• Second, we can scan the sorted subarray from right to left until the
first element smaller than or equal to A[n – 1] is encountered and
then insert A[n – 1] right after that element.
Decrease by constant factor: an = (an/2)2 if n is even
o This is implemented in practice because it is better for sorted or
almost-sorted arrays. The resulting algorithm is called straight
424 425 insertion sort or simply insertion sort. 426
an = (a(n-1)/2)2 if n is odd
As shown in below figure, starting with A[1] and ending with A[n – 1], printf("\nEnter the array elements\n");
A[i] is inserted in its appropriate place among the first i elements of the for(i=0;i<n;i++)
scanf("%d",&a[i]);
array that have been already sorted.
printf("\nArray before sorting\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
A[0] ≤ . . . ≤ A[j] < A[j + 1] ≤ . . . ≤ A[i – 1] | A[i] . . . A[n – 1] insertion(a,n);
printf("\nArray after sorting\n");
Smaller than or equal to A[i] greater than A[i]
for(i=0;i<n;i++)
427 428 printf("%d\n",a[i]); 429
48
21-Feb-20
Insertion Sort
Insertion Sort Example: Insertion Sort Example:
Analysis:
Insert Action: i=3, third iteration Insert Action: i=4, fourth iteration
temp a[0] a[1] a[2] a[3] a[4] temp a[0] a[1] a[2] a[3] a[4] The number of key comparisons in this algorithm obviously
depends on the nature of the input.
10 5 8 20 10 7 7 5 8 10 20 7
In the worst case, A[j] > v is executed largest number of times. i.e.,
10 5 8 20 20 7 7 5 8 10 20 20 for every j = i – 1, . . .,0.
• Thus, for the worst-case input, we get A[0] > A[1](for i =1), A[1]
> A[2](for i=2), . . . A[n – 2] > A[n – 1](for i=n-1).
--- 5 8 10 20 7 7 5 8 10 10 20 n-1 i-1 n-1
--- 5 7 8 10 20 • Thus, in the worst-case, insertion sort makes exactly the same
433 434
number of comparisons as selection sort. 435
Sorted ARRAY
Insertion Sort
Analysis: Graph Traversal Graph Traversal
Depth-First Search (DFS) and Breadth-First Search (BFS):
In the Best case, the comparison A[j] > v is executed Many graph algorithms require processing • Two elementary traversal algorithms that provide an
only once on every iteration of outer loop. vertices or edges of a graph in a systematic efficient way to “visit” each vertex and edge exactly
• It happens if and only if A[i – 1] ≤ A[i] for every fashion. once.
i = 1, . . . n – 1, i.e., if the input array is already sorted • Both work on directed or undirected graphs.
in ascending order. • Many advanced graph algorithms are based on the
n-1
Two principal Graph traversal algorithms: concepts of DFS or BFS.
Cbest(n) = ∑1 = n - 1 Є Θ(n) • The difference between the two algorithms is in the
i=1 Depth-first search (DFS) order in which each “visits” vertices.
In the Average case,
Cavg(n) ≈ n2/4 Є Θ(n2 ) Breadth-first search (BFS)
436 437 438
Depth-First Search
DFS starts visiting vertices of a graph at an arbitrary vertex by
Depth-First Search Pseudo code of DFS
marking it as having been visited.
On each iteration, the algorithm proceeds to an unvisited vertex that is Whenever a new unvisited vertex is reached for the first
adjacent to the last visited vertex . time, it is attached as a child to the vertex from which it
is being reached. Such an edge is called a tree edge
This process continues until a dead end – a vertex with no adjacent because the set of all such edges forms a forest.
unvisited vertices – is encountered.
49
21-Feb-20
e c
A A A A
d B D E B D E B D E B D E
tree edge
h
C C C C
back edge 442 443 444
Applications of DFS
How efficient is Depth-First Search ? Depth-First Search Checking connectivity:
Since DFS halts after visiting all the vertices connected by a path to the
DFS algorithm is quite efficient since it takes just the We can look at the DFS forest as the given graph with its edges starting vertex, checking a graph’s connectivity can be done as follows:
time proportional to the size of the data structure used classified by the DFS traversal into two disjoint classes: tree edges Start a DFS traversal at an arbitrary vertex and check, after the
for representing the graph in question. and back edges. algorithm halts, whether all the graph’s vertices will have been visited.
- tree edges are edges used by the DFS traversal to reach previously If they have, the graph is connected; otherwise, it is not connected.
unvisited vertices.
Thus, for the adjacency matrix representation, the - back edges connect vertices to previously visited vertices other
than their immediate predecessors in the traversal. Identifying connected components of a graph.
traversal’s time is in Θ(|V|2), and for the adjacency list
representation, it is in Θ(|V| + |E|) where |V| and DFS yields two distinct orderings of vertices:
|E| are the number of the graph’s vertices and edges, Checking acyclicity:
- order in which the vertices are reached for the first time (pushed
respectively. onto stack). If the graph does not have back edges, then it is clearly acyclic.
- order in which the vertices become dead-ends (popped off stack).
These orders are qualitatively different , and various applications
can take advantage of either of them. Finding articulation points of a graph:
A vertex of a connected graph is said to be its articulation point if its
removal with all edges incident to it breaks the graph into disjoint
445 446 pieces. 447
50
21-Feb-20
L1 L1 L1 L1
B C D B C D B C D B C D BFS yields a single ordering of vertices because the queue is a FIFO
structure and hence the order in which vertices are added to the
L2 L2 L2 queue is the same order in which they are removed from it (order
E F E F E F E F added/deleted from queue is the same).
Tree edges are the ones used to reach previously unvisited vertices.
L0 L0 L0
A A A
Cross edges connect vertices to those visited before, but, unlike back
L1 L1 L1 edges in a DFS tree, they connect vertices either on the same or
B C D B C D B C D adjacent levels of a BFS tree.
L2 L2 L2
E F E F E F
454 455 456
Applications of BFS Main facts about DFS and BFS Few Basic Facts about Directed graphs
Checking connectivity of a graph DFS BFS
A directed graph, or digraph, is a graph with directions
Data structure stack queue
Checking acyclicity of a graph specified for all its edges.
No. of vertex orderings 2 orderings 1 ordering
BFS can be helpful in some situations where DFS cannot. Edge types tree and back tree and cross
There are only two notable differences between
• For example, BFS can be used for finding a path with the fewest (undirected graphs) edges edges undirected and directed graphs in representing the
number of edges between two given vertices. We start a BFS Applications connectivity, connectivity, adjacency matrix and adjacency list:
traversal at one of the two vertices given and stop it as soon as
the other vertex is reached. acyclicity, acyclicity, The adjacency matrix of a directed graph does not
articulation minimum-edge have to be symmetric.
points paths An edge in a directed graph has just one ( not two)
Efficiency for adjacency matrix Θ(|V|2) Θ(|V|2) corresponding nodes in the digraph’s adjacency lists.
Note: BFS is not applicable for finding articulation points
of a graph.
Efficiency for adjacency lists Θ(|V| + |E|) Θ(|V| + |E|)
51
21-Feb-20
Topological sorting Algorithms Topological Ordering: Source Removal Topological Ordering: Source Removal
2. Source removal algorithm: Algorithm: Example Algorithm: Example
This algorithm is based on direct implementation of the
decrease( by one) – and – conquer technique: Repeatedly identify
and remove a source vertex, ie, a vertex that has no incoming v2 v3 v2 v3
edges
v7 v11 v7
52
21-Feb-20
Topological Ordering: Source Removal Topological Ordering: Source Removal Topological Ordering: Source Removal
Algorithm: Example Algorithm: Example Algorithm: Example
v3
v6 v5 v4 v6 v5 v4 v6 v5
v7 v7 v7
Topological order: v1, v2 Topological order: v1, v2, v3 Topological order: v1, v2, v3 , v4
Topological Ordering: Source Removal Topological Ordering: Source Removal Topological Ordering: Source Removal
Algorithm: Example Algorithm: Example Algorithm: Example
v2 v3
v6 v6 v5 v4 v1 v2 v3 v4 v5 v6 v7
v7 v1
v7 v7
Topological order: v1, v2, v3, v4, v5, v6 Topological order: v1, v2, v3 , v4, v5, v6, v7.
Topological order: v1, v2, v3, v4, v5
53
21-Feb-20
Generating permutations
Generating Permutations Lexicographic order algorithm
The Johnson-Trotter algorithm does not produce 1. Initial permutation: a 1, a2, … an in increasing order
An application of Johnson Trotter permutations in lexicographic order
2. Scan a current permutation from right to left looking
algorithm :
Example: for the first pair of consecutive elements a i and ai+1
such that ai < ai+1
o Johnson-Trotter algorithm: 123, 132, 312, 321, 231, 213
123 132 312 321 231 213 o Lexicographic order: 123, 132, 213, 231, 312, 321 3. Find the smallest digit in the tail that is larger than a i
and put it in position i
This algorithm is one of the most efficient for generating 4. Put the rest n elements in increasing order in
permutations. position i+1 to n
It can be implemented to run in time proportional to the
number of permutations, i.e., in Θ(n!).
481 482 483
54