Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
knn improved
  • Loading branch information
sitaras committed Oct 20, 2021
commit bd4e8fcb54b263a8c35c9b6deb20ac0b7b04d6f8
2 changes: 2 additions & 0 deletions Vector/vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ 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]);
Expand Down
114 changes: 80 additions & 34 deletions hashTable/hashTableList/hashTableList.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,64 +119,110 @@ void swapVectors(Vector *xp, Vector *yp)
*yp = temp;
}

// A function to implement bubble sort
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]);
}
// 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");
for (int i = 0; i < k; i++){
if(nearestDist[i]<0){
flag=0;
for (int i = 0; i < k; i++){
if (nearest[i] != NULL && compareVectors(nearest[i], temp->v))
eq = 1;
}
if (eq)
break;
nearestDist[i]=dist;
nearest[i]=temp->v;
break;
}

int index=binarySearch(nearestDist,0,k-1,dist);
if(index!=-1 && nearest[index] != NULL && compareVectors(nearest[index], temp->v)){
eq = 1;
}

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

nearestDist[i] = dist;
nearest[i] = temp->v;
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;
}
}
bubbleSort(nearestDist,nearest,k);
if(added)
quickSort(nearestDist, nearest,0, k-1);
temp = temp->next;
}
}