Design and Analysis of Algorithms - Chapter 21 Analysis of Algorithms b Issues: CorrectnessCorrectness Time efficiencyTime efficiency Space efficiencySpace.

Slides:



Advertisements
Similar presentations
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Advertisements

Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 2 Some of the sides are exported from different sources.
Analysis of Algorithms
1 Recursive Algorithm Analysis Dr. Ying Lu RAIK 283: Data Structures & Algorithms September 13, 2012.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Chapter 2: Algorithm Analysis
Chapter 2: Fundamentals of the Analysis of Algorithm Efficiency
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Chapter 2: Fundamentals of the Analysis of Algorithm Efficiency
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Analysis of Algorithm.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Chapter 2 Analysis Framework 2.1 – 2.2. Homework Remember questions due Wed. Remember questions due Wed. Read sections 2.1 and 2.2 Read sections 2.1 and.
Design and Analysis of Algorithms Chapter Analysis of Algorithms Dr. Ying Lu August 28, 2012
COSC 3100 Fundamentals of Analysis of Algorithm Efficiency
Lecture 6 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Asymptotic Growth Rates Themes –Analyzing the cost of programs –Ignoring constants and Big-Oh –Recurrence Relations & Sums –Divide and Conquer Examples.
1 Chapter 2 Program Performance – Part 2. 2 Step Counts Instead of accounting for the time spent on chosen operations, the step-count method accounts.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Analysis and Design of Algorithms. According to math historians the true origin of the word algorism: comes from a famous Persian author named ál-Khâwrázmî.
Theory of Algorithms: Fundamentals of the Analysis of Algorithm Efficiency James Gain and Edwin Blake {jgain | Department of Computer.
Last Class: Graph Undirected and directed (digraph): G=, vertex and edge Adjacency matrix and adjacency linked list
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
{ CS203 Lecture 7 John Hurley Cal State LA. 2 Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Lecture 2 Computational Complexity
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
CS 3343: Analysis of Algorithms
Design and Analysis of Algorithms - Chapter 21 Analysis of Algorithms b Issues: CorrectnessCorrectness Time efficiencyTime efficiency Space efficiencySpace.
Analysis of Algorithm Efficiency Dr. Yingwu Zhu p5-11, p16-29, p43-53, p93-96.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
Lecture 5 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Recursion Algorithm : Design & Analysis [3]. In the last class… Asymptotic growth rate The Sets ,  and  Complexity Class An Example: Maximum Subsequence.
Algorithmic Analysis Charl du Plessis and Robert Ketteringham.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
2-0 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 2 Theoretical.
UNIT-I FUNDAMENTALS OF THE ANALYSIS OF ALGORITHM EFFICIENCY ANALYSIS AND DESIGN OF ALGORITHMS CHAPTER 2:
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
Lecture 4 Jianjun Hu Department of Computer Science and Engineerintg University of South Carolina CSCE350 Algorithms and Data Structure.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
LECTURE 2 : fundamentals of analysis of algorithm efficiency Introduction to design and analysis algorithm 1.
Mathematical Analysis of Recursive Algorithm CSG3F3 Lecture 7.
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
1-0 Design and Analysis of Algorithms – Unit I Chapter 1 Introduction.
CSG523/ Desain dan Analisis Algoritma
CSG523/ Desain dan Analisis Algoritma
Algorithm Analysis 1.
Theoretical analysis of time efficiency
Analysis of Algorithms
Analysis of algorithms
Analysis of algorithms
Design and Analysis of Algorithms Chapter -2
Introduction to the Design and Analysis of Algorithms
Analysis of algorithms
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency
Analysis of algorithms
CS 3343: Analysis of Algorithms
Chapter 2.
Fundamentals of the Analysis of Algorithm Efficiency
Analysis of algorithms
At the end of this session, learner will be able to:
Analysis of algorithms
Fundamentals of the Analysis of Algorithm Efficiency
Presentation transcript:

Design and Analysis of Algorithms - Chapter 21 Analysis of Algorithms b Issues: CorrectnessCorrectness Time efficiencyTime efficiency Space efficiencySpace efficiency OptimalityOptimality b Approaches: Theoretical analysisTheoretical analysis Empirical analysisEmpirical analysis

Design and Analysis of Algorithms - Chapter 22 Theoretical analysis of time efficiency Time efficiency is analyzed by determining the number of repetitions of the basic operation as a function of input size Basic operation: the operation that contributes most towards the running time of the algorithm. T(n) ≈ c op C(n) T(n) ≈ c op C(n) running time execution time for basic operation Number of times basic operation is executed input size

Design and Analysis of Algorithms - Chapter 23 Input size and basic operation examples Problem Input size measure Basic operation Search for key in list of n items Number of items in list n Key comparison Multiply two matrices of floating point numbers Dimensions of matrices Floating point multiplication Compute a n n Floating point multiplication Graph problem #vertices and/or edges Visiting a vertex or traversing an edge

Design and Analysis of Algorithms - Chapter 24 Empirical analysis of time efficiency b Select a specific (typical) sample of inputs b Use physical unit of time (e.g., milliseconds) OR OR b Count actual number of basic operations b Analyze the empirical data

Design and Analysis of Algorithms - Chapter 25 Best-case, average-case, worst-case For some algorithms efficiency depends on type of input: b Worst case: W(n) – maximum over inputs of size n b Best case: B(n) – minimum over inputs of size n b Average case: A(n) – “average” over inputs of size n Number of times the basic operation will be executed on typical inputNumber of times the basic operation will be executed on typical input NOT the average of worst and best caseNOT the average of worst and best case Expected number of basic operations repetitions considered as a random variable under some assumption about the probability distribution of all possible inputs of size nExpected number of basic operations repetitions considered as a random variable under some assumption about the probability distribution of all possible inputs of size n

Design and Analysis of Algorithms - Chapter 26 Example: Sequential search b Problem: Given a list of n elements and a search key K, find an element equal to K, if any. b Algorithm: Scan the list and compare its successive elements with K until either a matching element is found (successful search) or the list is exhausted (unsuccessful search) b Worst case b Best case b Average case

Design and Analysis of Algorithms - Chapter 27 Types of formulas for basic operation count b Exact formula e.g., C(n) = n(n-1)/2 e.g., C(n) = n(n-1)/2 b Formula indicating order of growth with specific multiplicative constant e.g., C(n) ≈ 0.5 n 2 e.g., C(n) ≈ 0.5 n 2 b Formula indicating order of growth with unknown multiplicative constant e.g., C(n) ≈ cn 2 e.g., C(n) ≈ cn 2

Design and Analysis of Algorithms - Chapter 28 Order of growth b Most important: Order of growth within a constant multiple as n→∞ b Example: How much faster will algorithm run on computer that is twice as fast?How much faster will algorithm run on computer that is twice as fast? How much longer does it take to solve problem of double input size?How much longer does it take to solve problem of double input size? b See table 2.1

Design and Analysis of Algorithms - Chapter 29 Table 2.1

Design and Analysis of Algorithms - Chapter 210 Asymptotic growth rate b A way of comparing functions that ignores constant factors and small input sizes b O(g(n)): class of functions f(n) that grow no faster than g(n) b Θ(g(n)): class of functions f(n) that grow at same rate as g(n) b Ω(g(n)): class of functions f(n) that grow at least as fast as g(n) see figures 2.1, 2.2, 2.3 see figures 2.1, 2.2, 2.3

Design and Analysis of Algorithms - Chapter 211 Big-oh

Design and Analysis of Algorithms - Chapter 212 Big-omega

Design and Analysis of Algorithms - Chapter 213 Big-theta

Design and Analysis of Algorithms - Chapter 214 Establishing rate of growth: Method 1 – using limits lim n→∞ T(n)/g(n) = T(n)g(n) 0 order of growth of T(n) ___ order of growth of g(n) T(n)g(n) c>0 order of growth of T(n) ___ order of growth of g(n) T(n)g(n) ∞ order of growth of T(n) ___ order of growth of g(n) Examples: 10n vs. 2n 2 n(n+1)/2 vs. n 2 log b n vs. log c n

Design and Analysis of Algorithms - Chapter 215 L’Hôpital’s rule If b lim n→∞ f(n) = lim n→∞ g(n) = ∞ b The derivatives f´, g´ exist, Then f(n)f(n)g(n)g(n)f(n)f(n)g(n)g(n)lim n→∞ = f ´(n) g ´(n) lim n→∞ Example: logn vs. n Example: logn vs. n

Design and Analysis of Algorithms - Chapter 216 Establishing rate of growth: Method 2 – using definition b f(n) is O(g(n)) if order of growth of f(n) ≤ order of growth of g(n) (within constant multiple) b There exist positive constant c and non-negative integer n 0 such that f(n) ≤ c g(n) for every n ≥ n 0 f(n) ≤ c g(n) for every n ≥ n 0Examples: b 10n is O(2n 2 ) b 5n+20 is O(10n)

Design and Analysis of Algorithms - Chapter 217 Basic Asymptotic Efficiency classes 1constant log n logarithmic nlinear n log n n2n2n2n2quadratic n3n3n3n3cubic 2n2n2n2nexponential n!factorial

Design and Analysis of Algorithms - Chapter 218 Time efficiency of nonrecursive algorithms Steps in mathematical analysis of nonrecursive algorithms: b Decide on parameter n indicating input size b Identify algorithm’s basic operation b Determine worst, average, and best case for input of size n b Set up summation for C(n) reflecting algorithm’s loop structure b Simplify summation using standard formulas (see Appendix A)

Design and Analysis of Algorithms - Chapter 219 A simple example Algorithm MaxElement(A[0..n-1]) // Determines the value of the largest element in a given array // Input: An array A[0..n-1] of real numbers // Output: The value of the largest element in A maxval  A[0] for i  1 to n-1 do if A[i]>maxval maxval  A[i] return maxval

Design and Analysis of Algorithms - Chapter 220 Another simple example Algorithm UniqueElement(A[0..n-1]) // Check whether all the elements in a array are distinct // Input: An array A[0..n-1] of real numbers // Output: Returns “true” if all the elements are distinct for i  0 to n-2 do for j  i+1 to n-1 do if A[i]=A[j] return false return true

Design and Analysis of Algorithms - Chapter 221 Other examples b Matrix multiplication b Selection sort b Insertion sort b Mystery Algorithm

Design and Analysis of Algorithms - Chapter 222 Matrix multipliacation

Design and Analysis of Algorithms - Chapter 223 A third simple example Algorithm Binary(n) // Input: A positive decimal integer n // Output: The number of binary digits in n’s binary representation count  1 while n>1 do count  count+1 n  └n/2┘ return count

Design and Analysis of Algorithms - Chapter 224 Selection sort

Design and Analysis of Algorithms - Chapter 225 Insertion sort

Design and Analysis of Algorithms - Chapter 226 Mystery algorithm for i := 1 to n-1 do max := i ; for j := i+1 to n do if |A[j,i]| > |A[max,i ]| then max := j ; for k := i to n+1 do swap A[i,k] with A[max,k]; for j := i+1 to n do for k := n+1 downto i do A[j,k] := A[j,k] - A[i,k]*A[j,i]/A[i,i] ; A[j,k] := A[j,k] - A[i,k]*A[j,i]/A[i,i] ;

Design and Analysis of Algorithms - Chapter 227 Time efficiency of recursive algorithms Steps in mathematical analysis of recursive algorithms: b Decide on parameter n indicating input size b Identify algorithm’s basic operation b Determine worst, average, and best case for input of size n b Set up a recurrence relation and initial condition(s) for C(n)-the number of times the basic operation will be executed for an input of size n (alternatively count recursive calls). b Solve the recurrence to obtain a closed form or estimate the order of magnitude of the solution (see Appendix B)

Design and Analysis of Algorithms - Chapter 228 Recursive evaluation of n ! b Definition: n ! = 1*2*…*(n-1)*n b Recursive definition of n!: n ! = (n-1)!*n b Algorithm: if n=0 then F(n) := 1 else F(n) := F(n-1) * n return F(n) b Recurrence for number of multiplications: M(n) = M(n-1) + 1 for n>0 M(0) = 0 M(0) = 0 b Method: backward substitution

Design and Analysis of Algorithms - Chapter 229 Towers of Hanoi b Problem: to count the number of moves b Recurrence for number of moves: M(n) = M(n-1) M(n-1) for n>1 M(1) = 1 M(1) = 1 b Alternatively: construct a tree for the number of recursive calls b Comment: inherent inefficiency

Design and Analysis of Algorithms - Chapter 230 Revisiting the “Binary” algorithm b Algorithm BinRec(n) // Input: A positive decimal integer n // Output: The number of binary digits in n’s binary representation if n=1 return 1 else return BinRec(└n/2┘)+1 b Recurrence for number of additions: A(n) = A(└n/2┘) + 1 for n>1 A(1) = 0 A(1) = 0 b Apply the smoothness rule: n=2 k

Design and Analysis of Algorithms - Chapter 231 Computing Fibonacci numbers (1) 1. Definition based recursive algorithm 2. Nonrecursive brute-force algorithm 3. Explicit formula algorithm 4. Algorithm based on matrix multiplications

Design and Analysis of Algorithms - Chapter 232 b Leonardo Fibonacci, b The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, … b Fibonacci recurrence: F(n) = F(n-1) + F(n-2) F(0) = 0 F(1) = 1 b Algorithm F(n): // Computes the n-th Fibonacci number recursively // Input: A nonnegative integer n // Output: The n-th Fibonacci number if n <= 1 return n else return F(n-1)+F(n-2) Computing Fibonacci numbers (2)

Design and Analysis of Algorithms - Chapter 233 b 2 nd order linear homogeneous recurrence relation with constant coefficients b Characteristic equation b Solution F(n) = φ n /√5 – φ’ n /√5 φ = (1+√5)/2 (golden ratio)φ’ = (1-√5)/2 F(n) ≈ φ n /√5 b Construct a tree for the number of recursive calls b Recursive algorithms solutions, not a panacea Computing Fibonacci numbers (3)

Design and Analysis of Algorithms - Chapter 234 b Algorithm F(n): // Computes the n-th Fibonacci number iteratively // Input: A nonnegative integer n // Output: The n-th Fibonacci number F[0]  0; F[1]  1; for i  2 to n do F[i]  F[i-1] + F[i-2] return F(n) b Complexity ? Computing Fibonacci numbers (4)

Design and Analysis of Algorithms - Chapter 235 b Alternatively algorithmic use of equation F(n) ≈ φ n /√5 with smart rounding b Relies on the exponentiation algorithm b Solutions: Θ(n), Θ(logn) Computing Fibonacci numbers (5)

Design and Analysis of Algorithms - Chapter 236 b It holds that for n≥1, b b Assuming an efficient way of computing matrix powers, solution in Θ(logn) Computing Fibonacci numbers (6) F(n-1) F(n) F(n) F(n+1) = n

Design and Analysis of Algorithms - Chapter 237 Important recurrence types: b One (constant) operation reduces problem size by one. T(n) = T(n-1) + c T(1) = d Solution: T(n) = (n-1)c + d linear b A pass through input reduces problem size by one. T(n) = T(n-1) + cn T(1) = d Solution: T(n) = [n(n+1)/2 – 1] c + d quadratic b One (constant) operation reduces problem size by half. T(n) = T(n/2) + c T(1) = d Solution: T(n) = c lg n + d logarithmic b A pass through input reduces problem size by half. T(n) = 2T(n/2) + cn T(1) = d Solution: T(n) = cn lg n + d n n log n

Design and Analysis of Algorithms - Chapter 238 A general divide-and-conquer recurrence T(n) = aT(n/b) + f (n) where f (n) ∈ Θ(n k ) 1. a < b k T(n) ∈ Θ(n k ) 2. a = b k T(n) ∈ Θ(n k lg n ) 3. a > b k T(n) ∈ Θ( n log b a ) Note: the same results hold with O instead of Θ.

Design and Analysis of Algorithms - Chapter 239 Solving linear homogeneous recurrence relations with constant coefficients b Easy first: 1 st order LHRRCCs: C(n) = a C(n -1) C(0) = t … Solution: C(n) = t a n b Extrapolate to 2 nd order L(n) = a L(n-1) + b L(n-2) … A solution?: L(n) = r n b Characteristic equation (quadratic) b Solve to obtain roots r 1 and r 2 — e.g.: A(n) = 3A(n-1) - 2(n-2) b General solution to RR: linear combination of r 1 n and r 2 n b Particular solution: use initial conditions — e.g.: A(0) = 1 A(1) = 3