拓朴排序算法实现

关键词 拓朴排序

                   

[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);

 

}
AddThis Social Bookmark Button

相关文档(Relevant Entries)
使用RSA算法进行加密和解密
深度优先搜索和广度优先搜索
PageRank 彻底解说.中文版
Base64算法详解和实现
动态规划求解最长公共子串问题
质数填表问题的回溯算法
经典IPC问题 - 哲学家进餐
双核CPU上的快速排序效率
WoW Powerleveling
Posted on October 19, 2006 10:27 PM | | | Comments (0) | | TrackBacks (0)

引用地址(TRACKBACKS)
 
TrackBack URL for this entry:
http://www.wujianrong.com/mt-tb.cgi/4060

发布评论(ADD YOUR COMMENTS)
 
感谢您参与评论;发表您的意见时请保持文章的相关性;不相关的或是单纯宣传的内容可能会被删掉。您的E-mail只是用来确认您发表的文章,不会出现在网页上。
Please keep your comments relevant to this blog entry. Email addresses are never displayed, but they are required to confirm your comments.

称呼(Name):      记住我的个人信息(Remember)
邮箱(Email):
网址(URL):
评论(Add your comments):

相关内容
广告计划