Browse Source

initial commit

master
dimakuv 6 years ago
commit
b84420fdf1
  1. 97
      c_sequential/3sat/3sat.c
  2. 19
      c_sequential/3sat/Makefile
  3. 20
      c_sequential/bucketsort/Makefile
  4. 54
      c_sequential/bucketsort/bucketsort.c
  5. 6
      c_sequential/bucketsort/bucketsort.h
  6. 65
      c_sequential/bucketsort/main.c
  7. 20
      c_sequential/friendly/Makefile
  8. 69
      c_sequential/friendly/friendly.c
  9. 27
      c_sequential/haar/Makefile
  10. 127
      c_sequential/haar/haar.c
  11. 54
      c_sequential/haar/input_generator.c
  12. 20
      c_sequential/knapsack/Makefile
  13. 67
      c_sequential/knapsack/knapsack.c
  14. BIN
      problemset.pdf

97
c_sequential/3sat/3sat.c

@ -0,0 +1,97 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// Solves the 3-SAT system using an exaustive search
// It finds all the possible values for the set of variables using
// a number. The binary representation of this number represents
// the values of the variables. Since a long variable has 64 bits,
// this implementations works with problems with up to 64 variables.
long solveClauses(short **clauses, int nClauses, int nVar) {
long *iVar = (long *) malloc(nVar * sizeof(long));
int i;
for (i = 0; i < nVar; i++)
iVar[i] = exp2(i);
unsigned long maxNumber = exp2(nVar);
long number;
short var;
int c;
for (number = 0; number < maxNumber; number++) {
for (c = 0; c < nClauses; c++) {
var = clauses[0][c];
if (var > 0 && (number & iVar[var - 1]) > 0)
continue; // clause is true
else if (var < 0 && (number & iVar[-var - 1]) == 0)
continue; // clause is true
var = clauses[1][c];
if (var > 0 && (number & iVar[var - 1]) > 0)
continue; // clause is true
else if (var < 0 && (number & iVar[-var - 1]) == 0)
continue; // clause is true
var = clauses[2][c];
if (var > 0 && (number & iVar[var - 1]) > 0)
continue; // clause is true
else if (var < 0 && (number & iVar[-var - 1]) == 0)
continue; // clause is true
break; // clause is false
}
if (c == nClauses)
return number;
}
return -1;
}
// Read nClauses clauses of size 3. nVar represents the number of variables
// Clause[0][i], Clause[1][i] and Clause[2][i] contains the 3 elements of the i-esime clause.
// Each element of the caluse vector may contain values selected from:
// k = -nVar, ..., -2, -1, 1, 2, ..., nVar. The value of k represents the index of the variable.
// A negative value remains the negation of the variable.
short **readClauses(int nClauses, int nVar) {
short **clauses = (short **) malloc(3 * sizeof(short *));
clauses[0] = (short *) malloc(nClauses * sizeof(short));
clauses[1] = (short *) malloc(nClauses * sizeof(short));
clauses[2] = (short *) malloc(nClauses * sizeof(short));
int i;
for (i = 0; i < nClauses; i++) {
scanf("%hd %hd %hd", &clauses[0][i], &clauses[1][i], &clauses[2][i]);
}
return clauses;
}
int main(int argc, char *argv[]) {
int nClauses;
int nVar;
scanf("%d %d", &nClauses, &nVar);
short **clauses = readClauses(nClauses, nVar);
long solution = solveClauses(clauses, nClauses, nVar);
int i;
if (solution >= 0) {
printf("Solution found [%ld]: ", solution);
for (i = 0; i < nVar; i++)
printf("%d ", (int) ((solution & (long) exp2(i)) / exp2(i)));
printf("\n");
} else
printf("Solution not found.\n");
return EXIT_SUCCESS;
}

19
c_sequential/3sat/Makefile

@ -0,0 +1,19 @@
CC = gcc
FLAGS = -O3 -Wall -Wno-unused-result
SRC = 3sat.c
EXEC = bin/3sat
all: make_dirs $(EXEC)
clean:
rm -f -r bin
make_dirs:
mkdir -p bin
$(EXEC): $(SRC)
$(CC) $(FLAGS) $^ -o $@ -lm
run:
./$(EXEC) < input/small1.in

20
c_sequential/bucketsort/Makefile

@ -0,0 +1,20 @@
CC = gcc
FLAGS = -O3 -Wall -Wno-unused-result
SRC = bucketsort.c main.c
EXEC = bin/bucketsort
all: make_dirs $(EXEC)
clean:
rm -f -r bin
make_dirs:
mkdir -p bin
$(EXEC): $(SRC)
$(CC) $(FLAGS) $^ -o $@
run:
./$(EXEC) input/medium.in input/medium.out

54
c_sequential/bucketsort/bucketsort.c

@ -0,0 +1,54 @@
#include <stdlib.h>
#include <string.h>
#include "bucketsort.h"
#define N_BUCKETS 94
typedef struct {
long int *data;
int length;
long int total;
} bucket;
void sort(char *a, bucket *bucket) {
int j, i, length;
long int key;
length = bucket->length;
for (j = 1; j < bucket->total; j++) {
key = bucket->data[j];
i = j - 1;
while (i >= 0
&& strcmp(a + bucket->data[i] * length, a + key * length) > 0) {
bucket->data[i + 1] = bucket->data[i];
i--;
}
bucket->data[i + 1] = key;
}
}
long int* bucket_sort(char *a, int length, long int size) {
long int i;
bucket buckets[N_BUCKETS], *b;
long int *returns;
// allocate memory
returns = malloc(sizeof(long int) * size);
for (i = 0; i < N_BUCKETS; i++) {
buckets[i].data = returns + i * size / N_BUCKETS;
buckets[i].length = length;
buckets[i].total = 0;
}
// copy the keys to "buckets"
for (i = 0; i < size; i++) {
b = &buckets[*(a + i * length) - 0x21];
b->data[b->total++] = i;
}
// sort each "bucket"
for (i = 0; i < N_BUCKETS; i++)
sort(a, &buckets[i]);
return returns;
}

6
c_sequential/bucketsort/bucketsort.h

@ -0,0 +1,6 @@
#ifndef BUCKETSORT_H_
#define BUCKETSORT_H_
long int* bucket_sort(char *, int, long int);
#endif /* BUCKETSORT_H_ */

65
c_sequential/bucketsort/main.c

@ -0,0 +1,65 @@
#include <stdio.h>
#include <stdlib.h>
#include "bucketsort.h"
#define LENGTH 8
FILE *fin, *fout;
char *strings;
long int N;
void openfiles(char* in, char* out) {
fin = fopen(in, "r+");
if (fin == NULL) {
perror("fopen fin");
exit(EXIT_FAILURE);
}
fout = fopen(out, "w");
if (fout == NULL) {
perror("fopen fout");
exit(EXIT_FAILURE);
}
}
void closefiles(void) {
fclose(fin);
fclose(fout);
}
int main(int argc, char* argv[]) {
long int i, *r;
if (argc != 3) {
printf("usage: %s [input_file] [output_file]\n", argv[0]);
return 0;
}
openfiles(argv[1], argv[2]);
fscanf(fin, "%ld", &N);
strings = (char*) malloc(N * LENGTH);
if (strings == NULL) {
perror("malloc strings");
exit(EXIT_FAILURE);
}
for (i = 0; i < N; i++)
fscanf(fin, "%s", strings + (i * LENGTH));
fflush(stdout);
r = bucket_sort(strings, LENGTH, N);
fflush(stdout);
for (i = 0; i < N; i++)
fprintf(fout, "%s\n", strings + (r[i] * LENGTH));
free(r);
free(strings);
closefiles();
return EXIT_SUCCESS;
}

20
c_sequential/friendly/Makefile

@ -0,0 +1,20 @@
CC = gcc
FLAGS = -O3 -Wall -Wno-unused-result
SRC = friendly.c
EXEC = bin/friendly
all: make_dirs $(EXEC)
clean:
rm -f -r bin
make_dirs:
mkdir -p bin
$(EXEC): $(SRC)
$(CC) $(FLAGS) $^ -o $@
run:
./$(EXEC) < input/small.in

69
c_sequential/friendly/friendly.c

@ -0,0 +1,69 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int gcd(int u, int v) {
if (v == 0)
return u;
return gcd(v, u % v);
}
void friendly_numbers(long int start, long int end) {
long int last = end - start + 1;
long int *the_num;
the_num = (long int*) malloc(sizeof(long int) * last);
long int *num;
num = (long int*) malloc(sizeof(long int) * last);
long int *den;
den = (long int*) malloc(sizeof(long int) * last);
long int i, j, factor, ii, sum, done, n;
for (i = start; i <= end; i++) {
ii = i - start;
sum = 1 + i;
the_num[ii] = i;
done = i;
factor = 2;
while (factor < done) {
if ((i % factor) == 0) {
sum += (factor + (i / factor));
if ((done = i / factor) == factor)
sum -= factor;
}
factor++;
}
num[ii] = sum;
den[ii] = i;
n = gcd(num[ii], den[ii]);
num[ii] /= n;
den[ii] /= n;
} // end for
for (i = 0; i < last; i++) {
for (j = i + 1; j < last; j++) {
if ((num[i] == num[j]) && (den[i] == den[j]))
printf("%ld and %ld are FRIENDLY\n", the_num[i], the_num[j]);
}
}
free(the_num);
free(num);
free(den);
}
int main(int argc, char **argv) {
long int start;
long int end;
while (1) {
scanf("%ld %ld", &start, &end);
if (start == 0 && end == 0)
break;
printf("Number %ld to %ld\n", start, end);
friendly_numbers(start, end);
}
return EXIT_SUCCESS;
}

27
c_sequential/haar/Makefile

@ -0,0 +1,27 @@
CC = gcc
FLAGS = -O3 -Wall -Wno-unused-result
SRC = haar.c
EXEC = bin/haar
GEN = bin/input_generator
all: make_dirs $(EXEC) $(GEN)
clean:
rm -f -r bin
rm -f -r input
make_dirs:
mkdir -p bin
mkdir -p input
$(EXEC): $(SRC)
$(CC) $(FLAGS) $^ -o $@
$(GEN): input_generator.c
$(CC) $(FLAGS) $^ -o $@
run:
./$(GEN) input/medium.in
./$(EXEC) input/medium.in input/medium.out

127
c_sequential/haar/haar.c

@ -0,0 +1,127 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <stdint.h>
#define pixel(x,y) pixels[((y)*size)+(x)]
//#define DEBUG
void print(int *pixels, int size) {
int x, y;
for (y = 0; y < size; y++) {
for (x = 0; x < size; x++) {
printf("%10d ", pixel(x,y));
}
printf("\n");
fflush(stdout);
}
}
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("usage: %s [input_file] [output_file]\n", argv[0]);
return 0;
}
FILE *in;
FILE *out;
clock_t start;
clock_t end;
in = fopen(argv[1], "rb");
if (in == NULL) {
perror(argv[1]);
exit(EXIT_FAILURE);
}
out = fopen(argv[2], "wb");
if (out == NULL) {
perror(argv[2]);
exit(EXIT_FAILURE);
}
long long int s, size, mid;
int x, y;
long long int a, d;
double SQRT_2;
fread(&size, sizeof(size), 1, in);
fwrite(&size, sizeof(size), 1, out);
int *pixels = (int *) malloc(size * size * sizeof(int));
start = clock();
if (!fread(pixels, size * size * sizeof(int), 1, in)) {
perror("read error");
exit(EXIT_FAILURE);
}
end = clock();
printf("time to read: %ju (%f s)\n", (uintmax_t)(end - start), (float)(end - start) / CLOCKS_PER_SEC);
#ifdef DEBUG
printf("origin: %lld\n", size);
print(pixels, size);
#endif
// haar
start = clock();
SQRT_2 = sqrt(2);
for (s = size; s > 1; s /= 2) {
mid = s / 2;
// row-transformation
for (y = 0; y < mid; y++) {
for (x = 0; x < mid; x++) {
a = pixel(x,y);
a = (a+pixel(mid+x,y))/SQRT_2;
d = pixel(x,y);
d = (d-pixel(mid+x,y))/SQRT_2;
pixel(x,y) = a;
pixel(mid+x,y) = d;
}
}
#ifdef DEBUG
printf("after row-transformation: %lld\n", mid);
print(pixels, size);
#endif
// column-transformation
for (y = 0; y < mid; y++) {
for (x = 0; x < mid; x++) {
a = pixel(x,y);
a = (a+pixel(x,mid+y))/SQRT_2;
d = pixel(x,y);
d = (d-pixel(x,mid+y))/SQRT_2;
pixel(x,y) = a;
pixel(x,mid+y) = d;
}
}
#ifdef DEBUG
printf("after column-transformation: %lld\n", mid);
print(pixels, size);
#endif
}
end = clock();
printf("time to haar: %ju (%f s)\n", (uintmax_t)(end - start), (float)(end - start) / CLOCKS_PER_SEC);
start = clock();
fwrite(pixels, size * size * sizeof(int), 1, out);
end = clock();
printf("time to write: %ju (%f s)\n", (uintmax_t)(end - start), (float)(end - start) / CLOCKS_PER_SEC);
free(pixels);
fclose(out);
fclose(in);
return EXIT_SUCCESS;
}

54
c_sequential/haar/input_generator.c

@ -0,0 +1,54 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
// change size to make image bigger/smaller
#define IMGSIZE 0x3000 // 0x6000
#define image(x,y) pixels[((unsigned int)y*size+x)]
// generates an image
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("usage: %s [create_file]", argv[0]);
return 0;
}
unsigned long long int size;
unsigned long long int x, y;
FILE *out;
srand(time(NULL));
out = fopen(argv[1], "wb");
if (out == NULL ) {
perror(argv[1]);
exit(EXIT_FAILURE);
}
size = IMGSIZE;
printf("%lld\n", size);
fwrite(&size, sizeof(size), 1, out);
fflush(out);
int *pixels = (int*) malloc(size * size * sizeof(int));
if (pixels == NULL ) {
perror("malloc pixels");
exit(EXIT_FAILURE);
}
for (y = 0; y < size; y++) {
for (x = 0; x < size; x++) {
image(x,y)= (rand()&0xFFFF);
}
}
fwrite(pixels, size * size * sizeof(int), 1, out);
fflush(out);
fclose(out);
free(pixels);
return EXIT_SUCCESS;
}

20
c_sequential/knapsack/Makefile

@ -0,0 +1,20 @@
CC = gcc
FLAGS = -O3 -Wall -Wno-unused-result
SRC = knapsack.c
EXEC = bin/knapsack
all: make_dirs $(EXEC)
clean:
rm -f -r bin
make_dirs:
mkdir -p bin
$(EXEC): $(SRC)
$(CC) $(FLAGS) $^ -o $@
run:
./$(EXEC) < input/small.in

67
c_sequential/knapsack/knapsack.c

@ -0,0 +1,67 @@
/* @author St\'efano Drimon Kurz M\'or */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _item_t {
int value; // v_i
int weight; // w_i
float density; // v_i/w_i
} item_t;
int greater_f(const int x, const int y);
int compare_f(const void *x, const void *y);
int knapsack_f(const int n, const int M, const item_t * const itens) {
int v = 0, w = 0;
int r = 0;
if (n < 1)
return 0;
while (M - w >= 0) {
r = greater_f(r, v + knapsack_f(n - 1, M - w, &(itens[1])));
v += itens[0].value;
w += itens[0].weight;
}
return r;
}
int main(int argc, const char *argv[]) {
int n;
int M;
item_t *itens;
int i;
scanf("%d %d", &n, &M);
itens = (item_t*) calloc(n, sizeof(item_t));
for (i = 0; i < n; ++i) {
scanf("%d %d", &(itens[i].value), &(itens[i].weight));
itens[i].density = (float) (itens[i].value) / itens[i].weight;
}
qsort(itens, (size_t) n, sizeof(item_t), compare_f);
printf("%d\n", knapsack_f(n, M, itens));
free(itens);
return 0;
}
int greater_f(const int x, const int y) {
return (x > y) ? x : y;
}
int compare_f(const void *x, const void *y) {
return ((item_t*) x)->density - ((item_t*) y)->density;
}

BIN
problemset.pdf

Loading…
Cancel
Save