summaryrefslogtreecommitdiffstats
path: root/src/common.c
diff options
context:
space:
mode:
authorAlexandre Jesus <adbjesus@gmail.com>2016-09-22 02:02:20 +0100
committerAlexandre Jesus <adbjesus@gmail.com>2016-09-22 02:02:20 +0100
commite7fcca35efae60ca2e24225b046ab4e9a801b031 (patch)
tree3b1fcc72ee7ecce4a5be414b7cfae3ac6c2f2c2d /src/common.c
parentd3878228d84dd2296cd85d914f27087d3cae2835 (diff)
downloadlibuknapsack-e7fcca35efae60ca2e24225b046ab4e9a801b031.tar.gz
libuknapsack-e7fcca35efae60ca2e24225b046ab4e9a801b031.zip
Create tree structure, adapt dfs to use tree (with iterative technique), fix random sort
Diffstat (limited to 'src/common.c')
-rw-r--r--src/common.c18
1 files changed, 13 insertions, 5 deletions
diff --git a/src/common.c b/src/common.c
index ee98bba..7a8b316 100644
--- a/src/common.c
+++ b/src/common.c
@@ -5,8 +5,6 @@
#include "structs.h"
#include "common.h"
-#define RAND_MAX 1
-
struct data * input(char fp[]){
int i,j;
FILE *f;
@@ -39,9 +37,7 @@ int cmp_items_ratio(const void * a, const void * b){
}
int cmp_items_random(const void *a, const void *b) {
- struct item c = *(struct item *)a;
- struct item d = *(struct item *)b;
- if(rand() > 0.5){
+ if(rand() % 2 == 0){
return 1;
}
return -1;
@@ -94,6 +90,18 @@ struct front_item * new_front_item(double p, double w, struct front_item * prev,
return f;
}
+struct tree_item * new_tree_item(long int depth, long int itemi, double w, double p, struct tree_item * prev, struct tree_item * next) {
+ struct tree_item * t = (struct tree_item *)malloc(sizeof(struct tree_item));
+ t->depth = depth;
+ t->itemi = itemi;
+ t->values.w = w;
+ t->values.p = p;
+ t->prev = prev;
+ t->next = next;
+
+ return t;
+}
+
double dist_items(struct item * a, struct item * b){
return sqrt((a->p - b->p)*(a->p - b->p) + (a->w - b->w)*(a->w - b->w));
}