SlideShare une entreprise Scribd logo
1  sur  29
Analysis of Algorithms The Non-recursive Case Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License.
Key Topics: ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Why Analyze Algorithms? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Generalizing Running Time Comparing the growth of the running time as the input grows to the growth of known functions. 10 3000 10 12 10 8 10 5 10000 13 1 10000 10 300 10 9 10 6 10 4 1000 10 1 1000 10 30 10 6 10 4 664 100 7 1 100 10³ 10³ 100 33 10 4 1 10 32 125 25 15 5 3 1 5 2ⁿ n³ n² n log n  n log n (1) Input Size: n
Analyzing Running Time 1. n = read input from user 2. sum = 0 3. i = 0 4. while i < n  5. number = read input from user 6. sum = sum + number 7. i = i + 1 8. mean = sum / n T(n), or the running time of a particular algorithm on input of size n, is taken to be the number of times the instructions in the algorithm are executed. Pseudo code algorithm illustrates the calculation of the mean (average) of a set of n numbers: Statement Number of times executed 1 1 2 1 3 1 4 n+1 5 n 6 n 7 n 8 1 The computing time for this algorithm in terms on input size n is: T(n) = 4n + 5.
Big-Oh Notation Definition 1 : Let f(n) and g(n) be two functions.  We write: f(n) = O(g(n))  or  f = O(g) (read &quot;f of n is big oh of g of n&quot; or &quot;f is big oh of g&quot;) if there is a positive integer C such that f(n) <= C * g(n) for all positive integers n. The basic idea of big-Oh notation is this: Suppose f and g are both real-valued functions of a real variable x. If, for large values of x, the graph of f lies closer to the horizontal axis than the graph of some multiple of g, then f is of order g, i.e., f(x) = O(g(x)). So, g(x) represents an  upper bound  on f(x).
Example 1 ,[object Object],[object Object],[object Object],Therefore, a constant C exists (we only need one) and f = O(g).
Example 2 In the previous timing analysis, we ended up with T(n) = 4n + 5, and we concluded intuitively that T(n) = O(n) because the running time grows linearly as n grows.  Now, however, we can prove it mathematically: To show that f(n) = 4n + 5 = O(n), we need to produce a constant C such that: f(n) <= C * n for all n. If we try C = 4, this doesn't work because 4n + 5 is not less than 4n.  We need C to be at least 9 to cover  all  n.  If n = 1, C has to be 9, but C can be smaller for greater values of n (if n = 100, C can be 5).  Since the chosen C must work for all n, we must use 9: 4n + 5 <= 4n + 5n = 9n Since we have produced a constant C that works for all n, we can conclude: 
Example 3 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],f(n)   O(n) when f(n)    n2
Example 4 Suppose f(n) = n 2   + 3n - 1.  We want to show that f(n) = O(n 2 ). f(n)  = n 2  + 3n - 1 < n 2  + 3n  (subtraction makes things smaller so drop it) <= n 2  + 3n 2  (since n <= n 2  for all integers n)   = 4n 2 Therefore, if C = 4, we have shown that f(n) = O(n 2 ).  Notice that all we are doing is finding a simple function that is an upper bound on the original function.  Because of this, we could also say that This would be a much weaker description, but it is still valid. f(n) = O(n 3 ) since (n 3 ) is an upper bound  on n2
Example 5 Show:  f(n) = 2n 7  - 6n 5  + 10n 2  – 5 = O(n 7 ) f(n) < 2n 7  + 6n 5  + 10n 2 <= 2n 7  + 6n 7  + 10n 7 = 18n 7 thus, with  C = 18  and we have shown that  f(n) = O(n 7 ) Any polynomial is big-Oh of its  term of highest degree .  We are also ignoring constants. Any polynomial (including a general one) can be manipulated to satisfy the big-Oh definition by doing what we did in the last example: take the absolute value of each coefficient (this can only increase the function); Then since  we can change the exponents of all the terms to the highest degree (the original function must be less than this too).  Finally, we add these terms together to get the largest constant C we need to find a function that is an upper bound on the original one. n j   <=  n d   if j <= d
Adjusting the definition of big-Oh: Many algorithms have a rate of growth that matches logarithmic functions. Recall that log2 n is the number of times we have to divide n by 2 to get 1; or alternatively, the number of 2's we must multiply together to get n:    n = 2 k      log 2  n = k Many &quot;Divide and Conquer&quot; algorithms solve a problem by dividing it into 2 smaller problems. You keep dividing until you get to the point where solving the problem is trivial.  This constant division by 2 suggests a logarithmic running time. Definition 2 : Let f(n) and g(n) be two functions.  We write: f(n) = O(g(n))  or  f = O(g) if there are positive integers C and N such that  f(n) <= C * g(n)  for all integers n >= N. Using this more general definition for big-Oh, we can now say that if we have f(n) = 1, then f(n) = O(log(n)) since C = 1 and N = 2 will work.
With this definition, we can clearly see the difference between the three types of notation: ,[object Object],There is a handy theorem that relates these notations: Theorem : For any two functions f(n) and g(n), f(n) =   (g(n)) if and only if f(n) = O(g(n)) and f(n) =    (g(n)).
Example 6: Show:  f(n) = 3n 3  + 3n - 1 =    (n 3 ) As implied by the theorem above, to show this result, we must show two properties: f(n) = O (n 3 ) f(n) =    (n 3 ) First, we show (i), using the same techniques we've already seen for big-Oh. We consider N = 1, and thus we only consider n >= 1 to show the big-Oh result. f(n) = 3n 3  + 3n - 1 < 3n 3  + 3n + 1   <= 3n 3  + 3n 3  + 1n 3 = 7n 3 thus, with  C = 7 and N = 1  we have shown that  f(n) = O(n 3 )
Next, we show (ii).  Here we must provide a lower bound for  f(n).   Here, we choose a value for N, such that the highest order term in  f(n)  will always dominate (be greater than) the lower order terms.  We choose  N = 2 , since for  n >=2 , we have  n3 >= 8 .  This will allow  n 3  to be larger than the remainder of the polynomial  (3n - 1) for all n >= 2.   So, by subtracting an extra n 3  term, we will form a polynomial that will always be less than  f(n) for n >= 2 . f(n) = 3n 3  + 3n - 1 > 3n 3  - n3  since n 3  > 3n - 1 for any n >= 2 = 2n 3 Thus, with  C = 2  and  N = 2 , we have shown that  f(n) =    (n 3 ) since  f(n)  is shown to always be greater than  2n 3 .
Big-Oh Operations Summation Rule Suppose T1(n) = O(f1(n)) and T2(n) = O(f2(n)).  Further, suppose that f2 grows no faster than f1, i.e., f2(n) = O(f1(n)).  Then, we can conclude that T1(n) + T2(n) = O(f1(n)).  More generally, the summation rule tells us O(f1(n) + f2(n)) = O(max(f1(n), f2(n))). Proof :  Suppose that C and C' are constants such that T1(n) <= C * f1(n) and T2(n) <= C' * f2(n).  Let D = the larger of C and C'.  Then, T1(n) + T2(n) <=  C * f1(n)  + C' * f2(n) <=  D * f1(n) + D * f2(n) <=  D * (f1(n)  +  f2(n)) <=  O(f1(n)  +  f2(n))
Product Rule Suppose T1(n) = O(f1(n))  and T2(n) = O(f2(n)).  Then, we can conclude that T1(n) * T2(n) =  O(f1(n) * f2(n)).  The Product Rule can be proven using a similar strategy as the Summation Rule proof. ,[object Object],[object Object],[object Object],[object Object],[object Object]
Example 7 Compute the big-Oh running time of the following C++ code segment: for (i = 2; i < n; i++) { sum += i; } The number of iterations of a for loop is equal to the top index of the loop minus the bottom index, plus one more instruction to account for the final conditional test.  Note: if the for loop terminating condition is  i <= n , rather than  i < n , then the number of times the conditional test is performed is: ((top_index + 1) – bottom_index) + 1) In this case, we have  n - 2 + 1 = n - 1 . The assignment in the loop is executed  n - 2  times.  So, we have  (n - 1) + (n - 2) = (2n - 3)  instructions executed  = O(n).
Example 8 Consider the sorting algorithm shown below.  Find the number of instructions executed and the complexity of this algorithm. 1) for (i = 1; i < n; i++) { 2) SmallPos = i; 3) Smallest = Array[SmallPos]; 4) for (j = i+1; j <= n; j++)  5) if (Array[j] < Smallest) { 6) SmallPos = j; 7) Smallest  = Array[SmallPos] } 8) Array[SmallPos] = Array[i]; 9)  Array[i] = Smallest; } The total computing time is:  T(n)  = (n) + 4(n-1) + n(n+1)/2 – 1 + 3[n(n-1) / 2] = n  + 4n - 4 + (n 2  + n)/2 – 1 + (3n 2  - 3n) / 2 = 5n - 5 + (4n 2  - 2n) / 2 = 5n - 5 + 2n 2  - n = 2n 2  + 4n - 5  = O(n 2 )
Example 9 What is the complexity of this C++ code? 1) cin >> n; // Same as: n = GetInteger(); 2) for (i = 1; i <= n; i ++)  3) for (j = 1; j <= n; j ++)  4) A[i][j] = 0; 5) for (i = 1; i <= n; i ++)  6) A[i][i] = 1; The following program segment initializes a two-dimensional array A (which has n rows and n columns) to be an n x n identity matrix – that is, a matrix with 1’s on the diagonal and 0’s everywhere else.  More formally, if A is an n x n identity matrix, then: A x M = M x A = M, for any n x n matrix M.
Example 10 Here is a simple linear search algorithm that returns the index location of a value in an array. /* a is the array of size n we are searching through */ i = 0; while ((i < n) && (x != a[i])) i++; if (i < n) location = i;  else location = -1; 1 + 3 + 5 + ... + (2n - 1)  /  n = (2 (1 + 2 + 3 + ... + n) - n)  /  n We know that   1 + 2 + 3 + ... + n = n (n + 1) / 2,   so the average number of lines executed is: [2[n(n+1)/2] – n]/n =n  =O(n) Average number of lines executed equals:
Analyzing Programs with Non-Recursive Subprogram Calls ,[object Object],[object Object],[object Object],int a, n, x;  int bar(int x, int n) { int i; 1)  for (i = 1; i < n; i++)  2)  x = x + i; 3)  return x; } int foo(int x, int n) { int i; 4)  for (i = 1; i <= n; i++)  5)  x = x + bar(i, n); 6)  return x; } ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Here is the body of a function:   sum = 0;   for (i = 1; i <= f(n); i++)   sum += i; where f(n) is a function call.  Give a big-oh upper bound on this function if the running time of f(n) is O(n), and the  value  of f(n) is n!:
Classes of Problems ,[object Object],[object Object],[object Object],[object Object]
Problems, Problems, Problems… Different sets of problems: ,[object Object],[object Object],[object Object],NP-Complete? Polynomial Exponential Undecidable
Two Famous Problems ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Polynomial Transformation Informally, if P 1     P 2 , then we can think of a solution to P 1  being obtained from a solution to P 2  in polynomial time.   The code below gives some idea of what is implied when we say P 1     P 2 :  Convert_To_P2 p1 = ... /*  Takes an instance of p1 and converts   it to an instance of P2 in polynomial    time.  */ Solve_P2 p2 = ... /*  Solves problem P2  */ Solve_P1 p1 = Solve_P2(Convert_To_P2 p1);
Given the above definition of a transformation, these theorems should not be very surprising: If    1        2  then      2      P        1      P If    1        2  then      2       P        1       P These theorems suggest a technique for proving that a given problem    is NP-complete. To Prove    NP:  1) Find a known NP-complete problem   NP 2) Find a transformation such that   NP    3) Prove that the transformation is polynomial.
The meaning of NP-Completeness A Statement of a Problem:  Solving a problem means finding one algorithm that will solve all instances of the problem.    

Contenu connexe

Tendances

DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSGayathri Gaayu
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihmSajid Marwat
 
asymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysisasymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysisAnindita Kundu
 
Lecture 5: Asymptotic analysis of algorithms
Lecture 5: Asymptotic analysis of algorithmsLecture 5: Asymptotic analysis of algorithms
Lecture 5: Asymptotic analysis of algorithmsVivek Bhargav
 
Design and Analysis of algorithms
Design and Analysis of algorithmsDesign and Analysis of algorithms
Design and Analysis of algorithmsDr. Rupa Ch
 
Fundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm EfficiencyFundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm EfficiencySaranya Natarajan
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IMohamed Loey
 
01 Analysis of Algorithms: Introduction
01 Analysis of Algorithms: Introduction01 Analysis of Algorithms: Introduction
01 Analysis of Algorithms: IntroductionAndres Mendez-Vazquez
 
Unit i basic concepts of algorithms
Unit i basic concepts of algorithmsUnit i basic concepts of algorithms
Unit i basic concepts of algorithmssangeetha s
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of AlgorithmsArvind Krishnaa
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithmsGanesh Solanke
 
Lecture 4 asymptotic notations
Lecture 4   asymptotic notationsLecture 4   asymptotic notations
Lecture 4 asymptotic notationsjayavignesh86
 
Data Structure: Algorithm and analysis
Data Structure: Algorithm and analysisData Structure: Algorithm and analysis
Data Structure: Algorithm and analysisDr. Rajdeep Chatterjee
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of AlgorithmsSwapnil Agrawal
 

Tendances (20)

DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
 
chapter 1
chapter 1chapter 1
chapter 1
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
 
asymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysisasymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysis
 
Lecture 5: Asymptotic analysis of algorithms
Lecture 5: Asymptotic analysis of algorithmsLecture 5: Asymptotic analysis of algorithms
Lecture 5: Asymptotic analysis of algorithms
 
Complexity analysis in Algorithms
Complexity analysis in AlgorithmsComplexity analysis in Algorithms
Complexity analysis in Algorithms
 
Design and Analysis of algorithms
Design and Analysis of algorithmsDesign and Analysis of algorithms
Design and Analysis of algorithms
 
Fundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm EfficiencyFundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm Efficiency
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms I
 
Slide2
Slide2Slide2
Slide2
 
01 Analysis of Algorithms: Introduction
01 Analysis of Algorithms: Introduction01 Analysis of Algorithms: Introduction
01 Analysis of Algorithms: Introduction
 
Unit i basic concepts of algorithms
Unit i basic concepts of algorithmsUnit i basic concepts of algorithms
Unit i basic concepts of algorithms
 
Daa unit 2
Daa unit 2Daa unit 2
Daa unit 2
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of Algorithms
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Lecture 4 asymptotic notations
Lecture 4   asymptotic notationsLecture 4   asymptotic notations
Lecture 4 asymptotic notations
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
 
Data Structure: Algorithm and analysis
Data Structure: Algorithm and analysisData Structure: Algorithm and analysis
Data Structure: Algorithm and analysis
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of Algorithms
 

En vedette

Data structures and Big O notation
Data structures and Big O notationData structures and Big O notation
Data structures and Big O notationMuthiah Abbhirami
 
9 big o-notation
9 big o-notation9 big o-notation
9 big o-notationirdginfo
 
QUESTION BANK FOR ANNA UNNIVERISTY SYLLABUS
QUESTION BANK FOR ANNA UNNIVERISTY SYLLABUSQUESTION BANK FOR ANNA UNNIVERISTY SYLLABUS
QUESTION BANK FOR ANNA UNNIVERISTY SYLLABUSJAMBIKA
 
Big o notation
Big o notationBig o notation
Big o notationkeb97
 
Linear Search & Binary Search
Linear Search & Binary SearchLinear Search & Binary Search
Linear Search & Binary SearchReem Alattas
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithmNeoClassical
 
Slideshare Powerpoint presentation
Slideshare Powerpoint presentationSlideshare Powerpoint presentation
Slideshare Powerpoint presentationelliehood
 

En vedette (13)

Analysis of algo
Analysis of algoAnalysis of algo
Analysis of algo
 
Analysis of algorithm
Analysis of algorithmAnalysis of algorithm
Analysis of algorithm
 
Algorithum Analysis
Algorithum AnalysisAlgorithum Analysis
Algorithum Analysis
 
Data structures and Big O notation
Data structures and Big O notationData structures and Big O notation
Data structures and Big O notation
 
9 big o-notation
9 big o-notation9 big o-notation
9 big o-notation
 
QUESTION BANK FOR ANNA UNNIVERISTY SYLLABUS
QUESTION BANK FOR ANNA UNNIVERISTY SYLLABUSQUESTION BANK FOR ANNA UNNIVERISTY SYLLABUS
QUESTION BANK FOR ANNA UNNIVERISTY SYLLABUS
 
Algorithm big o
Algorithm big oAlgorithm big o
Algorithm big o
 
Big o notation
Big o notationBig o notation
Big o notation
 
Linear Search & Binary Search
Linear Search & Binary SearchLinear Search & Binary Search
Linear Search & Binary Search
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithm
 
Compiler Chapter 1
Compiler Chapter 1Compiler Chapter 1
Compiler Chapter 1
 
Gephi Toolkit Tutorial
Gephi Toolkit TutorialGephi Toolkit Tutorial
Gephi Toolkit Tutorial
 
Slideshare Powerpoint presentation
Slideshare Powerpoint presentationSlideshare Powerpoint presentation
Slideshare Powerpoint presentation
 

Similaire à Analysis Of Algorithms I

Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic NotationsNagendraK18
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfAmayJaiswal4
 
Weekends with Competitive Programming
Weekends with Competitive ProgrammingWeekends with Competitive Programming
Weekends with Competitive ProgrammingNiharikaSingh839269
 
Lec03 04-time complexity
Lec03 04-time complexityLec03 04-time complexity
Lec03 04-time complexityAbbas Ali
 
Algorithm Analysis
Algorithm AnalysisAlgorithm Analysis
Algorithm AnalysisMegha V
 
Asymptotic Notation and Complexity
Asymptotic Notation and ComplexityAsymptotic Notation and Complexity
Asymptotic Notation and ComplexityRajandeep Gill
 
Asymptotic notation
Asymptotic notationAsymptotic notation
Asymptotic notationsajinis3
 
Data_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.pptData_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.pptISHANAMRITSRIVASTAVA
 
Data Structure & Algorithms - Mathematical
Data Structure & Algorithms - MathematicalData Structure & Algorithms - Mathematical
Data Structure & Algorithms - Mathematicalbabuk110
 
Algorithms required for data structures(basics like Arrays, Stacks ,Linked Li...
Algorithms required for data structures(basics like Arrays, Stacks ,Linked Li...Algorithms required for data structures(basics like Arrays, Stacks ,Linked Li...
Algorithms required for data structures(basics like Arrays, Stacks ,Linked Li...DebiPrasadSen
 
Daa unit 6_efficiency of algorithms
Daa unit 6_efficiency of algorithmsDaa unit 6_efficiency of algorithms
Daa unit 6_efficiency of algorithmssnehajiyani
 
Basic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusBasic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusNANDINI SHARMA
 
lecture 1
lecture 1lecture 1
lecture 1sajinsc
 
Asymptotic Analysis in Data Structure using C
Asymptotic Analysis in Data Structure using CAsymptotic Analysis in Data Structure using C
Asymptotic Analysis in Data Structure using CMeghaj Mallick
 
module1_Introductiontoalgorithms_2022.pdf
module1_Introductiontoalgorithms_2022.pdfmodule1_Introductiontoalgorithms_2022.pdf
module1_Introductiontoalgorithms_2022.pdfShiwani Gupta
 

Similaire à Analysis Of Algorithms I (20)

big_oh
big_ohbig_oh
big_oh
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
 
Weekends with Competitive Programming
Weekends with Competitive ProgrammingWeekends with Competitive Programming
Weekends with Competitive Programming
 
Lec03 04-time complexity
Lec03 04-time complexityLec03 04-time complexity
Lec03 04-time complexity
 
Algorithm Analysis
Algorithm AnalysisAlgorithm Analysis
Algorithm Analysis
 
Time complexity.ppt
Time complexity.pptTime complexity.ppt
Time complexity.ppt
 
Asymptotic Notation and Complexity
Asymptotic Notation and ComplexityAsymptotic Notation and Complexity
Asymptotic Notation and Complexity
 
algorithm_analysis1
algorithm_analysis1algorithm_analysis1
algorithm_analysis1
 
Asymptotic notation
Asymptotic notationAsymptotic notation
Asymptotic notation
 
Data_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.pptData_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.ppt
 
Data Structure & Algorithms - Mathematical
Data Structure & Algorithms - MathematicalData Structure & Algorithms - Mathematical
Data Structure & Algorithms - Mathematical
 
Time complexity
Time complexityTime complexity
Time complexity
 
Algorithms required for data structures(basics like Arrays, Stacks ,Linked Li...
Algorithms required for data structures(basics like Arrays, Stacks ,Linked Li...Algorithms required for data structures(basics like Arrays, Stacks ,Linked Li...
Algorithms required for data structures(basics like Arrays, Stacks ,Linked Li...
 
Daa unit 6_efficiency of algorithms
Daa unit 6_efficiency of algorithmsDaa unit 6_efficiency of algorithms
Daa unit 6_efficiency of algorithms
 
Basic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusBasic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV Syllabus
 
lecture 1
lecture 1lecture 1
lecture 1
 
Asymptotic Analysis in Data Structure using C
Asymptotic Analysis in Data Structure using CAsymptotic Analysis in Data Structure using C
Asymptotic Analysis in Data Structure using C
 
module1_Introductiontoalgorithms_2022.pdf
module1_Introductiontoalgorithms_2022.pdfmodule1_Introductiontoalgorithms_2022.pdf
module1_Introductiontoalgorithms_2022.pdf
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 

Plus de Sri Prasanna

Plus de Sri Prasanna (20)

Qr codes para tech radar
Qr codes para tech radarQr codes para tech radar
Qr codes para tech radar
 
Qr codes para tech radar 2
Qr codes para tech radar 2Qr codes para tech radar 2
Qr codes para tech radar 2
 
Test
TestTest
Test
 
Test
TestTest
Test
 
assds
assdsassds
assds
 
assds
assdsassds
assds
 
asdsa
asdsaasdsa
asdsa
 
dsd
dsddsd
dsd
 
About stacks
About stacksAbout stacks
About stacks
 
About Stacks
About  StacksAbout  Stacks
About Stacks
 
About Stacks
About  StacksAbout  Stacks
About Stacks
 
About Stacks
About  StacksAbout  Stacks
About Stacks
 
About Stacks
About  StacksAbout  Stacks
About Stacks
 
About Stacks
About  StacksAbout  Stacks
About Stacks
 
About Stacks
About StacksAbout Stacks
About Stacks
 
About Stacks
About StacksAbout Stacks
About Stacks
 
Network and distributed systems
Network and distributed systemsNetwork and distributed systems
Network and distributed systems
 
Introduction & Parellelization on large scale clusters
Introduction & Parellelization on large scale clustersIntroduction & Parellelization on large scale clusters
Introduction & Parellelization on large scale clusters
 
Mapreduce: Theory and implementation
Mapreduce: Theory and implementationMapreduce: Theory and implementation
Mapreduce: Theory and implementation
 
Other distributed systems
Other distributed systemsOther distributed systems
Other distributed systems
 

Dernier

What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxnelietumpap1
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 

Dernier (20)

OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 

Analysis Of Algorithms I

  • 1. Analysis of Algorithms The Non-recursive Case Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License.
  • 2.
  • 3.
  • 4. Generalizing Running Time Comparing the growth of the running time as the input grows to the growth of known functions. 10 3000 10 12 10 8 10 5 10000 13 1 10000 10 300 10 9 10 6 10 4 1000 10 1 1000 10 30 10 6 10 4 664 100 7 1 100 10³ 10³ 100 33 10 4 1 10 32 125 25 15 5 3 1 5 2ⁿ n³ n² n log n n log n (1) Input Size: n
  • 5. Analyzing Running Time 1. n = read input from user 2. sum = 0 3. i = 0 4. while i < n 5. number = read input from user 6. sum = sum + number 7. i = i + 1 8. mean = sum / n T(n), or the running time of a particular algorithm on input of size n, is taken to be the number of times the instructions in the algorithm are executed. Pseudo code algorithm illustrates the calculation of the mean (average) of a set of n numbers: Statement Number of times executed 1 1 2 1 3 1 4 n+1 5 n 6 n 7 n 8 1 The computing time for this algorithm in terms on input size n is: T(n) = 4n + 5.
  • 6. Big-Oh Notation Definition 1 : Let f(n) and g(n) be two functions. We write: f(n) = O(g(n)) or f = O(g) (read &quot;f of n is big oh of g of n&quot; or &quot;f is big oh of g&quot;) if there is a positive integer C such that f(n) <= C * g(n) for all positive integers n. The basic idea of big-Oh notation is this: Suppose f and g are both real-valued functions of a real variable x. If, for large values of x, the graph of f lies closer to the horizontal axis than the graph of some multiple of g, then f is of order g, i.e., f(x) = O(g(x)). So, g(x) represents an upper bound on f(x).
  • 7.
  • 8. Example 2 In the previous timing analysis, we ended up with T(n) = 4n + 5, and we concluded intuitively that T(n) = O(n) because the running time grows linearly as n grows. Now, however, we can prove it mathematically: To show that f(n) = 4n + 5 = O(n), we need to produce a constant C such that: f(n) <= C * n for all n. If we try C = 4, this doesn't work because 4n + 5 is not less than 4n. We need C to be at least 9 to cover all n. If n = 1, C has to be 9, but C can be smaller for greater values of n (if n = 100, C can be 5). Since the chosen C must work for all n, we must use 9: 4n + 5 <= 4n + 5n = 9n Since we have produced a constant C that works for all n, we can conclude: 
  • 9.
  • 10. Example 4 Suppose f(n) = n 2 + 3n - 1. We want to show that f(n) = O(n 2 ). f(n) = n 2 + 3n - 1 < n 2 + 3n (subtraction makes things smaller so drop it) <= n 2 + 3n 2 (since n <= n 2 for all integers n) = 4n 2 Therefore, if C = 4, we have shown that f(n) = O(n 2 ). Notice that all we are doing is finding a simple function that is an upper bound on the original function. Because of this, we could also say that This would be a much weaker description, but it is still valid. f(n) = O(n 3 ) since (n 3 ) is an upper bound on n2
  • 11. Example 5 Show: f(n) = 2n 7 - 6n 5 + 10n 2 – 5 = O(n 7 ) f(n) < 2n 7 + 6n 5 + 10n 2 <= 2n 7 + 6n 7 + 10n 7 = 18n 7 thus, with C = 18 and we have shown that f(n) = O(n 7 ) Any polynomial is big-Oh of its term of highest degree . We are also ignoring constants. Any polynomial (including a general one) can be manipulated to satisfy the big-Oh definition by doing what we did in the last example: take the absolute value of each coefficient (this can only increase the function); Then since we can change the exponents of all the terms to the highest degree (the original function must be less than this too). Finally, we add these terms together to get the largest constant C we need to find a function that is an upper bound on the original one. n j <= n d if j <= d
  • 12. Adjusting the definition of big-Oh: Many algorithms have a rate of growth that matches logarithmic functions. Recall that log2 n is the number of times we have to divide n by 2 to get 1; or alternatively, the number of 2's we must multiply together to get n: n = 2 k  log 2 n = k Many &quot;Divide and Conquer&quot; algorithms solve a problem by dividing it into 2 smaller problems. You keep dividing until you get to the point where solving the problem is trivial. This constant division by 2 suggests a logarithmic running time. Definition 2 : Let f(n) and g(n) be two functions. We write: f(n) = O(g(n)) or f = O(g) if there are positive integers C and N such that f(n) <= C * g(n) for all integers n >= N. Using this more general definition for big-Oh, we can now say that if we have f(n) = 1, then f(n) = O(log(n)) since C = 1 and N = 2 will work.
  • 13.
  • 14. Example 6: Show: f(n) = 3n 3 + 3n - 1 =  (n 3 ) As implied by the theorem above, to show this result, we must show two properties: f(n) = O (n 3 ) f(n) =  (n 3 ) First, we show (i), using the same techniques we've already seen for big-Oh. We consider N = 1, and thus we only consider n >= 1 to show the big-Oh result. f(n) = 3n 3 + 3n - 1 < 3n 3 + 3n + 1 <= 3n 3 + 3n 3 + 1n 3 = 7n 3 thus, with C = 7 and N = 1 we have shown that f(n) = O(n 3 )
  • 15. Next, we show (ii). Here we must provide a lower bound for f(n). Here, we choose a value for N, such that the highest order term in f(n) will always dominate (be greater than) the lower order terms. We choose N = 2 , since for n >=2 , we have n3 >= 8 . This will allow n 3 to be larger than the remainder of the polynomial (3n - 1) for all n >= 2. So, by subtracting an extra n 3 term, we will form a polynomial that will always be less than f(n) for n >= 2 . f(n) = 3n 3 + 3n - 1 > 3n 3 - n3 since n 3 > 3n - 1 for any n >= 2 = 2n 3 Thus, with C = 2 and N = 2 , we have shown that f(n) =  (n 3 ) since f(n) is shown to always be greater than 2n 3 .
  • 16. Big-Oh Operations Summation Rule Suppose T1(n) = O(f1(n)) and T2(n) = O(f2(n)). Further, suppose that f2 grows no faster than f1, i.e., f2(n) = O(f1(n)). Then, we can conclude that T1(n) + T2(n) = O(f1(n)). More generally, the summation rule tells us O(f1(n) + f2(n)) = O(max(f1(n), f2(n))). Proof : Suppose that C and C' are constants such that T1(n) <= C * f1(n) and T2(n) <= C' * f2(n). Let D = the larger of C and C'. Then, T1(n) + T2(n) <= C * f1(n) + C' * f2(n) <= D * f1(n) + D * f2(n) <= D * (f1(n) + f2(n)) <= O(f1(n) + f2(n))
  • 17.
  • 18. Example 7 Compute the big-Oh running time of the following C++ code segment: for (i = 2; i < n; i++) { sum += i; } The number of iterations of a for loop is equal to the top index of the loop minus the bottom index, plus one more instruction to account for the final conditional test. Note: if the for loop terminating condition is i <= n , rather than i < n , then the number of times the conditional test is performed is: ((top_index + 1) – bottom_index) + 1) In this case, we have n - 2 + 1 = n - 1 . The assignment in the loop is executed n - 2 times. So, we have (n - 1) + (n - 2) = (2n - 3) instructions executed = O(n).
  • 19. Example 8 Consider the sorting algorithm shown below. Find the number of instructions executed and the complexity of this algorithm. 1) for (i = 1; i < n; i++) { 2) SmallPos = i; 3) Smallest = Array[SmallPos]; 4) for (j = i+1; j <= n; j++) 5) if (Array[j] < Smallest) { 6) SmallPos = j; 7) Smallest = Array[SmallPos] } 8) Array[SmallPos] = Array[i]; 9) Array[i] = Smallest; } The total computing time is: T(n) = (n) + 4(n-1) + n(n+1)/2 – 1 + 3[n(n-1) / 2] = n + 4n - 4 + (n 2 + n)/2 – 1 + (3n 2 - 3n) / 2 = 5n - 5 + (4n 2 - 2n) / 2 = 5n - 5 + 2n 2 - n = 2n 2 + 4n - 5 = O(n 2 )
  • 20. Example 9 What is the complexity of this C++ code? 1) cin >> n; // Same as: n = GetInteger(); 2) for (i = 1; i <= n; i ++) 3) for (j = 1; j <= n; j ++) 4) A[i][j] = 0; 5) for (i = 1; i <= n; i ++) 6) A[i][i] = 1; The following program segment initializes a two-dimensional array A (which has n rows and n columns) to be an n x n identity matrix – that is, a matrix with 1’s on the diagonal and 0’s everywhere else. More formally, if A is an n x n identity matrix, then: A x M = M x A = M, for any n x n matrix M.
  • 21. Example 10 Here is a simple linear search algorithm that returns the index location of a value in an array. /* a is the array of size n we are searching through */ i = 0; while ((i < n) && (x != a[i])) i++; if (i < n) location = i; else location = -1; 1 + 3 + 5 + ... + (2n - 1) / n = (2 (1 + 2 + 3 + ... + n) - n) / n We know that 1 + 2 + 3 + ... + n = n (n + 1) / 2, so the average number of lines executed is: [2[n(n+1)/2] – n]/n =n =O(n) Average number of lines executed equals:
  • 22.
  • 23. Here is the body of a function: sum = 0; for (i = 1; i <= f(n); i++) sum += i; where f(n) is a function call. Give a big-oh upper bound on this function if the running time of f(n) is O(n), and the value of f(n) is n!:
  • 24.
  • 25.
  • 26.
  • 27. Polynomial Transformation Informally, if P 1  P 2 , then we can think of a solution to P 1 being obtained from a solution to P 2 in polynomial time. The code below gives some idea of what is implied when we say P 1  P 2 : Convert_To_P2 p1 = ... /* Takes an instance of p1 and converts it to an instance of P2 in polynomial time. */ Solve_P2 p2 = ... /* Solves problem P2 */ Solve_P1 p1 = Solve_P2(Convert_To_P2 p1);
  • 28. Given the above definition of a transformation, these theorems should not be very surprising: If   1    2 then   2  P    1  P If   1    2 then   2   P    1   P These theorems suggest a technique for proving that a given problem  is NP-complete. To Prove  NP: 1) Find a known NP-complete problem  NP 2) Find a transformation such that  NP  3) Prove that the transformation is polynomial.
  • 29. The meaning of NP-Completeness A Statement of a Problem: Solving a problem means finding one algorithm that will solve all instances of the problem.    