This repository has been archived by the owner on Mar 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc67b3d
commit a80183b
Showing
12 changed files
with
231 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* This library is for handling distances calculations and related manners. | ||
**/ | ||
|
||
#ifndef _DISTS_H_ | ||
#define _DISTS_H_ | ||
|
||
#include <math.h> | ||
|
||
#include "./data.h" | ||
|
||
// "Rows" | ||
typedef struct distanceSample_t { | ||
char* id; | ||
long double distance; | ||
// On distanceSet | ||
sample_t *from, *to; | ||
} distanceSample_t; | ||
|
||
// "Lines" of rows | ||
typedef struct distanceDataSet_t { | ||
size_t nElements; | ||
size_t depth; | ||
distanceSample_t* samples; | ||
} distanceDataSet_t; | ||
|
||
distanceDataSet_t* calculateDistances(dataSet_t* points); | ||
void printDistanceSet(distanceDataSet_t* dataSet); | ||
void destroyDistanceDataSet(distanceDataSet_t* dataSet); | ||
|
||
#endif |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* This library is for handling dataSets as in they were a graph. Any graph algorithm should be here | ||
**/ | ||
|
||
#ifndef _GRAPH_H_ | ||
#define _GRAPH_H_ | ||
|
||
#define _GNU_SOURCE // Needed to use qsort_r when not using c11 or gnu89/gnu99 | ||
|
||
#include "./data.h" | ||
#include "./unionFind.h" | ||
|
||
union_t* kruskal(distanceDataSet_t* dataSet, size_t groupsNumber); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include "../lib/distances.h" | ||
|
||
distanceDataSet_t* initDistanceDataSet(size_t nElements) { | ||
distanceDataSet_t* dataSet = (distanceDataSet_t*)malloc(sizeof(distanceDataSet_t)); | ||
if (dataSet == NULL) { | ||
perror("Error allocating new dataSet. Exiting"); | ||
exit(1); | ||
}; | ||
dataSet->depth = nElements; | ||
dataSet->nElements = (nElements / 2) * (nElements - 1); | ||
dataSet->samples = (distanceSample_t*)malloc(sizeof(distanceSample_t) * dataSet->nElements); | ||
if (dataSet->samples == NULL) { | ||
perror("Error allocating new samples. Exiting"); | ||
exit(1); | ||
} | ||
return dataSet; | ||
} | ||
|
||
long double euclidianDistance(long double* a, long double* b, size_t* nFeatures) { | ||
long double accumulator = 0; | ||
|
||
// SUM(abs(a - b)^2) | ||
for (size_t i = 0; i < *nFeatures; i++) { | ||
// printf("[dim = %ld Val a = %Lf b = %Lf]", *nFeatures, a[i], b[i]); | ||
|
||
if (a[i] < b[i]) { | ||
accumulator += (b[i] - a[i]) * (b[i] - a[i]); | ||
} else { | ||
accumulator += (a[i] - b[i]) * (a[i] - b[i]); | ||
} | ||
} | ||
long double dist = sqrtl(accumulator); | ||
// printf(" Dist: %Lf\n", dist); | ||
return dist; | ||
} | ||
|
||
distanceDataSet_t* calculateDistances(dataSet_t* locationSet) { | ||
distanceDataSet_t* distanceSet = initDistanceDataSet(locationSet->nElements); | ||
|
||
for (size_t i = 0, count = 0; i < locationSet->nElements; i++) { | ||
for (size_t j = 0; j < i; j++, count++) { | ||
distanceSet->samples[count].from = &locationSet->samples[i]; | ||
distanceSet->samples[count].to = &locationSet->samples[j]; | ||
printf("Calculating distance from %ld to %ld, which are %s and %s\n", i, j, locationSet->samples[i].id, locationSet->samples[j].id); | ||
distanceSet->samples[count].distance = euclidianDistance(locationSet->samples[i].features, locationSet->samples[j].features, &locationSet->nFeatures); | ||
} | ||
} | ||
return distanceSet; | ||
} | ||
|
||
void printDistanceSet(distanceDataSet_t* dataSet) { | ||
for (size_t i = 0, k = 0; i < dataSet->depth; i++) { | ||
printf("%s:", dataSet->samples[k].from->id); | ||
for (size_t j = 0; j < i; j++, k++) { | ||
printf("\t%Lf", dataSet->samples[k].distance); | ||
} | ||
puts(""); | ||
} | ||
} | ||
|
||
void destroyDistanceDataSet(distanceDataSet_t* dataSet) { | ||
free(dataSet->samples); | ||
free(dataSet); | ||
dataSet = NULL; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include "../lib/graph.h" | ||
|
||
int compareDistanceSamples(const void* a, const void* b) { | ||
if (((distanceSample_t*)a)->distance < ((distanceSample_t*)b)->distance) return -1; | ||
if (((distanceSample_t*)a)->distance > ((distanceSample_t*)b)->distance) return 1; | ||
return 0; | ||
} | ||
|
||
union_t* kruskal(distanceDataSet_t* dataSet, size_t groupsNumber) { | ||
// Qsorting dataSet, Kruskal needs a sorted set | ||
qsort(dataSet->samples, dataSet->nElements, sizeof(distanceSample_t), &compareDistanceSamples); | ||
|
||
|
||
union_t* un = UF_init(dataSet->depth, dataSet->samples); | ||
size_t currentGroups = dataSet->depth; | ||
|
||
// Not executing K times, don't even need to remove later | ||
// Sice it's sorted, it's on the 3 largest distances | ||
for (size_t i = 0; currentGroups != groupsNumber; i++) { | ||
size_t p = dataSet->samples[i].from->index, q = dataSet->samples[i].to->index; | ||
if (UF_union(un, p, q) == 1) { | ||
currentGroups--; | ||
} | ||
} | ||
|
||
// puts("Final"); | ||
// for (size_t i = 0; i < dataSet->depth; i++) { | ||
// printf("%ld ", i); | ||
// } | ||
// puts(""); | ||
// for (size_t i = 0; i < dataSet->depth; i++) { | ||
// printf("%ld ", UF_find(un, i)); | ||
// } | ||
// puts(""); | ||
// for (size_t i = 0; i < dataSet->depth; i++) { | ||
// printf("%s ", un->sample[i].from->id); | ||
// } | ||
// puts(""); | ||
|
||
return un; | ||
} | ||
|
||
sample_t** sortMST(union_t* MST) { | ||
return NULL | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.