This repository was archived by the owner on Oct 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #163 from rajashree23/master
Added fractional knapsack in C++
- Loading branch information
Showing
4 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
122 changes: 122 additions & 0 deletions
122
dynamic_programing/fractional_knapsack/C++/frac_knapsack.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
#include<iostream> | ||
|
||
using namespace std; | ||
|
||
int main() | ||
|
||
{ | ||
|
||
int array[2][100], n, w, i, curw, used[100], maxi = -1, totalprofit = 0; | ||
|
||
|
||
cout << "Enter number of objects: "; | ||
|
||
cin >> n; | ||
|
||
|
||
|
||
cout << "Enter the weight of the knapsack: "; | ||
|
||
cin >> w; | ||
|
||
cout<<"Array's first row is to store weights and second row is to store profits"<<endl; | ||
|
||
for (i = 0; i < n; i++) | ||
|
||
{ | ||
|
||
cin >> array[0][i] >> array[1][i]; | ||
|
||
} | ||
|
||
for (i = 0; i < n; i++) | ||
|
||
{ | ||
|
||
used[i] = 0; | ||
|
||
} | ||
|
||
curw = w; | ||
|
||
|
||
|
||
while (curw >= 0) | ||
|
||
{ | ||
|
||
maxi = -1; | ||
|
||
|
||
|
||
for (i = 0; i < n; i++) | ||
|
||
{ | ||
|
||
if ((used[i] == 0) && ((maxi == -1) || (((float) array[1][i] | ||
|
||
/ (float) array[0][i]) > ((float) array[1][maxi] | ||
|
||
/ (float) array[0][maxi])))) | ||
|
||
{ | ||
|
||
maxi = i; | ||
|
||
} | ||
|
||
} | ||
|
||
used[maxi] = 1; | ||
|
||
|
||
|
||
curw -= array[0][maxi]; | ||
|
||
|
||
|
||
totalprofit += array[1][maxi]; | ||
|
||
if (curw >= 0) | ||
|
||
{ | ||
|
||
cout << "\nAdded object " << maxi + 1 << " Weight: " | ||
|
||
<< array[0][maxi] << " Profit: " << array[1][maxi] | ||
|
||
<< " completely in the bag, Space left: " << curw; | ||
|
||
} | ||
|
||
else | ||
|
||
{ | ||
|
||
cout << "\nAdded object " << maxi + 1 << " Weight: " | ||
|
||
<< (array[0][maxi] + curw) << " Profit: " | ||
|
||
<< (array[1][maxi] / array[0][maxi]) * (array[0][maxi] | ||
|
||
+ curw) << " partially in the bag, Space left: 0" | ||
|
||
<< " Weight added is: " << curw + array[0][maxi]; | ||
|
||
totalprofit -= array[1][maxi]; | ||
|
||
totalprofit += ((array[1][maxi] / array[0][maxi]) * (array[0][maxi] | ||
|
||
+ curw)); | ||
|
||
} | ||
|
||
} | ||
|
||
|
||
|
||
cout << "\nBags filled with objects worth: " << totalprofit; | ||
|
||
return 0; | ||
|
||
} |
Binary file not shown.
63 changes: 63 additions & 0 deletions
63
dynamic_programing/longest_common_subsequence/C/long_comm_subs.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include<stdio.h> | ||
#include<string.h> | ||
|
||
int i,j,m,n,c[20][20]; | ||
char x[20],y[20],b[20][20]; | ||
|
||
void print(int i,int j) | ||
{ | ||
if(i==0 || j==0) | ||
return; | ||
if(b[i][j]=='c') | ||
{ | ||
print(i-1,j-1); | ||
printf("%c",x[i-1]); | ||
} | ||
else if(b[i][j]=='u') | ||
print(i-1,j); | ||
else | ||
print(i,j-1); | ||
} | ||
|
||
void lcs() | ||
{ | ||
m=strlen(x); | ||
n=strlen(y); | ||
for(i=0;i<=m;i++) | ||
c[i][0]=0; | ||
for(i=0;i<=n;i++) | ||
c[0][i]=0; | ||
|
||
//c, u and l denotes cross, upward and downward directions respectively | ||
for(i=1;i<=m;i++) | ||
for(j=1;j<=n;j++) | ||
{ | ||
if(x[i-1]==y[j-1]) | ||
{ | ||
c[i][j]=c[i-1][j-1]+1; | ||
b[i][j]='c'; | ||
} | ||
else if(c[i-1][j]>=c[i][j-1]) | ||
{ | ||
c[i][j]=c[i-1][j]; | ||
b[i][j]='u'; | ||
} | ||
else | ||
{ | ||
c[i][j]=c[i][j-1]; | ||
b[i][j]='l'; | ||
} | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
printf("Enter 1st sequence:"); | ||
scanf("%s",x); | ||
printf("Enter 2nd sequence:"); | ||
scanf("%s",y); | ||
printf("\nThe Longest Common Subsequence is "); | ||
lcs(); | ||
print(m,n); | ||
return 0; | ||
} |