Professional Documents
Culture Documents
Divide the problem (instance) into sub problems of sizes that are fractions of the
original problem size.
The value stored at location 7 is not a match, rather it is more than what we are looking
for. So, the value must be in the lower part from this location.
Here it is, 0 + (9 - 0 ) / 2 = 4 (integer value of 4.5). So, 4 is the mid of the array.
Hence, we calculate the mid again. This time it is 5.
.
We compare the value stored at location 5 with our target value. We find that it is a match
Our new mid is 7 now. We compare the value stored at location 7 with our target
value 31.
BINARY SEARCH (ALGORITHM)
Algorithm BinSearch(l, h, key) T(n) else
{ {
if (l==h) mid = (l+h)/2; 1
{ if(key == A[mid]) 1
if(A[l] == key) 1 return mid;
return l; if(key < A[mid])
else return BinSearch(l,mid-1, key);
T(n/2)
return 0; else
} return BinSearch(mid +1, h,
key)
}
}
BINARY SEARCH (RECURRENCE
RELATION FROM THE ALGORITHM)
b
O(1) if n =2 1
Recurrence T (n)
2T (n / 2) bn1 if n >2 1
Relation:
Mergesort
• Mergesort.
• Divide array into two halves.
• Recursively sort each half.
• Merge two halves to make sorted whole.
A L G O R I T H M S
A L G O R I T H M S divide O(1)
A G L O R H I M S T sort 2T(n/2)
A G H I L M O R S T merge O(n)
Merge-Sort
• Merge-sort on an input sequence S with n elements consists of three steps:
• Divide: partition S into two sequences S1 and S2 of about n/2 elements each
• Recur: recursively sort S1 and S2
• Conquer: merge S1 and S2 into a unique sorted sequence
Part Divide and
3 Conquer
MergeSort Example
85 24 63 45 17 31 96 50
Master Informatique
Part Divide and
3 Conquer
MergeSort Example
17 31 96 50
85 24 63 45
Master Informatique
Part Divide and
3 Conquer
MergeSort Example
17 31 96 50
63 45
85 24
Master Informatique
Part Divide and
3 Conquer
MergeSort Example
17 31 96 50
63 45
24
85
Master Informatique
Part Divide and
3 Conquer
MergeSort Example
17 31 96 50
63 45
85
8 24
5
Master Informatique
Part Divide and
3 Conquer
MergeSort Example
17 31 96 50
63 45
85
8
5
24
Master Informatique
Part
3
MergeSort Example
17 31 96 50
63 45
85
8 24
2
5 4
Master Informatique
Part Divide and
3 Conquer
MergeSort Example
17 31 96 50
63 45
24 85
Master Informatique
Part Divide and
3 Conquer
MergeSort Example
17 31 96 50
24 85 63 45
Master Informatique
Part
3
MergeSort Example
17 31 96 50
24 85
63 45
Master Informatique
Part Divide and
3 Conquer
MergeSort Example
17 31 96 50
24 85
45
63
Master Informatique
Part Divide and
3 Conquer
MergeSort Example
17 31 96 50
24 85
63 45
Master Informatique
Part
3
MergeSort Example
17 31 96 50
24 85
63
45
Master Informatique
Part Divide and
3 Conquer
MergeSort Example
17 31 96 50
24 85
63 45
Master Informatique
Part Divide and
3 Conquer
MergeSort Example
17 31 96 50
24 85
45 63
Master Informatique
Part
3
MergeSort Example
17 31 96 50
24 85 45 63
Master Informatique
Part Divide and
3 Conquer
MergeSort Example
17 31 96 50
24 45 63 85
Master Informatique
Part Divide and
3 Conquer
MergeSort Example
24 45 63 85 17 31 96 50
Master Informatique
Part
3
MergeSort Example
24 45 63 85
17 31 96 50
Master Informatique
Part
3
MergeSort Example
24 45 63 85
17 31 96 50
Master Informatique
Part Divide and
3 Conquer
MergeSort Example
24 45 63 85
17 31 50 96
Master Informatique
Part Divide and
3 Conquer
MergeSort Example
24 45 63 85 17 31 50 96
Master Informatique
Part Divide and
3 Conquer
MergeSort Example
17 24 31 45 50 63 85 96
Master Informatique
MERGE SORT (ALGORITHM)
Algorithm MergeSort(l,h) T(n)
{
if (l < h) 1
{
1
mid = (l+h)/2;
MergeSort(l, mid); T(n/2)
MergeSort(mid+1,h);
T(n/2)
Merge ( l, mid, mid+1, h); n
}
b
O(1) if n =2 1
T (n)
}
Recurrence
2T (n / 2) bn if n >2 1
Relation:
RECURRENCE RELATION
• Many algorithms are recursive in nature, it is natural to analyze algorithms based
on recurrence relations.
• Recurrence relation is a mathematical model that captures the underlying time-
complexity of an algorithm.
• Methods given below are used to derive solutions to recurrence relations that
yields the time-complexity of algorithms:
Substitution method,
Recurrence tree method,
Master Theorem
SUBSTITUTION METHOD
(BINARY SEARCH)
O(1)
b if n =2 1
T (n)
2T (n / 2) bn
1 if n >2 1
T(n) = T(n/2) + c (1)
but T(n/2) = T(n/22) + c, so
T(n) = T(n/22) + c + c
T(n) = T(n/22) + 2c (2)
T(n/22) = T(n/23) + c
T(n) = T(n/23) + c + 2c
T(n) = T(n/23) + 3c (3)
RECURSION TREE
Master Informatique
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
T(n)
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn
T(n/2) T(n/2)
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn
cn/2 cn/2
(1)
September 7, 2005
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn
cn/2 cn/2
h = lg n cn/4 cn/4 cn/4 cn/4
(1)
September 7, 2005
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn cn
cn/2 cn/2
h = lg n cn/4 cn/4 cn/4 cn/4
(1)
September 7, 2005
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn cn
cn/2 cn/2 cn
h = lg n cn/4 cn/4 cn/4 cn/4
(1)
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn cn
cn/2 cn/2 cn
h = lg n cn/4 cn/4 cn/4 cn/4 cn
…
(1)
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn cn
cn/2 cn/2 cn
h = lg n cn/4 cn/4 cn/4 cn/4 cn
…
(1) #leaves = n (n)
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn cn
cn/2 cn/2 cn
h = lg n cn/4 cn/4 cn/4 cn/4 cn
…
(1) #leaves = n (n)
Total (n lg n)
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
T(n)
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
n2
T(n/4) T(n/2)
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
n2
(n/4)2 (n/2)2
(1)
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
n2 n2
(n/4)2 (n/2)2
(1)
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
n2 n2
5 n2
(n/4)2 (n/2)2
16
(n/16)2 (n/8)2 (n/8)2 (n/4)2
(1)
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
n2 n2
5 n2
(n/4)2 (n/2)2
16
(n/16)2 (n/8)2 (n/8)2 (n/4)2 25 n2
256
…
(1)
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
n2 n2
5 n2
(n/4)2 (n/2)2
16
(n/16)2 (n/8)2 (n/8)2 (n/4)2 25 n2
256
…
(1) Total = n2
1 16 16
5 5 2 5 3
16
= (n2) geometric series
The master method
Invariant: x x x ?
p i j q
Example of partitioning
6 10 13 5 8 3 2 11
i j
Example of partitioning
6 10 13 5 8 3 2 11
i j
Example of partitioning
6 10 13 5 8 3 2 11
i j
Example of partitioning
6 10 13 5 8 3 2 11
6 5 13 10 8 3 2 11
i j
Example of partitioning
6 10 13 5 8 3 2 11
6 5 13 10 8 3 2 11
i j
Example of partitioning
6 10 13 5 8 3 2 11
6 5 13 10 8 3 2 11
i j
Example of partitioning
6 10 13 5 8 3 2 11
6 5 13 10 8 3 2 11
6 5 3 10 8 13 2 11
i j
Example of partitioning
6 10 13 5 8 3 2 11
6 5 13 10 8 3 2 11
6 5 3 10 8 13 2 11
i j
Example of partitioning
6 10 13 5 8 3 2 11
6 5 13 10 8 3 2 11
6 5 3 10 8 13 2 11
6 5 3 2 8 13 10 11
i j
Example of partitioning
6 10 13 5 8 3 2 11
6 5 13 10 8 3 2 11
6 5 3 10 8 13 2 11
6 5 3 2 8 13 10 11
i j
Example of partitioning
6 10 13 5 8 3 2 11
6 5 13 10 8 3 2 11
6 5 3 10 8 13 2 11
6 5 3 2 8 13 10 11
i j
Example of partitioning
6 10 13 5 8 3 2 11
6 5 13 10 8 3 2 11
6 5 3 10 8 13 2 11
6 5 3 2 8 13 10 11
2 5 3 6 8 13 10 11
i
Pseudocode for quicksort
QUICKSORT(A, p, r)
if p < r
then q PARTITION(A, p, r)
QUICKSORT(A, p, q–1)
QUICKSORT(A, q+1, r)
Analysis of quicksort
(1)
Worst-case recursion tree
T(n) = T(0) + T(n–1) + cn
cn n
T(0) c(n–1) k n2
k 1
T(0) c(n–2)
T(0)
(1)
Worst-case recursion tree
T(n) = T(0) + T(n–1) + cn
cn n
(1) c(n–1) k n2
k 1
(1) c(n–2)
h=n T(n) = (n) + (n2)
(1) = (n2)
(1)
Best-case analysis
If we’re lucky, PARTITION splits the array evenly:
T(n) = 2T(n/2) + (n)
= (n lg n) (same as merge sort)
1 : 9
What if the split is always 10 10 ?
T (n) T 10
1
n T 109 n (n)
What is the solution to this recurrence?
Analysis of “almost-best” case
T(n)
Analysis of “almost-best” case
cn
T 101 n T 109 n
Analysis of “almost-best” case
cn
1
10
cn 9cn
10
T 100
1
n T 100
9
n T 100
9
n T 100
81
n
Analysis of “almost-best” case
cn cn
1
10
cn 9cn cn
10
log10/9n
1 cn 9 cn 9cn 81 cn
cn
100 100 100 100
…
(1) O(n) leaves
(1)
Analysis of “almost-best” case
cn cn
1
10
cn 9cn cn
10
log10 log10/9n
n 1 cn 9 cn 9cn 81 cn
cn
100 100 100 100
…
(1) O(n) leaves
(n lg n) (1)
Lucky! cn log10n T(n) cn log10/9n + (n)
Randomized quicksort
IDEA: Partition around a random element.
• Running time is independent of the input order.
• No assumptions need to be made about the input distribution.
• No specific input elicits the worst-case behavior.
• The worst case is determined only by the output of a random-
number generator.
Randomized quicksort analysis
Let T(n) = the random variable for the running time of
randomized quicksort on an input of size n, assuming random
numbers are independent.
Matrix multiplication
Input: A = [aij], B = [bij].
i, j = 1, 2,… , n.
Output: C = [cij] = A B.
c11 c12 c1n a11 a12 a1n b11 b12 b1n
c c c2n a21 a22 a2n b21 b22 b 2n
21 22
c c
n1 n2 cnn an1 an2 ann bn1 bn2 bnn
n
cij aik bkj
k 1
Standard algorithm
for i 1 to n
do for j 1 to n
do cij 0
for k 1 to n
do cij cij + aik bkj
Standard algorithm
for i 1 to n
do for j 1 to n
do cij 0
for k 1 to n
do cij cij + aik bkj
The number 2.81 may not seem much smaller than 3, but because
the difference is in the exponent, the impact on running time is
significant.
Best to date (of theoretical interest only): (n2.376).