00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 #ifndef  ORDERTREE_SLEP
00018 #define  ORDERTREE_SLEP
00019 
00020 #define IGNORE_IN_CLASSLIST
00021 
00022 #include <stdlib.h>
00023 #include <stdio.h>
00024 #include <time.h>
00025 #include <math.h>
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00042 
00043 
00044 
00045 
00046 
00047 
00048 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00049 IGNORE_IN_CLASSLIST struct NodeNum
00050 {
00051     int node_num;
00052     struct NodeNum *next;
00053 };
00054 
00055 IGNORE_IN_CLASSLIST struct ChildrenNum
00056 {
00057     int children_num;
00058     int *children;
00059 };
00060 
00061 IGNORE_IN_CLASSLIST struct Node
00062 {
00063     int flag; 
00064     double m; 
00065 
00066 
00067 
00068 
00069     int num;  
00070     struct Node *brother; 
00071     struct Node *child; 
00072     struct NodeNum *firstNode; 
00073     struct NodeNum *lastNode; 
00074 };
00075 #endif
00076 
00077 
00078 
00079 
00080 
00081 
00082 
00083 
00084 
00085 
00086 
00087 
00088 
00089 
00090 
00091 
00092 
00093 
00094 
00095 
00096 
00097 
00098 
00099 
00100 
00101 
00102 
00103 
00104 
00105 
00106 
00107 
00108 
00109 
00110 
00111 
00112 
00113 
00114 void readFromFile(char * FileName, struct ChildrenNum ** TreeInfo, int n){
00115     FILE *fp;
00116     struct ChildrenNum * treeInfo;
00117     int i, j, num, nodeId;
00118 
00119 
00120     fp=fopen(FileName, "r");
00121 
00122     if(!fp){
00123         printf("\n\n Fatal Error!!!");
00124         printf("\n\n Failure in reading the file:%s!", FileName);
00125         printf("\n\n The program does not check the correctness of the tree provided in the file: %s!", FileName);
00126         return;
00127     }
00128 
00129     treeInfo=(struct ChildrenNum *)malloc(sizeof(struct ChildrenNum)*n);
00130 
00131     if(!treeInfo){
00132         printf("\n Allocation of treeInfo failure!");
00133         return;
00134     }
00135 
00136     for(i=0;i<n;i++){
00137         treeInfo[i].children_num=0;
00138         treeInfo[i].children=NULL;
00139     }
00140 
00141 
00142     while (!feof(fp)) {
00143 
00144         i=-1;num=-1;
00145         if ( fscanf(fp, "%d %d", &i, &num)!=2){
00146 
00147             
00148             if(feof(fp))
00149                 break;
00150 
00151             printf("\n For each row, it should has at least two numbers: nodeNum and number of children");
00152             return;
00153         }
00154 
00155         if (i>n || i<1){
00156             printf("\n The node number should be between [1, %d]!",n);
00157             return;
00158         }
00159 
00160         i=i-1;
00161         
00162         if (num>0){            
00163             treeInfo[i].children_num=num;            
00164 
00165             treeInfo[i].children=(int *)malloc(sizeof(int)*num);
00166 
00167             if(!treeInfo[i].children){
00168                 printf("\n Allocation of treeInfo failure!");
00169                 return;
00170             }
00171 
00172             for(j=0;j<num;j++){
00173                 if(!fscanf(fp, "%d", &nodeId) ){
00174                     printf("\n This row should have %d children nodes!", num);
00175                     return;
00176                 }
00177                 else{
00178                     if (nodeId>n || nodeId<1){
00179                         printf("\n The node number should be between [1, %d]!", n);
00180                         return;
00181                     }
00182 
00183                     treeInfo[i].children[j]=nodeId-1;
00184                     
00185                 }
00186 
00187             }
00188         }
00189     }
00190 
00191     fclose(fp);
00192 
00193     
00194 
00195 
00196 
00197 
00198 
00199 
00200 
00201 
00202 
00203 
00204 
00205     *TreeInfo=treeInfo;
00206 }
00207 
00208 
00209 
00210 
00211 
00212 
00213 
00214 void buildTree(struct Node* root, struct ChildrenNum * treeInfo, double *u){
00215 
00216 
00217     struct Node * newNode;
00218     struct NodeNum * currentNode;
00219     int currentRoot=root->firstNode->node_num;
00220     int numberOfChildren=treeInfo[currentRoot].children_num;
00221     int i;
00222 
00223     
00224 
00225     for(i=0;i<numberOfChildren;i++){
00226 
00227 
00228         newNode=(struct Node *)malloc(sizeof(struct Node));
00229         currentNode=(struct NodeNum *)malloc(sizeof(struct NodeNum));
00230 
00231         if(!newNode){
00232             printf("\n Allocation in buildTree failure!");
00233             return;
00234         }
00235 
00236         if(!currentNode){
00237             printf("\n Allocation in buildTree failure!");
00238             return;
00239         }
00240 
00241 
00242         newNode->flag=0;
00243         newNode->m=u[treeInfo[currentRoot].children[i]];
00244         newNode->num=1;
00245         newNode->child=NULL;
00246 
00247         currentNode->node_num=treeInfo[currentRoot].children[i];
00248         currentNode->next=NULL;
00249         newNode->firstNode=newNode->lastNode=currentNode;
00250 
00251         
00252 
00253 
00254 
00255         newNode->brother=root->child;
00256         root->child=newNode;
00257 
00258         
00259 
00260 
00261 
00262 
00263         buildTree(newNode, treeInfo, u);
00264     }
00265 }
00266 
00267 
00268 
00269 
00270 
00271 
00272 
00273 
00274 void initializeRoot(struct Node ** Root, char * FileName, double *u, int rootNum, int n){
00275 
00276     struct NodeNum * currentNode;
00277     struct Node *root;
00278     struct ChildrenNum * treeInfo;
00279     int i;
00280 
00281     
00282     readFromFile(FileName, &treeInfo, n);
00283 
00284     if(rootNum>n || rootNum <1){
00285         printf("\n The node number of the root should be between [1, %d]!", n);
00286         return;
00287     }
00288 
00289     rootNum=rootNum-1;
00290     
00291 
00292     root=(struct Node *)malloc(sizeof(struct Node));
00293     currentNode=(struct NodeNum *)malloc(sizeof(struct NodeNum));
00294 
00295     if(!root){
00296         printf("\n Allocation in computeGroups failure!");
00297         return;
00298     }
00299 
00300     if(!currentNode){
00301         printf("\n Allocation in computeGroups failure!");
00302         return;
00303     }
00304 
00305 
00306     root->flag=0;
00307     root->m=u[rootNum];
00308     root->num=1;
00309     root->brother=root->child=NULL;
00310 
00311     currentNode->node_num=rootNum;
00312     currentNode->next=NULL;
00313     root->firstNode=root->lastNode=currentNode;
00314 
00315     
00316     buildTree(root, treeInfo, u);
00317 
00318     
00319     for(i=0;i<n;i++){
00320         if (treeInfo[i].children_num)
00321             free(treeInfo[i].children);
00322     }
00323     free(treeInfo);
00324 
00325     *Root=root;
00326 }
00327 
00328 
00329 
00330 
00331 
00332 
00333 
00334 
00335 
00336 void initializeRootBinary(struct Node ** Root, double *u, int n){
00337 
00338     struct NodeNum * currentNode;
00339     struct Node *root;
00340     struct ChildrenNum * treeInfo;
00341     int rootNum=1;
00342     int i, half=n/2;
00343 
00344     
00345 
00346 
00347 
00348 
00349 
00350 
00351 
00352 
00353 
00354     treeInfo=(struct ChildrenNum *)malloc(sizeof(struct ChildrenNum)*n);    
00355     if(!treeInfo){
00356         printf("\n Allocation of treeInfo failure!");
00357         return;
00358     }
00359 
00360     for(i=0;i<half;i++){
00361         treeInfo[i].children_num=2;
00362         treeInfo[i].children=(int *)malloc(sizeof(int)*2);
00363         treeInfo[i].children[0]=2*i+1;
00364         treeInfo[i].children[1]=2*i+2;
00365     }
00366 
00367     for(i=half;i<n;i++){
00368         treeInfo[i].children_num=0;
00369         treeInfo[i].children=NULL;
00370     }
00371 
00372 
00373     rootNum=rootNum-1;
00374     
00375 
00376     root=(struct Node *)malloc(sizeof(struct Node));
00377     currentNode=(struct NodeNum *)malloc(sizeof(struct NodeNum));
00378 
00379     if(!root){
00380         printf("\n Allocation in computeGroups failure!");
00381         return;
00382     }
00383 
00384     if(!currentNode){
00385         printf("\n Allocation in computeGroups failure!");
00386         return;
00387     }
00388 
00389 
00390     root->flag=0;
00391     root->m=u[rootNum];
00392     root->num=1;
00393     root->brother=root->child=NULL;
00394 
00395     currentNode->node_num=rootNum;
00396     currentNode->next=NULL;
00397     root->firstNode=root->lastNode=currentNode;
00398 
00399     
00400     buildTree(root, treeInfo, u);
00401 
00402     
00403     for(i=0;i<half;i++){
00404         free(treeInfo[i].children);
00405     }
00406     free(treeInfo);
00407 
00408     *Root=root;
00409 }
00410 
00411 
00412 
00413 
00414 
00415 
00416 
00417 
00418 void initializeRootDepth1(struct Node ** Root, double *u, int n){
00419 
00420     struct NodeNum * currentNode;
00421     struct Node *root;
00422     struct ChildrenNum * treeInfo;
00423     int rootNum=1;
00424     int i;
00425 
00426     
00427 
00428 
00429 
00430 
00431 
00432 
00433     treeInfo=(struct ChildrenNum *)malloc(sizeof(struct ChildrenNum)*n);    
00434     if(!treeInfo){
00435         printf("\n Allocation of treeInfo failure!");
00436         return;
00437     }
00438 
00439     for(i=0;i<n;i++){
00440         treeInfo[i].children_num=0;
00441         treeInfo[i].children=NULL;
00442     }
00443 
00444     
00445     if (n>1){
00446         treeInfo[0].children_num=n-1;
00447         treeInfo[0].children=(int *)malloc(sizeof(int)*(n-1));
00448         for(i=1;i<n;i++)
00449             treeInfo[0].children[i-1]=i;
00450     }
00451 
00452     rootNum=rootNum-1;
00453     
00454 
00455     root=(struct Node *)malloc(sizeof(struct Node));
00456     currentNode=(struct NodeNum *)malloc(sizeof(struct NodeNum));
00457 
00458     if(!root){
00459         printf("\n Allocation in computeGroups failure!");
00460         return;
00461     }
00462 
00463     if(!currentNode){
00464         printf("\n Allocation in computeGroups failure!");
00465         return;
00466     }
00467 
00468 
00469     root->flag=0;
00470     root->m=u[rootNum];
00471     root->num=1;
00472     root->brother=root->child=NULL;
00473 
00474     currentNode->node_num=rootNum;
00475     currentNode->next=NULL;
00476     root->firstNode=root->lastNode=currentNode;
00477 
00478     
00479     buildTree(root, treeInfo, u);
00480 
00481     
00482     if(n>1)
00483         free(treeInfo[0].children);
00484     free(treeInfo);
00485 
00486     *Root=root;
00487 }
00488 
00489 
00490 
00491 
00492 
00493 
00494 void merge(struct Node * root, struct Node * maxNode ){
00495     struct Node * childrenNode, *maxNodeChild;
00496 
00497     root->m= (root->m* root->num + maxNode->m *maxNode->num)/(root->num + maxNode->num);
00498     root->num+=maxNode->num;
00499     root->lastNode->next=maxNode->firstNode;
00500     root->lastNode=maxNode->lastNode;
00501 
00502     
00503 
00504 
00505 
00506     if (root->child==maxNode){
00507         root->child=maxNode->brother;
00508     }
00509     else{
00510         childrenNode=root->child;
00511 
00512         while(childrenNode->brother!=maxNode){
00513             childrenNode=childrenNode->brother;
00514         }
00515         
00516         childrenNode->brother=maxNode->brother;
00517     }
00518 
00519 
00520     
00521 
00522 
00523     maxNodeChild=maxNode->child;
00524     if (maxNodeChild){
00525         
00526 
00527         while(maxNodeChild->brother)
00528             maxNodeChild=maxNodeChild->brother;
00529         
00530 
00531         maxNodeChild->brother=root->child;
00532         root->child=maxNode->child;
00533     }
00534 
00535     
00536 
00537 
00538     free(maxNode);
00539 
00540 }
00541 
00542 
00543 
00544 
00545 
00546 
00547 
00548 
00549 void computeMaximalMean(struct Node * root){
00550     struct Node * childrenNode, *maxNode;
00551     double mean;
00552 
00553     
00554     if(!root->child){
00555 
00556         
00557         if (root->m <0)
00558             root->m =0;
00559 
00560         root->flag=1;
00561         return;
00562     }
00563 
00564     
00565     while(1){
00566 
00567         childrenNode=root->child;
00568         if(!childrenNode){
00569 
00570             if (root->m <0)
00571                 root->m =0;
00572 
00573             root->flag=1;
00574             return;
00575         }
00576 
00577         
00578 
00579         mean=0;
00580 
00581         
00582         while(childrenNode){
00583 
00584             
00585             if (!childrenNode->flag)
00586                 computeMaximalMean(childrenNode);
00587 
00588             if (childrenNode->m >= mean){
00589                 mean=childrenNode->m;
00590                 maxNode=childrenNode;
00591             }
00592 
00593             childrenNode=childrenNode->brother;            
00594         }
00595 
00596         if ( (root->m <= 0) && (mean==0) ){
00597             
00598 
00599 
00600 
00601             childrenNode=root->child;
00602             while(childrenNode){
00603                 merge(root, childrenNode);
00604                 childrenNode=root->child;
00605             }
00606 
00607             root->m =0;            
00608             root->flag=1;
00609             return;
00610         }
00611 
00612         if (root->m > mean){
00613 
00614             root->flag=1;
00615             return;
00616         }
00617 
00618         merge(root,maxNode);
00619     }
00620 
00621 }
00622 
00623 
00624 
00625 
00626 
00627 
00628 
00629 
00630 
00631 
00632 void computeMaximalMean_without_nonnegative(struct Node * root){
00633     struct Node * childrenNode, *maxNode;
00634     double mean;
00635     int mean_flag;
00636 
00637     
00638     if(!root->child){
00639 
00640         
00641 
00642         
00643 
00644 
00645 
00646 
00647 
00648 
00649         root->flag=1;
00650         return;
00651     }
00652 
00653     
00654     while(1){
00655 
00656         childrenNode=root->child;
00657         if(!childrenNode){
00658 
00659             
00660 
00661             
00662 
00663 
00664 
00665 
00666 
00667 
00668             root->flag=1;
00669             return;
00670         }
00671 
00672         
00673 
00674 
00675 
00676         mean_flag=0; 
00677 
00678         
00679         while(childrenNode){
00680 
00681             
00682             if (!childrenNode->flag)
00683                 computeMaximalMean_without_nonnegative(childrenNode);
00684 
00685             
00686             if (!mean_flag){
00687                 mean=childrenNode->m;
00688                 mean_flag=1;
00689             }
00690 
00691             if (childrenNode->m >= mean){
00692                 mean=childrenNode->m;
00693                 maxNode=childrenNode;
00694             }
00695 
00696             childrenNode=childrenNode->brother;            
00697         }
00698 
00699         if (root->m > mean){
00700 
00701             root->flag=1;
00702             return;
00703         }
00704 
00705         merge(root,maxNode);
00706     }
00707 
00708 }
00709 
00710 
00711 
00712 
00713 
00714 
00715 
00716 
00717 void computeSolution(double *x, struct Node *root){
00718     struct Node * child;
00719     struct NodeNum *currentNode;
00720     double mean;
00721 
00722     if (root){        
00723         
00724 
00725 
00726 
00727 
00728 
00729         mean=root->m;
00730 
00731         currentNode=root->firstNode;
00732         while(currentNode){
00733             x[currentNode->node_num]=mean;
00734             currentNode=currentNode->next;
00735         }            
00736 
00737         
00738         child=root->child;
00739         while(child){
00740             computeSolution(x, child);
00741 
00742             child=child->brother;
00743         }
00744     }
00745 }
00746 
00747 
00748 
00749 
00750 
00751 
00752 void traversalTree(struct Node *root){
00753     struct Node * child;
00754     struct NodeNum *currentNode;
00755 
00756     if (root){
00757         printf("\n\n root->m =%2.5f, num:%d \n Nodes:",root->m,root->num);
00758 
00759         currentNode=root->firstNode;
00760         while(currentNode){
00761             printf(" %d ", currentNode->node_num);
00762             currentNode=currentNode->next;
00763         }     
00764 
00765         printf("\n root: %d, child:", root->m);
00766 
00767         
00768         child=root->child;
00769         while(child){
00770             printf(" %d", child->m);
00771             child=child->brother;
00772         }
00773 
00774         
00775         child=root->child;
00776         while(child){
00777             traversalTree(child);
00778 
00779             child=child->brother;
00780         }
00781     }
00782 }
00783 
00784 
00785 
00786 
00787 
00788 
00789 
00790 
00791 
00792 void deleteTree(struct Node *root){
00793     struct Node *child, *temp;
00794     struct NodeNum *currentNode;
00795 
00796     if (root){
00797 
00798         child=root->child;
00799 
00800         while(child){
00801 
00802             temp=child->brother;
00803             
00804 
00805             deleteTree(child);
00806             
00807 
00808             child=temp;
00809         }
00810 
00811         
00812 
00813 
00814 
00815 
00816 
00817         currentNode=root->firstNode;
00818         while(currentNode){
00819             root->firstNode=currentNode->next;
00820             free(currentNode);
00821 
00822             currentNode=root->firstNode;
00823         }
00824         root->lastNode=NULL;
00825         free(root);
00826     }
00827 }
00828 
00829 
00830 
00831 
00832 
00833 
00834 void orderTree(double *x, char * FileName, double *u, int rootNum, int n){
00835     struct Node * root;
00836 
00837     
00838 
00839 
00840     initializeRoot(&root, FileName, u, rootNum, n);  
00841 
00842     
00843 
00844 
00845 
00846 
00847 
00848     
00849 
00850 
00851 
00852     computeMaximalMean(root);
00853 
00854 
00855     
00856 
00857     computeSolution(x, root);
00858 
00859 
00860     
00861 
00862 
00863 
00864 
00865 
00866     
00867 
00868     deleteTree(root);
00869 }
00870 
00871 
00872 
00873 
00874 
00875 
00876 
00877 void orderTree_without_nonnegative(double *x, char * FileName, double *u, int rootNum, int n){
00878     struct Node * root;
00879 
00880     
00881 
00882 
00883     initializeRoot(&root, FileName, u, rootNum, n);  
00884 
00885     
00886 
00887 
00888 
00889 
00890 
00891     
00892 
00893 
00894 
00895     computeMaximalMean_without_nonnegative(root);
00896 
00897 
00898     
00899 
00900     computeSolution(x, root);
00901 
00902 
00903     
00904 
00905 
00906 
00907 
00908 
00909     
00910 
00911     deleteTree(root);
00912 }
00913 
00914 
00915 
00916 
00917 
00918 
00919 
00920 
00921 void orderTreeBinary(double *x, double *u, int n){
00922     struct Node * root;
00923 
00924     
00925 
00926 
00927 
00928 
00929 
00930 
00931     initializeRootBinary(&root, u, n);
00932 
00933     
00934 
00935 
00936 
00937 
00938 
00939     
00940 
00941 
00942 
00943     computeMaximalMean(root);
00944 
00945 
00946     
00947 
00948     computeSolution(x, root);
00949 
00950 
00951     
00952 
00953 
00954 
00955 
00956 
00957     
00958 
00959     deleteTree(root);
00960 }
00961 
00962 
00963 
00964 
00965 
00966 
00967 
00968 void orderTreeDepth1(double *x, double *u, int n){
00969     struct Node * root;
00970 
00971     
00972 
00973 
00974 
00975 
00976     initializeRootDepth1(&root, u, n);
00977 
00978     
00979 
00980 
00981 
00982 
00983 
00984     
00985 
00986 
00987 
00988     computeMaximalMean(root);
00989 
00990 
00991     
00992 
00993     computeSolution(x, root);
00994 
00995 
00996     
00997 
00998 
00999 
01000 
01001 
01002     
01003 
01004     deleteTree(root);
01005 }
01006 #endif