Skip to main content
edited the initialization to infinity otherwise the algorithm will not work
Source Link

It is always a good idea to find a recursive algorithm first and then turn it into a table.

  1. $f(C,n)$
  2. $~~$if(C = $\emptyset$) return 0;
  3. $~~$else
  4. $~~~~$opt = 0;infinity;
  5. $~~~~$for each $c\in C$ do
  6. $~~~~~~D=\{d\in C:d<c\}$
  7. $~~~~~~E=\{e-c:e\in D,e>c\}$
  8. $~~~~~~opt = min\{opt,f(D,c)+f(E,n-c)\}$
  9. $~~~~$return $opt+n$;

So you may ask: isn't there too many subsets of C to be put in a table? Observe that only 'consecutive' subsets are needed. And there are only $n \choose 2$ of them.(why?) Another problem is: some entries will change value in $E$. We can walk around this by indicating start and end in each $f$ rather than just specifying the length.

It is always a good idea to find a recursive algorithm first and then turn it into a table.

  1. $f(C,n)$
  2. $~~$if(C = $\emptyset$) return 0;
  3. $~~$else
  4. $~~~~$opt = 0;
  5. $~~~~$for each $c\in C$ do
  6. $~~~~~~D=\{d\in C:d<c\}$
  7. $~~~~~~E=\{e-c:e\in D,e>c\}$
  8. $~~~~~~opt = min\{opt,f(D,c)+f(E,n-c)\}$
  9. $~~~~$return $opt+n$;

So you may ask: isn't there too many subsets of C to be put in a table? Observe that only 'consecutive' subsets are needed. And there are only $n \choose 2$ of them.(why?) Another problem is: some entries will change value in $E$. We can walk around this by indicating start and end in each $f$ rather than just specifying the length.

It is always a good idea to find a recursive algorithm first and then turn it into a table.

  1. $f(C,n)$
  2. $~~$if(C = $\emptyset$) return 0;
  3. $~~$else
  4. $~~~~$opt = infinity;
  5. $~~~~$for each $c\in C$ do
  6. $~~~~~~D=\{d\in C:d<c\}$
  7. $~~~~~~E=\{e-c:e\in D,e>c\}$
  8. $~~~~~~opt = min\{opt,f(D,c)+f(E,n-c)\}$
  9. $~~~~$return $opt+n$;

So you may ask: isn't there too many subsets of C to be put in a table? Observe that only 'consecutive' subsets are needed. And there are only $n \choose 2$ of them.(why?) Another problem is: some entries will change value in $E$. We can walk around this by indicating start and end in each $f$ rather than just specifying the length.

Post Migrated Here from math.stackexchange.com (revisions)
Source Link
FiniteA
FiniteA

It is always a good idea to find a recursive algorithm first and then turn it into a table.

  1. $f(C,n)$
  2. $~~$if(C = $\emptyset$) return 0;
  3. $~~$else
  4. $~~~~$opt = 0;
  5. $~~~~$for each $c\in C$ do
  6. $~~~~~~D=\{d\in C:d<c\}$
  7. $~~~~~~E=\{e-c:e\in D,e>c\}$
  8. $~~~~~~opt = min\{opt,f(D,c)+f(E,n-c)\}$
  9. $~~~~$return $opt+n$;

So you may ask: isn't there too many subsets of C to be put in a table? Observe that only 'consecutive' subsets are needed. And there are only $n \choose 2$ of them.(why?) Another problem is: some entries will change value in $E$. We can walk around this by indicating start and end in each $f$ rather than just specifying the length.