Professional Documents
Culture Documents
(ESO207A)
Lecture 17
• Divide and Conquer -II
• Proof of correctness of the algorithm for 2-Majority element
1
Recap of the last Lecture
2
Divide and Conquer paradigm
An Overview
1. Divide the problem instance into two or more instances of the same problem
3
Example 2
4
Pursuing Divide and Conquer approach
MSB LSB
𝑨 𝑩 𝑿
×
n-1 n-2…......………………n/2.............................2 1 0
multiplications
𝑪 𝑫 𝒀
5
Pursuing Divide and Conquer approach
MSB LSB
𝑨 𝑩 𝑿
×
n-1 n-2…......………………n/2.............................2 1 0
𝑪 𝑫 𝒀
= ( ) ?? ( + ) ??𝟐𝒏 + 𝟐𝒏/𝟐
Multiplications
Multiplications
6
Pursuing Divide and Conquer approach
MSB LSB
𝑨 𝑩 𝑿
×
n-1 n-2…......………………n/2.............................2 1 0
𝑪 𝑫 𝒀
= ( ) ?? ( + ) ??𝟐𝒏 + 𝟐𝒏/𝟐
𝑨× 𝑪+¿ ( + ) +¿𝑩× 𝑫 ¿ () ()
7
Pursuing Divide and Conquer approach
MSB LSB
𝑨 𝑩 𝑿
×
n-1 n-2…......………………n/2.............................2 1 0
𝑪 𝑫 𝒀
= ( ) ?? ( + ) ??𝟐𝒏 + 𝟐𝒏/𝟐
(+)
¿ () ()
8
Pursuing Divide and Conquer approach
MSB LSB
𝑨 𝑩 𝑿
×
n-1 n-2…......………………n/2.............................2 1 0
𝑪 𝑫 𝒀
= ( ) ?? ( + ) ??𝟐𝒏 + 𝟐𝒏/𝟐
𝑨× 𝑪−( + ) +¿𝑩× 𝑫 ¿ () ()
9
Pursuing Divide and Conquer approach
MSB LSB
𝑨 𝑩 𝑿
×
n-1 n-2…......………………n/2.............................2 1 0
𝑪 𝑫 𝒀
= ( ) ?? ( + ) ??𝟐𝒏 + 𝟐𝒏/𝟐
−( + ) ¿ () ()
− 𝑨× 𝑪 − 𝑩× 𝑫
10
Pursuing Divide and Conquer approach
MSB LSB
𝑨 𝑩 𝑿
×
n-1 n-2…......………………n/2.............................2 1 0
𝑪 𝑫 𝒀
= ( ) ?? ( + ) ??𝟐𝒏 + 𝟐𝒏/𝟐
(+)
¿− () ()
+¿ 𝑨× 𝑪 +¿ 𝑩× 𝑫
11
Pursuing Divide and Conquer approach
MSB LSB
𝑨 𝑩 𝑿
×
n-1 n-2…......………………n/2.............................2 1 0
𝑪 𝑫 𝒀
= ( ) ?? ( + ) ??𝟐𝒏 + 𝟐𝒏/𝟐
(+)
¿ () ()
12
Pursuing Divide and Conquer approach
MSB LSB
𝑨 𝑩 𝑿
×
n-1 n-2…......………………n/2.............................2 1 0
𝑪 𝑫 𝒀
= ( ) ?? ( + ) ??𝟐𝒏 + 𝟐𝒏/𝟐
(+)
¿ () () +¿ 𝑨× 𝑪 +¿ 𝑩× 𝑫
Multiplications
14
Conclusion
Theorem: There is a divide and conquer based algorithm for multiplying any
two -bit numbers in () time (bit operations).
Note:
The fastest algorithm for this problem runs in almost ( log ) time.
One such algorithm was designed in 2008 at CSE, IIT Kanpur.
By (Dey, Kurur, Saha, and Saptharishi).
But this algorithm is beyond the scope of this course
15
Example 3
16
Counting Inversions in an array
A 3 15 8 19 9 67 11 27
Inversions are :
(1,2), (1,4), (1,6),
(3,4),(3,6),
(5,6), (5,7)
17
Counting Inversions in an array
Problem familiarization
Trivial-algo(A[])
{ count 0;
For(=1 to ) do
{ For( =0 to )
{ If ( A[]>A[]
? ) count count + 1;
}
} return count;
}
Time complexity: O()
18
Let us try to design a
Divide and Conquer based algorithm
19
How do we approach using divide & conquer
𝑐𝑜𝑢𝑛𝑡 I 𝑐𝑜𝑢𝑛𝑡 I I
0 1 2 3 4 5 6 7
A 3 15 8 19 9 67 11 27
𝑐𝑜𝑢𝑛𝑡 I II
20
Homework from last class
Hint:
21
IMPORTANT LESSON
THAT WE WILL LEARN TODAY…
22
Role of Data Structures in
designing efficient algorithms
Parameters:
• Query/Update time
• Space
• Preprocessing time
23
Role of Data Structures in
designing efficient algorithms
Consider an Algorithm .
Suppose performs many operations of same type on some data.
…. Code for ….
return + + ;
}
26
How to efficiently compute
(Inversions of type ?
𝒊 𝐦𝐢𝐝 𝒋 𝒌
A 3 15 8 19 9 67 11 27
𝑐𝑜𝑢𝑛𝑡 I II
Aim: For each , count the elements in A[..] that are greater than A[].
Trivial way: O( size of the subarray A[..]) time for a given .
O() time for a given in the first call of the algorithm.
O() time for computing since there are possible values of .
27
How to efficiently compute
(Inversions of type ?
Let us state clearly what we
want to achieve …
𝒊 𝒋 𝒌
A 3 15 8 19 9 67 11 27
Sort(A,);
For each
….doCode for search
binary …. for A[] in A[..] to compute c log
the number of elements greater than A[].
Add this number to ;
return + + ;
}
29
Counting Inversions
First algorithm based on divide & conquer
Time complexity analysis:
If = 1,
T() = c for some constant c
If > 1,
T() = c log + 2 T(/2)
= c log + c ((log )1) + T(/)
= c log + c ((log )1) + c ((log )2) + T(/)
= O( )
30
Counting Inversions
First algorithm based on divide & conquer
CountInversion( A,)
If (=) return 0;
Else{ ()/2;
CountInversion(A,);
2 T()
CountInversion(A,);
Sort(A,);
For each
….doCode for search
binary …. for A[] in A[..] to compute c log
the number of elements greater than A[].
Add this number to ;
return + + ;
}
31
Sequence of observations
To achieve better running time
• The extra log factor arises because for the “combine” step,
we are spending O( log ) time instead of O().
• So the only way to improve the running time of “combine” step is …some new idea
32
Revisiting MergeSort algorithm
33
Relook
Merging A[] and A[]
Sorted Sorted
A x
𝒊 >x
𝒋 𝒌
34
Pesudo-code for Merging two sorted arrays
Merge(A,,C)
; ; 0;
While( ≤ and )
{ If(A[]< A[]) { C[] A[]; ++; ++ }
Else { C[] A[];++; ++ }
}
While( ≤ ) { C[] A[]; ++; ++ }
While( ≤ ) { C[] A[]; ++; ++ }
return C ;
36
Proof of correctness
37
Algorithm for 2-majority element
scans and using O() extra space
Algo-2-majority(A) Question: What is to be proved ?
{ count ;
for(to ) Observation: If A does not have any
{ if ( count) { ? A[]
; ; majority element, the output of the
? algorithm is correct.
count 1; ;
} Proof of correctness ?
else if(<> A[])
count ? count 1;
else count? count 1 ; Inference: To prove correctness, it
suffices to prove the following:
}
If appears more than times in in A,
If A has a majority element, say ,
print( is 2-majority element)
then at the end of the For loop, =
else print(there is no majority element in A)
} Question:
What assertion holds at end of th iteration ?
38
The Algorithm at the end of th iteration
First scan
39
The Algorithm at the end of th iteration
𝒊 −𝟏
𝒙
count
Question: What assertion holds at the end of th iteration ?
Answer:
P() : isisaamajority
majorityelement
elementof
of{A[],A[],…,A[]}
{A[],A[],…,A[]}
40
To be continued and concluded tomorrow