3

How to implement Activity Selection Problem using Dynamic Programming (CLRS Exercise 16.1-1). I've implemented it using Greedy Method, which runs in linear time (assuming array is already sorted with finish time).

I know it poses Optimal substructure.

Let $S_{ij}$ the set of activities that start after activity $a_i$ finishes and that finish before activity $a_j$ starts. If we denote the size of an optimal solution for the set $S_{ij}$ by $c[i j]$ , then we would have the recurrence

$c[i j]  = c[i k] + c[k j] + 1$
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • what is your question? – leeor Nov 08 '15 at 13:34
  • My question how can I implement the code in any programming language (say Python). I mean how can I select a particular job and partition the list into parts and solve them recursively and combine the result. –  Nov 08 '15 at 13:38

1 Answers1

2

We can solve it using dynamic programming by keeping a state that contains the detail about the current index of the activity and the current finish time of the activity so far which we have taken, at each index of the activity we can make 2 decisions either to choose a activity or not, and finally we need to take the maximum of both the choices and recurse. I have implemented a recursive dp solution in c++ :

#include<bits/stdc++.h>

using namespace std;

int n;
int st[1000], en[1000];
int dp[1000][1000];

int solve(int index, int currentFinishTime){
    if(index == n) return 0;
    int v1 = 0, v2 = 0;
    if(dp[index][currentFinishTime] != -1) return dp[index][currentFinishTime];

    //do not choose the current activity
    v1 = solve(index+1, currentFinishTime);

    //try to choose the current activity
    if(st[index] >= currentFinishTime){
        v2 = solve(index+1, en[index]) + 1;
    }
    return dp[index][currentFinishTime] = max(v1, v2);
}

int main(){
    cin >> n;
    for(int i = 0;i < n;i++) cin >> st[i] >> en[i];
    memset(dp, -1, sizeof dp);

    cout << solve(0, 0) << endl;
return 0;
}

http://ideone.com/m0mxx2

In this code the dp[index][finish time] is the dp table used to store the result.

uSeemSurprised
  • 1,826
  • 2
  • 15
  • 18
  • Can you include the code in your answer? It might be helpful and it's best to include outside content (particularly if it's yours) in your posts, because if the link vanishes, your post will lose some value. – Artjom B. Dec 11 '15 at 17:37
  • Do you think you could comment a little further? I was trying to understand, but I was having a hard time..Thanks! – denyzprahy Dec 03 '17 at 19:55
  • @Denis Cappelini What part exactly you don't understand? – uSeemSurprised Dec 05 '17 at 05:37
  • @uSeemSurprised I didn't quite get the use of the "dp" matrix. It seems like I don't really need it. If I remove it, I still get the right output. – denyzprahy Dec 06 '17 at 00:12
  • 1
    @Denis Cappelini It is required for memoisation, you should google it and read about it. Basically it stores the state answer and whenever we have the same state to compute instead of doing it all over again we just use the result that we have stored. If you remove it, the algorithm will become exponential instead of polynomial. Try this code with large input it would work but if you remove the dp matrix and try it again it would not work. – uSeemSurprised Dec 06 '17 at 06:20
  • @uSeemSurprised Thank you for your answer. Just to confirm, you are getting the stored state on this line, right? `if(dp[index][currentFinishTime] != -1) return dp[index][currentFinishTime];` – denyzprahy Dec 06 '17 at 12:25
  • @Denis Cappelini Yes, if the state is set we use its previously computed value. – uSeemSurprised Dec 07 '17 at 04:45
  • @uSeemSurprised, final question..Why on this line `v1 = solve(index+1, currentFinishTime);` you have the comment saying that you will not choose the current activity?I didn't get you passing a 0 to `currentFinishTime` all along. Thank you for the answers! – denyzprahy Dec 07 '17 at 23:10
  • @Denis Cappelini We try to make decisions in a dp solution i.e to choose some object or not and then select the one that gives the optimal answer. Passing current finish time as 0 is base condition i.e no activity has been selected yet and finish time is 0. – uSeemSurprised Dec 12 '17 at 06:33