hw4-sol - Assignment #4, CS/531 Due Date: Mon. Nov. 7, 2011...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Assignment #4, CS/531 Due Date: Mon. Nov. 7, 2011 UNSUPPORTED SOLUTIONS RECEIVE NO CREDIT. Total points: 51 1 (7 pts). Maximum Contiguous Subsequence Sum Problem revisited. Let A [1 ..n ] be an array of numbers. The elements in A can be either positive or negative. We want to find the indices k,l so that the sum l i = k A [ i ] is maximum among all possible choices of k,l . For example if A = { 3 , 12 , 6 , 10 , 5 , 2 } , the answer is k = 2 ,l = 4, since A [2] + A [3] + A [4] = 12 + ( 6) + 10 = 16 is the maximum sum of all possible choices. This is exactly the problem 3 in HW2. However, this time you must describe an O ( n ) time algorithm for solving this problem. Answer: Define an array B [0 , ,n ], where each element B [ i ] is defined to be: B [ i ] = the maximum contiguous subsequence sum of subsequences in A [1 ..i ] that ends at A [ i ]. Then we have: B [ i ] = braceleftBigg if i = 0 max { ,B [ i 1] + A [ i ] } if i > Explanation: When i = 0, there is no element in A [ ] that we can include in the subsequence. So B [0] is the default value 0. When i > 0, there are two possibilities: The maximum contiguous subsequence that ends at A [ i ] contains A [ i ]. In this case, the entry is B [ i 1] + A [ i ]. The maximum contiguous subsequence that ends at A [ i ] contains no element from A at all. (This can happen when all contiguous subsequences ending with A [ i ] have negative sum. In this case, we just take empty subsequence as the answer). In this case B [ i ] = 0. Since we dont know which case gives the larger sum, we take the maximum of the two. The algorithm is as follows: Algorithm 1 Max-Subsequence-Sum 1: Max := B [0] := 0 2: For i = 1 to n Do: B [ i ] = max { ,B [ i 1] + A [ i ] } Max = max { B [ i ] ,Max } 3: Output Max Clearly, the algorithm takes ( n ) time. Note: This problem was an interview problem when Bloomburg visited UB campus a few years ago. For students who were only able to describe a ( n 2 ) time algorithm, they basicalled said Bye-Bye. For students who were able to give a ( n log n ) solution, Bloomburg kept them in contact list. For those students who got the ( n ) time algorithm, they offered them on-site interviews. 1 2. (1 pt) Consider the graph G = ( V,E ) shown in Figure 1. The integers near an edge is its weight. Using Kruskals algorithm, compute the minimum spanning tree T 1 of G . List the edges of T 1 in the order they are added into T 1 . Answer: 5 1 7 8 6 3 4 2 9 2 3 4 6 b c d e f h a g Figure 1: Kruskals Algorithm. The MST is { ae,hf,eb,cd,eh,hg,dh } . 3. (6 pts) For a given undirected, edge weighted graph G = ( V,E ), there might be more than one MST of G . This can happen if G has two edges e and e such that w ( e ) = w ( e ). In this case, when we run Kruskal algorithm on G , the edges e and e may be processed in different order, and this might results in two different MSTs of G ....
View Full Document

This note was uploaded on 02/27/2012 for the course CSE 431/531 taught by Professor Xinhe during the Fall '11 term at SUNY Buffalo.

Page1 / 6

hw4-sol - Assignment #4, CS/531 Due Date: Mon. Nov. 7, 2011...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online