CSC 2300 Data Structures & Algorithms January 26, 2007 Chapter 2. Algorithm Analysis

Preview:

Citation preview

CSC 2300Data Structures & Algorithms

January 26, 2007

Chapter 2. Algorithm Analysis

Today

Maximum subsequence sum problem

Maximum Subsequence Sum

Given (possibly negative) integers A1, A2,…, AN, find the maximum value of ∑ Ak (where k goes from i to j inside the summation).

For input -2, 11, -4, 13, -5, -2, what is the answer? 20 (A2 through A4). Base case: What if all the given integers are negative? We take the maximum subsequence sum to be 0. Let us discuss four algorithms to solve this problem.

Running Times of Four Algorithms

Plot of Time versus N

Plot of Time versus N

Algorithm 1

What is the running time? O(n3). Why O(n3)?

Improve Algorithm 1?

Which statement requires O(n3) work? Line 14. Can we reduce the work?

Algorithm 2

What is the running time? O(n2). Why O(n2)?

Algorithm 3

What is the running time? O(N logN). Why?

Recursive Procedure

Algorithm 3 is a recursive O(N log N) technique. It uses a divide-and-conquer strategy. Divide Part: The idea is to split the problem into

two roughly equal subproblems, which are then solved recursively.

Conquer Part: Patch together the two solutions of the subproblems, and possibly do a small amount of additional work to arrive at a solution for the whole problem.

Three Possibilities

The maximum subsequence sum can be in one of three places: entirely in the left half of the input, entirely in the right half, or in both halves by crossing the middle.

The first two cases can be solved recursively. The third case can be obtained by finding the largest

sum in the first half that includes the last element in the first half, and the largest sum in the second half that includes the first element in the second half. The two sums are then added together.

Example

First Half Second Half

----------------------------------

4 -3 5 -2 -1 2 6 -2

The maximum subsequence sum for the first half is 6 (elements A1 through A3).

The maximum subsequence sum for the second half is 8 (elements A6 through A7).

The maximum subsequence sum in the first half that includes the last element in the first half is 4 (elements A1 through A4), and the maximum subsequence sum in the second half that includes the first element in the second half is 7 (elements A5 through A7). Thus, the maximum sum that spans both halves and goes through the middle is 11 (elements A1 through A7).

Thus, the third choice provides the solution.

Running Time Analysis

Let T(N) be the time it takes to solve a maximum subsequence sum problem of size N.

If N=1, then the program takes some constant amount of time to execute lines 8 to 12, which we shall call one unit. Thus, T(1) = 1.

Otherwise, the program must perform two recursive calls, the two FOR loops between lines 19 and 32, and some small amount of bookkeeping, such as lines 14 and 34. The time expended in lines 19 to 32 is O(N). The code in lines 8 to 14, 18, 26, and 34 is all a constant amount of work and can be ignored compared with O(N). The remainder of the work is performed in lines 15 and 16. These lines solve two subsequence problems of size N/2 (assuming N is even).

We get the equations:T(1) = 1,T(N) = 2 T(N/2) + O(N).

Finding T(N)

We have T(N) = 2 T(N/2) + O(N), T(1) = 1. To simplify the calculations, we can replace the O(N) term in the equation

by N: T(N) = 2 T(N/2) + N, T(1) = 1. Since T(N) will be expressed in Big-Oh notation anyway, this will not affect

the answer. We shall learn how to solve the equation rigorously. For now, we have T(1) = 1 = 1*1,

T(2) = 2*1 + 2 = 4 = 2*2,

T(4) = 2*4 + 4 = 12 = 4*3,

T(8) = 2*12 + 8 = 32 = 8*4,

T(16) = 2*32 + 16 = 80 = 16*5. The pattern that can be derived is that if N = 2k, then

T(N) = N*(k+1) = N logN + N = O(N logN).

Algorithm 4

What is the running time? O(n). Why O(n)?

Example 1

Input: 4 -3 5 -2 -1 2 6 -2

j thisSum maxSum

0 0

0 4 4

1 1 4

2 6 6

3 4 6

4 3 6

5 5 6

6 11 11

7 9 11

Example 2

Input: 4 -7 5 -2 -8 2 6 -3

j thisSum maxSum

0 0

0 4 4

1 0 4

2 5 5

3 3 5

4 0 5

5 2 5

6 8 8

7 5 8

Explanation

Like Algorithms 1 and 2, the subsequence goes from i to j.

To find the sum, we do not need i. If a[i] is negative, it cannot be the start of the

optimal subsequence. Any negative subsequence cannot be the prefix

of the optimal subsequence.