拓撲排序程式

來源:互聯網
上載者:User
//拓撲排序
#include <stdio.h>
#include <stdio.h>
#define MAX_VERTEX_NUM 50
#define STACK_SIZE 50
typedef struct ArcNode{
        int adjvex;  //頂點在數組中的位置
        struct ArcNode *nextarc; //下一條弧的指標
        }ArcNode;//鄰接表結點
typedef struct VNode{
        int data; //頂點資訊
        ArcNode  *firstarc;//第一條依附該定點的弧
        }VNode,AdjList[MAX_VERTEX_NUM];//頭結點
typedef struct {
        AdjList vertices;
        int vexnum,arcnum;
        }ALGraph;//鄰接圖的資訊
typedef struct {
        int *base;
        int *top;
        int stacksize;//堆棧大小
        }SqStack;//堆棧結構體
//*****************初始化堆棧**********************
void InitStack(SqStack *S)
   {
        S->base=(int *)malloc(STACK_SIZE*sizeof(int));
          if(!S->base)
               exit(1);
        S->top=S->base;
        S->stacksize=STACK_SIZE;
   }
 //*****************入棧*************************
 void Push(SqStack *S,int e)
    {
             if(S->top-S->base>=S->stacksize)
                   exit(1);
             *(S->top++)=e;
    }     
 //*****************出棧************************
void Pop(SqStack *S,int *e)
     {
            if(S->top==S->base)
                  exit(1);
             *e=*(--S->top);
     }
//****************判斷棧空************************
int StackEmpty(SqStack *S)
     {
             if(S->base==S->top)
                 return 1;
             else
                 return 0;
     }                                                                 
  //***********************建立圖******************
  void CreatGraph(ALGraph *G)
       {
               int i,n,m;
               ArcNode *p; 
               printf("please input the vexnum and arcnum:");
               scanf("%d %d",&G->vexnum,&G->arcnum);
               for(i=1;i<=G->vexnum;i++)//初始圖的表頭結點
                    {
                         G->vertices[i].data=i;
                         G->vertices[i].firstarc=NULL;
                    }
               for(i=1;i<=G->arcnum;i++)
                    {  //把弧存入鄰接表
                           printf("/nplease input edge vertex and vertex:");
                           scanf("%d %d",&n,&m);            
                           while(n<0||n>G->vexnum||m<0||m>G->vexnum)
                                 {
                                       printf("/nERROR!please input again:");                                                                          
                                       scanf("%d %d",&n,&m);//滿足條件的頂點 ,n,m之間有邊
                                 }
                           p=(ArcNode *)malloc(sizeof(ArcNode));//為鄰接表結點申請空間
                           if(!p)
                              exit(1);
                           p->adjvex=m;
                          p->nextarc = G->vertices[n].firstarc;//鏈表的操作
                           G->vertices[n].firstarc=p;
                    }
                printf("The Adjacency List :/n");
                for(i=1;i<=G->vexnum;i++)//輸出此鄰接表
                     {
                              printf("%d",G->vertices[i].data);//頭結點
                              for(p=G->vertices[i].firstarc;p;p=p->nextarc)
                                    printf("%3d",p->adjvex);//表結點
                               printf("/n");
                     }
      }
   //*******************求入度***************************
   void FindInDegree(ALGraph G,int indegree[])
         {
                int i;
                for(i=1;i<=G.vexnum;i++)
                     indegree[i]=0;//初始為0
                for(i=1;i<=G.vexnum;i++)
                     { //遍曆鄰接表,求入度
                            while(G.vertices[i].firstarc)
                                 {
                                        indegree[G.vertices[i].firstarc->adjvex]++; //弧頭元素入度加一
                                        G.vertices[i].firstarc=G.vertices[i].firstarc->nextarc;
                                 }
                     }
         }
 //**********************拓撲排序***************************
 void TopologicalSort(ALGraph G)
      {
              int indegree[MAX_VERTEX_NUM];
              int i,k,n;
              int count=0;
              ArcNode *p;
              SqStack S;
         FindInDegree(G,indegree);//用數組名 。
         InitStack(&S);
         for(i=1;i<=G.vexnum;i++)
             {
                      if(!indegree[i])//入度為0,進棧
                           Push(&S,i);
             }
         printf("Topological Sort is /n");
         while(!StackEmpty(&S)) //棧不空,出棧並輸出結果
               {
                      Pop(&S,&n);
                      printf("%d  ",G.vertices[n].data);
                      count++;//累加輸出點個數
                      for(p=G.vertices[n].firstarc;p!=NULL;p=p->nextarc)
                            {//鄰接表的優點,計算入度的改變
                                         k=p->adjvex;
                                         if(!(--indegree[k]))
                                            Push(&S,k);//入度為0,進棧
                            }
               }
         if(count<G.vexnum)
               printf("Error!/n");
         else
               printf("/nSort Success !");
      }    
    //*********************主函數**********************                    
  int main()
    {
                ALGraph G;
                CreatGraph(&G);
                TopologicalSort(G);
                getch();
                return 0;
    }                                                                                          
                                                                                                                                                        
        
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.