Skip to content

Commit 0840a1c

Browse files
New Commit
1 parent 3403daf commit 0840a1c

File tree

6 files changed

+266
-0
lines changed

6 files changed

+266
-0
lines changed

activity_select.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <stdio.h>
2+
int start[100], end[100], acti[100];
3+
4+
void sort (int act) //Sorting the activities wrt finish time
5+
{
6+
int temp, i, j, temp2;
7+
for (i= act-1;i>=1; i--)
8+
{
9+
for (j=1;j<=i;j++)
10+
{
11+
if (end[j-1]>end[j])
12+
{
13+
temp = end[j];
14+
end[j]= end[j-1];
15+
end[j-1] = temp;
16+
17+
temp2 = start[j];
18+
start[j] = start[j-1];
19+
start[j-1] = temp2;
20+
}
21+
}
22+
}
23+
}
24+
25+
26+
void activity_sel (int act)
27+
{
28+
int i,j, c=0;
29+
acti[0] = 0;
30+
j = 0;
31+
for (i=1;i<act;i++)
32+
{
33+
if (start[i]>=end[j])
34+
{
35+
acti[i] = i;
36+
j=i;
37+
c++;
38+
}
39+
else
40+
c++;
41+
}
42+
43+
printf ("Activity set: ");
44+
for (i = 0; i<=c; i++)
45+
printf ("%d", acti[i]);
46+
printf ("\n");
47+
}
48+
49+
50+
int main ()
51+
{
52+
int i, act, wish;
53+
do
54+
{
55+
printf ("Enter the number of Activities to be done: ");
56+
scanf ("%d", &act);
57+
58+
printf ("Enter the start and end time of the activities: \n");
59+
for (i =0;i<act;i++)
60+
{
61+
printf ("%d-->", i+1);
62+
printf ("Start Time: ");
63+
scanf ("%d", &start[i]);
64+
printf ("\tEnd Time: ");
65+
scanf ("%d", &end[i]);
66+
}
67+
68+
69+
sort (act);
70+
71+
printf ("Start set: ");
72+
for (i = 0; i<act; i++)
73+
printf ("%d", start[i]);
74+
printf ("\n");
75+
printf ("End set: ");
76+
for (i = 0; i<act; i++)
77+
printf ("%d", end[i]);
78+
printf ("\n");
79+
80+
activity_sel (act);
81+
82+
printf ("Do you want to continue? (1/0): ");
83+
scanf ("%d", &wish);
84+
}while (wish!=0);
85+
}

activity_select.exe

41.2 KB
Binary file not shown.

fractional_knapsack.c

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#include <stdio.h>
2+
float wt[100], pt[100];
3+
float pw[100];
4+
int capacity;
5+
6+
void sort(int n)
7+
{
8+
int i, j;
9+
float t, t1, t2;
10+
for(int i=0;i<n-1;i++)
11+
{
12+
for(int j=0;j<n-1-i;j++)
13+
{
14+
if(pw[j]<pw[j+1]){
15+
t= pw[j];
16+
t1 = wt[j];
17+
t2 = pt[j];
18+
19+
pw[j] = pw[j+1];
20+
wt[j] = wt[j+1];
21+
pt[j] = pt[j+1];
22+
23+
pw[j+1] = t;
24+
wt[j+1] = t1;
25+
pt[j+1] = t2;
26+
}
27+
}
28+
}
29+
}
30+
31+
void knapsack(int capacity, int n)
32+
{
33+
int i,k=0;
34+
float mul, profit = 0;
35+
// Calculating the unit profit values
36+
for(i = 0; i<n; i++)
37+
{
38+
pw[i] = pt[i]/wt[i];
39+
}
40+
41+
sort(n); // Sorting the arrays wrt unit profit pw
42+
43+
/*for (i=0;i<n;i++)
44+
{
45+
printf("%f ", pw[i]);
46+
}
47+
printf ("\n");
48+
for (i=0;i<n;i++)
49+
{
50+
printf("%f ", pt[i]);
51+
}
52+
printf ("\n");
53+
for (i=0;i<n;i++)
54+
{
55+
printf("%f ", wt[i]);
56+
}
57+
printf ("\n");
58+
*/
59+
60+
while(capacity != 0)
61+
{
62+
if(capacity >= wt[k])
63+
{
64+
capacity = (float)capacity - wt[k];
65+
profit = profit+pt[k];
66+
k++;
67+
}
68+
else
69+
{
70+
mul = capacity/wt[k];
71+
wt[k] = wt[k]*mul;
72+
pt[k] = pt[k]*mul;
73+
capacity = (float)capacity - wt[k];
74+
profit = profit + pt[k];
75+
k++;
76+
}
77+
}
78+
printf("The Maximum profit that can be made is: %f", profit);
79+
}
80+
81+
82+
int main()
83+
{
84+
int n,i;
85+
printf("Enter the number of weights and profit: ");
86+
scanf("%d", &n);
87+
88+
printf("Enter the capacity of the knapsack: ");
89+
scanf("%d", &capacity);
90+
91+
for(i = 0; i< n; i++)
92+
{
93+
printf("Weight: ");
94+
scanf("%f", &wt[i]);
95+
printf("Profit: ");
96+
scanf("%f", &pt[i]);
97+
}
98+
99+
100+
knapsack(capacity, n);
101+
}

fractional_knapsack.exe

41.1 KB
Binary file not shown.

job_scheduling.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include<stdio.h>
2+
3+
int p[100], jobs, d[100], final_dead[100];
4+
5+
void input(){
6+
printf("Enter the profits and deadlines\n\n");
7+
for(int i=0;i<jobs;i++){
8+
printf("Job %d\n", i+1);
9+
printf("Profit : ");
10+
scanf("%d", &p[i]);
11+
printf("Deadline : ");
12+
scanf("%d", &d[i]);
13+
}
14+
printf("\n");
15+
}
16+
17+
void sort(){
18+
int i, j, t1, t2;
19+
for(i=0;i<jobs-1;i++){
20+
for(j=0;j<jobs-1-i;j++){
21+
if(p[j]<p[j+1]){
22+
t1 = p[j];
23+
t2 = d[j];
24+
25+
p[j] = p[j+1];
26+
d[j] = d[j+1];
27+
28+
p[j+1] = t1;
29+
d[j+1] = t2;
30+
}
31+
}
32+
}
33+
}
34+
35+
void jobSelection(){
36+
sort();
37+
int i, j;
38+
printf("Profit Array: ");
39+
for(i=0;i<jobs;i++){
40+
printf("%d ", p[i]);
41+
}
42+
43+
printf("\nDeadlines: ");
44+
for(i=0;i<jobs;i++)
45+
printf("%d ", d[i]);
46+
47+
int max = d[0];
48+
for(i=0;i<jobs;i++)
49+
if(d[i]>max) max = d[i];
50+
51+
int profit = 0; //Stores the maximum profit
52+
53+
for(i=0;i<max;i++)
54+
final_dead[i] = 0;
55+
56+
for(i=max;i>=0;i--){
57+
if(final_dead[i]!=0)
58+
continue;
59+
else{
60+
//Searching for the deadline index in array d
61+
for(j=0;j<jobs;j++){
62+
if(d[j]==i){
63+
final_dead[i] = p[j]; //Store the profits in the final deadline list
64+
profit += p[j];
65+
break;
66+
}
67+
}
68+
}
69+
}
70+
printf("\nMaximum Profit = %d\n", profit);
71+
72+
}
73+
74+
int main(){
75+
printf("Enter number of jobs: ");
76+
scanf("%d", &jobs);
77+
input();
78+
jobSelection();
79+
return 0;
80+
}

job_scheduling.exe

41.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)