Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CSC 2300 Data Structures & Algorithms January 26, 2007 Chapter 2. Algorithm Analysis."— Presentation transcript:

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

2 Today Maximum subsequence sum problem

3 Maximum Subsequence Sum Given (possibly negative) integers A 1, A 2,…, A N, find the maximum value of ∑ A k (where k goes from i to j inside the summation). For input -2, 11, -4, 13, -5, -2, what is the answer? 20 (A 2 through A 4 ). 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.

4 Running Times of Four Algorithms

5 Plot of Time versus N

6

7 Algorithm 1 What is the running time? O(n 3 ). Why O(n 3 )?

8 Improve Algorithm 1? Which statement requires O(n 3 ) work? Line 14. Can we reduce the work?

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

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

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

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

13 Example First Half Second Half ---------------------------------- 4 -3 5 -2 -1 2 6 -2 The maximum subsequence sum for the first half is 6 (elements A 1 through A 3 ). The maximum subsequence sum for the second half is 8 (elements A 6 through A 7 ). The maximum subsequence sum in the first half that includes the last element in the first half is 4 (elements A 1 through A 4 ), and the maximum subsequence sum in the second half that includes the first element in the second half is 7 (elements A 5 through A 7 ). Thus, the maximum sum that spans both halves and goes through the middle is 11 (elements A 1 through A 7 ). Thus, the third choice provides the solution.

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

15 Finding T(N) We haveT(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 = 2 k, then T(N) = N*(k+1) = N logN + N = O(N logN).

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

17 Example 1 Input: 4 -3 5 -2 -1 2 6 -2 jthisSummaxSum 00 044 114 266 346 436 556 611 79

18 Example 2 Input: 4 -7 5 -2 -8 2 6 -3 jthisSummaxSum 00 044 104 255 335 405 525 688 758

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


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

Similar presentations


Ads by Google