Skip to content

Commit aaa74fc

Browse files
committed
Moar algorithms
1 parent 6e2a694 commit aaa74fc

File tree

7 files changed

+416
-35
lines changed

7 files changed

+416
-35
lines changed

ALG Lab/Backtracking and Bounds/Knapsack/knapsack.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,16 @@ void findKnapsack (BOOL incl[N], int i) {
5252
mval = cval;
5353
}
5454
}
55-
if (i == N || cwt >= cap) {
55+
if (i == N || cwt >= cap || (cval < mval && i == N)) {
5656
return;
5757
}
5858
int x = wts[i];
5959
BOOL use[N], nouse[N];
60-
memcpy(use, incl, sizeof(use));
61-
memcpy(nouse, incl, sizeof(nouse));
60+
int j;
61+
for (j = 0; i < N; ++j) {
62+
use[j] = incl[j];
63+
nouse[j] = incl[j];
64+
}
6265
use[i] = YES;
6366
nouse[i] = NO;
6467
findKnapsack(use, i+1);
@@ -94,4 +97,22 @@ int main(int argc, char const * argv[]) {
9497
Included = { 1 }; Total value = 15
9598
Included = { 1 5 }; Total value = 25
9699
Included = { 1 3 4 }; Total value = 29
100+
101+
102+
Sample input:
103+
5
104+
2 40
105+
3 50
106+
2 100
107+
5 95
108+
3 30
109+
10
110+
111+
Sample output:
112+
Included = { 2 }; Total value = 40
113+
Included = { 2 3 }; Total value = 90
114+
Included = { 2 3 2 }; Total value = 190
115+
Included = { 2 3 2 3 }; Total value = 220
116+
Included = { 2 2 5 }; Total value = 235
117+
Included = { 3 2 5 }; Total value = 245
97118
*/
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
#include <math.h>
5+
#include <limits.h>
6+
7+
#define MAX 10
8+
#define TWOMAX 1024
9+
10+
int n; // Number of items
11+
int pown; // 2^n
12+
int cap; // Max weight it can hold
13+
14+
float wts[MAX]; // All the weights
15+
float vals[MAX]; // All the values
16+
17+
float mv = -1; // Maximum value
18+
19+
int mvals[MAX]; // Holds the max value combination
20+
21+
void input () {
22+
printf("Enter the number of items: ");
23+
scanf(" %d", &n);
24+
pown = pow(2, n);
25+
int i;
26+
for (i = 0; i < n; ++i) {
27+
printf("Enter weight and value of item %d: ", i+1);
28+
scanf(" %f %f", &wts[i], &vals[i]);
29+
}
30+
printf("Enter knapsack capacity: ");
31+
scanf(" %d", &cap);
32+
}
33+
34+
float getWeight (int incl[MAX]) {
35+
int i;
36+
float wt = 0;
37+
for (i = 0; i < n; ++i) {
38+
if (incl[i]) {
39+
wt += wts[i];
40+
}
41+
}
42+
return wt;
43+
}
44+
45+
float getValue (int incl[MAX]) {
46+
int i;
47+
float value = 0.0;
48+
for (i = 0; i < n; ++i) {
49+
if (incl[i]) {
50+
value += vals[i];
51+
}
52+
}
53+
return value;
54+
}
55+
56+
void findMax () {
57+
58+
int i, j;
59+
int k;
60+
61+
int curr[MAX];
62+
63+
for (i = 0; i < pow(2, n); ++i) {
64+
65+
for (j = 0; j < n; ++j) {
66+
curr[j] = (int)(i / (pow(2, j))) % 2;
67+
// printf("%d", curr[j]);
68+
}
69+
70+
float v = getValue(curr);
71+
float w = getWeight(curr);
72+
// printf(" | %.2f, %.2f", w, v);
73+
74+
if (w <= cap && v > mv) {
75+
mv = v;
76+
// printf(" | *");
77+
for (j = 0; j < n; ++j) {
78+
mvals[j] = curr[j];
79+
}
80+
}
81+
82+
// printf("\n");
83+
84+
}
85+
86+
}
87+
88+
int main (int argc, char const * argv []) {
89+
90+
int i;
91+
92+
input();
93+
findMax();
94+
95+
printf("\nOptimal solution:\nWeight\t| Value\n");
96+
97+
for (i = 0; i < n; ++i) {
98+
if (mvals[i]) {
99+
printf(" %.2f\t| %.2f\n", wts[i], vals[i]);
100+
}
101+
}
102+
103+
printf("\nMax value: %.2f\n", getValue(mvals));
104+
printf("Weight fit: %.2f\n", getWeight(mvals));
105+
106+
return 0;
107+
}
108+
109+
/**
110+
111+
Sample input:
112+
113+
4
114+
1 15
115+
5 10
116+
3 9
117+
4 5
118+
8
119+
120+
Sample output:
121+
122+
123+
124+
*/

ALG Lab/Brute Forcing/Partition 2/partitons.txt

Lines changed: 0 additions & 31 deletions
This file was deleted.

ALG Lab/Brute Forcing/Partition 2/partition.c renamed to ALG Lab/Brute Forcing/Partition Alt/partition.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,6 @@ int main (int argc, char const * argv []) {
6464
arr[i] = i + 1;
6565
curr[i] = NO;
6666
}
67-
partitons(curr, 0, 6);
67+
partitons(curr, 0, 7);
6868
return 0;
6969
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
1 2 3 4 5 6 --------- 7
2+
1 2 3 4 5 --------- 6 7
3+
1 2 3 4 6 --------- 5 7
4+
1 2 3 4 --------- 5 6 7
5+
1 2 3 5 6 --------- 4 7
6+
1 2 3 5 --------- 4 6 7
7+
1 2 3 6 --------- 4 5 7
8+
1 2 3 --------- 4 5 6 7
9+
1 2 4 5 6 --------- 3 7
10+
1 2 4 5 --------- 3 6 7
11+
1 2 4 6 --------- 3 5 7
12+
1 2 4 --------- 3 5 6 7
13+
1 2 5 6 --------- 3 4 7
14+
1 2 5 --------- 3 4 6 7
15+
1 2 6 --------- 3 4 5 7
16+
1 2 --------- 3 4 5 6 7
17+
1 3 4 5 6 --------- 2 7
18+
1 3 4 5 --------- 2 6 7
19+
1 3 4 6 --------- 2 5 7
20+
1 3 4 --------- 2 5 6 7
21+
1 3 5 6 --------- 2 4 7
22+
1 3 5 --------- 2 4 6 7
23+
1 3 6 --------- 2 4 5 7
24+
1 3 --------- 2 4 5 6 7
25+
1 4 5 6 --------- 2 3 7
26+
1 4 5 --------- 2 3 6 7
27+
1 4 6 --------- 2 3 5 7
28+
1 4 --------- 2 3 5 6 7
29+
1 5 6 --------- 2 3 4 7
30+
1 5 --------- 2 3 4 6 7
31+
1 6 --------- 2 3 4 5 7
32+
1 --------- 2 3 4 5 6 7
33+
2 3 4 5 6 --------- 1 7
34+
2 3 4 5 --------- 1 6 7
35+
2 3 4 6 --------- 1 5 7
36+
2 3 4 --------- 1 5 6 7
37+
2 3 5 6 --------- 1 4 7
38+
2 3 5 --------- 1 4 6 7
39+
2 3 6 --------- 1 4 5 7
40+
2 3 --------- 1 4 5 6 7
41+
2 4 5 6 --------- 1 3 7
42+
2 4 5 --------- 1 3 6 7
43+
2 4 6 --------- 1 3 5 7
44+
2 4 --------- 1 3 5 6 7
45+
2 5 6 --------- 1 3 4 7
46+
2 5 --------- 1 3 4 6 7
47+
2 6 --------- 1 3 4 5 7
48+
2 --------- 1 3 4 5 6 7
49+
3 4 5 6 --------- 1 2 7
50+
3 4 5 --------- 1 2 6 7
51+
3 4 6 --------- 1 2 5 7
52+
3 4 --------- 1 2 5 6 7
53+
3 5 6 --------- 1 2 4 7
54+
3 5 --------- 1 2 4 6 7
55+
3 6 --------- 1 2 4 5 7
56+
3 --------- 1 2 4 5 6 7
57+
4 5 6 --------- 1 2 3 7
58+
4 5 --------- 1 2 3 6 7
59+
4 6 --------- 1 2 3 5 7
60+
4 --------- 1 2 3 5 6 7
61+
5 6 --------- 1 2 3 4 7
62+
5 --------- 1 2 3 4 6 7
63+
6 --------- 1 2 3 4 5 7
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
#include <string.h>
5+
6+
#define MAX 20
7+
8+
int N;
9+
10+
typedef enum { NO, YES } BOOL;
11+
12+
int arr[MAX];
13+
14+
BOOL checkSum (BOOL curr[N]) {
15+
16+
int lsum = 0;
17+
int rsum = 0;
18+
19+
int i;
20+
21+
for (i = 0; i < N; ++i) {
22+
if (curr[i]) {
23+
lsum += arr[i];
24+
} else {
25+
rsum += arr[i];
26+
}
27+
}
28+
29+
return (lsum == rsum);
30+
31+
}
32+
33+
// Generate all the partitions of the set...
34+
35+
void partitons (BOOL curr[N], int k, int n) {
36+
37+
int i, j;
38+
39+
int ic = 0, ec = 0;
40+
for (i = 0; i < n; ++i) {
41+
if (curr[i]) ic += 1;
42+
else ec += 1;
43+
}
44+
45+
// Only print the last decent
46+
if (k == n-1) {
47+
48+
if (ic > 0 && ec > 0) {
49+
50+
if (checkSum(curr)) {
51+
52+
for (i = 0; i < n; ++i) {
53+
if (curr[i])
54+
printf("%d ", arr[i]);
55+
}
56+
printf(" --------- ");
57+
for (i = 0; i < n; ++i) {
58+
if (!curr[i])
59+
printf("%d ", arr[i]);
60+
}
61+
printf("\n");
62+
63+
}
64+
65+
}
66+
67+
return;
68+
}
69+
70+
BOOL incl[N];
71+
BOOL excl[N];
72+
for (i = 0; i < N; ++i) {
73+
incl[i] = curr[i];
74+
excl[i] = curr[i];
75+
}
76+
incl[k] = YES;
77+
excl[k] = NO;
78+
79+
// Recurse by including current and one by excluding current.
80+
partitons(incl, k+1, n);
81+
partitons(excl, k+1, n);
82+
}
83+
84+
int main (int argc, char const * argv []) {
85+
printf("Enter the number of numbers: ");
86+
scanf(" %d", &N);
87+
printf("Enter the numbers: ");
88+
int i;
89+
BOOL curr[MAX];
90+
for (i = 0; i < N; ++i) {
91+
curr[i] = NO;
92+
scanf(" %d", &arr[i]);
93+
}
94+
printf("\nPartitions with equal sum:\n");
95+
partitons(curr, 0, N);
96+
return 0;
97+
}

0 commit comments

Comments
 (0)