{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

assignment3_sol

# assignment3_sol - CSIS0250B Design and Analysis of...

This preview shows pages 1–2. Sign up to view the full content.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CSIS0250B Design and Analysis of Algorithms Sample Solution to Assignment Three Questions 1 - 4 carry 25 marks each. 1. (Solution by Tong Chiu Man) Assume U = ∑ n i =1 a i is a multiple of three and a i ≤ U 3 for all i = 1 , 2 , ··· ,n . Otherwise, it is not possible to divide the n integers into three disjoint subsets with equal sum. Let S i ( u,v ) be true if it is possible to divide the first i integers a 1 ,a 2 , ··· ,a i into three disjoint subsets with sums u,v, ( ∑ i j =1 a j- u- v ). Then it is possible to divide the n integers into three disjoint subsets of equal sum if and only if S n ( U 3 , U 3 ) is true. S i ( u,v ) =      true if i = 0 and u = v = 0 true if S i − 1 ( u- a i ,v ) or S i − 1 ( u,v- a i ) or S i − 1 ( u,v ) false otherwise We observe that the value of S i ( u,v ) depends only on values of S i − 1 ( u ′ ,v ′ ) computed in round ( i- 1) where u ′ ,v ′ are no bigger than u,v respectively. If we update the entries ( u,v ) in descending order of u,v from U 3 down to 0, we would not overwrite the values of ( u ′ ,v ′ ) computed in round ( i- 1). So it suffices to use one copy of the two-dimensional array for S . The function ThreeEqualSums( a [1 ..n ]) returns true if a [1] ,a [2] , ··· ,a [ n ] can be partitioned into three disjoint subsets with equal sum. The array S is of size ( U 3 + 1) · ( U 3 + 1). Each entry of S takes constant time to compute for each i = 1 , 2 , ··· ,n . So time complexity of the algorithm is O ( n · U 2 ) and the space complexity is O ( U 2 ). ThreeEqualSums(a[1..n]) begin 1. if U is not a multiple of three or a[i]>U/3 for some i then return false; 2. Initialize S[u,v] to false for all u,v; 3. S[0,0] = true; 4. for i=1 to n do for u=U/3 to 0 do for v=U/3 to 0 do if u >= a[i] and S[u-a[i],v]=true then S[u,v] = true; if v >= a[i] and S[u,v-a[i]]=true then S[u,v] = true; 5. return S[U/3,U/3]; end 2. Assume each word w i is of length f ( w i ) ≤ M . Otherwise it is not possible to print w i on a line of size M . Let S ( i,j ) = ( ∑ k k = i f ( w k )) + ( j- i ) be the spaces required for printing words w i ,w i +1 , ··· ,w j on the same line. S ( i,j ) ≤ M implies that the words w i ,w i +1 , ··· ,w j can fit on one line....
View Full Document

{[ snackBarMessage ]}

### Page1 / 5

assignment3_sol - CSIS0250B Design and Analysis of...

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

View Full Document
Ask a homework question - tutors are online