c++ - Dynamic Programming Problem -


well, don't need code itself, understanding trying in order write code. in nutshell, given 1000 projects each set of resources, , have (set ammount) of resources utilize calculate best projects can pick.

the pseudocode bestprofit function follows:

bestprofit:

parameters -              projects: vector of projects             r:        resources available solve subinstance              valuemap: map data structure used memoization             n:        projects numbered 0,...,n-1 may                        used solve subinstance returns -   maximum amount of profit may obtained on            subinstance of optimization problem post-condition – if n > 0, valuemap contains entry                  key (r, n). 

if n equals 0      return 0 check valuemap see whether subinstance has been solved -   if so, return computed result (function terminates) best1 = bestprofit(projects, r, valuemap, n-1) check whether r provides enough resources undertake project n-1 -   if so, best2 = bestprofit(projects, r – projects[n-1].requirements, valuemap, n-1)  + projects[n-1].profit  else      best2 = 0  if best1 >= best2    store (r, n) -> (best1, false) in valuemap    return best1 else    store (r, n) -> (best2, true) in valuemap    return best2 

when recursive call bestprofit, best1 supposed check if subproblem has been resolved. , best2, considered feasibility check based on last project. in other words looks balanced tree. unable understand how insert values on map during recursion? final if/else statement won't happen until whole set of projects has been traversed. , final value gets inserted on map. want pointers on how should approach write recursion correctly.

like said before not looking code way understand requirements of pseudo code project in c++. logic driving me crazy @ point because can't put work. thank looking @ , providing better insight if possible.

i'd return data structure indicates both profit , solution gets profit. store exact data structure in valuemap. make code more straightforward.

basically, "write correct recursive solution. add memoization make fast."


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -