Data Structure chap1, Lecture notes of Data Structures and Algorithms

it includes detail in chapter one

Typology: Lecture notes

2019/2020

Uploaded on 04/11/2022

Alazard
Alazard 🇪🇹

5

(1)

2 documents

1 / 26

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
©Alemitu Mequanint · Email: [email protected] · Software Engineering , AASTU
Alemitu Mequanint
Addis Ababa Science & Technology University
2009 E.C., Semester I
Data Structure and Algorithm Analysis
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Data Structure chap1 and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

©Alemitu Mequanint · Email: [email protected] · Software Engineering , AASTU Alemitu Mequanint Addis Ababa Science & Technology University 2009 E.C., Semester I Data Structure and Algorithm Analysis

 (^) Outline:

  • (^) Growth Rate
  • (^) Best, Average & Worst Cases
  • (^) Upper Bound, Lower Bound, Theta Notation Page 2

More on Asymptotic Analysis

©Alemitu Mequanint · Email: [email protected] · Software Engineering , AASTU

Growth Rate

T(n) Type c constant log n logarithmic n linear n log n "n log n" n^2 quadratic n^3 cubic 2 n exponential ©Alemitu Mequanint · Email: [email protected] · Software Engineering , AASTU The growth rate for an algorithm is the rate at which the cost of the algorithm grows as the size of its input grows. Examples :

Growth Rate Graph ©Alemitu Mequanint · Email: [email protected] · Software Engineering , AASTU

Asymptotic Analysis: Upper Bounds

 (^) Several terms are used to describe the running-time equation for an algorithm.  (^) Upper bound - indicates the upper or highest growth rate that an algorithm can have.  (^) measured on the best-case, average-case, or worst- case inputs  (^) Big-Oh notation: If the upper bound for an algorithm’s growth rate (say, the worst case) is f(n), then this algorithm is “in the set O(f(n))in the worst case” (or just “in O(f(n))in the worst case”).

Asymptotic Analysis: Big-oh Notation Definition : For T ( n ) a non-negatively valued function, T ( n ) is in the set O( f ( n )) if there exist two positive constants c and n 0 such that T ( n ) <= cf ( n ) for all n > n 0.  (^) n 0 is the smallest value of n for which the claim of an upper bound holds true. Usage : The algorithm is in O( n^2 ) in [best, average, worst] case. [Must pick one of these to complete the statement. Big-oh notation applies to some set of inputs.] Meaning : For all data sets big enough (i.e., n > n 0 ), the algorithm always executes in less than c f ( n ) steps in [best, average, worst] case.

Big-Oh Examples

Example 1: Finding value X in an array (average cost, assuming equal probability of appearing in any position). T ( n ) = c s n /2. For all values of n > 1, c s n /2 <= c s n. Therefore, by the definition, T ( n ) is in O( n ) for n 0 = 1 and c = c s

Big-Oh Examples

Example 2: T ( n ) = c 1 n 2

  • c 2 n in average case. c 1 n^2 + c 2 n <= c 1 n^2 + c 2 n^2 <= ( c 1 + c 2 ) n^2 for all n >

T ( n ) <= cn^2 for c = c 1 + c 2 and n 0 = 1. Therefore, T ( n ) is in O( n^2 ) by the definition. Example 3: T ( n ) = c. We say this is in O(1).

Lower Bound  Describes the least amount of a resource that an algorithm needs for some class of input.  Like big-Oh notation, this is a measure of the algorithm’s growth rate.  Measured for some particular class of inputs: the worst-, average-, or best-case input of size n.

Big-Omega

Definition: For T ( n ) a non-negatively valued function, T ( n ) is in the set ( g ( n )) if there exist two positive constants c and n 0 such that T ( n ) >= cg ( n ) for all n > n 0. Meaning: For all data sets big enough (i.e., n > n 0 ), the algorithm always executes in more than cg ( n ) steps. wish to get the “tightest” (for notation, the largest) bound possible.

Theta Notation

When big-Oh and meet, we indicate this by using (big-Theta) notation. Definition: An algorithm is said to be ( h ( n )) if it is in O( h ( n )) and it is in ( h ( n )). For polynomial equations on T(n), we always have . There is no uncertainty, since once we have the equation, we have a “complete” analysis.

A Common Misunderstanding Confusing worst case with upper bound: Upper bound refers to a growth rate. Worst case refers to the worst input from among the choices for possible inputs of a given size.

Simplifying Rules (cont.)

Taking the first three rules, ignore all constants and all lower-order terms to determine the asymptotic growth rate for any cost function. The higher-order terms soon swamp the lower-order terms in their contribution to the total cost as n becomes larger. Example: if T(n) = 3n^4 +5n^2 , then T(n) is in O(n^4 ). n^2 contributes relatively little to the total cost.

Running Time Examples (1) [Asymptotic analysis is defined for equations. We need to convert programs to equations to analyze them.] Example 1: a = b; [This assignment takes constant time, so it is (1).] Example 2: sum = 0; for (i=1; i<=n; i++) sum += n; [1st^ line is (1). The for loop is repeated n times. 3rd^ line takes constant time so, by rule (4), total cost for the two lines is (n). By rule (3), the cost of the entire code fragment is also (n).]