Professional Documents
Culture Documents
m - CS5
o rit h
A lg
ysis of
A n a l
a n d
sig n Dr. Jeno
De Dept. of CSE
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
De Dept. of CSE
2 2
4
Merge sort : Pros and cons
De Dept. of CSE
De Dept. of CSE
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
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
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
1. Don’t select the first element as pivot. Select the middle element
Then it can be reduced to O(nlogn) from O(n2 )
De Dept. of CSE
Matrix Multiplication
1. Normal 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
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
a = 8 , b= 2, f(n) = n2 k = 2
= θ(n3)
The quick brown fox jumps over the lazy dog
Pangram
Exercise !!!
Divide and conquer
Observations: Large Problems
• Time complexity (both methods) - θ(n3)
a = 7 , b= 2, f(n) = n2 k = 2
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
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
Not a CBT
Heap is a CBT
Min heap – every node is having a value <= all its descends
Max heap - Insertion
Max heap – Insertion - Analysis
Time taken
O(log n) - maximum
Time taken
- Adjustments : depends on the height . Maximum height – log n
Max heap - Deletion
16 20 15 10 8 50
8 16 20 15 10 50
20 16 8 15 10 50
20 16 10 15 8 50
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
Time taken
O(n logn)
Step 2: Deletion
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
• 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
p ← P [n]
for i ← n − 1 downto 0 do
p ← x ∗ p + P [i]
return p
Horner’s Rule: Try it !!!
• 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