You are on page 1of 39

Data Structures and Algorithms

(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

2. Solve each smaller instances recursively (base case suitably defined).

3. Combine the solutions of the smaller instances


to get the solution of the original instance.

This is usually the main nontrivial step


in the design of an algorithm using
divide and conquer strategy

3
Example 2

Faster algorithm for


multiplying two integers

4
Pursuing Divide and Conquer approach
MSB LSB

𝑨 𝑩 𝑿
×
n-1 n-2…......………………n/2.............................2 1 0
multiplications

𝑪 𝑫 𝒀

O() time algo

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

𝑪 𝑫 𝒀
= ( ) ?? ( + ) ??𝟐𝒏 + 𝟐𝒏/𝟐

(+)
¿ () ()

Each of these terms could


− 𝑨× 𝑪 − 𝑩× 𝑫
be bit long. What to do ?

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

𝑪 𝑫 𝒀
= ( ) ?? ( + ) ??𝟐𝒏 + 𝟐𝒏/𝟐

(+)
¿ () ()

Each of these terms is +¿ 𝑨× 𝑪 +¿ 𝑩× 𝑫


surely bit long. Good 

12
Pursuing Divide and Conquer approach
MSB LSB

𝑨 𝑩 𝑿
×
n-1 n-2…......………………n/2.............................2 1 0

𝑪 𝑫 𝒀
= ( ) ?? ( + ) ??𝟐𝒏 + 𝟐𝒏/𝟐

(+)
¿ () () +¿ 𝑨× 𝑪 +¿ 𝑩× 𝑫
Multiplications

Multiplications Using Subtractions and 2 more additions


13
Pursuing Divide and Conquer approach
Let T() : time complexity of the new algo for multiplying two -bit numbers
T() c T for some constant c
c c T
c c … ?
() T()
()

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

Counting the number of


“inversions” in an array

16
Counting Inversions in an array

Definition (Inversion): Given an array A of size n,


a pair (i,j), 0≤i<j<n is called an inversion if A[i]>A[j].
Example:
0 1 2 3 4 5 6 7

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)

AIM: An efficient algorithm to count the number of inversions in an array A.

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

• Ponder over ways to compute efficiently.

Hint:

Make use of ( log ) sorting algorithm

21
IMPORTANT LESSON
THAT WE WILL LEARN TODAY…

1. Role of Data structures in algorithms

22
Role of Data Structures in
designing efficient algorithms

Definition: A collection of data elements arranged and connected in a way


which can facilitate efficient executions of a
(possibly long) sequence of operations.

Parameters:
• Query/Update time
• Space
• Preprocessing time

23
Role of Data Structures in
designing efficient algorithms

Definition: A collection of data elements arranged and connected in a way


which can facilitate efficient executions of a
(possibly long) sequence of operations.

Consider an Algorithm .
Suppose performs many operations of same type on some data.

Improving time complexity


Improving the time complexity of .
of these operations

So, it is worth designing


a suitable data structure.
24
Counting Inversions
Divide and Conquer based algorithm
CountInversion( A,) // Counting no. of inversions in A[]
If () return 0;
Else{  ()/2;
 CountInversion(A,);
 CountInversion(A,);

…. Code for ….

return + + ;
}

26
How to efficiently compute
(Inversions of type ?

𝒊 𝐦𝐢𝐝 𝒋 𝒌
A 3 15 8 19 9 67 11 27

O() time algo

𝑐𝑜𝑢𝑛𝑡 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

For each 𝑐𝑜𝑢𝑛𝑡 I II


count the elements in A[..] that are greater than A[].

What should be Sorted subarray A[..].


Time to apply the Lesson the data structure ?
28
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 + + ;
}

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( )

Can we improve it further ?

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().

• The reason for O( log ) time for the “combine” step:


– Sorting A[0.. /2] takes O( log ) time.
– Doing Binary Search for /2 elements from A[/2… -1]

• Each of the above tasks have optimal running time.

• So the only way to improve the running time of “combine” step is …some new idea

32
Revisiting MergeSort algorithm

MSort(A,)// Sorting A[]


{ If ()
{ ()/2;
MSort(A,);
MSort(A,);
Create a temporary array C[] We shall carefully look at the Merge()
Merge(A,, C); procedure to find an efficient way to count
the number of elements from A[ ] which
Copy C[] to A[] are smaller than A[] for any given
}
}

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 ;

We shall make just a slight change in the above


pseudo-code to achieve our main objective of
computing . If you understood the discussion of the
previous slide, can you guess it now ? 35
Homework

• Suitably modify the Merge procedure to compute .

36
Proof of correctness

Algorithm for majority element

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 

You might also like