You are on page 1of 118

3 3 P

m - CS5
o rit h
A lg
ysis of
A n a l
a n d
sig n Dr. Jeno

De Dept. of CSE

U3- SESSION 1: Divide ad conquer - merge sort


Divide and conquer
• Find an optimal solution of a problem.
• Basic idea is to decompose a given problem
into two or more similar, but simpler,
subproblems, to solve them in turn, and to
compose their solutions to solve the given
problem.
8 4 1 6 7 2 3 9

8 4 1 6 7 2 3 9
Divide

8 4 1 6 7 2 3 9

2 7 3 9
8 4 1 6

4 8 1 6 2 7 3 9
Combin

1 4 6 8 2 3 7 9

1 2 3 4 6 7 8 9
ALGORITHM Mergesort(A[0..n − 1])
//Sorts array A[0..n − 1] by recursive
mergesort
//Input: An array A[0..n − 1] of orderable
elements
//Output: Array A[0..n − 1] sorted in
nondecreasing order
if n > 1
copy A[0.. n/2 − 1] to B[0.. n/2 − 1]
copy A[ n/2 ..n − 1] to C[0.. n/2 − 1]
Mergesort(B[0.. n/2 − 1])
Mergesort(C[0.. n/2 − 1])
Merge(B, C, A) //see below
ALGORITHM Merge(B[0..p − 1], C[0..q − 1], A[0..p
+ q − 1])
//Merges two sorted arrays into one sorted array
//Input: Arrays B[0..p − 1] and C[0..q − 1] both
sorted
//Output: Sorted array A[0..p + q − 1] of the
elements of B and C
i ←0; j ←0; k←0
while i <p and j <q do
if B[i]≤ C[j ]
A[k]←B[i]; i ←i + 1
else A[k]←C[j ]; j ←j + 1
k←k + 1
if i = p
copy C[j..q − 1] to A[k..p + q − 1]
else copy B[i..p − 1] to A[k..p + q − 1]
Compare A[i] with B[j]. Smallest element
Merge sort
Copy into C[k], and increment index (element
which is copied) and k

Merging Copy the remaining items


Merging

Multiple lists can be merged in various pattern


Merging 2 way merge sort – iterative
Merge sort – recursive
Merge Sort : Recursive , Divide and conquer

Traversal – post oreder


Merge sort – Time complexity

Apply masters theorem


3 3 P
m - CS5
o rit h
A lg
ysis of
A n a l
a n d
sig n Dr. Jeno

De Dept. of CSE

U3- SESSION 2: Divide ad conquer - merge sort


Merge sort : Pros and cons 1 3

2 2
4
Merge sort : Pros and cons

Only in case of array not n LL

Insertion and merge – suitable for LL


3 3 P
m - CS5
o rit h
A lg
ysis of
A n a l
a n d
sig n Dr. Jeno

De Dept. of CSE

U3- SESSION 3: Divide ad conquer - merge sort


Try it later
Explain merge sort algorithm. Apply master’s theorem to find the average case
time complexity. Sort the list 8, 3, 2, 9, 7, 1, 5, 4 in ascending order using
merge sort technique.

Array of integers: Register number along with 2021


Draw this array after the TWO recursive calls of merge sort are completed, and
before the final merge step has occured.
3 3 P
m - CS5
o rit h
A lg
ysis of
A n a l
a n d
sig n Dr. Jeno

De Dept. of CSE

U3- SESSION 4: Divide ad conquer - Quick sort


Quick sort : Idea
• Students in the class : arrange height wise
– Teacher shows the place
– Students themselves

All the elements before the sorted elements are small and after ones are bigger,
then that element is in sorted position
low
Quick sort high
10 16 8 12 15 6 3 9 5 ꝏ

i i – search for element greater than pivot


j- smaller than pivot j
Increment i until you find an element greater than pivot
10 16 8 12 15 6 3 9 5 ꝏ

i
Decrement j until you find an element smaller than pivot ,

10 16 8 12 15 6 3 9 5 ꝏ

i j
Quick sort
exchange them

10 5 8 12 15 6 3 9 16 ꝏ

i j
Continue the step. Increment i until you find an element greater than pivot

10 5 8 12 15 6 3 9 16 ꝏ

i j
10 5 8 12 15 6 3 9 16 ꝏ

i j
Quick sort
Decrement j until you find an element smaller than pivot ,
10 5 8 12 15 6 3 9 16 ꝏ

i j
exchange them

10 5 8 9 15 6 3 12 16 ꝏ

i j
Continue the step. Increment i until you find an element greater than pivot

10 5 8 9 15 6 3 12 16 ꝏ

i j
Quick sort
Decrement j until you find an element smaller than pivot ,
10 5 8 9 15 6 3 12 16 ꝏ

exchange them i j
10 5 8 9 3 6 15 12 16 ꝏ

i j
Continue the step. Increment i until you find an element greater than pivot
10 5 8 9 3 6 15 12 16 ꝏ

i j
10 5 8 9 3 6 15 12 16 ꝏ

ij
Quick sort
Decrement j until you find an element smaller than pivot ,
10 5 8 9 3 6 15 12 16 ꝏ

exchange ?
j i
when i > j, DO NOT swap i,j . SWAP pivot , jth element

6 5 8 9 3 10 15 12 16 ꝏ

j i

Now perform partitioning recursively on both the sides of the list


Quick sort
6 5 8 9 3 10 15 12 16 ꝏ

j i
• partition(l,h)
• if (i <j)
• { pivot = A[l]
swap (A[i], A[j])
• i=l, j=h
}
• while (i<j)
{
● swap(A[l] , A[j])
• do
retturn j
i++
}
while (A[i] <=pivot)
• do
j--
while (A[j] >pivot)
Quick sort
• ALGORITHM Quicksort(A[l..r])
• //Sorts a subarray by quicksort
• //Input: Subarray of array A[0..n − 1], defined by
its left and right
• // indices l and r
• //Output: Subarray A[l..r] sorted in
nondecreasing order
• if l < r
• s ←Partition(A[l..r]) //s is a split position
• Quicksort(A[l..s − 1])
• Quicksort(A[s + 1..r])
• ALGORITHM
HoarePartition(A[l..r]) • p←A[l]
• //Partitions a subarray by Hoare’s • i ←l; j ←r + 1
algorithm, using the first element • repeat
as a pivot
repeat i ←i + 1 until A[i]≥ p
• //Input: Subarray of array A[0..n
repeat j ←j − 1 until A[j ]≤ p
− 1], defined by its left and
rightindices l and r (l<r) swap(A[i], A[j ])
• //Output: Partition of A[l..r], with until i ≥ j
the split position returned as this • swap(A[i], A[j ]) //undo last swap
function’s value when i ≥ j
• swap(A[l], A[j ])
• return j
Quick sort analysis
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Best case
8
Partition at middle 1, 7 9,15 n

4 12
1, 3 5,7 9, 11 13,15 n

2 6 10 14
1, 1 3,3 5, 5 7,7 9, 9 11,11 13, 13 15,15

Height of the tree - log n


n
O(n log2n)
n * log2n
Quick sort – Analysis

Best case: Element that is selected as pivot should


be median . We will not come to know the median if
the list is not sorted. Achieving the best may not be
possible in quick sort

Middle element of the sorted list


Quick sort – Analysis

Partitioning – always happens at the


beginning of the list
Quick sort - improvement

Worst case : when elements are already sorted.

1. Don’t select the first element as pivot. Select the middle element
Then it can be reduced to O(nlogn) from O(n2 )

2. Select random as pivot element


3 3 P
m - CS5
o rit h
A lg
ysis of
A n a l
a n d
sig n Dr. Jeno

De Dept. of CSE

U3- SESSION 4: Divide ad conquer - Strassen’s Matrix


Multiplication
5BT IT
CHRIST
Deemed to be University

Matrix Multiplication

1. Normal method

2. Divide and conquer method

3. Strassen's Method

Excellence and
Service
Normal method
Normal method contd..
• for ( i = 0; i <n ; i++)
• { for ( j = 0; j <n ; j++)
• { C[i,j] = 0;
• for ( k = 0; k <n ; k++)
• { C[i,j] += A[i,k] * B[k,j]
• }
• }
• }
O( n 3
)

Divide and conquer method

• Large problem divided into small


problems
• size of small problem : 2 * 2 matrix
• Higeher dimensions - divide and conquer
Small Problem
Large Problem
• More than 2 * 2 dimension
• Dimensions - in the order of pow(2) .
• 4*4, 8*8, 16 * 16 , 64*64 etc
Large Problem
A11 A12
B11 B12

A21 A22
B21 B22
Algorithm mm(A,B,n)
{
if (n<=2)
C = 4 formulas
else
{
mid = n/2
C11= mm(A11 , B11, n/2) + mm(A12, B21, n/2)
C12= mm(A11 , B12, n/2) + mm(A12, B22, n/2)
C21=mm(A21 , B11, n/2) + mm(A22, B21, n/2)
C22=mm(A21 , B12, n/2) + mm(A22, B22, n/2)
}
Large Problem- Time complexity
Algorithm mm(A,B,n)
{ Recurrence relation
if (n<=2)
C = 4 formulas T(n) = 1 , n<=2
else
{ = 8T(n/2) + n2
, n>2
mid = n/2
mm(A11 , B11, n/2) + mm(A12, B21, n/2)
mm(A11 , B12, n/2) + mm(A12, B22, n/2)
mm(A21 , B11, n/2) + mm(A22, B21, n/2)
mm(A21 , B12, n/2) + mm(A22, B22, n/2)
}
Large Problem- Time complexity
Apply Masters theorem Recurrence relation

T(n) = aT(n/b) + f(n) T(n) = 1 , n<=2


= a T(n/b) + θ(nk logpn)
= 8T(n/2) + n2 , n>2

a = 8 , b= 2, f(n) = n2 k = 2

compare a and bk 8 > 22

T(n) = θ(nlogba) = θ(nlog28) logba = logxa /logxb

= θ(n3)
The quick brown fox jumps over the lazy dog
Pangram
Exercise !!!
Divide and conquer
Observations: Large Problems
• Time complexity (both methods) - θ(n3)

• Divide & conquer -


• recursive
• internally use stack
• consume extra space

• How to reduce θ(n3) ?


ans: Strassen's matrix multiplication
Strassen's matrix multiplication
Apply Masters theorem Recurrence relation

T(n) = aT(n/b) + f(n) T(n) = 1 , n<=2


= a T(n/b) + θ(nk logpn)
= 7T(n/2) + n2 , n>2

a = 7 , b= 2, f(n) = n2 k = 2

compare a and bk 7> 22

T(n) = θ(nlogba) = θ(nlog27)

= θ(n2.81) < θ(n3)


Remembering Strassen's formula

1.AHED
2. Diagonal
3.Last CR consider X as (Row +) and Y as (Column -) matrix
4. First CR

1. Write P1 = A; P2 = H; P3 = E; P4 = D

2. For P5 we will use Diagonal Rule i.e.


(Sum the Diagonal Elements Of Matrix X ) * (Sum the Diagonal Elements Of Matrix Y ),

P5 = (A + D)* (E + H)
Remembering Strassen's formula

1.AHED
2. Diagonal
3.Last CR consider X as (Row +) and Y as (Column -) matrix
4. First CR

3. For P6 we will use Last CR Rule i.e. (Last Column of X) * (Last Row of Y)
(remember that Row+ and Column-)

P6 = (B – D) * (G + H)
Remembering Strassen's formula

1.AHED
2. Diagonal
3.Last CR consider X as (Row +) and Y as (Column -) matrix
4. First CR

4. For P7 we will use First CR Rule i.e. (First Column of X) * (First Row of Y )
remember that Row+ and Column-

P7 = (A – C) * (E + F)
Remembering Strassen's formula

P1 = A; P2 = H; P3 = E; P4 = D

Come Back to P1 : we have A there and it’s adjacent element in Y Matrix is E, since Y
is Column Matrix so we select a column in Y such that E won’t come, we find F H
Column, so multiply A with (F – H)

P1 = A * (F – H)
Remembering Strassen's formula

P1 = A; P2 = H; P3 = E; P4 = D

Come Back to P2 : we have H there and it’s adjacent element in X Matrix is D, since X
is Row Matrix so we select a row in X such that D won’t come, we find A B Row, so
multiply H with (A + B)

P2 = (A + B) * H
Remembering Strassen's formula

P1 = A; P2 = H; P3 = E; P4 = D

Come Back to P3 : we have E there and it’s adjacent element in X Matrix is A, since X
is Row Matrix so we select a row in X such that A won’t come, we find C D Row, so
multiply E with (C + D)

P3 = (C + D) * E
Remembering Strassen's formula

P1 = A; P2 = H; P3 = E; P4 = D

Come Back to P4 : we have D there and it’s adjacent element in Y Matrix is H, since Y
is Column Matrix so we select a column in Y such that H won’t come, we find G E
column, so multiply D with (G - E)

P4 = (G - E) * D
Remembering Strassen's formula
Resultant matrix
Exercise 2 !!!
5 7 9 10
Matrix A 2 3 3 8
8 10 2 3
3 3 4 8

3 10 12 18
Matrix B 12 1 4 9
9 10 12 2
3 12 4 10
Exercise 2 !!!
3 3 P
m - CS5
o rit h
A lg
ysis of
A n a l
a n d
sig n
De
U3- SESSION 5: Binary Tree traversal, Dr. Jeno
Heap sort Dept. of CSE
Binary Tree traversal
• A binary tree T is defined as a finite set of nodes that is either
empty or consists of a root and two disjoint binary trees TL
and TR called, respectively, the left and right subtree of the
root.
• In the preorder traversal, the root is visited before the left and
right subtrees are visited (in that order).
• In the inorder traversal, the root is visited after visiting its left
subtree but before visiting the right subtree.
• In the postorder traversal, the root is visited after visiting the
left and right subtrees (in that order).
Binary Tree Traversal
Binary Tree Traversal
Try yourself !!!
• Write an algoritm to count the number of
leave in a binary tree
• Height of the tree
Heap sort : - Array representation of Binary tree
Heap Sort : - Full BT , Complete BT

BT with maximum number of node

Not a CBT

Array representation, no gap


Complete BT
CBT is FBT up to height (h-1).
Last level elements are fill from left to right

Height of CBT is minimum. logn


Heap
Heap is a CBT
Max heap – every node is having a value >= all its descends

Heap is a CBT
Min heap – every node is having a value <= all its descends
Max heap - Insertion
Max heap – Insertion - Analysis
Time taken

Basic operation – swap -> depends on the height of the tree.

O(log n) - maximum

O (1) - minimum, when no swap is required

Note : While inserting – add the element at the leaf .


Adjust the element. Direction of adjustment is upward.
3 3 P
m - CS5
o rit h
A lg
ysis of
A n a l
a n d
sig n
De
U3- SESSION 6: Heap sort Dr. Jeno
Dept. of CSE
Max heap - Deletion
Remove the root element
Max heap - Deletion

CBT but not max heap. Adjust the element


from the root towards leaf
Max heap - deletion
Compare the children of root.

No swap is needed .16>15

Time taken
- Adjustments : depends on the height . Maximum height – log n
Max heap - Deletion

Heap size =6 , After deleting 50


Can store 50 in free space

Next element to be deleted is 30

16 20 15 10 8 50

8 16 20 15 10 50

20 16 8 15 10 50

20 16 10 15 8 50

20 16 10 15 8 30 50 Concept of heap sort


Heap sort
• Create a heap for the given set of element by
inserting one by one.
• Delete all the elements from the heap.
Resultant will be in sorted order.
Heap sort
Heap sort
Heap sort

Assume 1st
element is
already in
heap Compare with
Insert 2nd Compare with parent and adjust. ( 3/2 = 1 ,
element parent and adjust element 20. No swap needed.
Adjust

Adjust

Max heap tree

Time taken
O(n logn)
Step 2: Deletion

Delete 40 CBT but not a


max heap. Adjust

Store 40 Delete 30
Compare with children.
20 is bigger - moves up ,
10 comes down Delete 20
Last element will
move to root

No need to adjust.
Child < parent

Time taken
Delete 15
Creation – n logn
Deletion – n logn
Total – 2n logn

O(n logn)
3 3 P
m - CS5
o rit h
A lg
ysis of
A n a l
a n d
sig n
De
U3- SESSION 7: Heap sort Dr. J.eno
Dept. of CSE
Kruskal algorithm by SAK
3 3 P
m - CS5
o rit h
A lg
ysis of
A n a l
a n d
sig n
De
U3- SESSION 8: Heap sort Dr. Jeno
Dept. of CSE
Heapify : procedure for creating heap

Start from last element – 18


Look for its children . No children.
Hence it is a heap. Similarly 25,40,12

Look for 15 and it children. Not a max heap.


Adjust . 25 goes up , 15 comes
Down
After 25 , look for 20 and its children. not max heap . Adjust
Heap sort
• It is implemented using heaps.
Working of Heap Sort

• Since the tree satisfies Max-Heap property, then the largest


item is stored at the root node.
• Swap: Remove the root element and put at the end of the
array (nth position) Put the last item of the tree (heap) at the
vacant place.
• Remove: Reduce the size of the heap by 1.
• Heapify: Heapify the root element again so that we have the
highest element at root.
• The process is repeated until all the items of the list are
sorted.
Heap sort algorithm
3 3 P
m - CS5
o rit h
A lg
ysis of
A n a l
a n d
sig n
De
U3- SESSION 11: Heap sort Dr. Jeno
Dept. of CSE
3 3 P
m - CS5
o rit h
A lg
ysis of
A n a l
a n d
sig n
De
U3- SESSION 12: Honer’s rule, Binary Exponentiation
Dr. Jeno
Dept. of CSE
Horner’s Rule and Binary Exponentiation

• Horner’s rule is an old but very elegant and efficient algorithm for evaluating a
polynomial. It is named after the British mathematician W. G. Horner
Horner’s Rule

• Write the coefficients


2 -1 3 1 -5
2 3*2+(-1)=5 3*5+3=18 3*18+1=55 3*55+(-5)= 160

P(3) =160 ALGORITHM Horner(P [0..n], x)

p ← P [n]

for i ← n − 1 downto 0 do
p ← x ∗ p + P [i]

return p
Horner’s Rule: Try it !!!

• P(x) = 5x4 + 2x3 - 4x2+ 3x + 7, x= 4


• 2x3 - 6x2 + 2x - 1 for x = 3
• 2x3 + 3x + 1 for x = 2
• x4 + 3x3 + 5x2 + 7x + 9 at x = 2
• p(x) = 6x 3 − 2x 2 + 7x + 5 when x = 4
• p(x) = 5x 4 − 3x 2 + 12 at x = −4
• p(x) = 5x 4 + 3x 3 − 2x 2 + 4x − 6 at x = −2
Binary Exponentiation

• https://www.youtube.com/watch?v=L-
Wzglnm4dM

• https://www.youtube.com/watch?v=-3Lt-
EwR_Hw
Try Heap sort
Array = {1, 3, 5, 4, 6, 13, 10, 9, 8, 15, 17}
PRESORTING
• So far, we have discussed three elementary sorting
algorithms—selection sort,bubble sort, and insertion sort—
that are quadratic in the worst and average cases,
• Two advanced algorithms—mergesort, which is always in (n
log n), and
• quicksort, whose efficiency is also (n log n) in the average
case but is quadratic in the worst case.
• Are there faster sorting algorithms? can have a better
efficiency than n log n in the worst case

EXAMPLE 1 Checking element uniqueness in an array


• sort the array first and then check only its consecutive
elements: if the array has equal elements
• ALGORITHM PresortElementUniqueness(A[0..n − 1])
• //Solves the element uniqueness problem by sorting the array
first
• //Input: An array A[0..n − 1] of orderable elements
• //Output: Returns “true” if A has no equal elements, “false”
otherwise
• sort the array A
for i ←0 to n − 2 do
if A[i]= A[i + 1] return false
return true
EXAMPLE 2 Computing a mode A mode is a value that occurs
most often in a
given list of numbers. For example, for 5, 1, 5, 7, 6, 5, 7, the
mode is 5.
• ALGORITHM PresortMode(A[0..n − 1])
• //Computes the mode of an array by sorting it first
• //Input: An array A[0..n − 1] of orderable elements
• //Output: The array’s mode
• sort the array A
• i ←0 //current run begins at position i
• modef requency ←0 //highest frequency seen so far
• while i ≤ n − 1 do
runlength←1; runvalue←A[i]
while i + runlength ≤ n − 1 and A[i + runlength]= runvalue
runlength←runlength + 1
if runlength > modef requency
modefrequency ←runlength;
modevalue←runvalue
• i ←i + runlength
• return modevalue
Thank YOU !!!

You might also like