You are on page 1of 30

Data Structures and Algorithms

Algorithm Analysis

College of Computer Studies, De La Salle University Manila 2401 Taft Avenue, Metro Manila 1004 Philippines

Outline

   

What is an Algorithm? Algorithm Formulation Algorithm Analysis Frequency Count Operation Count

College of Computer Studies, De La Salle University Manila 2401 Taft Avenue, Metro Manila 1004 Philippines

What is an Algorithm?

An organized sequence or list of clear steps or operations needed to solve a given programming problem 3 components:
  

Inputs Steps or instructions Output

College of Computer Studies, De La Salle University Manila 2401 Taft Avenue, Metro Manila 1004 Philippines

Metro Manila 1004 Philippines . De La Salle University Manila 2401 Taft Avenue.What is an Algorithm?   Any well-defined computational procedure that takes some value(s) as input and produces some value(s) as output Sequence of computational steps that transform the input to output College of Computer Studies.

Metro Manila 1004 Philippines . De La Salle University Manila 2401 Taft Avenue.What is an Algorithm?   A means to solve a problem A problem can be solved in many different ways:  Many algorithms can be used to solve the same problem  We choose between different algorithms based on their efficiency  A good metric of efficiency is the computational complexity of an algorithm College of Computer Studies.

Algorithms and Computer Science • • • Can a particular task be accomplished by a computing device? What is the minimum number of steps or operations for any algorithm to perform a certain function? Concerns: effectiveness. efficiency College of Computer Studies. Metro Manila 1004 Philippines . De La Salle University Manila 2401 Taft Avenue.

Metro Manila 1004 Philippines . the desired output.Algorithm Formulation  1. and the steps to reach the output 2.  Steps: Understand the problem Determine the given inputs. De La Salle University Manila 2401 Taft Avenue.  Design the algorithm Write the algorithm that will solve the problem given the inputs College of Computer Studies.

De La Salle University Manila 2401 Taft Avenue.  Analyze the algorithm Determine the resources that the algorithm requires – memory. Refine the algorithm Verification College of Computer Studies. bandwidth. hardware. Metro Manila 1004 Philippines .Algorithm Formulation 3. 5. computational time 4.

De La Salle University Manila 2401 Taft Avenue. Metro Manila 1004 Philippines .Algorithm Analysis  Study the behavior of the algorithm to determine its pattern and performance   Measured in terms of execution time (speed) and amount of memory (space) consumed Designing efficient algorithms College of Computer Studies.

or the number of times a statement is executed Space consumed may also be estimated   College of Computer Studies.  2 phases: Priori estimates Obtain a function which bounds the algorithm’s complexity time The amount of time a single execution will take. Metro Manila 1004 Philippines .How to analyze programs?  1. De La Salle University Manila 2401 Taft Avenue.

Metro Manila 1004 Philippines .How to analyze programs  It is impossible to know the exact amount of time to execute any command unless the following are known:     The machine for execution machine instruction set time required by each machine instruction translation of the compiler from source to machine language College of Computer Studies. De La Salle University Manila 2401 Taft Avenue.

 Posteriori estimates Study of exact time & space required for execution College of Computer Studies. Metro Manila 1004 Philippines .How to analyze programs? 2. De La Salle University Manila 2401 Taft Avenue.

Metro Manila 1004 Philippines .Frequency Count   Number of statements or steps needed by the algorithm to finish Simple statements:   a  10 ba*2 Count: 1 Count: 1 College of Computer Studies. De La Salle University Manila 2401 Taft Avenue.

Metro Manila 1004 Philippines . De La Salle University Manila 2401 Taft Avenue. Sum of the following:  1 + Max{ Count(<S1>). else <S2>. Count(<S2>) } College of Computer Studies.Frequency Count  Conditional Statements if (<condition>) <S1>.

Frequency Count . 2 1 2 1 } Total = 3 College of Computer Studies. De La Salle University Manila 2401 Taft Avenue. z  30. Metro Manila 1004 Philippines .Example if x > 1 y  10. else { y  20.

1 + 2 = n+1 n Total = 2n+1 . Metro Manila 1004 Philippines n .lb + 1 Example for i  1 to n xx+1 College of Computer Studies. De La Salle University Manila 2401 Taft Avenue.Frequency Count  Loop Statements for i  <lb> to <ub> <S1> ub .lb + 2 ub .

Metro Manila 1004 Philippines . z  30 } else { for i  1 to x x+1 print(i) x } 1 1 2x+1 Total = 2x+3 College of Computer Studies.Frequency Count .Example if x < 1 1 y  10 else if x < 2 2 {y  20. De La Salle University Manila 2401 Taft Avenue.

De La Salle University Manila 2401 Taft Avenue. print(n-k). } College of Computer Studies.Frequency Count . for i  1 to k-1 zz+1 2. for k  0 to n { print (k). k  500. Metro Manila 1004 Philippines .Exercise 1.

1 + 2 = n+1 for i  1 to n for j  1 to n (n+1)(n) (n) = <S1> xx+1 (n) (n) Total = n+1 + n2+n + n2 = 2n2 + 2n + 1 College of Computer Studies. Metro Manila 1004 Philippines . De La Salle University Manila 2401 Taft Avenue.Frequency Count  Nested Statements n .

Example for i  2 to n-1 n-1 .2n .2 + 2 = n-1 for j  1 to n (n+1) (n-2) <S1> xx+1 = (n-2) (n) (n-2) Total = n-1 + n2-2n+n-2 + n2-2n = 2n2 .Frequency Count . De La Salle University Manila 2401 Taft Avenue.3 College of Computer Studies. Metro Manila 1004 Philippines .

Frequency Count . De La Salle University Manila 2401 Taft Avenue. Metro Manila 1004 Philippines = 3n2 + 1 .Example <S1> = (n) for i  1 to n n+1 { xx+1 n for j  3 to n+1 n(n) { y  y + 1 (n-1) (n) <S2> z  z + 1 (n-1) (n) = (n-1) } } Total = n+1 + n + n2 + (n-1)n + (n-1)n = 2n+1 + n2 + n2-n + n2-n College of Computer Studies.

Frequency Count . Metro Manila 1004 Philippines .Exercise for i  1 to n for j  1 to n for k  1 to n zz+1 College of Computer Studies. De La Salle University Manila 2401 Taft Avenue.

Metro Manila 1004 Philippines n-x=n-1 n-x=n-1 Total = 2n-1 . De La Salle University Manila 2401 Taft Avenue.Frequency Count  Loop Statements do <S1> while <condition> Ex. x1 do xx+1 while x < n 1 What if while x <= n? What if x = 0? College of Computer Studies.

De La Salle University Manila 2401 Taft Avenue.Frequency Count  Loop Statements Ex. while <condition> <S1> x1 while x < n xx+1 1 n-x+1=n n-1 Total = 2n What if while x <= n? What if x = 0? College of Computer Studies. Metro Manila 1004 Philippines .

College of Computer Studies.5 2.Frequency Count . De La Salle University Manila 2401 Taft Avenue.Exercise 1. x1 while x <= n xx+1 x1 do yy+1 xx+1 while x <> n-1 1 n-x+2=n+1 n Total = 2n + 2 1 n-1-x=n-2 n-1-x=n-2 n-1-x=n-2 Total = 3n . Metro Manila 1004 Philippines .

Frequency Count . De La Salle University Manila 2401 Taft Avenue. for i  1 to n for j  1 to 2n xx+1 for k  2 to n+1 for j  3 to n-3 xx+1 2.Exercise 1. Metro Manila 1004 Philippines . College of Computer Studies.

College of Computer Studies. for i  2 to n+1 for j  3 to n-3 for k  4 to n-4 xx+1 for i  1 to n { j2 while j<=n+3 { print(A[i]. Metro Manila 1004 Philippines .Exercise 3.Frequency Count . De La Salle University Manila 2401 Taft Avenue. } } 4. j++.A[j-1]).

De La Salle University Manila 2401 Taft Avenue. for i  1 to n for j  2i downto 1 xx+1 for i  1 to n for j  i to n xx+1 7. Metro Manila 1004 Philippines .Exercise 5. College of Computer Studies. for i  1 to n for j  1 to i xx+1 6.Frequency Count .

for i  1 to n for j  1 to i for k  1 to j xx+1 College of Computer Studies. for i  1 to n-1 for j  1 to i xx+1 for i  4 to n for j  1 to i xx+1 9. Metro Manila 1004 Philippines .Frequency Count . 10. De La Salle University Manila 2401 Taft Avenue.Exercise 8.

i = i+1. 14. } 13. while (i>=n) { k= k+1. College of Computer Studies. while (i < n) { k = k+1. De La Salle University Manila 2401 Taft Avenue. } while (h >= n). } while (b != n-10) b= b+1. i = i-1. x = x+1.Exercise 11.Frequency Count . i = i-1. while (i > n) { k = k+1. do { h = h-1. Metro Manila 1004 Philippines . } 12. 15.

985065..

0&3.  %.3  3 3  3  3  3  3 3  3 3 3 3 3 40041425:907$9:/08 0.3.0789.03:0 0974. .473 $  3 147 943 3      3 147 943  3 3     3  3 $    3  3  3  < < %49.$.3.19. !55308 3  .

985065..

985065..

 !55308 .0789.0&3.19.$.  %.3.03:0 0974.3. 90: 147 147 943 147 147 943 147 943 147     40041425:907$9:/08 0.

43/943  3 3  3 3  %49.0&3. 667&.985065.0789.: /4 $ 0..3.45.19...$. !55308   /4    03  . .35 .03:0 0974.3  40041425:907$9:/08 0.3.  %..

985065.3....45.3 .19. 40041425:907$9:/08 0.$.: 0.3.0789.0&3.35 .. !55308 .  %. 667&.43/943 $    03     3  3 3  %49.03:0 0974.

.985065.

. 985065.

90:    03      /4       03   5.

35   5.. 5  5 '6.

.

5.

 5.

.

5.

 5.

.

5.

. '6.35.

0789.   40041425:907$9:/08 0.19.3.0&3.3.$.03:0 0974. !55308 .  %.

985065..

. 985065.

0&3.  %.03:0 0974.0789.3.$. 90:  147 943 147 943    147 943  147 943      40041425:907$9:/08 0.3. !55308 .19.

985065..

985065..

0789. 90:  147 943  147 943  147 943     147 943    03   5739 (  (    < < 40041425:907$9:/08 0.   %.19.$.3.3. !55308 .03:0 0974.0&3.

.985065.

. 985065.

 !55308 .    %.3.3. 90:  147 943 147 94    147 943 147  /4394     147 943 147  943    40041425:907$9:/08 0.03:0 0974.$.19.0&3.0789.

985065..

. 985065.

$.3.19. 90:  147 943  147 94    147 943 147 94     40041425:907$9:/08 0. !55308   147 943 147 94 147 94    .0&3.  %.3.0789.03:0 0974.

985065..

985065..

90:  3 5  < 22    >  3 5 <22    .

 .

>   3 5  <22  .

 .

>   3 /5.

 /5.

//   16< .

 .

0789.0&3.3.03:0 0974. >3 5  40041425:907$9:/08 0.$.19.3. !55308 .  %.