C++實現(xiàn)拓撲排序(AOV網(wǎng)絡(luò))
本文實例為大家分享了C++實現(xiàn)拓撲排序的具體代碼,供大家參考,具體內(nèi)容如下
一、思路

先掃描所有頂點,把入度為0的頂點(如C,E)進棧。然后,取棧頂元素,退棧,輸出取得的棧頂元素v(即入度為0的頂點v)。接著,把頂點v的鄰接頂點w的入度減1,如果w的入度變?yōu)?,則進棧。接著,取頂點w的兄弟結(jié)點(即取頂點v的鄰接頂點w的下一鄰接頂點),做同樣的操作。重復(fù)上面步驟,直到輸出n個頂點。
如上圖:
(1)掃描所有頂點,把入度為0的頂點進棧:將頂點C,E進棧;
(2)取棧頂元素,退棧,輸出取得的棧頂元素E。接著,把頂點E的鄰接頂點A、B和F的入度減1,如果入度變?yōu)?,則進棧。因為頂點A入度變?yōu)?,所以要進棧;
(3)重復(fù)(2)步驟,直到輸出n個頂點。
二、實現(xiàn)程序:
1.Graph.h:有向圖
#ifndef Graph_h
#define Graph_h
#include <iostream>
using namespace std;
const int DefaultVertices = 30;
template <class T, class E>
struct Edge { // 邊結(jié)點的定義
int dest; // 邊的另一頂點位置
Edge<T, E> *link; // 下一條邊鏈指針
};
template <class T, class E>
struct Vertex { // 頂點的定義
T data; // 頂點的名字
Edge<T, E> *adj; // 邊鏈表的頭指針
};
template <class T, class E>
class Graphlnk {
public:
const E maxValue = 100000; // 代表無窮大的值(=∞)
Graphlnk(int sz=DefaultVertices); // 構(gòu)造函數(shù)
~Graphlnk(); // 析構(gòu)函數(shù)
void inputGraph(int count[]); // 建立鄰接表表示的圖
void outputGraph(); // 輸出圖中的所有頂點和邊信息
T getValue(int i); // 取位置為i的頂點中的值
bool insertVertex(const T& vertex); // 插入頂點
bool insertEdge(int v1, int v2); // 插入邊
bool removeVertex(int v); // 刪除頂點
bool removeEdge(int v1, int v2); // 刪除邊
int getFirstNeighbor(int v); // 取頂點v的第一個鄰接頂點
int getNextNeighbor(int v,int w); // 取頂點v的鄰接頂點w的下一鄰接頂點
int getVertexPos(const T vertex); // 給出頂點vertex在圖中的位置
int numberOfVertices(); // 當前頂點數(shù)
private:
int maxVertices; // 圖中最大的頂點數(shù)
int numEdges; // 當前邊數(shù)
int numVertices; // 當前頂點數(shù)
Vertex<T, E> * nodeTable; // 頂點表(各邊鏈表的頭結(jié)點)
};
// 構(gòu)造函數(shù):建立一個空的鄰接表
template <class T, class E>
Graphlnk<T, E>::Graphlnk(int sz) {
maxVertices = sz;
numVertices = 0;
numEdges = 0;
nodeTable = new Vertex<T, E>[maxVertices]; // 創(chuàng)建頂點表數(shù)組
if(nodeTable == NULL) {
cerr << "存儲空間分配錯誤!" << endl;
exit(1);
}
for(int i = 0; i < maxVertices; i++)
nodeTable[i].adj = NULL;
}
// 析構(gòu)函數(shù)
template <class T, class E>
Graphlnk<T, E>::~Graphlnk() {
// 刪除各邊鏈表中的結(jié)點
for(int i = 0; i < numVertices; i++) {
Edge<T, E> *p = nodeTable[i].adj; // 找到其對應(yīng)鏈表的首結(jié)點
while(p != NULL) { // 不斷地刪除第一個結(jié)點
nodeTable[i].adj = p->link;
delete p;
p = nodeTable[i].adj;
}
}
delete []nodeTable; // 刪除頂點表數(shù)組
}
// 建立鄰接表表示的圖
template <class T, class E>
void Graphlnk<T, E>::inputGraph(int count[]) {
int n, m; // 存儲頂點樹和邊數(shù)
int i, j, k;
T e1, e2; // 頂點
cout << "請輸入頂點數(shù)和邊數(shù):" << endl;
cin >> n >> m;
cout << "請輸入各頂點:" << endl;
for(i = 0; i < n; i++) {
cin >> e1;
insertVertex(e1); // 插入頂點
}
cout << "請輸入圖的各邊的信息:" << endl;
i = 0;
while(i < m) {
cin >> e1 >> e2;
j = getVertexPos(e1);
k = getVertexPos(e2);
if(j == -1 || k == -1)
cout << "邊兩端點信息有誤,請重新輸入!" << endl;
else {
insertEdge(j, k); // 插入邊
count[k]++; // 記錄入度
i++;
}
} // while
}
// 輸出有向圖中的所有頂點和邊信息
template <class T, class E>
void Graphlnk<T, E>::outputGraph() {
int n, m, i;
T e1, e2; // 頂點
Edge<T, E> *p;
n = numVertices;
m = numEdges;
cout << "圖中的頂點數(shù)為" << n << ",邊數(shù)為" << m << endl;
for(i = 0; i < n; i++) {
p = nodeTable[i].adj;
while(p != NULL) {
e1 = getValue(i); // 有向邊<i, p->dest>
e2 = getValue(p->dest);
cout << "<" << e1 << ", " << e2 << ">" << endl;
p = p->link; // 指向下一個鄰接頂點
}
}
}
// 取位置為i的頂點中的值
template <class T, class E>
T Graphlnk<T, E>::getValue(int i) {
if(i >= 0 && i < numVertices)
return nodeTable[i].data;
return NULL;
}
// 插入頂點
template <class T, class E>
bool Graphlnk<T, E>::insertVertex(const T& vertex) {
if(numVertices == maxVertices) // 頂點表滿,不能插入
return false;
nodeTable[numVertices].data = vertex; // 插入在表的最后
numVertices++;
return true;
}
// 插入邊
template <class T, class E>
bool Graphlnk<T, E>::insertEdge(int v1, int v2) {
if(v1 == v2) // 同一頂點不插入
return false;
if(v1 >= 0 && v1 < numVertices && v2 >= 0 && v2 < numVertices) {
Edge<T, E> *p = nodeTable[v1].adj; // v1對應(yīng)的邊鏈表頭指針
while(p != NULL && p->dest != v2) // 尋找鄰接頂點v2
p = p->link;
if(p != NULL) // 已存在該邊,不插入
return false;
p = new Edge<T, E>; // 創(chuàng)建新結(jié)點
p->dest = v2;
p->link = nodeTable[v1].adj; // 鏈入v1邊鏈表
nodeTable[v1].adj = p;
numEdges++;
return true;
}
return false;
}
// 有向圖刪除頂點較麻煩
template <class T, class E>
bool Graphlnk<T, E>::removeVertex(int v) {
if(numVertices == 1 || v < 0 || v > numVertices)
return false; // 表空或頂點號超出范圍
Edge<T, E> *p, *s;
// 1.清除頂點v的邊鏈表結(jié)點w 邊<v,w>
while(nodeTable[v].adj != NULL) {
p = nodeTable[v].adj;
nodeTable[v].adj = p->link;
delete p;
numEdges--; // 與頂點v相關(guān)聯(lián)的邊數(shù)減1
} // while結(jié)束
// 2.清除<w, v>,與v有關(guān)的邊
for(int i = 0; i < numVertices; i++) {
if(i != v) { // 不是當前頂點v
s = NULL;
p = nodeTable[i].adj;
while(p != NULL && p->dest != v) {// 在頂點i的鏈表中找v的頂點
s = p;
p = p->link; // 往后找
}
if(p != NULL) { // 找到了v的結(jié)點
if(s == NULL) { // 說明p是nodeTable[i].adj
nodeTable[i].adj = p->link;
} else {
s->link = p->link; // 保存p的下一個頂點信息
}
delete p; // 刪除結(jié)點p
numEdges--; // 與頂點v相關(guān)聯(lián)的邊數(shù)減1
}
}
}
numVertices--; // 圖的頂點個數(shù)減1
nodeTable[v].data = nodeTable[numVertices].data; // 填補,此時numVertices,比原來numVertices小1,所以,這里不需要numVertices-1
nodeTable[v].adj = nodeTable[numVertices].adj;
// 3.要將填補的頂點對應(yīng)的位置改寫
for(int i = 0; i < numVertices; i++) {
p = nodeTable[i].adj;
while(p != NULL && p->dest != numVertices) // 在頂點i的鏈表中找numVertices的頂點
p = p->link; // 往后找
if(p != NULL) // 找到了numVertices的結(jié)點
p->dest = v; // 將鄰接頂點numVertices改成v
}
return true;
}
// 刪除邊
template <class T, class E>
bool Graphlnk<T, E>::removeEdge(int v1, int v2) {
if(v1 != -1 && v2 != -1) {
Edge<T, E> * p = nodeTable[v1].adj, *q = NULL;
while(p != NULL && p->dest != v2) { // v1對應(yīng)邊鏈表中找被刪除邊
q = p;
p = p->link;
}
if(p != NULL) { // 找到被刪除邊結(jié)點
if(q == NULL) // 刪除的結(jié)點是邊鏈表的首結(jié)點
nodeTable[v1].adj = p->link;
else
q->link = p->link; // 不是,重新鏈接
delete p;
return true;
}
}
return false; // 沒有找到結(jié)點
}
// 取頂點v的第一個鄰接頂點
template <class T, class E>
int Graphlnk<T, E>::getFirstNeighbor(int v) {
if(v != -1) {
Edge<T, E> *p = nodeTable[v].adj; // 對應(yīng)鏈表第一個邊結(jié)點
if(p != NULL) // 存在,返回第一個鄰接頂點
return p->dest;
}
return -1; // 第一個鄰接頂點不存在
}
// 取頂點v的鄰接頂點w的下一鄰接頂點
template <class T, class E>
int Graphlnk<T, E>::getNextNeighbor(int v,int w) {
if(v != -1) {
Edge<T, E> *p = nodeTable[v].adj; // 對應(yīng)鏈表第一個邊結(jié)點
while(p != NULL && p->dest != w) // 尋找鄰接頂點w
p = p->link;
if(p != NULL && p->link != NULL)
return p->link->dest; // 返回下一個鄰接頂點
}
return -1; // 下一個鄰接頂點不存在
}
// 給出頂點vertex在圖中的位置
template <class T, class E>
int Graphlnk<T, E>::getVertexPos(const T vertex) {
for(int i = 0; i < numVertices; i++)
if(nodeTable[i].data == vertex)
return i;
return -1;
}
// 當前頂點數(shù)
template <class T, class E>
int Graphlnk<T, E>::numberOfVertices() {
return numVertices;
}
#endif /* Graph_h */
2.TopLogicalSort.h
#ifndef TopLogicalSort_h
#define TopLogicalSort_h
#include "Graph.h"
template <class T, class E>
void TopLogicalSort(Graphlnk<T, E> &G) {
int i, w, v;
int n; // 頂點數(shù)
int *count = new int[DefaultVertices]; // 入度數(shù)組
int top = -1;
// 清零
for(i = 0; i< DefaultVertices; i++)
count[i] = 0;
// 輸入頂點和邊
G.inputGraph(count);
n = G.numberOfVertices(); // 獲取圖的頂點數(shù)
for(i = 0; i < n; i++) { // 檢查網(wǎng)絡(luò)所有頂點
if(count[i] == 0) { // 入度為0的頂點進棧
count[i] = top;
top = i;
}
}
// 進行拓撲排序,輸出n個頂點
for(i = 0; i < n; i++) {
if(top == -1) { // 空棧
cout << "網(wǎng)絡(luò)中有回路!" << endl;
return;
} else {
v = top;
top = count[top];
cout << G.getValue(v) << " "; // 輸出入度為0的頂點
w = G.getFirstNeighbor(v); // 鄰接頂點
while(w != -1) { // 掃描出邊表
if(--count[w] == 0) { // 鄰接頂點入度減1,如果入度為0則進棧
count[w] = top;
top = w;
}
w = G.getNextNeighbor(v, w); // 兄弟結(jié)點(取頂點v的鄰接頂點w的下一鄰接頂點)
}
}
}
cout << endl;
}
#endif /* TopLogicalSort_h */
3.main.cpp
#include "TopLogicalSort.h"
int main(int argc, const char * argv[]) {
Graphlnk<char, int> G; // 聲明圖對象
TopLogicalSort(G); // AOV網(wǎng)絡(luò)的拓撲排序
return 0;
}
測試數(shù)據(jù):
6 8
A B C D E F
A B
A D
B F
C B
C F
E A
E F
E B
測試結(jié)果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++調(diào)用libcurl開源庫實現(xiàn)郵件的發(fā)送功能流程詳解
libcurl是一個免費開源的網(wǎng)絡(luò)傳輸庫,支持ftp、ftps、tftp,http、https、telnet、ldap、pop3、smtp等多種協(xié)議,接下來讓我們一起來了解吧2021-11-11
vscode C++遠程調(diào)試運行(學(xué)習(xí)C++用)
這篇文章主要介紹了vscode C++遠程調(diào)試運行(學(xué)習(xí)C++用),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04

