




























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of dynamic programming and its application to the knapsack problem. It discusses the motivation behind using dynamic programming, the issue of redundancy in recursion, and how to overcome it through bottom-up computation and memoization. Examples using fibonacci numbers and the knapsack problem.
Typology: Study notes
1 / 36
This page cannot be seen from the preview
Don't miss anything!





























public class Fibonacci { public Fibonacci() {} public int fib(int k) { if (k==0) return 0; else if (k==1) return 1; else return fib(k-1)+fib(k-2); } public static void main(String[] args) { System.out.println("Fib(12) = " + new Fibonacci().fib(12)); } }
public class Fibonacci { public Fibonacci() {} public int fib(int k) { int tab[] = new int[k+1]; tab[0] = 0; tab[1] = 1; for(int i = 2;i<=k;i++) tab[i] = tab[i-1] + tab[i-2]; return tab[i]; } public static void main(String[] args) { System.out.println("Fib(12) = " + new Fibonacci().fib(12)); } }
public class Fibonacci { private int[] _tab; public Fibonacci() { _tab = new int[2];_tab[0]=0;_tab[1]=1;} public int fib(int k) { if (k >= _tab.length) { int[] nt = new int[k+1]; for(int k=0;k<_tab.length;k++) nt[k] = _tab[k]; for(int i = _tab.length;i<=k;i++) nt[i] = nt[i-1] + nt[i-2]; _tab = nt; } return _tab[i]; } public static void main(String[] args) { System.out.println("Fib(12) = " + new Fibonacci().fib(12)); } }