Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximization using Dynamic Programming

I am trying to come up with the solution for a problem analogous to the following:

  • Let M be a matrix of n rows and T columns.
  • Let each row have positive non-decreasing values. (e.g. row = [1, 2, 30, 30, 35])
  • Let M[i][j] correspond to the score obtained by spending j units of time on exam i.

Using dynamic programming, solve the problem as to find the optimal way of spending T units of time to study which will yield the highest total score.

Thanks in advance for any help :)

My attempt:

S[][] = 0

for i = 1:n
   for j = 0:T
       max = 0
       for k = 0:j
           Grade = G[i][j]+ S[i-1][T-k]
           if Grade > max
              max = Grade
       end for
       S[i][j] = max
    end for
end for
like image 615
Pi_ Avatar asked Nov 23 '12 20:11

Pi_


People also ask

Is dynamic programming used for optimization?

Dynamic programming is an optimization approach that transforms a complex problem into a sequence of simpler problems; its essential characteristic is the multistage nature of the optimization procedure.

Which technique is used in dynamic programming?

Dynamic Programming Technique is similar to divide-and-conquer technique. Both techniques solve a problem by breaking it down into several sub-problems that can be solved recursively.


1 Answers

Let S[i][j] represent the best score you can achieve spending j units of time on the first i exams. You can calculate S[i][j] by looking at S[i-1][k] for each value of k. For each element of S, remember the value of k from the previous row that gave the best result. The answer to what the best score for studying all exams in time T is just S[n][T], and you can use the values of k that you remembered to determine how much time to spend on each exam.

S[][] = 0

for j = 0:T
   S[0][j] = M[0][j]

for i = 1:n
   for j = 0:T
       max = 0
       for k = 0:j
           # This is the score you could get by spending j time, spending
           # k time on this exam and j-k time on the previous exams.
           Grade = S[i-1][j-k] + M[i][k]
           if Grade > max
              max = Grade
       end for
       S[i][j] = max
    end for
end for
like image 137
Vaughn Cato Avatar answered Sep 23 '22 05:09

Vaughn Cato