邻接表储存的图拓扑排序
生活随笔
收集整理的這篇文章主要介紹了
邻接表储存的图拓扑排序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
拓撲排序,可以檢查圖中是否存在環,若圖中的頂點都在他的 拓撲排序中,則它不存在環,若有的點不存在拓撲排序序列中,不在序列的結點存在環,或者它在環之中。
//拓撲排序 #include<iostream> #define max 100 #define maxint 32726 #define maxvex 100 using namespace std ; typedef struct stack{char *base;char *top;int maxsize; }Stack; //定義棧 void initstack(Stack &S){S.base=new char [max];if(!S.base){return ;} S.top=S.base;S.maxsize=max; } //入棧 char pushstack(Stack &S ,char e){if(S.top-S.base==S.maxsize){printf("棧已滿\n");return 0;}*S.top++=e; } //出棧 char popstack (Stack &S){char e;if(S.top==S.base){printf("棧已空\n");return 0; }e=*--S.top;return e; } //判斷???int emptystack(Stack &S){if(S.top==S.base)return 1;else return 0; } //邊節點 typedef struct vexnode {int adjvex;char v;struct vexnode *nextarc;float info; }ArNode; typedef struct Vnode { //頂點信息 char data;ArNode *firstarc; //指向第一個邊結點 }Vonde,Adjust[100]; typedef struct {Adjust survice;//鄰接表 int vexnum,acrnum;//圖的當前頂點數和邊數 }ALgraph; //鄰接表查找UNDint locate1(ALgraph&R, char v,int n){int j,i;for(i=1;i<=n;i++){if(R.survice[i].data==v){j=i;}}return j;} void creatUND(ALgraph &R){char v1,v2;int i1,i2;//兩個頂點的位置和權值 float i3;printf("請輸入頂點數和邊數:"); cin>>R.vexnum>>R.acrnum; //輸入頂點數和邊數 printf("請輸入各個頂點的值:\n"); for(int i=1;i<=R.vexnum;i++){cin>>R.survice[i].data;//輸入各個頂點的信息 R.survice[i].firstarc=NULL;}printf("請輸入每條邊依附的兩個結點和權值:\n");for(int i=1;i<=R.acrnum;i++){cin>>v1>>v2>>i3;i1=locate1(R,v1,R.vexnum);i2=locate1(R,v2,R.vexnum);ArNode *p1=new ArNode;p1->adjvex=i1;//記錄頂點的位置 p1->info=i3;//記錄權值 p1->v=v2;//記錄邊點的值 p1->nextarc=R.survice[i1].firstarc;R.survice[i1].firstarc=p1;// 將新節點*p2插入頂點Vi1的鄰接表中 } ArNode *p2; for(int i=1;i<=R.vexnum;i++){p2=R.survice[i].firstarc;while(p2){printf("<%c , %c> : %.1f ",R.survice[i].data,p2->v,p2->info);p2=p2->nextarc;}printf("\n");} } //拓撲排序void Toposort(ALgraph &R ,Stack &S){int degree[R.vexnum+1]={0};//記錄各個結點的入度 char topo[R.vexnum];//排序之后存入的數組 ArNode *p; for(int i=1;i<=R.vexnum;i++){//遍歷將各個結點的入度記錄在數組中 p=R.survice[i].firstarc;while(p){int b=locate1(R,p->v,R.vexnum);degree[b]++;p=p->nextarc;} }char s;//入度為零的結點 char m;//出棧元素 int k=0,u; initstack(S);for(int i=1;i<=R.vexnum;i++){//將頂點入度為零的點入棧 if(degree[i]==0){s=R.survice[i].data;pushstack(S,s);}}ArNode *p1;//棧非空的情況下進行以下操作 while(!emptystack(S)){m=popstack(S);//棧頂元素出棧 topo[k]=m;//把輸出的元素放到此數組中 k++;//下標進行加加 u=locate1(R,m,R.vexnum);//找到此頂點的下標,讓其入度減一 degree[u]--;p1=R.survice[u].firstarc;//訪問它的臨結點 while(p1){degree[locate1(R,p1->v,R.vexnum)]--;//讓輸出結點的臨結點的入度減一if(degree[locate1(R,p1->v,R.vexnum)]==0){//如果臨結點的入度等于零的話,讓其進棧 pushstack(S,p1->v);}p1=p1->nextarc; }/*for(int i=1;i<=R.vexnum;i++){if(degree[i]==0){pushstack(S,R.survice[i].data);}} */}printf("拓撲排序結果為:"); for(int i=0;i<k;i++){printf("%c " ,topo[i]);}printf("\n");if(k<R.vexnum-1){printf("該有向圖有環\n");}else{printf("該有向圖沒環\n"); } } /* 輸入頂點數 6 邊數 8 各個頂點的值 1 2 3 4 5 6 7 8 分別輸入的頂點和權值 1 2 1 1 4 1 1 3 1 3 2 1 3 5 1 4 5 1 6 4 1 6 5 1 */int main(){Stack S; ALgraph R;creatUND(R);Toposort(R ,S); }總結
以上是生活随笔為你收集整理的邻接表储存的图拓扑排序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VC2017 opencv 的项目属性继
- 下一篇: FlexNet Code Insight