summaryrefslogtreecommitdiffstats
path: root/01-knapsack/ks_dp.c
blob: 1ba27b9afe4fec7702d4f708a27f5bbce44e3445 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

static int debug = 0;

typedef uint32_t word_t;
enum { WORD_BITS = sizeof(word_t) * 8 };

static inline int bindex(int b) { return b / WORD_BITS; }
static inline int boffset(int b) { return b % WORD_BITS; }

static inline void set_bit(word_t *bits, int b)
{
   bits[bindex(b)] |= (1 << (boffset(b)));
}

static inline int get_bit(word_t *bits, int b)
{
   return ((bits[bindex(b)] & (1 << (boffset(b)))) >> (boffset(b)));
}

typedef struct _SolverData {
     int v;
     word_t *bits;
} SolverData;

typedef struct _Solver
{
   int k;               // capacity
   int n;               // items #
   int *values;         // item values
   int *weights;        // item weights
   int nw;              // # bytes per bits array
   size_t sd_sz;        // solver data size
   SolverData *data;    // solver data
} Solver;

static void solve(Solver* solver)
{
   int n, k, nw;
   size_t sd_sz;
   int *values, *weights;

   int i, j;
   int v, w;
   char *data_base;
   SolverData *c, *p;

   sd_sz = solver->sd_sz;
   nw = solver->nw;
   n = solver->n;
   k = solver->k;
   values = solver->values;
   weights = solver->weights;
   data_base = (char *) solver->data;

   /* SOLVE */
   for (i = 0; i < n; i++)
     {
        v = values[i];
        w = weights[i];
        c = (SolverData *) (data_base + (sd_sz * k));
        p = (SolverData *) (data_base + (sd_sz * (k - w)));

        for (j = k; j > 0; j--)
          {
             if (j < w)
               break;
             if ((j >= w) && (c->v < (v + p->v)))
               {
                  c->v = v + p->v;
                  memcpy(c->bits, p->bits, (nw * sizeof(word_t)));
                  set_bit(c->bits, i);
               }
             c = (SolverData *) (((char *) c) - sd_sz);
             p = (SolverData *) (((char *) p) - sd_sz);
          }

        if (debug)
          {
             printf("i=% 4d : ", i);
             for (j = 0; j <= k; j++)
               {
                  c = (SolverData *) (data_base + (sd_sz * j));
                  printf("% 4d ", c->v);
               }
             printf("\n");
          }
     }
}

static void print(Solver* solver)
{
   int b;
   int i;
   int v, w;
   SolverData *sol;

   v = 0;
   w = 0;
   sol = (SolverData *) (((char *) solver->data) + (solver->sd_sz * solver->k));

   printf("%d %d\n", sol->v, 1);
   for (i = 0; i < solver->n; i++)
     {
        b = get_bit(sol->bits, i);
        printf("%d ", b);
        if (b)
          {
             v += solver->values[i];
             w += solver->weights[i];
          }
     }
   printf("\n");

   if (v != sol->v)
     fprintf(stderr, "ERROR: value %d != %d\n", v, sol->v);
   if (w > solver->k)
     fprintf(stderr, "ERROR: weight %d > %d\n", w, solver->k);
}

int main(int argc, char** argv, char** env)
{
   FILE *fp;
   Solver solver;    // solver

   int i;
   int *vp, *wp;
   SolverData *data;

   if(argc < 2)
     {
        fprintf(stderr,"input file missing\n");
        return EXIT_FAILURE;
     }

   if (debug) printf("%s read %s\n", argv[0], argv[1]);
   fp = fopen(argv[1], "r");
   if (fp == NULL)
     {
        fprintf(stderr, "I couldn't open results.dat for reading.\n");
        exit(EXIT_FAILURE);
     }

   /* read k and n */
   if (fscanf(fp, "%d %d\n", &solver.n, &solver.k) != 2)
     {
        fprintf(stderr, "ERROR: read first line\n");
        return EXIT_FAILURE;
     }
   if (debug) printf("  K:%d N:%d\n", solver.k, solver.n);

   /* allocate */
   solver.values = calloc(solver.n, sizeof(int));
   if (!solver.values)
     {
        fprintf(stderr, "ERROR: values calloc\n");
        return EXIT_FAILURE;
     }
   vp = solver.values;

   solver.weights = calloc(solver.n, sizeof(int));
   if (!solver.weights)
     {
        free(solver.values);
        fprintf(stderr, "ERROR: weights calloc\n");
        return EXIT_FAILURE;
     }
   wp = solver.weights;

   solver.nw = (solver.n / WORD_BITS + 1);
   solver.sd_sz = sizeof(SolverData) + (solver.nw * sizeof(word_t));
   solver.data= calloc((solver.k + 1), solver.sd_sz);
   if (!solver.data)
     {
        free(solver.values);
        free(solver.weights);
        fprintf(stderr, "ERROR: solver calloc\n");
        return EXIT_FAILURE;
     }
   for (i = 0; i <= solver.k; i++)
     {
        data = (SolverData *) (((char *)  solver.data) + solver.sd_sz * i);
        data->bits = (word_t *) (((char *) data) + sizeof(SolverData));
     }

   /* read items */
   for (i = 0; i < solver.n; i++)
     fscanf(fp, "%d %d\n", vp++, wp++);
   fclose(fp);

   if (debug)
     {
        printf("     index    value   weight\n");
        for (i = 0; i < solver.n; i++)
          printf("  % 8d % 8d % 8d\n", i, solver.values[i], solver.weights[i]);
     }

   solve(&solver);
   print(&solver);

   free(solver.values);
   free(solver.weights);

   return EXIT_SUCCESS;
}