拓朴排序算法实现

 

拓朴排序算法实现

            EmilMatthew ([email protected])       06/07/1 4

[  类别  ]算法实现   

[推荐指数]★★

[ 关键词 ]拓朴排序

                   

[0引言]

   有时,可以用图来表示一类事物或集合间的先后依赖关系。比如:集合A 包含于B,就画一条由B指向A的箭头;如果C事件较D事件先发生,则画一条由C指向D的箭头。建立完这样的依赖关系图,就可以对其依赖的先后关系进行排序。比如:将不依赖于其它的集合先挑选出来,再挑仅依赖于已挑选出集合的集合,就可将所有集合的内部组成有个完整的结果。再如:把需要先做的事件(即不需要依赖于其它事件即可完成的事件先挑出来)放在序列的前面,由这样决定好的序列,将会使得全局的完成时间最短,而且工序间的衔接紧密,提高生产效益。

 

[1实现]

    有鉴于以上对拓朴排序的认识,可以得到对依赖关系统图进行拓朴排序的算法思路:

       Clear ansArr to null

       Index=0

       While TopGraph still has node to be sorted

For each node in the TopGraph

If current node is a independent node

                                   Ans[index]=current node

                                   Index<--index+1

                                   Delete current node from the TopGraph

End if

              End for

              If no independent node founded

                            Output “Error”

Break

              End if

       End While

 

   并不是很复杂,是一个每次寻找独立结点的贪心算法。

 

算法的实现采用了基于关系矩阵的图的形式:

void    topologySort(int** inMatrix,int* topSeqArr,int row,int col)

{

       int i,j;

       int* labeledRow;

       int labeledNum;

       unsigned noFirstNode;

      

       if(inMatrix==NULL||topSeqArr==NULL)

       {

              printf("in topologySort ,pass in matrix or topSeqArr is null/n");

              return;

       }

      

       labeledRow=(int*)malloc(sizeof(int)*row);

       if(labeledRow==NULL)

       {

              printf("in topologySort, mem apply failure./n");

              return;

       }

      

 

       for(i=0;i<row;i++)

              labeledRow[i]=0;

 

       labeledNum=0;

       while(labeledNum!=row)

       {

              noFirstNode=1;

             

              for(j=0;j<col;j++)

              {

                     if(!labeledRow[j])   //forget this line.

                     {

                            for(i=0;i<row;i++)

                            {

                                   if(!labeledRow[i])

                                   {      //j is not a first node

                                          if(i!=j&&inMatrix[i][j]==1)

                                          {

                                                 break; 

                                          }

                                   }

                            }

                    

                            if(i==row)//j is a first node,note it , del from the graph

                            {

                                   topSeqArr[labeledNum]=j;

                                   labeledRow[j]=1;                

                                   labeledNum++;

                                   noFirstNode=0;

                            }

                     }

              }

 

              if(noFirstNode)

              {

                     printf("can't arrange a toplogy sort./n");

                     return;

              }

       }

      

       if(DEBUG)

       {

              printf("topsort result:/n");

              for(i=0;i<row;i++)

                     printf("%d ",topSeqArr[i]);

              printf("/n");

       }

       free(labeledRow);

}

 

//测试主程序

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

#define SIZE 8

#define DEBUG 1

int staticM[SIZE][SIZE]={{1,1,1,0,0,0,1,0},

                                          {0,1,0,0,0,0,1,0},

                                          {0,0,1,0,0,1,0,0},

                                          {0,1,0,1,1,0,1,0},

                                          {0,0,0,0,1,1,0,1},

                                          {0,0,0,0,0,1,0,0},

                                          {0,0,0,0,0,0,1,0},

                                          {0,0,0,0,0,0,0,1},

                                          };

 

void    topologySort(int** inMatrix,int* topSeqArr,int row,int col);

 

void main()

{

       int** testMatrix;

       int* topSeq;

 

       int len=SIZE;

       int i,j;

 

       testMatrix=(int**)malloc(sizeof(int*)*len);

       for(i=0;i<len;i++)

              testMatrix[i]=(int*)malloc(sizeof(int)*len);

 

       topSeq=(int*)malloc(sizeof(int)*len);

 

       if(testMatrix==NULL||topSeq==NULL)

       {

              printf("testMatrix==NULL/n");

              return;

       }

 

       for(i=0;i<len;i++)

              for(j=0;j<len;j++)

                     testMatrix[i][j]=staticM[i][j];

      

       topologySort(testMatrix,topSeq,len,len);

 

}

 

 

[参考文献与网站]

[1] http://knight.fcu.edu.tw/~d9046876/ds/d_55.htm

[2] http://www.ihabits.com/zh:AOV%E7%BD%91

[3] http://www.cqzxzx.cn/it/zbjiaocheng/aoshaifutao/tlsf3.htm

[4] 张乃孝,算法与数据结构----C语言描述,高等教育出版社,2002

 

 

你可能感兴趣的:(数据结构,算法,null,delete,Graph,Matrix)