0% found this document useful (0 votes)
93 views

3 Compare The Algorithms:, A,, A, Find The Maximum Value of

The document compares four algorithms for finding the maximum subsequence sum in an array of integers. Algorithm 1 has a running time of O(N3), Algorithm 2 is O(N2), Algorithm 3 is O(N log N) using divide and conquer, and Algorithm 4 is an online algorithm with O(N) running time. The algorithms are tested on input sizes from 10 to 100,000 elements, with Algorithm 1 becoming impractical fastest as N increases. Log N running times are useful when data is pre-sorted, like binary search of a dictionary. Checking algorithm analysis includes comparing T(2N)/T(N) to characteristic constants and examining asymptotic behavior.

Uploaded by

Leming Shen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views

3 Compare The Algorithms:, A,, A, Find The Maximum Value of

The document compares four algorithms for finding the maximum subsequence sum in an array of integers. Algorithm 1 has a running time of O(N3), Algorithm 2 is O(N2), Algorithm 3 is O(N log N) using divide and conquer, and Algorithm 4 is an online algorithm with O(N) running time. The algorithms are tested on input sizes from 10 to 100,000 elements, with Algorithm 1 becoming impractical fastest as N increases. Log N running times are useful when data is pre-sorted, like binary search of a dictionary. Checking algorithm analysis includes comparing T(2N)/T(N) to characteristic constants and examining asymptotic behavior.

Uploaded by

Leming Shen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 9

§3 Compare the Algorithms

〖 Example 〗 Given (possibly negative) integers A1, A2,



j
…, AN, find the maximum value ofk i Ak .
Algorithm 1
Max sum is 0 if all
int MaxSubsequenceSum ( const int A[ ], int N )
the integers are
{
int ThisSum, MaxSum, i, j, k; negative.
/* 1*/ MaxSum = 0; /* initialize the maximum sum */
/* 2*/ for( i = 0; i < N; i++ ) /* start from A[ i ] */
/* 3*/ for( j = i; j < N; j++ ) { /* end at A[ j ] */
Detailed analysis
/* 4*/ ThisSum = 0;
is given on p.18-
/* 5*/ for( k = i; k <= j; k++ )
19.
/* 6*/ ThisSum += A[ k ]; /* sum from A[ i ] to A[ j ] */
/* 7*/ if ( ThisSum > MaxSum )
/* 8*/ MaxSum = ThisSum; /* update max sum */
} /* end for-j and for-i */
/* 9*/ return MaxSum;
} T( N ) = O( N3 )

1/8
§3 Compare the Algorithms

Algorithm 2

int MaxSubsequenceSum ( const int A[ ], int N )


{
int ThisSum, MaxSum, i, j;
/* 1*/ MaxSum = 0; /* initialize the maximum sum */
/* 2*/ for( i = 0; i < N; i++ ) { /* start from A[ i ] */
/* 3*/ ThisSum = 0;
/* 4*/ for( j = i; j < N; j++ ) { /* end at A[ j ] */
/* 5*/ ThisSum += A[ j ]; /* sum from A[ i ] to A[ j ] */
/* 6*/ if ( ThisSum > MaxSum )
/* 7*/ MaxSum = ThisSum; /* update max sum */
} /* end for-j */
} /* end for-i */
/* 8*/ return MaxSum;
}

T( N ) = O( N2 )

2/8
§3 Compare the Algorithms

Algorithm 3 Divide and Conquer


conquer divide

4 3 5 2 1 2 6 2
4 5 2 6
6 8
11

T T
O( N )
( N/2 ) ( N/2 )

T ( N ) = 2 T( N/2 ) + c N , T(1) = O(1)


= 2 [2 T( N/22 ) + c N/2] + c N Also
The true for
program can
= 2k O(1) + c k N where N/2k = 1 N  on
be found 2k p.21.
= O( N log N )
3/8
§3 Compare the Algorithms

Algorithm 4 On-line Algorithm


int MaxSubsequenceSum( const int A[ ], int N )
{
int ThisSum, MaxSum, j;
/* 1*/ ThisSum = MaxSum = 0;
/* 2*/ for ( j = 0; j < N; j++ ) { 1 33 2
2 44 6 1 6 1
/* 3*/ ThisSum += A[ j ];
/* 4*/ if ( ThisSum > MaxSum )
/* 5*/ MaxSum = ThisSum;
/* 6*/ else if ( ThisSum < 0 )
/* 7*/ ThisSum = 0;
} /* end for-j */
/* 8*/ return MaxSum;
}
At any point in time, the
algorithm can correctly give an
answer to the subsequence
T( N ) = O( N ) problem for the data it has
A[ ] is scanned once only. already read.

4/8
§3 Compare the Algorithms

Running times of several algorithms for maximum


subsequence sum (in seconds)

Algorithm 1 2 3 4
Time O( N3 ) O( N2 ) O(N log N) O( N )
N =10 0.00103 0.00045 0.00066 0.00034
N =100 0.47015 0.01112 0.00486 0.00063
Input
N =1,000 448.77 1.1233 0.05843 0.00333
Size N =10,000 NA 111.13 0.68631 0.03042
N =100,000 NA NA 8.0113 0.29832

Note:
Note:The
Thetime
timerequired
requiredto
toread
readthe
theinput
inputisisnot
notincluded.
included.

5/8
§4 Logarithms in the Running Time
〖 Example 〗 Binary Search:
Given: A [0]  A [1]  ……  A [N  1] ; X
Task: Find X
Output: i if X = = A [ i ]
1 if X is not found

low mid high

X ~ A [mid]
< == >
low high = mid  1 low= mid + 1 high

mid

6/8
§4 Logarithms in the Running Time

int BinarySearch ( const ElementType A[ ],


ElementType X, int N )
{
int Low, Mid, High; T(N) = ?
/* 1*/ Low = 0; High = N - 1; Very useful in
/* 2*/ while ( Low <= High ) { Very useful in
case
case the
the data
data are
are
/* 3*/ Mid = ( Low + High ) / 2; static and isisin
/* 4*/ if ( A[ Mid ] < X ) static and in
sorted
sorted order
order (e.g.
(e.g.
/* 5*/ LowHome
= Mid + 1;work: find words from aa
else find words from
/* 6*/ Self-study Euclid’s
if ( A[ Mid ] > X ) Algorithm
dictionary).
dictionary).
/* 7*/ andHigh Exponentiation
= Mid - 1;
else
/* 8*/ return Mid; /* Found */
} /* end while */
/* 9*/ return NotFound; /* NotFound is defined as -1 */
}
Tworst( N ) = O( log N )

7/8
§5 Checking Your Analysis
When T(N) = O(N), check if T(2N)/T(N)  2
Method 1
When T(N) = O(N2), check if T(2N)/T(N)  4
When T(N) = O(N3), check if T(2N)/T(N)  8
……

Method 2 When T(N) = O( f (N) ), check if


T(N )
lim  Constant
N  f ( N )

Read the example given on p.28 (Figures 2.12 & 2.13).

8/8
Laboratory Project 1
Performance Measurement

Due: Monday, September 23th, 2019 at 10:00pm

Real Programmers
don't comment their code.
If it was hard to write,
I will not read and grade
it should be hard any
to understand
program which has
and harder to modify.
less than 30% lines
commented.

You might also like