final_soln

final_soln - MIT OpenCourseWare http/ocw.mit.edu 6.006...

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 Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: MIT OpenCourseWare http://ocw.mit.edu 6.006 Introduction to Algorithms Spring 2008 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms . Introduction to Algorithms: 6.006 Massachusetts Institute of Technology May 9, 2008 Professors Srini Devadas and Erik Demaine Handout 13 Final Practice Problems 1 Subset Sum You are given a sequence of n numbers (positive or negative): x 1 , x 2 , . . . , x n Your job is to select a subset of these numbers of maximum total sum, subject to the constraint that you can’t select two elements that are adjacent (that is, if you pick x i then you cannot pick either x i − 1 or x i +1 ). Explain how you can find, in time polynomial in n, the subset of maximum total sum. Solution: Let sum i be the maximum sum of the numbers x 1 , x 2 , . . . , x i given the adjacency constraint. sum = 0 sum 1 = max(0 , x 1 ) sum i = max( sum i − 2 + x i , sum i − 1 ) This last step works because either we include x i , in which case we also want to include the best...
View Full Document

{[ snackBarMessage ]}

Page1 / 5

final_soln - MIT OpenCourseWare http/ocw.mit.edu 6.006...

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