csc 2300 data structures & algorithms january 26, 2007 chapter 2. algorithm analysis

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

Post on 21-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

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

CSC 2300Data Structures & Algorithms

January 26, 2007

Chapter 2. Algorithm Analysis

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

Today

Maximum subsequence sum problem

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

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.

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

Running Times of Four Algorithms

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

Plot of Time versus N

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

Plot of Time versus N

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

Algorithm 1

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

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

Improve Algorithm 1?

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

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

Algorithm 2

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

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

Algorithm 3

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

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

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.

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

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.

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

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.

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

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).

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

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).

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

Algorithm 4

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

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

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

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

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

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

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.