日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

AOV网拓扑排序(c/c++)

發(fā)布時間:2025/6/17 c/c++ 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 AOV网拓扑排序(c/c++) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

拓撲排序

工程是否順序進行,流程是否合理,采用AOV網(wǎng)來表示,頂點用來表示工程(活動),弧表示工程間的順序關(guān)系。如a有一個指向b的弧,意味著a結(jié)束了b才能開始(a為弧尾,b為弧頭)
算法思路:1,在有向圖中找到無前驅(qū)(入度為0)的結(jié)點V,輸出。2,刪除V及以V為弧尾的弧。3,重復1,2,輸出全部結(jié)點/或者網(wǎng)中沒有無前驅(qū)的點(沒有入度為0的點,即存在環(huán))
拓撲排序采用鄰接表較方便實現(xiàn),下面給出以鄰接表存儲圖的算法:

void topoSort(aGraph& G) {int* inDegree = new int[G.vexnum];int i, j;arcNode* temp;for (i = 0; i < G.vexnum; i++)inDegree[i] = 0;for (i = 0; i < G.vexnum; i++)for (temp = G.vertices[i].firstArc; temp; temp = temp->nextArc)inDegree[temp->adjvex]++;//保存所有結(jié)點的入度int* stack = new int[G.vexnum];//用棧來保存入度為0的點,當然,用別的存儲結(jié)構(gòu)也行int top = -1;for (i = 0; i < G.vexnum;i++)if (inDegree[i] == 0)stack[++top] = i;int count = 0;//判斷是否有環(huán)while (top != -1){j = stack[top--];std::cout << G.vertices[j].data << ' ';++count;for (temp = G.vertices[j].firstArc; temp; temp = temp->nextArc){inDegree[temp->adjvex]--;if (inDegree[temp->adjvex]==0)stack[++top] = temp->adjvex;}}if (count != G.vexnum)std::cout << "存在環(huán)!" << '\n'; }
完整示例

輸入為下面的有向圖,求它的拓撲排序:

#include<iostream> #define maxSize 7//頂點數(shù)目 #define MAX 11//邊的個數(shù)//鄰接表 typedef struct arcNode {int adjvex;//與該邊所連頂點的序號//int weight;//邊上的權(quán)值struct arcNode* nextArc; }arcNode;//邊表結(jié)點 typedef struct {char data;//頂點中存儲的數(shù)據(jù)arcNode* firstArc; }adjList;//頂點 typedef struct{int vexnum, arcnum;adjList vertices[maxSize]; }aGraph;void topoSort(aGraph& G);//拓撲排序 int main() {using namespace std;aGraph G;//鄰接矩陣存儲圖并進行初始化G.vexnum = maxSize;G.arcnum = MAX;char vexData[maxSize] = { 'a', 'b', 'c', 'd', 'e','f','g' };//頂點信息int arcData[MAX][2] = { { 0, 2 }, { 0, 3 }, { 0, 4 }, { 1, 4 }, { 1, 3 }, { 2, 5 }, { 4, 3 },{ 3, 6 },{ 3, 5 }, { 4, 6 }, { 4, 5 } };//連接的邊int i, j;for (i = 0; i < G.vexnum; ++i){G.vertices[i].data = vexData[i];G.vertices[i].firstArc = NULL;for (j = 0; j < G.arcnum;++j)if (i == arcData[j][0]){arcNode* temp = new arcNode;temp->adjvex = arcData[j][1];temp->nextArc = G.vertices[i].firstArc;G.vertices[i].firstArc = temp;//通過頭插法構(gòu)造}}//鄰接表初始化完畢cout << "拓撲排序: ";topoSort(G);cout << endl;//釋放空間(類似析構(gòu)函數(shù))arcNode* temp=NULL,* tt=NULL;for (i = 0; i < G.vexnum; i++){if (G.vertices[i].firstArc){temp = G.vertices[i].firstArc;while (temp){tt = temp->nextArc;delete temp;temp = tt;}}}return 0; }void topoSort(aGraph& G) {int* inDegree = new int[G.vexnum];int i, j;arcNode* temp;for (i = 0; i < G.vexnum; i++)inDegree[i] = 0;for (i = 0; i < G.vexnum; i++)for (temp = G.vertices[i].firstArc; temp; temp = temp->nextArc)inDegree[temp->adjvex]++;//保存所有結(jié)點的入度int* stack = new int[G.vexnum];//用棧來保存入度為0的點,當然,用別的存儲結(jié)構(gòu)也行int top = -1;for (i = 0; i < G.vexnum;i++)if (inDegree[i] == 0)stack[++top] = i;int count = 0;//判斷是否有環(huán)while (top != -1){j = stack[top--];std::cout << G.vertices[j].data << ' ';++count;for (temp = G.vertices[j].firstArc; temp; temp = temp->nextArc){inDegree[temp->adjvex]--;if (inDegree[temp->adjvex]==0)stack[++top] = temp->adjvex;}}if (count != G.vexnum)std::cout << "存在環(huán)!" << '\n'; }

輸出結(jié)果為:

總結(jié)

以上是生活随笔為你收集整理的AOV网拓扑排序(c/c++)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。