SlideShare a Scribd company logo
1 of 37
Download to read offline
Complexity Analysis of
Algorithms
Algorithms
Algorithm is any well defined computational procedure that takes
some value or set of values as input and produce some value or set of
values as output.
2
Properties of an Algorithm
 Be correct.
 Be unambiguous.
 Give the correct solution for all cases.
 Be simple.
 It must terminate.
3
Designing of an Algorithm
Divide and Conquer
• Works by recursively breaking down a problem into
two or more sub-problems of the same (or related)
type, until these become simple enough to be solved
directly.
• The solutions to the sub-problems are then combined
to give a solution to the original problem
4
Designing of an Algorithm (Cont.)
Dynamic Programming
 It is both a mathematical optimization method
and a computerprogramming method.
 Saving results of sub-problems so that they do not need to
be recomputed. And can be used in solving other sub-
problems.
5
Divide & conquer vs Dynamic programming
6
1.The D&C involves three steps at each level of the recursion:
* Divide the problem into a number of sub problems.
* Conquer the sub problems by solving them recursively. If the sub problem sizes are
small enough, however, just solve the sub problems in a straightforward manner.
* Combine the solutions to the sub problems into the solution for the original
problem.
2.D&C does more work on the sub-problems and hence has more time
consumption.
3.In D&C the sub problems are independent of each other. Example:
Merge Sort, Binary Search
Divide & conquer vs Dynamic programming
 Dynamic Programming solves the sub problems only once and then
stores it in the table.
 In DP the sub-problems are not independent.
Example : Matrix chain multiplication
7
Designing of an Algorithm (Cont.)
The Greedy Method
Follows the problem-solving heuristic of making the locally optimal
choice at each stage with the hope of finding a global optimum.
8
Designing of an Algorithm (Cont.)
Backtracking
The Backtracking is a general algorithmic technique that considers
searching every possible combination in order to solve an
optimization problem
“systematically searches for a solution to a problem among all
available option”
9
Advantages of above methods
 Provide a template.
 Translation to data structures is easy.
 The temporal and spatial requirements can be precisely analyzed.
10
Algorithm as Technology
 Different algorithms devised to solve the same problem often
differ dramatically in their efficiency.
 These differences can be much more significant than
differences due to hardware and software.
11
For an example, let us pit a faster computer (computer A)
running insertion sort against a slower computer
(computer B) running merge sort. They each must sort an
array of 10 million numbers(if numbers are 8byte integers
:- 80 megabytes). Suppose that computer A executes 10
billion instructions per second and computer B executes
only 10 million instructions per second.
 Insertion sort (IS) takes time 2n2 to sort n numbers.
 Merge sort (MS) takes time 50n lg n
12
Algorithm as .. (contd. )
 Assume CPU A runs IS, and CPU B runs MS
 A takes:
 B takes:
1010instructions/sec
2(107 )2 instructions  20000seconds(5.5hours)
107instructions/sec
13
50 107 lg107 instructions 
1163seconds( 20minutes)
Algorithm as .. (contd. )
 But computer A is 1000 times faster than
computer B in raw computing power.
 In general, as the problem size increases, so does the relative
advantage of merge sort.
14
Analysis of Algorithms
Idea is to predict the resource usage.
• Memory
• Logic Gates
• Computational Time(***)
Why do we need an analysis?
• To compare
• Predict the growth of run time
15
Analysis of Algorithms
 Study of computer program performance and resource usage.
 Efficiency measure:
- Speed: How long an algorithm takes to produce results
- Space requirement
- Memory requirement
 Use the same computation model for the analyzed algorithms
16
Analysis of Algorithms
 In general, the time taken by an algorithm grows with the size of the
input.
 Input size: depends on problems being studied
(e.g. #elements, #nodes, #links).
 Running time on a particular input: #of primitive operations or steps
executed.
17
Analysis of Algorithms(Contd.)
 Space Complexity
 Time Complexity
18
 Methods for time complexity analysis.
 Select one or more operations such as add, multiply and
compare.
 Operation count: Omits accounting for the time spent on all
but the chosen operations.
Operation count
19
int sum = 0;
20
// 1time
}
T(n) = 3n+3
int i = 0; // 1 time
while (i < n){ // n+1 times
sum++;
i++;
// ntimes
// ntimes int sum = 0; // 1time
int i = 1;
while (i < n) {
// 1 time
// n times
sum++; // n-1times
i++; // n-1 times
}
T(n) =3n
Examples
Q1
Activity
21
int i = 0;
While(i<=10)
{
sum = sum+1;
i++;
}
Q2
int i = 0;
While(i<=n)
{
sum = sum+1;
i++;
}
Q3
int i = 1;
While(i<=n)
{
sum = sum+1;
i++;
}
Operation count (Cont.)
 1 assignment
 n+1comparrisions
 n additions
 n additions
22
int sum = 0;  1 assignmant
for (int i = 0; i < n; i++ )
{
sum++;
}
T(n) = 3n+3
Worst, Best and Average case
Running time will depend on the chosen instance
characteristics.
• Best case: minimum number of steps taken onany
instance of size n.
• Worst case: maximum number of steps taken onany
instance of size n.
• Average case: An average number of steps takenon any
instance of size n.
23
Worst, Best and Average case
Average case
24
worst case
Best case
Input Size
# of steps
Step Count (RAM Model)
 Random Access Machine
 Assume a generic one processor.
 Instructions are executed one after another, with no concurrent
operations.
 +,-,=,it takes exactly one step.
 Each memory access takes exactly 1 step.
 Running Time = Sum of the steps.
25
n  100
26
n  n +100
Print n
1step
2steps
1step
4steps
RAM Model Analysis
sum  0
for i  0 to n
sum  sum A[i]
Steps 6n+9
1 assignment
n+2 assignments
n+2 comparisons
n+1 additions
n+1 assignments
n+1 additions
n+1 memory accesses
Problems with RAM Model
 Differ number of steps with different architecture.
 It is difficult to count the exact number of steps in the
algorithm.
ex: See the insertion sort
27
Asymptotic Notations
Suppose that programs A and B perform the same task.
Assume that one person has determined the step counts of
these programs to be t A(n)=2n2+3n and t B(n)=13n.
 Which program is the faster one ?
 What is the answer if the step count of the program B is
2n+n2 ?
28
Graphs of functions
29
Asymptotic Notations
There are three notations.
O - Notation
 - Notation
 - Notation
30
Big O -Notation
 Introduced by Paul Bechman in 1892.
 We use Big O-notation to give an upper bound on afunction.
Definition:
Eg: What is the big O value of f(n)=2n + 6?
31
Big O -Notation
32
Big O -Notation
Find the Big O value for following fragment of code.
33
for i  1 to n
for j  1 to i
Print j
O(n2)
Big O -Notation
Assignment (s 1)
Addition (s+1)
Multiplication (s*2)
Comparison (S<10)
34
O(1)
Big O -Notation
Find the Bog O value for the following functions.
(i) T(n)= 3 +5n + 3n2
(ii) T(n)= 2n + n2 +8n +7
(iii)T(n)= n + logn +6
35
Int i =10;
While(i>0)
{
print i;
i--;
}
Int i =n;
While(i>0)
{
print i ;
i--;
}
int I, j;
For(int I =0; I<10; i++)
{
for (int j=0; j<=10; j++)
{
print i + j;
}
}
Int k =0;
While(k<=n+1)
{
print k;
k++;
}
Find the time complexity and Big(O) for each of the following code segment.
36
44

More Related Content

Similar to complexity analysis.pdf

Lecture 01-2.ppt
Lecture 01-2.pptLecture 01-2.ppt
Lecture 01-2.pptRaoHamza24
 
Aad introduction
Aad introductionAad introduction
Aad introductionMr SMAK
 
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...TechVision8
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...AntareepMajumder
 
Data structures algorithms basics
Data structures   algorithms basicsData structures   algorithms basics
Data structures algorithms basicsayeshasafdar8
 
Introduction to Data Structure and algorithm.pptx
Introduction to Data Structure and algorithm.pptxIntroduction to Data Structure and algorithm.pptx
Introduction to Data Structure and algorithm.pptxesuEthopi
 
Analysis of Algorithms
Analysis of AlgorithmsAnalysis of Algorithms
Analysis of AlgorithmsAmna Saeed
 
Design & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesDesign & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesFellowBuddy.com
 
Unit i basic concepts of algorithms
Unit i basic concepts of algorithmsUnit i basic concepts of algorithms
Unit i basic concepts of algorithmssangeetha s
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm AnalysisMary Margarat
 
Chapter 1 Data structure.pptx
Chapter 1 Data structure.pptxChapter 1 Data structure.pptx
Chapter 1 Data structure.pptxwondmhunegn
 
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMSTIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMSTanya Makkar
 
CP4151 ADSA unit1 Advanced Data Structures and Algorithms
CP4151 ADSA unit1 Advanced Data Structures and AlgorithmsCP4151 ADSA unit1 Advanced Data Structures and Algorithms
CP4151 ADSA unit1 Advanced Data Structures and AlgorithmsSheba41
 

Similar to complexity analysis.pdf (20)

Lecture 01-2.ppt
Lecture 01-2.pptLecture 01-2.ppt
Lecture 01-2.ppt
 
Aad introduction
Aad introductionAad introduction
Aad introduction
 
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
 
Data structures algorithms basics
Data structures   algorithms basicsData structures   algorithms basics
Data structures algorithms basics
 
Daa chapter 1
Daa chapter 1Daa chapter 1
Daa chapter 1
 
Introduction to Data Structure and algorithm.pptx
Introduction to Data Structure and algorithm.pptxIntroduction to Data Structure and algorithm.pptx
Introduction to Data Structure and algorithm.pptx
 
Analysis of Algorithms
Analysis of AlgorithmsAnalysis of Algorithms
Analysis of Algorithms
 
Daa
DaaDaa
Daa
 
Design & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesDesign & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture Notes
 
Unit i basic concepts of algorithms
Unit i basic concepts of algorithmsUnit i basic concepts of algorithms
Unit i basic concepts of algorithms
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm Analysis
 
Unit 2 algorithm
Unit   2 algorithmUnit   2 algorithm
Unit 2 algorithm
 
Chapter 1 Data structure.pptx
Chapter 1 Data structure.pptxChapter 1 Data structure.pptx
Chapter 1 Data structure.pptx
 
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMSTIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
 
Cs2251 daa
Cs2251 daaCs2251 daa
Cs2251 daa
 
ALGO.ppt
ALGO.pptALGO.ppt
ALGO.ppt
 
CP4151 ADSA unit1 Advanced Data Structures and Algorithms
CP4151 ADSA unit1 Advanced Data Structures and AlgorithmsCP4151 ADSA unit1 Advanced Data Structures and Algorithms
CP4151 ADSA unit1 Advanced Data Structures and Algorithms
 
Algorithms
Algorithms Algorithms
Algorithms
 
Unit 1.pptx
Unit 1.pptxUnit 1.pptx
Unit 1.pptx
 

Recently uploaded

代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改atducpo
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationshipsccctableauusergroup
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home ServiceSapana Sha
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfLars Albertsson
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Jack DiGiovanna
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)jennyeacort
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998YohFuh
 
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...ThinkInnovation
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024thyngster
 
Data Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxData Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxFurkanTasci3
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdfHuman37
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFAAndrei Kaleshka
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一F sss
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptxthyngster
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts ServiceSapana Sha
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130Suhani Kapoor
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfgstagge
 
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort servicejennyeacort
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...Pooja Nehwal
 

Recently uploaded (20)

代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdf
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998
 
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
 
Data Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxData Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptx
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFA
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts Service
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdf
 
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
 

complexity analysis.pdf

  • 2. Algorithms Algorithm is any well defined computational procedure that takes some value or set of values as input and produce some value or set of values as output. 2
  • 3. Properties of an Algorithm  Be correct.  Be unambiguous.  Give the correct solution for all cases.  Be simple.  It must terminate. 3
  • 4. Designing of an Algorithm Divide and Conquer • Works by recursively breaking down a problem into two or more sub-problems of the same (or related) type, until these become simple enough to be solved directly. • The solutions to the sub-problems are then combined to give a solution to the original problem 4
  • 5. Designing of an Algorithm (Cont.) Dynamic Programming  It is both a mathematical optimization method and a computerprogramming method.  Saving results of sub-problems so that they do not need to be recomputed. And can be used in solving other sub- problems. 5
  • 6. Divide & conquer vs Dynamic programming 6 1.The D&C involves three steps at each level of the recursion: * Divide the problem into a number of sub problems. * Conquer the sub problems by solving them recursively. If the sub problem sizes are small enough, however, just solve the sub problems in a straightforward manner. * Combine the solutions to the sub problems into the solution for the original problem. 2.D&C does more work on the sub-problems and hence has more time consumption. 3.In D&C the sub problems are independent of each other. Example: Merge Sort, Binary Search
  • 7. Divide & conquer vs Dynamic programming  Dynamic Programming solves the sub problems only once and then stores it in the table.  In DP the sub-problems are not independent. Example : Matrix chain multiplication 7
  • 8. Designing of an Algorithm (Cont.) The Greedy Method Follows the problem-solving heuristic of making the locally optimal choice at each stage with the hope of finding a global optimum. 8
  • 9. Designing of an Algorithm (Cont.) Backtracking The Backtracking is a general algorithmic technique that considers searching every possible combination in order to solve an optimization problem “systematically searches for a solution to a problem among all available option” 9
  • 10. Advantages of above methods  Provide a template.  Translation to data structures is easy.  The temporal and spatial requirements can be precisely analyzed. 10
  • 11. Algorithm as Technology  Different algorithms devised to solve the same problem often differ dramatically in their efficiency.  These differences can be much more significant than differences due to hardware and software. 11
  • 12. For an example, let us pit a faster computer (computer A) running insertion sort against a slower computer (computer B) running merge sort. They each must sort an array of 10 million numbers(if numbers are 8byte integers :- 80 megabytes). Suppose that computer A executes 10 billion instructions per second and computer B executes only 10 million instructions per second.  Insertion sort (IS) takes time 2n2 to sort n numbers.  Merge sort (MS) takes time 50n lg n 12
  • 13. Algorithm as .. (contd. )  Assume CPU A runs IS, and CPU B runs MS  A takes:  B takes: 1010instructions/sec 2(107 )2 instructions  20000seconds(5.5hours) 107instructions/sec 13 50 107 lg107 instructions  1163seconds( 20minutes)
  • 14. Algorithm as .. (contd. )  But computer A is 1000 times faster than computer B in raw computing power.  In general, as the problem size increases, so does the relative advantage of merge sort. 14
  • 15. Analysis of Algorithms Idea is to predict the resource usage. • Memory • Logic Gates • Computational Time(***) Why do we need an analysis? • To compare • Predict the growth of run time 15
  • 16. Analysis of Algorithms  Study of computer program performance and resource usage.  Efficiency measure: - Speed: How long an algorithm takes to produce results - Space requirement - Memory requirement  Use the same computation model for the analyzed algorithms 16
  • 17. Analysis of Algorithms  In general, the time taken by an algorithm grows with the size of the input.  Input size: depends on problems being studied (e.g. #elements, #nodes, #links).  Running time on a particular input: #of primitive operations or steps executed. 17
  • 18. Analysis of Algorithms(Contd.)  Space Complexity  Time Complexity 18
  • 19.  Methods for time complexity analysis.  Select one or more operations such as add, multiply and compare.  Operation count: Omits accounting for the time spent on all but the chosen operations. Operation count 19
  • 20. int sum = 0; 20 // 1time } T(n) = 3n+3 int i = 0; // 1 time while (i < n){ // n+1 times sum++; i++; // ntimes // ntimes int sum = 0; // 1time int i = 1; while (i < n) { // 1 time // n times sum++; // n-1times i++; // n-1 times } T(n) =3n Examples
  • 21. Q1 Activity 21 int i = 0; While(i<=10) { sum = sum+1; i++; } Q2 int i = 0; While(i<=n) { sum = sum+1; i++; } Q3 int i = 1; While(i<=n) { sum = sum+1; i++; }
  • 22. Operation count (Cont.) 1 assignment n+1comparrisions n additions n additions 22 int sum = 0; 1 assignmant for (int i = 0; i < n; i++ ) { sum++; } T(n) = 3n+3
  • 23. Worst, Best and Average case Running time will depend on the chosen instance characteristics. • Best case: minimum number of steps taken onany instance of size n. • Worst case: maximum number of steps taken onany instance of size n. • Average case: An average number of steps takenon any instance of size n. 23
  • 24. Worst, Best and Average case Average case 24 worst case Best case Input Size # of steps
  • 25. Step Count (RAM Model)  Random Access Machine  Assume a generic one processor.  Instructions are executed one after another, with no concurrent operations.  +,-,=,it takes exactly one step.  Each memory access takes exactly 1 step.  Running Time = Sum of the steps. 25
  • 26. n  100 26 n  n +100 Print n 1step 2steps 1step 4steps RAM Model Analysis sum  0 for i  0 to n sum  sum A[i] Steps 6n+9 1 assignment n+2 assignments n+2 comparisons n+1 additions n+1 assignments n+1 additions n+1 memory accesses
  • 27. Problems with RAM Model  Differ number of steps with different architecture.  It is difficult to count the exact number of steps in the algorithm. ex: See the insertion sort 27
  • 28. Asymptotic Notations Suppose that programs A and B perform the same task. Assume that one person has determined the step counts of these programs to be t A(n)=2n2+3n and t B(n)=13n.  Which program is the faster one ?  What is the answer if the step count of the program B is 2n+n2 ? 28
  • 30. Asymptotic Notations There are three notations. O - Notation  - Notation  - Notation 30
  • 31. Big O -Notation  Introduced by Paul Bechman in 1892.  We use Big O-notation to give an upper bound on afunction. Definition: Eg: What is the big O value of f(n)=2n + 6? 31
  • 33. Big O -Notation Find the Big O value for following fragment of code. 33 for i  1 to n for j  1 to i Print j O(n2)
  • 34. Big O -Notation Assignment (s 1) Addition (s+1) Multiplication (s*2) Comparison (S<10) 34 O(1)
  • 35. Big O -Notation Find the Bog O value for the following functions. (i) T(n)= 3 +5n + 3n2 (ii) T(n)= 2n + n2 +8n +7 (iii)T(n)= n + logn +6 35
  • 36. Int i =10; While(i>0) { print i; i--; } Int i =n; While(i>0) { print i ; i--; } int I, j; For(int I =0; I<10; i++) { for (int j=0; j<=10; j++) { print i + j; } } Int k =0; While(k<=n+1) { print k; k++; } Find the time complexity and Big(O) for each of the following code segment. 36
  • 37. 44