Docsity

Dynamic Programming and the Knapsack Problem: Overcoming Redundancy, Study notes of Computer Science

Partial preview of the text

Download Dynamic Programming and the Knapsack Problem: Overcoming Redundancy and more Study notes Computer Science in PDF only on Docsity!

CSE 2100

Dynamic Programming

Overview

Motivation

One-dimension example

Recurrence - Recursion

The Knapsack

Problem

Recurrence

Solution method

Tracing a solution

Redundancy

Recursion speed?

Slow because many sub-problems can be redundant

Sub Problems & Redudancy

Sub-Problems can even by identical!

One Dimension Example

Remember the Fibonacci Numbers?

Fib(0) = 0

Fib(1) = 1

Fib(k) = Fib(k-1)+Fib(k-2)

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)); } }

Recursion Tree

Fib(6)

Fib(5) Fib(4)

Fib(4) Fib(3) Fib(3) Fib(2)

Fib(3) Fib(2) Fib(2) Fib(1) Fib(2) Fib(1) Fib(2) Fib(1)

Fib(2) Fib(1) Fib(1) Fib(0) Fib(1) Fib(0)

Fib(1) Fib(0)

How To Achieve It?

Two ways to achieve it

Top-Down computation

Memoization

Bottom-Up computation

Dynamic Programming

Bottom Up!

Fib(6)

Fib(5)

Fib(4)

Fib(3) Fib(2)

Fib(4)

Fib(3) Fib(2)

Fib(2) Fib(1) Fib(2) Fib(1)

Fib(2) Fib(1) Fib(1) Fib(0)

Fib(3)

Fib(2) Fib(1)

Fib(1) Fib(0)

Fib(1) Fib(0)

The Code

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)); } }

The Code... Even Better....

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)); } }

Dilemma

What is the catch?

The Catch

Only limited space

Not everything is valuable

You CANNOT TAKE EVERYTHING

You MUST CHOOSE

What is rational?

Maximize the value of what you take....

Respect weight limit!

A Recursive Solution?

Idea

Solve Recursively!

Decomposition?

Base case?

Induction?

Decomposition

Something must shrink!

Two options

The collection of items under consideration

The size of the knapsack

Suggestion

Imagine you can do it with n-1 items....