• Tidak ada hasil yang ditemukan

COL106: Data Structures and Algorithms

N/A
N/A
Protected

Academic year: 2024

Membagikan "COL106: Data Structures and Algorithms"

Copied!
12
0
0

Teks penuh

(1)

Ragesh Jaiswal, IIT Delhi

(2)

Dynamic Programming

(3)

Longest common subsequence

Here is amemoizedversion of the recursive algorithm.

Algorithm

LCS-mem(S,n,T,m)

- If (n= 0 ORm= 0) then return(0) - If (L[n,m] is known) then return(L[n,m]) - If (S[n] =S[m])

- length←1 +LCS-mem(S,n−1,T,m−1) - If (S[n]6=T[m])

- length←max{LCS-mem(S,n,T,m−1), LCS-mem(S,n−1,T,m)}

-L[n,m]←length - return(length)

What is the running time of the above algorithm? O(nm)

(4)

Dynamic Programming

0-1 Knapsack

Problem

You are givennitems with non-negative integer weights w(i) and an integerW. You have to determine a subsetS of{1, ...,n}such that P

i∈Sw(i) is maximized subject toP

i∈Sw(i)≤W.

Example: Let ({1,2,3,5,6,7},10) be the input instance.

What is the optimal solution?

(5)

0-1 Knapsack

Problem

You are givennitems with non-negative integer weights w(i) and an integerW. You have to determine a subsetS of{1, ...,n}such that P

i∈Sw(i) is maximized subject toP

i∈Sw(i)≤W.

Example: Let ([1,2,3,5,6,7],10) be the input instance.

What is the optimal solution?{2, 3, 4}

Sincew(2) = 2,w(3) = 3,w(4) = 5 and w(2) +w(3) +w(4) = 10.

(6)

Dynamic Programming

0-1 Knapsack

Problem

You are givennitems with non-negative integer weights w(i) and an integerW. You have to determine a subsetS of{1, ...,n}such that P

i∈Sw(i) is maximized subject toP

i∈Sw(i)≤W.

How do we define the subproblems for the Dynamic Program?

(7)

0-1 Knapsack

Problem

You are givennitems with non-negative integer weights w(i) and an integerW. You have to determine a subsetS of{1, ...,n}such that P

i∈Sw(i) is maximized subject toP

i∈Sw(i)≤W.

How do we define the subproblems for the Dynamic Program?

Let us try the following:

M(i): The maximum weight that can be filled using items {1, ...,i}subject to the sum being≤W.

How do we defineM(i) in terms ofM(1), ...,M(i−1)?

(8)

Dynamic Programming

0-1 Knapsack

Problem

You are givennitems with non-negative integer weights w(i) and an integerW. You have to determine a subsetS of{1, ...,n}such that P

i∈Sw(i) is maximized subject toP

i∈Sw(i)≤W.

How do we define the subproblems for the Dynamic Program?

Let us try the following:

M(i): The maximum weight that can be filled using items {1, ...,i}subject to the sum being≤W.

How do we defineM(i) in terms ofM(1), ...,M(i−1)?

Case 1: ithitem is not in the optimal solution. Then M(i) =M(i1).

Case 2: ithitem is in the optimal solution. There is a problem here.

(9)

0-1 Knapsack

Problem

You are givennitems with non-negative integer weights w(i) and an integerW. You have to determine a subsetS of{1, ...,n}such that P

i∈Sw(i) is maximized subject toP

i∈Sw(i)≤W.

How do we define the subproblems for the Dynamic Program?

Let us try the following:

M(i,w): The maximum weight that can be filled using items {1, ...,i}subject to the sum being≤w.

Recursive formulation:

Case 1: ithitem is not in the optimal solution. Then M(i,w) =M(i1,w).

Case 2: ithitem is in the optimal solution. Then M(i,w) =M(i1,ww(i)) +w(i)

(10)

Dynamic Programming

0-1 Knapsack

Problem

You are givennitems with non-negative integer weightsw(i) and an integerW. You have to determine a subsetS of{1, ...,n}such that P

i∈Sw(i) is maximized subject toP

i∈Sw(i)W.

Dynamic Programming solution:

M(i,w): The maximum weight that can be filled using items {1, ...,i}subject to the sum beingw.

Ifw(i)>w, thenM(i,w) =M(i1,w) Ifw(i)w, then

M(i,w) = max{M(i1,w),M(i1,ww(i)) +w(i)}

∀wW,M(1,w) =w(1) ifw(1)w and 0 otherwise.

(11)

0-1 Knapsack

Problem

You are givennitems with non-negative integer weightsw(i) and an integerW. You have to determine a subsetS of{1, ...,n}such that P

i∈Sw(i) is maximized subject toP

i∈Sw(i)W. Dynamic Programming solution:

M(i,w): The maximum weight that can be filled using items {1, ...,i}subject to the sum beingw.

Ifw(i)>w, thenM(i,w) =M(i1,w) Ifw(i)w, then

M(i,w) = max{M(i1,w),M(i1,ww(i)) +w(i)}

∀wW,M(1,w) =w(1) ifw(1)w and 0 otherwise.

What is the running time for filling the above table? O(n·W)

(12)

End

Referensi

Dokumen terkait

In the scheme of the (unbalanced) priority search tree outlined above, each node p divides the plane into two parts along the line x = p.x. All nodes of the left subtree lie to

This study compares the Decision Trees and Maximum Likelihood algorithms for land cover classification in the Surabaya and Bangkalan areas using Landsat 8 data.. This research

Since the search tree guarantees that the left child has a value that is less than or equal to the parent and the right child has a value greater than or equal to the value of

He has contributed lectures on algorithms to the Massively Empowered Classroom MEC project of Microsoft Research and the QEEE programme of MHRD COURSE PLAN: Week 1: Informal

As a rule, the curricula for all six majors in this body of knowledge 121 Software Engineering, 122 Computer Science, 123 Computer Engineering, 124 System Analysis, 125 Cybersecurity,

Are you looking for work in process, electrical or other types of engineering, R&D, sales & marketing or support professions such as information technology?. We’re interested in

Display 8 Marks CO3 L5 c With an example explain Queues using Dynamic Arrays 4 Marks OR 6 a What is Linked List, Write down the steps to modify a node in linked lists with an