Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:\\Program Files\\LLVM\\bin\\clang.exe",
"cStandard": "c11",
"cppStandard": "c++14",
"intelliSenseMode": "windows-clang-x64"
}
],
"version": 4
}
31 changes: 31 additions & 0 deletions LSH/lsh.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,34 @@ void nearestNeigbor(LSH lsh,Vector q){
printf("- DID NOT FIND NEAREST NEIGHBOR\n");
}
}

void kNearestNeigbors(LSH lsh,Vector q,int k){
printf("ABOUT TO SEARCH %d NEAREST NEIGHBORS FOR : ",k);
printVector(q);
Vector nearest[k];
double knearestDists[k];
for (int i = 0; i < k; i++){
knearestDists[i]=-1;
nearest[i]=NULL;
}

int l = getL(lsh);
HashTable *hts = getHts(lsh);
g_function *gfuns = getGfuns(lsh);
for(int i=0;i<l;i++){
int q_index = computeG(gfuns[i],q);
htKFindNearestNeighbors(hts[i], q_index, q, nearest, knearestDists, d,k);
}
int flag=1;
for (int i = k-1; i >= 0; i--){
if (knearestDists[i] >= 0 && nearest[i] != NULL){
printf("FOUND %d NEAREST NEIGHBOR: ",i);
printVector(nearest[i]);
printf("WITH DISTANCE = %f\n", knearestDists[i]);
flag=0;
}
}
if(flag){
printf("- DID NOT FIND NEAREST NEIGHBOR\n");
}
}
1 change: 1 addition & 0 deletions LSH/lsh.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ void printLSH(LSH );
void destroyLSH(LSH );

void nearestNeigbor(LSH ,Vector );
void kNearestNeigbors(LSH, Vector,int);
#endif
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ EXEC= demo
all: $(EXEC)

$(EXEC): $(OBJ1)
$(CC) $(CFLAGS) $(OBJ1) -o $(EXEC)
$(CC) $(CFLAGS) $(OBJ1) -o $(EXEC) -lm


.PHONY: clean
Expand Down
11 changes: 11 additions & 0 deletions Vector/vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,24 @@ void deleteVector(Vector v){


void printVector(Vector v){
if(v==NULL)
return;
printf("\n[");
for(int i=0;i<d;i++){
printf(" %f",v->coords[i]);
}
printf(" ]\n");
}

int compareVectors(Vector v1,Vector v2){
for(int i=0;i<d;i++){
if(v1->coords[i]!=v2->coords[i])
return 0;
}
return 1;
}


double *getCoords(Vector v){
return v->coords;
}
4 changes: 3 additions & 1 deletion Vector/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ void deleteVector(Vector);

void printVector(Vector );

double *getCoords(Vector );
int compareVectors(Vector , Vector );

double *getCoords(Vector);

#endif
3 changes: 3 additions & 0 deletions hashTable/hashTable.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,6 @@ HashTable htDelete(HashTable ht,int freeVectors){
void htFindNearestNeighbor(HashTable ht,int index,Vector q,Vector *nearest,double *nearestDist,int d){
listFindNearestNeighbor(ht->table[index].head,q,nearest,nearestDist,d);
}
void htKFindNearestNeighbors(HashTable ht,int index,Vector q,Vector *nearest,double *nearestDist,int d,int k){
listFindKNearestNeighbors(ht->table[index].head, q, nearest, nearestDist, d,k);
}
1 change: 1 addition & 0 deletions hashTable/hashTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ HashTable htDelete(HashTable,int );
int htDeleteNode(HashTable,int);

void htFindNearestNeighbor(HashTable ,int ,Vector ,Vector *,double *,int );
void htKFindNearestNeighbors(HashTable, int, Vector, Vector *, double *, int ,int );

#endif
121 changes: 121 additions & 0 deletions hashTable/hashTableList/hashTableList.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,124 @@ void listFindNearestNeighbor(List list,Vector q,Vector *nearest,double *nearestD
temp=temp->next;
}
}

void swapDoubles(double *xp, double *yp)
{
double temp = *xp;
*xp = *yp;
*yp = temp;
}
void swapVectors(Vector *xp, Vector *yp)
{
Vector temp = *xp;
*xp = *yp;
*yp = temp;
}

// void bubbleSort(double arr[], Vector *nearest, int n)
// {
// for (int i = 0; i < n - 1; i++){
// for (int j = 0; j < n - i - 1; j++){
// if (arr[j] < arr[j + 1]){
// swapDoubles(&arr[j], &arr[j + 1]);
// swapVectors(&nearest[j], &nearest[j + 1]);
// }
// }
// }
// }


// function to find the partition position
int partition(double arr[], Vector *nearest, int low, int high){
double pivot = arr[high];
int i = (low - 1);
for (int j = low; j < high; j++) {
if (arr[j] >= pivot) {

i++;

swapDoubles(&arr[i], &arr[j]);
swapVectors(&nearest[i], &nearest[j]);
}
}

swapDoubles(&arr[i + 1], &arr[high]);
swapVectors(&nearest[i + 1], &nearest[high]);

return (i + 1);
}

void quickSort(double arr[], Vector *nearest, int low, int high){
if (low < high) {

int pi = partition(arr, nearest, low, high);

quickSort(arr,nearest, low, pi - 1);

quickSort(arr, nearest, pi + 1, high);
}
}

int binarySearch(double arr[], int l, int r, double x){
if (r >= l){
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;

if (arr[mid] < x)
return binarySearch(arr, l, mid - 1, x);

return binarySearch(arr, mid + 1, r, x);
}

return -1;
}

void listFindKNearestNeighbors(List list,Vector q,Vector *nearest,double *nearestDist,int d,int k){
if(list==NULL){ return;}
List temp=list;
int filled=0;
while(temp!=NULL){
int flag = 1;
int eq = 0;
int added=0;
double dist = distance_metric(temp->v,q,d);

printf("---------------------------------------------------\n");
printf("Vector:\n");
printVector(temp->v);
printf("WITH DISTANCE = %f\n", dist);
printf("---------------------------------------------------\n");

int index=binarySearch(nearestDist,0,k-1,dist);
if(index!=-1 && nearest[index] != NULL && compareVectors(nearest[index], temp->v)){
eq = 1;
}
if (eq){
temp = temp->next;
continue;
}
if(!filled)
for (int i = 0; i < k; i++){
if(nearestDist[i]<0){
flag=0;
added=1;
nearestDist[i]=dist;
nearest[i]=temp->v;
break;
}
}

if (flag){
filled=1;
if(dist<nearestDist[0]){
added = 1;
nearestDist[0] = dist;
nearest[0] = temp->v;
}
}
if(added)
quickSort(nearestDist, nearest,0, k-1);
temp = temp->next;
}
}
2 changes: 1 addition & 1 deletion hashTable/hashTableList/hashTableList.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ void listPrint(List );
List listDelete(List ,int );

void listFindNearestNeighbor(List ,Vector ,Vector *,double *,int );

void listFindKNearestNeighbors(List, Vector, Vector *, double *, int,int);
#endif
8 changes: 5 additions & 3 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ int main(int argc, char const *argv[]) {

// test
srand(time(NULL));
int l = 3;
int l =5;
hashTableSize = 5;
k = 4;
w = 6;
Expand All @@ -26,13 +26,15 @@ int main(int argc, char const *argv[]) {

LSH temp = initializeLSH(l);
readFile("testing.txt",temp);
printLSH(temp);
// printLSH(temp);


double vec[2] = {17.0 , 5.5};
double vec[2] = {1.0 , 1.0};
Vector vecTmp=initVector(vec);

nearestNeigbor(temp,vecTmp);
printf("================================================\n");
kNearestNeigbors(temp, vecTmp, 3);

deleteVector(vecTmp);

Expand Down