Docsity

Running Time Calculations in Algorithm Analysis, Slides of Data Structures and Algorithms

Partial preview of the text

Download Running Time Calculations in Algorithm Analysis and more Slides Data Structures and Algorithms in PDF only on Docsity!

7/7/2012 Algorithm Analysis 2-

Chapter 2

Algorithm Analysis

EE 232 Data Structures Session-05 , Spring-

7/7/2012 Algorithm Analysis 2-

Chapter 2: Algorithm Analysis

2.1 Mathematical Background

2.2 Model

2.3 What to Analyze

2.4 Running Time Calculations

7/7/2012 Algorithm Analysis 2-

Lets Recall

 What is an Algorithm?  a clearly specified set of simple instructions to be followed to solve a problem

  • Takes a set of values, as input and
  • produces a value, or set of values, as output

 What are Data structures?  Methods of organizing data

 Program = algorithms + data structures

7/7/2012 Algorithm Analysis 2-

Lets Recall…

 Why should we analyse an algorithm?

 Writing a working program is not good enough!  The program may be inefficient  If the program is run on a large data set, then the running time becomes an issue

  • Recall the selection problem

7/7/2012 Algorithm Analysis 2-

Mathematical Background

 We need a theoretical framework upon which we can compare algorithms

 The idea is to establish a relative order among different algorithms, in terms of their relative rates of growth (of the running time)

 The rates of growth are expressed as functions, which are generally in terms of the number of inputs n e.g. a running time function can be expressed as T( n )

7/7/2012 Algorithm Analysis 2-

Mathematical Background…

 Such functions describe the utilization of some precious resource (e.g. time or space) as the size of the problem increases

 We can determine the exact running time of an algorithm, however for large inputs, the multiplicative constants and lower-order terms are dominated by the effects of the input size itself

7/7/2012 Algorithm Analysis 2-

Mathematical Background…

 *If we look at a slightly larger range from n = [0, 10], we begin to note that they are more similar:

*http://www.ece.uwaterloo.ca/~ece250/ docsity.com

7/7/2012 Algorithm Analysis 2-

Mathematical Background…

 *Extending the range to n = [0, 100], the similarity increases:

*http://www.ece.uwaterloo.ca/~ece250/ docsity.com

7/7/2012 Algorithm Analysis 2-

Mathematical Background…

 *They are different absolutely, for example, f(1000) = 1 000 000 g(1000) = 997 002  however, the relative difference is very small

 and this difference goes to zero as n → ∞  The justification for the pair of polynomials being similar is that, both have the same leading term:  n^2

f( 1000 )

f( 1000 ) g( 1000 )

*http://www.ece.uwaterloo.ca/~ece250/ docsity.com

7/7/2012 Algorithm Analysis 2-

Mathematical Background…

We can throw away the lower-order terms and ignore the leading coefficients of the highest term as the problem size grows really, really large

7/7/2012 Algorithm Analysis 2-

Mathematical Background…

Asymptotic Notation  The “big-Oh” O - Notation  f( n ) 1 = O (g( n )) , if there exists positive constants c and n 0 , such that f( n )  cg( n ) for all n ≥ n 0

 f( n ) and g( n ) are functions over non-negative integers  The growth rate of f( n ) is less than or equal to that of g( n )

f ( n)

c g n  ( )

n (^0) Input Size

Running Time

1: means set membership i.e. f( n ) Є O (g( n ))

7/7/2012 Algorithm Analysis 2-

Mathematical Background…

 g( n ) is an upper bound on f( n )  O -notation provides an asymptotic upper bound on a function  Used for worst-case analysis

Examples  If f( n ) = 2 n^2 , then f( n ) = O ( n^4 ) , f( n ) = O ( n^3 ) and f( n ) = O ( n^2 ) are all technically correct but the last answer is the best

f( n)

c g n  ( )

n 0 Input Size

Running Time

7/7/2012 Algorithm Analysis 2-

Mathematical Background…

 W–notation provides an asymptotic lower bound on a function  Used to describe best- case running times

Examples  n^3 grows faster than n^2 , so we can say that n^3 = W( n^2 )

Input Size

Running Time

f ( n) c g n  ( )

n 0

7/7/2012 Algorithm Analysis 2-

Mathematical Background…

 The “big-Theta” Q-Notation

 f( n ) = Q(g( n )) if there exists positive constants c 1 , c 2 , and n 0 , such that c 1 g( n )  f( n )  c 2 g( n ) for all n  n 0

 for all values of n to the right of n 0 , the values of f( n ) lies at or above c 1 g( n ) and at or below c 2 g( n )

Input Size

Running Time

f ( n)

n 0

c 2 g( n)

c 1 g( n)