SlideShare a Scribd company logo
Design and Analysis of Algorithms
COT 5405
CLASS NOTES
14th
October 2003
Overview:
• Divide and Conquer
• Master theorem
• Master theorem based analysis for
 Binary Search
 Merge Sort
 Quick Sort
Divide and Conquer
Basic Idea:
1. Decompose problems into sub instances.
2. Solve sub instances successively and independently.
3. Combine the sub solutions to obtain the solution to the original
problem.
In order to look into the efficiency of the Divide and Conquer lets look into the
Multiplication of two n-digit Numbers
Traditional Multiplication:
Say we are multiplying 382 with 695(n=3)
382
* 695
- - - - -
Essentially, we are multiplying 1 digit with n other digits and then adding the n numbers,
which can give us a solution of at most 2n digits.
There are n additions, each of O(n) time at most, which gives us the running time of the
algorithm as O(n2
)
!!Using Divide and Conquer to multiply n-digit numbers
We will write the two n-digit numbers as follows:
(10n/2
X + Y) (10n/2
W+Z) =10n
XW + (XZ + YW) 10n/2
+YZ ---(1)
That is we are converting the multiplication of two n-digit numbers into multiplication of
four n/2 digit numbers, plus some extra work involved in additions. We are recursively
calling multiplication and performing some additions in every recursion.
Let T (n) be the running time of multiplying two n-digit numbers.
Then in our case,
T (n) = 4T (n/2) +O (n)
• Four multiplications of n/2 digit numbers
• Addition is going to be between numbers that have atmost 2n digits. Thus
addition can be O (n).
Recursively substituting the value of T (n):
T (n) = 4 [4T (n/4) + O (n/2)] +O (n)
=16 T (n/4) + 4O (n/2) + O (n)
-
-
-
=C T (1) + - - - -
Master’s Theorem
Let T(n) be the running time of an algorithm with an input size of n;
Suppose we can run the algorithm in such a way that we make ‘a’ recursive calls every
time with an input size of ‘n/b’ and do some extra work in every recursion (additions and
subtractions).
Such that T (n) can be represented as:
T (n) = a T (n/b) + O (nk
),
Then,
If log ba>k, T (n) =O (nlog
b
a
) (recursive calls dominates)
If log ba=k, T (n) =O (nk
log n) (almost equal work in rec. calls and in extra work)
If log ba<k, T (n) =O (nk
) (Extra work dominates)
In our multiplication problem:
T (n) = 4T (n/2) +O (n)
A=4, b=2
Log24=2, k=1
Since algorithm is dominated by recursive calls and the running time is O (n2
).
But this is as good as our traditional multiplication algorithm. Since we now know that
multiplications dominate the running time, if we can reduce the number of
multiplications to three, which can bring down our T(n) by 25%.
To calculate (1), we just need the following 3 multiplications separately:
1. (X+Y)(W+Z) 2 additions and one multiplication
2.XW
3.YZ
Then we can calculate
XZ+YW=(X+Y)(W+Z)-XW-YZ
Thus we use three multiplications at the expense of some extra additions and
subtractions, which run in constant time( each of O(n) time)
Thus,
T(n)=3T(n/2) + O(n)
Applying Master’s theorem,
A=3,b=2,k=1
Thus, T(n)=O(nlog
2
3
)
Since log
2
3 ~ 1.5,
We have reduced the total number of recursive calls in our program. For very large n, it will work well but in actual
implementation, we hardly code to gain advantage out if this feature.
Binary Search
Goal: Searching for nth value in a sorted list of length n.
(Divide the list into two and recursively search in the individual lists half the size)
Again,
Let T(n) be the running time of the algorithm. Then,
T(n)=T(n/2) + O(1)
In O(1) time we divide the list into two halves(n/2) that run in T(n/2) time.
Using Master’s theorem,
A=1,b=2
Log21=0
K=0;
So,
T(n)=O(log n)
Merge Sort
Goal: Sp litting the element list into 2 lists, sort them and merge them.
T(n)=2T(n/2) + O(n)
Here, the hidden constant is greater than the hiddent constant in the merge sort because
while dividing the lists into two different arrays and then sorting them, we are allocating
extra space and subsequently, copying into the original array.
Using Master’s theorem,
A=2,b=2,k=1
Log22=1
So, T(n)=O(n log n)
Quicksort
Goal: Pick one element as the partition element. Put all the elements smaller than it, to
the left of it and all elements greater than it, to the right of it. On the lists left and right of
the partition element recursively call Qsort.
Say the list is: 8,3,6,9,2,4,7,5
Partition element:5
8, 3, 6, 9, 2, 4, 7, 5
8 in the worng place, 7 fine.
8, 3, 6, 9, 2, 4, 7, 5
4 ,3 ,6 ,9 , 2 ,8 ,7 ,5
4, ,3, 2, 9, 6, 8, 7, 5
4, 3 ,2 ,9 ,6 ,8 ,7 ,5
front Last
front Last
front last
front last
last front
Now swap front with 5 and we have 5 in place.
4,3,2,5,6,8,7,9
Thus the only extra space utilized here is the temporary variable used for swapping.
In te worst case, we might end up choosing a partition element which is the first element
in our list.
In that case T(n)=O(n2
)
To make sure this rarely happens:
1. Pick a random partition element.
2. Probablity of picking a good partition element is as low as the probability of
picking a bad one. So, they will even out.
There are n possible partition elements
Element Split Prob(element)
1 0,n-1 1/n
2 1,n-2 1/n
3 1/n
N n-1,0 1/n
Now,
T (n) = 1/n [ T(0) + T(n-1) + O(n) ] +
1/n [ T(1) + T(n-2) + O(n) ] +
1/n [ T(2) + T(n-3) + O(n) ] +
……
……
……
1/n [T (n-1) + T (0) + O (n)]
n * T[n] = {2 k=0Σn-1
T(k)} + O(n2
) A
Substitute n = n-1,
(n-1) * T[(n-1)] = {2 k=0Σn-1
T(k)} + O((n-1)2
)B
Subtract A from B
n T(n) - (n-1)T(n-1) = 2 T(n-1) + O(n)
n T(n) = n+1 T(n-1) + O(n)
T(n) = ((n+1)/n )T(n-1) + O(1)
Divide by (n+1)
T(n)/(n+1) = [T(n-1) ]/ n + O(1/n)  C
Let,
S(n) = T(n)/(n+1)  D
S(n) = S(n-1) + O(1/n)
This can be written as a sum,
= S(n-2) + O(1/n-1) + O(1/n)
= S(n-3) + O(1/n-2) + O(1/n-1) + O(1/n)
S(n) = O(k=1 Σ n
1/k)
= O( Hn)
S(n) = T(n)/(n+1) = O (ln n) E
Substitute D in C
T(n) = S(n) . (n+1)
use E,
T(n) = O (ln n) . (n+1)
T(n) = O (n lg n )
Notes compiled by Shankar Vaithianthan and Harjinder Mandarh
n T(n) - (n-1)T(n-1) = 2 T(n-1) + O(n)
n T(n) = n+1 T(n-1) + O(n)
T(n) = ((n+1)/n )T(n-1) + O(1)
Divide by (n+1)
T(n)/(n+1) = [T(n-1) ]/ n + O(1/n)  C
Let,
S(n) = T(n)/(n+1)  D
S(n) = S(n-1) + O(1/n)
This can be written as a sum,
= S(n-2) + O(1/n-1) + O(1/n)
= S(n-3) + O(1/n-2) + O(1/n-1) + O(1/n)
S(n) = O(k=1 Σ n
1/k)
= O( Hn)
S(n) = T(n)/(n+1) = O (ln n) E
Substitute D in C
T(n) = S(n) . (n+1)
use E,
T(n) = O (ln n) . (n+1)
T(n) = O (n lg n )
Notes compiled by Shankar Vaithianthan and Harjinder Mandarh

More Related Content

What's hot

A Note on Latent LSTM Allocation
A Note on Latent LSTM AllocationA Note on Latent LSTM Allocation
A Note on Latent LSTM Allocation
Tomonari Masada
 
Recurrences
RecurrencesRecurrences
Master method
Master methodMaster method
Master method
arun jacob
 
Time complexity
Time complexityTime complexity
Time complexity
Katang Isip
 
Master theorem
Master theoremMaster theorem
Master theorem
fika sweety
 
Time complexity (linear search vs binary search)
Time complexity (linear search vs binary search)Time complexity (linear search vs binary search)
Time complexity (linear search vs binary search)Kumar
 
Time complexity
Time complexityTime complexity
Time complexity
Kartik Chandra Mandal
 
Time andspacecomplexity
Time andspacecomplexityTime andspacecomplexity
Time andspacecomplexity
LAKSHMITHARUN PONNAM
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notationsNikhil Sharma
 
002 ray modeling dynamic systems
002 ray modeling dynamic systems002 ray modeling dynamic systems
002 ray modeling dynamic systems
Institute of Technology Telkom
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
Rishabh Soni
 
Asymptotic notation
Asymptotic notationAsymptotic notation
Asymptotic notation
sajinis3
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notationsEhtisham Ali
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
Tareq Hasan
 
Asymptotic notation
Asymptotic notationAsymptotic notation
Asymptotic notation
mustafa sarac
 
Analysis of algorithn class 3
Analysis of algorithn class 3Analysis of algorithn class 3
Analysis of algorithn class 3Kumar
 

What's hot (18)

A Note on Latent LSTM Allocation
A Note on Latent LSTM AllocationA Note on Latent LSTM Allocation
A Note on Latent LSTM Allocation
 
Computational Complexity
Computational ComplexityComputational Complexity
Computational Complexity
 
Recurrences
RecurrencesRecurrences
Recurrences
 
Master method
Master methodMaster method
Master method
 
Time complexity
Time complexityTime complexity
Time complexity
 
Master theorem
Master theoremMaster theorem
Master theorem
 
Time complexity (linear search vs binary search)
Time complexity (linear search vs binary search)Time complexity (linear search vs binary search)
Time complexity (linear search vs binary search)
 
Time complexity
Time complexityTime complexity
Time complexity
 
Time andspacecomplexity
Time andspacecomplexityTime andspacecomplexity
Time andspacecomplexity
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
002 ray modeling dynamic systems
002 ray modeling dynamic systems002 ray modeling dynamic systems
002 ray modeling dynamic systems
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
 
Asymptotic notation
Asymptotic notationAsymptotic notation
Asymptotic notation
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Big o
Big oBig o
Big o
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Asymptotic notation
Asymptotic notationAsymptotic notation
Asymptotic notation
 
Analysis of algorithn class 3
Analysis of algorithn class 3Analysis of algorithn class 3
Analysis of algorithn class 3
 

Similar to pradeepbishtLecture13 div conq

CS330-Lectures Statistics And Probability
CS330-Lectures Statistics And ProbabilityCS330-Lectures Statistics And Probability
CS330-Lectures Statistics And Probability
bryan111472
 
time_complexity_list_02_04_2024_22_pages.pdf
time_complexity_list_02_04_2024_22_pages.pdftime_complexity_list_02_04_2024_22_pages.pdf
time_complexity_list_02_04_2024_22_pages.pdf
SrinivasaReddyPolamR
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
AmayJaiswal4
 
T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptx
GadaFarhan
 
Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingzukun
 
Find the compact trigonometric Fourier series for the periodic signal.pdf
Find the compact trigonometric Fourier series for the periodic signal.pdfFind the compact trigonometric Fourier series for the periodic signal.pdf
Find the compact trigonometric Fourier series for the periodic signal.pdf
arihantelectronics
 
Algorithm Assignment Help
Algorithm Assignment HelpAlgorithm Assignment Help
Algorithm Assignment Help
Programming Homework Help
 
lecture 15
lecture 15lecture 15
lecture 15sajinsc
 
1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx
pallavidhade2
 
Algorithms Exam Help
Algorithms Exam HelpAlgorithms Exam Help
Algorithms Exam Help
Programming Exam Help
 
lecture 1
lecture 1lecture 1
lecture 1sajinsc
 
Quicksort analysis
Quicksort analysisQuicksort analysis
Quicksort analysisPremjeet Roy
 
Data Structure & Algorithms - Mathematical
Data Structure & Algorithms - MathematicalData Structure & Algorithms - Mathematical
Data Structure & Algorithms - Mathematical
babuk110
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortzukun
 
DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..
KarthikeyaLanka1
 
2.pptx
2.pptx2.pptx
2.pptx
MohAlyasin1
 
19. algorithms and-complexity
19. algorithms and-complexity19. algorithms and-complexity
19. algorithms and-complexity
showkat27
 
Insersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in AlgoritmInsersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in Algoritm
Ehsan Ehrari
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
Vikas Sharma
 

Similar to pradeepbishtLecture13 div conq (20)

CS330-Lectures Statistics And Probability
CS330-Lectures Statistics And ProbabilityCS330-Lectures Statistics And Probability
CS330-Lectures Statistics And Probability
 
time_complexity_list_02_04_2024_22_pages.pdf
time_complexity_list_02_04_2024_22_pages.pdftime_complexity_list_02_04_2024_22_pages.pdf
time_complexity_list_02_04_2024_22_pages.pdf
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
 
T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptx
 
Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sorting
 
Find the compact trigonometric Fourier series for the periodic signal.pdf
Find the compact trigonometric Fourier series for the periodic signal.pdfFind the compact trigonometric Fourier series for the periodic signal.pdf
Find the compact trigonometric Fourier series for the periodic signal.pdf
 
Algorithm Assignment Help
Algorithm Assignment HelpAlgorithm Assignment Help
Algorithm Assignment Help
 
lecture 15
lecture 15lecture 15
lecture 15
 
1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx
 
Algorithms Exam Help
Algorithms Exam HelpAlgorithms Exam Help
Algorithms Exam Help
 
lecture 1
lecture 1lecture 1
lecture 1
 
Quicksort analysis
Quicksort analysisQuicksort analysis
Quicksort analysis
 
Data Structure & Algorithms - Mathematical
Data Structure & Algorithms - MathematicalData Structure & Algorithms - Mathematical
Data Structure & Algorithms - Mathematical
 
Quicksort
QuicksortQuicksort
Quicksort
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksort
 
DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..
 
2.pptx
2.pptx2.pptx
2.pptx
 
19. algorithms and-complexity
19. algorithms and-complexity19. algorithms and-complexity
19. algorithms and-complexity
 
Insersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in AlgoritmInsersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in Algoritm
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 

Recently uploaded

Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
Kamal Acharya
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
Jayaprasanna4
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
ssuser9bd3ba
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
MuhammadTufail242431
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
Kamal Acharya
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
Pipe Restoration Solutions
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 

Recently uploaded (20)

Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 

pradeepbishtLecture13 div conq

  • 1. Design and Analysis of Algorithms COT 5405 CLASS NOTES 14th October 2003 Overview: • Divide and Conquer • Master theorem • Master theorem based analysis for  Binary Search  Merge Sort  Quick Sort Divide and Conquer Basic Idea: 1. Decompose problems into sub instances. 2. Solve sub instances successively and independently. 3. Combine the sub solutions to obtain the solution to the original problem. In order to look into the efficiency of the Divide and Conquer lets look into the Multiplication of two n-digit Numbers Traditional Multiplication: Say we are multiplying 382 with 695(n=3) 382 * 695 - - - - - Essentially, we are multiplying 1 digit with n other digits and then adding the n numbers, which can give us a solution of at most 2n digits. There are n additions, each of O(n) time at most, which gives us the running time of the algorithm as O(n2 )
  • 2. !!Using Divide and Conquer to multiply n-digit numbers We will write the two n-digit numbers as follows: (10n/2 X + Y) (10n/2 W+Z) =10n XW + (XZ + YW) 10n/2 +YZ ---(1) That is we are converting the multiplication of two n-digit numbers into multiplication of four n/2 digit numbers, plus some extra work involved in additions. We are recursively calling multiplication and performing some additions in every recursion. Let T (n) be the running time of multiplying two n-digit numbers. Then in our case, T (n) = 4T (n/2) +O (n) • Four multiplications of n/2 digit numbers • Addition is going to be between numbers that have atmost 2n digits. Thus addition can be O (n). Recursively substituting the value of T (n): T (n) = 4 [4T (n/4) + O (n/2)] +O (n) =16 T (n/4) + 4O (n/2) + O (n) - - - =C T (1) + - - - - Master’s Theorem Let T(n) be the running time of an algorithm with an input size of n; Suppose we can run the algorithm in such a way that we make ‘a’ recursive calls every time with an input size of ‘n/b’ and do some extra work in every recursion (additions and subtractions). Such that T (n) can be represented as: T (n) = a T (n/b) + O (nk ), Then, If log ba>k, T (n) =O (nlog b a ) (recursive calls dominates) If log ba=k, T (n) =O (nk log n) (almost equal work in rec. calls and in extra work) If log ba<k, T (n) =O (nk ) (Extra work dominates) In our multiplication problem: T (n) = 4T (n/2) +O (n) A=4, b=2 Log24=2, k=1 Since algorithm is dominated by recursive calls and the running time is O (n2 ).
  • 3. But this is as good as our traditional multiplication algorithm. Since we now know that multiplications dominate the running time, if we can reduce the number of multiplications to three, which can bring down our T(n) by 25%. To calculate (1), we just need the following 3 multiplications separately: 1. (X+Y)(W+Z) 2 additions and one multiplication 2.XW 3.YZ Then we can calculate XZ+YW=(X+Y)(W+Z)-XW-YZ Thus we use three multiplications at the expense of some extra additions and subtractions, which run in constant time( each of O(n) time) Thus, T(n)=3T(n/2) + O(n) Applying Master’s theorem, A=3,b=2,k=1 Thus, T(n)=O(nlog 2 3 ) Since log 2 3 ~ 1.5, We have reduced the total number of recursive calls in our program. For very large n, it will work well but in actual implementation, we hardly code to gain advantage out if this feature. Binary Search Goal: Searching for nth value in a sorted list of length n. (Divide the list into two and recursively search in the individual lists half the size) Again, Let T(n) be the running time of the algorithm. Then, T(n)=T(n/2) + O(1) In O(1) time we divide the list into two halves(n/2) that run in T(n/2) time. Using Master’s theorem, A=1,b=2 Log21=0 K=0; So, T(n)=O(log n) Merge Sort Goal: Sp litting the element list into 2 lists, sort them and merge them. T(n)=2T(n/2) + O(n)
  • 4. Here, the hidden constant is greater than the hiddent constant in the merge sort because while dividing the lists into two different arrays and then sorting them, we are allocating extra space and subsequently, copying into the original array. Using Master’s theorem, A=2,b=2,k=1 Log22=1 So, T(n)=O(n log n) Quicksort Goal: Pick one element as the partition element. Put all the elements smaller than it, to the left of it and all elements greater than it, to the right of it. On the lists left and right of the partition element recursively call Qsort. Say the list is: 8,3,6,9,2,4,7,5 Partition element:5 8, 3, 6, 9, 2, 4, 7, 5 8 in the worng place, 7 fine. 8, 3, 6, 9, 2, 4, 7, 5 4 ,3 ,6 ,9 , 2 ,8 ,7 ,5 4, ,3, 2, 9, 6, 8, 7, 5 4, 3 ,2 ,9 ,6 ,8 ,7 ,5 front Last front Last front last front last last front
  • 5. Now swap front with 5 and we have 5 in place. 4,3,2,5,6,8,7,9 Thus the only extra space utilized here is the temporary variable used for swapping. In te worst case, we might end up choosing a partition element which is the first element in our list. In that case T(n)=O(n2 ) To make sure this rarely happens: 1. Pick a random partition element. 2. Probablity of picking a good partition element is as low as the probability of picking a bad one. So, they will even out. There are n possible partition elements Element Split Prob(element) 1 0,n-1 1/n 2 1,n-2 1/n 3 1/n N n-1,0 1/n Now, T (n) = 1/n [ T(0) + T(n-1) + O(n) ] + 1/n [ T(1) + T(n-2) + O(n) ] + 1/n [ T(2) + T(n-3) + O(n) ] + …… …… …… 1/n [T (n-1) + T (0) + O (n)] n * T[n] = {2 k=0Σn-1 T(k)} + O(n2 ) A Substitute n = n-1, (n-1) * T[(n-1)] = {2 k=0Σn-1 T(k)} + O((n-1)2 )B Subtract A from B
  • 6. n T(n) - (n-1)T(n-1) = 2 T(n-1) + O(n) n T(n) = n+1 T(n-1) + O(n) T(n) = ((n+1)/n )T(n-1) + O(1) Divide by (n+1) T(n)/(n+1) = [T(n-1) ]/ n + O(1/n)  C Let, S(n) = T(n)/(n+1)  D S(n) = S(n-1) + O(1/n) This can be written as a sum, = S(n-2) + O(1/n-1) + O(1/n) = S(n-3) + O(1/n-2) + O(1/n-1) + O(1/n) S(n) = O(k=1 Σ n 1/k) = O( Hn) S(n) = T(n)/(n+1) = O (ln n) E Substitute D in C T(n) = S(n) . (n+1) use E, T(n) = O (ln n) . (n+1) T(n) = O (n lg n ) Notes compiled by Shankar Vaithianthan and Harjinder Mandarh
  • 7. n T(n) - (n-1)T(n-1) = 2 T(n-1) + O(n) n T(n) = n+1 T(n-1) + O(n) T(n) = ((n+1)/n )T(n-1) + O(1) Divide by (n+1) T(n)/(n+1) = [T(n-1) ]/ n + O(1/n)  C Let, S(n) = T(n)/(n+1)  D S(n) = S(n-1) + O(1/n) This can be written as a sum, = S(n-2) + O(1/n-1) + O(1/n) = S(n-3) + O(1/n-2) + O(1/n-1) + O(1/n) S(n) = O(k=1 Σ n 1/k) = O( Hn) S(n) = T(n)/(n+1) = O (ln n) E Substitute D in C T(n) = S(n) . (n+1) use E, T(n) = O (ln n) . (n+1) T(n) = O (n lg n ) Notes compiled by Shankar Vaithianthan and Harjinder Mandarh