Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions C++/src/maxNetProfit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include<iostream>
using namespace std;
/*
Author: Jiankai Sun
Date: 2014.10.15
*/
/*
N jobs are to be scheduled for processing on one machine. Job i, 1 ≤ i ≤ N,
needs ti units of processing time. If job i is finished by time T, where T is a given
deadline, then a profit pi is earned; otherwise, a penalty qi
is imposed. (Both pi and qi are positive integers.) We want to select a subset S of jobs such that
(i) ∑i∈S ti ≤ T, and (ii) f(S) = ∑i∈S pi −∑i /∈S qi is maximum.
Show how to find such a set of jobs using dynamic programming.
*/
int const N=3;
int const T=6;
//profit
int p[N]={12,3,4};
//penalty
int q[N]={1,4,2};
//time consuming
int t[N]={1,4,3};
//net profit table
int f[N+1][T+1];
//pointer to index which job should be scheduled
int P[N+1][T+1];

void findMaxNetProfit(){
//non recursive implementation
for(int i=0;i<N;i++){
for(int j=0;j<T;j++){
//k is the job index; from 1 to N
int k=i+1;
// j is the time unit; form 1 to T
int l=j+1;
if(t[i]>l){
f[k][l]=f[k-1][l]-q[i];
}
else{
f[k][l]=max(f[k-1][l]-q[i],f[k-1][l-t[i]]+p[i]);
if(f[k][l]==f[k-1][l-t[i]]+p[i]){
P[k][l]=1;
}
}
}
}
cout<<"net profit is "<<f[N][T]<<endl;
}
int findMaxNetProfit_rec(int job_n,int deadline){
//recursive implementation
if(job_n==0){
//boundary condition
return 0;
}
if(t[job_n-1]>deadline){
return findMaxNetProfit_rec(job_n-1,deadline)-q[job_n-1];
}
else{
int a=findMaxNetProfit_rec(job_n-1,deadline)-q[job_n-1];
int b=findMaxNetProfit_rec(job_n-1,deadline-t[job_n-1])+p[job_n-1];
if(b>a)
P[job_n][deadline]=1;
return max(a,b);
}

}
void printJobSet(){
int l=T;
for(int i=N;i>=1;i--){
if(P[i][l]==1){
cout<<"job "<<i<<" is in job set"<<endl;
l=l-t[i-1];
}
}
}
int main(){

for(int i=0;i<i+1;i++)
f[0][0]=0;
for(int i=0;i<N;i++){
for(int j=0;j<T;j++){
P[i][j]=0;
}
}
//findMaxNetProfit();
cout<<findMaxNetProfit_rec(N,T)<<endl;
printJobSet();
return 0;
}
76 changes: 76 additions & 0 deletions C++/src/max_selected_intervals.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Author: Jiankai Sun
Date: 2014.10.21
*/
#include<iostream>
using namespace std;
//find the largest j which makes f[j]<=s[i]
int find_opt(double [], double [],int);
//return the maximum sum of the selected intervals
//non recurssive version
double max_select_nonrec(double [],double [],int);
//recurssive version
double max_select_rec(double [],double [],int);
//void print selected intervals
void print_interval(int *,int);

int find_opt(double s[],double f[],int i){
for(int j=i-1;j>=0;j--){
if(f[j]<=s[i]){
return j;
}
}
return 0;
}

double max_select_nonrec(double s[], double f[],int n){
double * T=new double[n];
int * P=new int[n];
for(int i=0;i<n;i++){
T[i]=0;
}
for(int i=1;i<n;i++){
int j=find_opt(s,f,i);
double temp=T[j]+f[i]-s[i];
if(temp>=T[i-1]){
T[i]=temp;
P[i]=j;
}
else{
T[i]=T[i-1];
P[i]=-1;
}

}
//print
print_interval(P,n);
return T[n-1];
}
void print_interval(int * P, int n){
int i=n-1;
while(P[i]!=0){
if(P[i]>0){
cout<<i<<" ";
i=P[i];
}
}
//print the last one
cout<<i<<endl;
}

double max_select_rec(double s[],double f[], int i){

if(i<=0)
return 0;
else
return max(max_select_rec(s,f,find_opt(s,f,i))+f[i]-s[i],max_select_rec(s,f,i-1));
}

int main(){
int n=7;
double s[]={0,1,5,6,7,15,1};
double f[]={0,4,10,12,14,20,21};
cout<<max_select_rec(s,f,n-1)<<endl;
cout<<max_select_nonrec(s,f,n)<<endl;
return 0;
}
54 changes: 54 additions & 0 deletions C++/src/min_operation_dp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include<iostream>
using namespace std;
/*
Author: Jiankai Sun
Date: 2014/10/14
*/
/*
Let A=a1a2...am and B=b1b2...bn be two strings of characers. we
want to transform A into B using following operations:
delete a character
and a character
change a character
write a dynamic programming algorithm that finds the minimum number
of operations needed to transform A into B
*/
int min_ope(char * c1,const int m, char *c2,const int n){

int F[m+1][n+1];//define operation matrix

//boundary condition
for(int i=0;i<=m;i++){
F[i][0]=i;
}
for(int i=0;i<=n;i++){
F[0][i]=i;
}
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
//the index of F starts from [1,1] to store c1[0] and c2[0]
int k=i+1;
int l=j+1;
if(c1[i]==c2[j])
//equal, don't need to change
F[k][l]=F[k-1][l-1];
else{
// add, delete or change operation
F[k][l]=min(min(F[k-1][l-1]+1,F[k-1][l]+1),F[k][l-1]+1);
}

}
}
return F[m][n];
}

int main(){
int const m=2;
int const n=1;
char c1[m]={'a','c'};
char c2[n]={'b'};

cout<<min_ope(c1,m,c2,n)<<endl;

return 0;
}
Binary file added C++/src/problems.pdf
Binary file not shown.
Loading