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

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

Pre 2010

Uploaded on 09/24/2009

koofers-user-iyw
koofers-user-iyw 🇺🇸

9 documents

1 / 36

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSE2100 DS & Algorithms 1
CSE 2100
Dynamic Programming
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24

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....