Algorithm Analysis PowerPoint PPT Presentation

presentation player overlay
1 / 10
About This Presentation
Transcript and Presenter's Notes

Title: Algorithm Analysis


1
Algorithm Analysis
  • Algorithm
  • An algorithm is a clearly specified set of simple
    instructions to be followed to solve a problem .
  • Running time analysis
  • Once an algorithm is decided to be correct for a
    problem , an important step is to determine how
    much recourses it requires
  • time
  • space

Our concern is from now on is not only to things
right , but to them efficiently !
2
Algorithm Analysis
  • In our analysis our main concern will be the
    running time , as a funcition of the input size .
  • We will find the big Oh for the algorithm we will
    analyze (O(f(n))
  • There are two aspects usually considered
  • .Worst case
  • .Average case
  • Generally the required quantity required is the
    worst case time , unless otherwise specified .
    One reason for this is to provide a bound for all
    input , including very bad ones .

3
Algorithm Analysis
  • Maximum subsequence sum problem
  • Given (possibly negative ) integers A1, A2,, An
    find the maximum value of Ak ( k form I to j )
    ( for convenience , the maximum subsequence sum
    is 0 if all integers are negative )
  • Example
  • For input 2 , 11, -4 , 13, -5, -2 the answer
    is 20 ( A2 through A4).

What is the most efficient solution?
4
Algorithm Analysis-Maximum subsequence sum
problem first solution
  • Public static in maxSubSum1(inta)
  • int maxSum 0
  • for (int I 0 I lta.length I)
  • for ( int j I j lt a.length j )
  • for ( int k I k lt j k )
  • thisSum a k
  • if (thisSum gtmaxSum)
  • maxSum thisSum
  • return maxSum

What is the running time ?
5
Algorithm Analysis-Maximum subsequence sum
problem first solution
  • The first loop is preformed n times ( the size of
    the input)
  • The second loop is preformed different amount of
    times each time , but since we are looking for an
    upper bound we take the worst case and consider
    it to be O(n)
  • The third loop is O(n) for the same reason .
  • The running time is thus O(1NNN) O(n3)
  • A more accurate analysis will bring us to the
    following computation ((n3)(3N2)(2N))/6
  • This leaves us with our original analysis.

6
Algorithm Analysis-Maximum subsequence sum
problem second solution
  • Public static int maxSubSum2(inta)
  • int maxSum 0
  • for ( int I 0 Ilt a. length I )
  • int thisSum 0
  • for ( int j I j lt a.length j )
  • thisSum aj
  • if ( thisSum gt maxSum)
  • maxSum thisSum
  • return maxsum

Running time is O(n2)
7
Algorithm Analysis-Maximum subsequence sum
problem third solution
  • Public static int maxSum4(inta)
  • int maxSum 0 , thisSum 0
  • for (int j 0 j lta.length j)
  • thisSum a j
  • if ( thisSum gt maxsum )
  • maxSum thisSum
  • else if ( thisSum lt 0)
  • thisSum 0
  • return maxSum

8
Algorithm Analysis-Maximum subsequence sum
problem third solution
  • As in many clever algorithms the running time is
    obvious , but the correctness is not .
  • The running time is linear O (n)
  • convince yourself about its correctness .

We have seen that there are many ways to solve a
problem , and only a few ways to solve it
efficiently !!
9
Algorithm Analysis
Evaluate the number of operations in the
following code
10
Algorithm Analysis
  • Solution
  • for-loop executed n times O(n)
  • inner for-loop executed i timesO(i)
  • while-loop executed 1 time O(1)
Write a Comment
User Comments (0)
About PowerShow.com