Skip to content

Commit

Permalink
Merge pull request #36 from BillyMich/sampling
Browse files Browse the repository at this point in the history
Sampling merge knn version
  • Loading branch information
BillyMich authored Nov 30, 2023
2 parents 778dbc1 + b3baee2 commit 27437e6
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 36 deletions.
20 changes: 0 additions & 20 deletions .github/workflows/ci.yml

This file was deleted.

Binary file added datasets/00020000-1.bin
Binary file not shown.
Binary file added datasets/00050000-3.bin
Binary file not shown.
7 changes: 4 additions & 3 deletions include/knn_improvments.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@

#include "../include/knn.h"

void knn_improved_algorithm(Graph** graph, int K, String distance_function);
void knn_improved_algorithm(Graph** graph, int K, String distance_function, double p);

void changeNeighbors(Graph** graph);

void localJoin(Node** node, String distance_function);
void localJoin(Node** node, String distance_function, int pK);

int incrementalSearch(NodeNeighborsLinkedList* neighbor1, NodeNeighborsLinkedList* neighbor2);

//sampling
NodeNeighborsLinkedList* sampling(NodeNeighborsLinkedList* neighbors, int pK);

//earlyTermination

#endif
4 changes: 2 additions & 2 deletions main/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ OBJS = main.o $(MODULES)/Graph.o $(MODULES)/Node.o $(MODULES)/Neighbors.o $(MODU
EXEC = program

# Args for examples
ARGSEUCL = $(DATASETS)/00001000-4.bin 60 399 euclidean
ARGSEUCL = $(DATASETS)/00000020.bin 10 65 euclidean 0.8

ARGSMAN = $(DATASETS)/00001000-4.bin 60 399 manhattan
ARGSMAN = $(DATASETS)/00001000-4.bin 100 399 manhattan 0.1

$(EXEC): $(OBJS)
$(CC) $(OBJS) -o $(EXEC) $(LDFLAGS)
Expand Down
3 changes: 2 additions & 1 deletion main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ int main(int argc, char *argv[]) {
int dimensions = atoi(argv[2]);
int K = atoi(argv[3]);
String distance_function = argv[4];
double p = atof(argv[5]);

// printf("Starting Creating Graph\n");

Expand All @@ -22,7 +23,7 @@ int main(int argc, char *argv[]) {
clock_t knn_start, knn_end;
knn_start = clock();

knn_improved_algorithm(&graph, K, distance_function);
knn_improved_algorithm(&graph, K, distance_function, p);
// knn_algorithm(&graph, K, distance_function);

knn_end = clock();
Expand Down
2 changes: 2 additions & 0 deletions modules/knn.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ void KRandomNodes(Graph** graph, int K, String distance_function) {

Node* currentNode = (*graph)->nodes;
int numNodes = (*graph)->numNodes;
printf("The Nodes are %d\n", numNodes);

if(K > numNodes){
fprintf(stderr, "Too many Neighbors. The Nodes are %d\n", numNodes);
exit(EXIT_FAILURE);
Expand Down
57 changes: 49 additions & 8 deletions modules/knn_improvements.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ static int changes;
/// @param graph
/// @param K
/// @param distance_function
void knn_improved_algorithm(Graph** graph, int K, String distance_function){
void knn_improved_algorithm(Graph** graph, int K, String distance_function, double p){

KRandomNodes(graph, K, distance_function);

Node * tempNode = (*graph)->nodes;
Node * tempNode = (*graph)->nodes;

int pK = p*K;
printf("The pK (neighbor) nodes for Sampling is %d\n", pK);

do {
changes = 0;
while (tempNode !=NULL) {
localJoin(&tempNode, distance_function);
while (tempNode != NULL) {
localJoin(&tempNode, distance_function, pK);
tempNode = tempNode->next;
}

Expand All @@ -33,7 +36,7 @@ void knn_improved_algorithm(Graph** graph, int K, String distance_function){
}
tempNode = (*graph)->nodes;

printf("---- changes: %d\n", changes);
printf("Changes: %d\n", changes);
} while (changes>0);

}
Expand Down Expand Up @@ -87,10 +90,16 @@ void changeNeighbors(Graph** graph) {
}
}

//functions for improvements

void localJoin(Node** node, String distance_function, int pK) {
// without sampling, we use the neighbours of the Node
// NodeNeighborsLinkedList* temp = (*node)->neighbors;

// With the sampling, we create a new list of the pK neighbors (that have the flag True)
NodeNeighborsLinkedList* head = sampling((*node)->neighbors, pK);
NodeNeighborsLinkedList* temp = head;

void localJoin(Node** node, String distance_function) {
NodeNeighborsLinkedList* temp = (*node)->neighbors;
while(temp != NULL) {
NodeNeighborsLinkedList* tempNeig = (*node)->neighbors;
while(tempNeig != NULL) {
Expand Down Expand Up @@ -124,6 +133,8 @@ void localJoin(Node** node, String distance_function) {
}
temp = temp->next;
}
// free the list of the Neighbours with true flags
freeNeighbors(head);
}


Expand All @@ -142,4 +153,34 @@ int incrementalSearch(NodeNeighborsLinkedList* neighbor1, NodeNeighborsLinkedLis


//sampling
//earlyTermination
NodeNeighborsLinkedList* sampling(NodeNeighborsLinkedList* neighbors, int pK) {
NodeNeighborsLinkedList* tempNeighbor = neighbors;
NodeNeighborsLinkedList* samplingNeighborsHead = NULL;

while (tempNeighbor != NULL && pK > 0) {
if (tempNeighbor->flag == 1) {
addNeighbor(&samplingNeighborsHead, tempNeighbor->node, tempNeighbor->cost); //free after use
pK--;
}
tempNeighbor = tempNeighbor->next;
}
return samplingNeighborsHead;

//if the above doesnt work

// int trueNeighbors[pK]; //maybe malloc in order to return it and free after use
// for (int i = 0; i < pK; i++) {
// trueNeighbors[i] = -1;
// }

// int count = 0;

// while (tempNeighbor != NULL && count < pK) {
// if(tempNeighbor->flag == 1){
// trueNeighbors[count] = tempNeighbor->node->nodeNameInt;
// count++;
// }
// tempNeighbor = tempNeighbor->next;
// }
// return trueNeighbors;
}
17 changes: 15 additions & 2 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ CFLAGS = -Wall -Werror -g -I$(INCLUDE)
LDFLAGS = -lm

# .o files
OBJSKNN_IMP = $(TESTS)/knn_improvements_test.o $(MODULES)/Cost.o $(MODULES)/knn_improvements.o $(MODULES)/Graph.o $(MODULES)/Node.o $(MODULES)/Neighbors.o $(MODULES)/Dimension.o $(MODULES)/MathematicalFunctions.o

OBJSKNN = $(TESTS)/knn_test.o $(MODULES)/Graph.o $(MODULES)/Node.o $(MODULES)/Neighbors.o $(MODULES)/Dimension.o $(MODULES)/MathematicalFunctions.o $(MODULES)/knn.o

OBJSG = $(TESTS)/graph_test.o $(MODULES)/Graph.o $(MODULES)/Node.o $(MODULES)/Neighbors.o $(MODULES)/Dimension.o $(MODULES)/MathematicalFunctions.o $(MODULES)/knn.o
Expand All @@ -27,6 +29,8 @@ OBJSMATH = $(TESTS)/mathematical_test.o $(MODULES)/MathematicalFunctions.o $(MOD
OBJSK = $(TESTS)/knn_test.o $(MODULES)/Graph.o $(MODULES)/Node.o $(MODULES)/Neighbors.o $(MODULES)/Dimension.o $(MODULES)/MathematicalFunctions.o $(MODULES)/FindAllRightNeighborsAlgorithm.o $(MODULES)/knn.o

# Executables
EXECKNN_IMP = knn_improvements_test

EXECKNN = knn_test

EXECG = graph_test
Expand All @@ -39,6 +43,9 @@ EXECNEI = neighbours_test

EXECMATH = mathFunctions_test

$(EXECKNN_IMP): $(OBJSKNN_IMP)
$(CC) $(OBJSKNN_IMP) -o $(EXECKNN_IMP) $(LDFLAGS)

$(EXECKNN): $(OBJSKNN)
$(CC) $(OBJSKNN) -o $(EXECKNN) $(LDFLAGS)

Expand All @@ -58,7 +65,10 @@ $(EXECMATH): $(OBJSMATH)
$(CC) $(OBJSMATH) -o $(EXECMATH) $(LDFLAGS)

clean:
rm -f $(OBJSG) $(OBJSN) $(OBJSD) $(OBJSKNN) $(OBJSMATH) $(OBJSNEI) $(EXECG) $(EXECN) $(EXECD) $(EXECKNN) $(EXECNEI) $(EXECMATH)
rm -f $(OBJSG) $(OBJSN) $(OBJSD) $(OBJSKNN) $(OBJSKNN_IMP) $(OBJSMATH) $(OBJSNEI) $(EXECG) $(EXECN) $(EXECD) $(EXECKNN) $(EXECNEI) $(EXECMATH) $(EXECKNN_IMP)

runKNN_imp: $(EXECKNN_IMP)
./$(EXECKNN_IMP)

runKNN: $(EXECKNN)
./$(EXECKNN)
Expand All @@ -78,7 +88,7 @@ runNEI: $(EXECNEI)
runMath: $(EXECMATH)
./$(EXECMATH)

run-all: runKNN runG runN runD runNEI runMath
run-all: runMath runG runN runD runNEI runKNN runKNN_imp

valgrind-graph: $(EXECG)
valgrind --error-exitcode=1 --leak-check=full --show-leak-kinds=all ./$(EXECG)
Expand All @@ -94,3 +104,6 @@ valgrind-neighbours: $(EXECNEI)

valgrind-knn: $(EXECKNN)
valgrind --error-exitcode=1 --leak-check=full --show-leak-kinds=all ./$(EXECKNN)

valgrind-knn-imp: $(EXECKNN_IMP)
valgrind --error-exitcode=1 --leak-check=full --show-leak-kinds=all ./$(EXECKNN_IMP)
34 changes: 34 additions & 0 deletions tests/knn_improvements_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/////////////////////////////////////////////////////////////////
//
//
// Unit tests for KNN improvements.
//
//
//////////////////////////////////////////////////////////////////

#include "acutest.h"
#include "knn_improvments.h"
#include "knn.h"


void testLocalJoin() {

}


void testIncrementalSearch() {

}


void testSampling() {

}


TEST_LIST = {
{"testLocalJoin", testLocalJoin},
{"testIncrementalSearch", testIncrementalSearch},
{"testSampling", testSampling},
{NULL, NULL}
};

0 comments on commit 27437e6

Please sign in to comment.