Lecture16

Lecture16 - Lecture 16, Nov 18 2010 Subset Sum Given n...

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

View Full Document Right Arrow Icon
1 Subset Sum Given n positive integers x 1 , x 2 ,… x n and a number B Decide whether there is a subset S of them such that For example, if x ={1,5,7} and B =8, then S ={1,3} Brute force approach ? How about greedy approach ? NP-hard problem We will still try to solve, at least for cases where “B is small”. 191 ,{ 1 , , } i iS x BS n  Lecture 16, Nov 18 2010 Subset Sum – continued We will use Dynamic Programming How to choose subproblem ? Subproblem: » M(k,b)= true” iff there is a solution to » Result in M(n,B) » M(1,b)= true” iff x 1 =b , “false” otherwise. 192 ' ,' { 1 , , } i xS bk Goal:Find {1, , } . . i Sn s t x B 
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Subset Sum – continued Main recurrence: » M(k+1,b)=M(k,b) or M(k,b-x k+1 ). » Explanation: Either use x k+1 or do not use it » Use “false” for non-existing elements of M() . » Running time: Matrix M is [n x B], each element constant time: O(nB).
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/08/2011 for the course CS 161 at Stanford.

Page1 / 4

Lecture16 - Lecture 16, Nov 18 2010 Subset Sum Given n...

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