Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <math.h>
  5. #include <stdbool.h>
  6. #include "vptree.h"
  7. #define INT_MAX 2147483647
  8.  
  9. vptree * buildvp(double *X,int n,int d){
  10.      if(isFather==5){
  11.         index.dataSet=X;
  12.         index.dataLength=n;
  13.         isFather=0;
  14.         //printf("\nGIA TON ISFATHER");
  15.      }
  16.     vptree * node=( vptree*)malloc(sizeof( vptree));
  17.  
  18.     node->d=d;
  19.     node->n=n;
  20.  
  21.     node->points=X;
  22.  
  23.     node->cooVP=getVP(node);
  24.  
  25.     node->median=getMD(node);
  26.  
  27.     node->innerTree=(vptree*)malloc(sizeof(vptree));
  28.     node->innerTree=getInner(node);
  29.     if(node->innerTree!=NULL)node->innerTree=buildvp(node->innerTree->points,node->innerTree->n,node->innerTree->d);
  30.  
  31.     node->outerTree=(vptree*)malloc(sizeof(vptree));
  32.     node->outerTree=getOuter(node);
  33.     if(node->outerTree!=NULL)node->outerTree=buildvp(node->outerTree->points,node->outerTree->n,node->outerTree->d);
  34.  
  35.     node->indx=getIDX(node);//den to exw valei akoma sth swsth seira
  36.  
  37.     return node;
  38.  
  39. };
  40.  
  41.  vptree * getInner( vptree * T){
  42.  
  43.    if(T->n>1){
  44.     vptree* inNode=( vptree*)malloc(sizeof( vptree));
  45.  
  46.    double coo[T->n][T->d];
  47.    int l;
  48.    int k;
  49.    for(l=0;l<(T->n);l++){
  50.         for(k=0;k<T->d;k++){
  51.             coo[l][k]=T->points[(T->d)*l+k];//einai swsto??? EINAI
  52.     }}
  53.     double* distance=(double*)malloc(((T->n)-1)*sizeof(double));
  54.     int i;
  55.     int j;
  56.     double sqrtValue;
  57.     for(i=0;i<(T->n)-1;i++){
  58.         sqrtValue=0;
  59.             for(j=0;j<(T->d);j++){
  60.                 sqrtValue+=(coo[i][j]-coo[(T->n)-1][j])*(coo[i][j]-coo[(T->n)-1][j]);
  61.  
  62.             }
  63.         distance[i]=sqrt(sqrtValue);}
  64.  
  65.         //------------------------------PANW GINETAI O UPOLOGISMOS DISTANCE
  66.  
  67.  
  68.         int w,h;
  69.         int z=0;
  70.         int indexCounter=0;
  71.         for(w=0;w<(T->n)-1;w++){
  72.                 if(distance[w]<T->median)indexCounter++;
  73.           }
  74.  
  75.  
  76.         inNode->points=(double*)malloc((indexCounter*T->d)*sizeof(double));
  77.         for(w=0;w<(T->n)-1;w++){
  78.                 if(distance[w]<T->median){
  79.  
  80.                     for(h=0;h<T->d;h++){
  81.                   inNode->points[(T->d)*z+h]=coo[w][h];}//vazei tis katallhles suntetagmenes ston points tou inner
  82.                   z++;
  83.                }
  84.           }
  85.         inNode->n=z;
  86.         inNode->d=T->d;
  87.  
  88. return inNode;
  89.    }
  90.    else //printf("\n  ---->  ",T->index, T->cooVP[0]);
  91. return NULL;
  92. };
  93.  vptree * getOuter( vptree * T){
  94.     if(T->n>1){
  95.     vptree* outNode=( vptree*)malloc(sizeof( vptree));
  96.  
  97.    double coo[T->n][T->d];
  98.    int l;
  99.    int k;
  100.    for(l=0;l<(T->n);l++){
  101.         for(k=0;k<T->d;k++){
  102.             coo[l][k]=T->points[(T->d)*l+k];//einai swsto??? EINAI
  103.     }}
  104.     double* distance=(double*)malloc(((T->n)-1)*sizeof(double));
  105.     int i;
  106.     int j;
  107.     double sqrtValue;
  108.     for(i=0;i<(T->n)-1;i++){
  109.         sqrtValue=0;
  110.             for(j=0;j<(T->d);j++){
  111.                 sqrtValue+=(coo[i][j]-coo[(T->n)-1][j])*(coo[i][j]-coo[(T->n)-1][j]);
  112.  
  113.             }
  114.         distance[i]=sqrt(sqrtValue);}
  115.  
  116.         //------------------------------PANW GINETAI O UPOLOGISMOS DISTANCE
  117.  
  118.  
  119.         int w,h;
  120.         int z=0;
  121.         int indexCounter=0;
  122.         for(w=0;w<(T->n)-1;w++){
  123.                 if(distance[w]>=T->median)indexCounter++;
  124.           }
  125.  
  126.  
  127.         outNode->points=(double*)malloc((indexCounter*T->d)*sizeof(double));
  128.         for(w=0;w<(T->n)-1;w++){
  129.                 if(distance[w]>=T->median){
  130.  
  131.                     for(h=0;h<T->d;h++){
  132.                   outNode->points[(T->d)*z+h]=coo[w][h];}//vazei tis katallhles suntetagmenes ston points tou inner
  133.                   z++;
  134.                }
  135.           }
  136.         outNode->n=z;
  137.         outNode->d=T->d;
  138.  
  139. return outNode;
  140.    }
  141.    else //printf("\n  ---->  ",T->index, T->cooVP[0]);
  142. return NULL;
  143.  
  144. };
  145.  
  146. double getMD( vptree *T){
  147.     double* distance=(double*)malloc(((T->n)-1)*sizeof(double));
  148.  
  149.     double coo[T->n][T->d]; //periexei oles tis suntetagmenes olwn twn stoixeiwn
  150.     double sqrtValue=0;
  151.  
  152.     int i;
  153.     int k;
  154.     int j;
  155.     int l;
  156.  
  157.     for(l=0;l<(T->n);l++){
  158.         for(k=0;k<T->d;k++){
  159.             coo[l][k]=T->points[(T->d)*l+k];//einai swsto????
  160.     }}
  161.  
  162.     for(i=0;i<(T->n)-1;i++){
  163.         sqrtValue=0;
  164.             for(j=0;j<(T->d);j++){
  165.                 sqrtValue+=(coo[i][j]-coo[(T->n)-1][j])*(coo[i][j]-coo[(T->n)-1][j]);
  166.  
  167.             }
  168.         distance[i]=sqrt(sqrtValue);}
  169.  
  170. return kthSmallest(distance,0,(T->n)-2,((T->n)-1)/2);
  171. };
  172.  
  173. double * getVP( vptree *T){//epistrefei gia to vptree T to vantage point pou tha xrhsimopoihthei
  174.     int j; // metavlhtes gia na prospelasw thn for
  175.  
  176.     int n=T->n;//krataw diastaseis tou pinaka twn point tou T gia na paiksw sth for apo katv
  177.     int d=T->d;//ta dia me panw
  178.  
  179.     double* arrayVP=(double*)malloc(d*sizeof(double));// o vector poy tha epistrafei kai periexei coordinates tou VP
  180.  
  181.         for(j=0;j<d;j++){
  182.             arrayVP[j]=(*(T->points+(n-1)*d+j));
  183.         }
  184.  
  185. return arrayVP;
  186. };
  187.  
  188. int getIDX( vptree *T){//nomizw me afton ton tropo epistrefw amesws ton deikth tou teleftaiou stoixeiou
  189.     int k;
  190.     int l;
  191.  
  192.     vptree* outNode=( vptree*)malloc(sizeof( vptree));
  193.  
  194.    double coo[index.dataLength][T->d];
  195.  
  196.    for(l=0;l<index.dataLength;l++){
  197.         for(k=0;k<T->d;k++){
  198.             coo[l][k]=index.dataSet[(T->d)*l+k];//einai swsto??? EINAI
  199.     }}
  200.     double* distance=(double*)malloc((index.dataLength)*sizeof(double));
  201.     int i;
  202.     int j,m;
  203.     double sqrtValue;
  204.     for(i=0;i<index.dataLength;i++){
  205.         sqrtValue=0;
  206.             for(j=0;j<(T->d);j++){
  207.                 sqrtValue+=(coo[i][j]-T->cooVP[j])*(coo[i][j]-T->cooVP[j]);
  208.  
  209.             }
  210.         distance[i]=sqrt(sqrtValue);
  211.  
  212.         }
  213.  
  214.  
  215.     for(m=0;m<index.dataLength;m++){if(distance[m]==0)return m;}
  216.  
  217.     //printf("h index exei themaaaaaaa");
  218.     return 0;
  219.  
  220. };
  221.  
  222. int partition(double arr[], int l, int r)
  223. {   double temp;
  224.     double x = arr[r];
  225.     int i = l;
  226.     int j;
  227.     for (j = l; j <= r - 1; j++) {
  228.         if (arr[j] <= x) {
  229.             temp=arr[i];
  230.             arr[i]=arr[j];
  231.             arr[j]=temp;
  232.  
  233.             i++;
  234.         }
  235.     }
  236.     temp=arr[i];
  237.     arr[i]=arr[r];
  238.     arr[r]=temp;
  239.  
  240.     return i;
  241. };
  242.  
  243. double kthSmallest(double arr[], int l, int r, int k)
  244. {
  245.     if (k > 0 && k <= r - l + 1) {
  246.         int index = partition(arr, l, r);
  247.         if (index - l == k - 1)
  248.             return arr[index];
  249.         if (index - l > k - 1)
  250.             return kthSmallest(arr, l, index - 1, k);
  251.  
  252.         return kthSmallest(arr, index + 1, r,
  253.                             k - index + l - 1);
  254.     }
  255.     return INT_MAX;
  256. };
  257.  
  258. //Ara kathe vptree stoixeio tha periexei mesa to VP tou (dhladh tha mas leei poio VP einai sa node) to index tou node aftou ston megalo pinaka kai to aristero kai deksi tree
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement