AOV 네트워크는 정점 사이에 우선 순위가 있지만 루프 구조가없는 방향성 그래프입니다.
즉, 한 가지 일이 끝나면 돌아가서 다시 할 필요가 없습니다.
토폴로지 정렬은 유 방향 그래프의 토폴로지 시퀀스를 구성하는 과정으로, 토폴로지 정렬을 구성 할 때
출력은 처음부터 내림차순으로 위의 그림과 같습니다.
출력 정점이 모두 정점이면 루프가없는 AOV 네트워크
이고, 출력 정점이 하나 적 으면 루프가있는 AOV 네트워크입니다.
토폴로지 정렬의 실현 아이디어
1. 유 방향 그래프의 인접 테이블을 구성하고 그래프의 구조 정보를 저장합니다
. 2. 0 도의 정점 정보를 저장할 스택 Q 또는 큐 Q를
구성합니다 (스택도 구성 할 수 있음). 토폴로지 정렬 결과를 저장하는 데 사용되는 T 또는 대기열 T)
3 Q에 정점 m이있는
경우 다음 단계를 수행합니다.
3.1 : Q에서 정점 m을
삭제하고 T에 저장합니다. 3.2 : 정점 m, <m, n>과 관련된 외각 모서리 삭제
3.3 : 정점 n 결정 indegree가 0이면 Q에 저장하십시오.
위는 0 도의 첫 번째 정점에서 시작한 다음 두 번째 정점을 찾아 레이어별로 처리하는 것을 의미합니다.
토폴로지 정렬의 실현
#include<iostream>
using namespace std;
#define MAX 25
typedef char Vertype;
typedef int Edgetype;
typedef int Status;
//构造图的有向图的邻接表结构体
typedef struct EdgeNode//边表结点 存放每个顶点的邻接点
{
int adjvex;//边表下标
Edgetype weight;//边表权重 若边不存在时即无NULL
struct EdgeNode *next;//指向下一个邻接点
}EdgeNode;
typedef struct VerNode//顶点表 存放顶点
{
int in;
Vertype data;//顶点元素
EdgeNode *firstedge;
}VerNode, AdjList[MAX];//邻接表的 顶点元素 和指向邻点的指针
typedef struct
{
AdjList adjList;//邻接表
int numVer, numEdge;//顶点数目和边的数目
}GraphAdjList;
//构造两个栈
typedef struct Stack
{
int data[MAX];
//int pop;
}SqStack;
//生成邻接表
Status CreatGraph(GraphAdjList &G)
{
int i, j, k;
Edgetype w;
EdgeNode *e;
cout << "Enter the number of vertices :" << endl;
cin >> G.numVer;
cout << "Enter the number of Edges :" << endl;
cin >> G.numEdge;
cout << "Input vertex content :" << endl;
for (i = 0; i < G.numVer; i++)
{
cin >> G.adjList[i].data;//输入顶点元素
G.adjList[i].in = 0;
G.adjList[i].firstedge = NULL;//初始化邻边表为NULL;
}
for (k = 0; k < G.numEdge; k++)
{
cout << "Enter the vertex number of the edge (Vi->Vj)" << endl;
cin >> i;
cin >> j;
cout << "Enter the weight of edge" << i << "-" << j << endl;
cin >> w;
e = new EdgeNode;//将两个顶点相结即可。
e->adjvex = j;// 邻接序号为j
e->next = G.adjList[i].firstedge;//i的第一个邻接指针 为e的指针
e->weight = w;
G.adjList[i].firstedge = e;
G.adjList[j].in++;
//有向图则只有生成一次即可
/*
e = new EdgeNode;
e->adjvex = i;//无向图 重复一遍
e->next = G.adjList[j].firstedge;
G.adjList[j].firstedge = e;
e->weight = w;*/
}
return 0;
}
//拓扑排序
Status TOpologicalSort(GraphAdjList &G, SqStack &Q)
{
EdgeNode *e;
int i, j, k, gettop;
int top = 0;
int count = 0;
for (i = 0; i < G.numVer; i++)
{
if (G.adjList[i].in == 0)
Q.data[++top] = i;
}
while (top != 0)
{
gettop = Q.data[top--];//出栈 入度为0的下标
cout << G.adjList[gettop].data << "->";
count++;//统计拓扑网顶点数目
//后面输出其边顶点
//并删除边,使得边顶点入度-1
for (e = G.adjList[gettop].firstedge; e; e = e->next)
{
j = e->adjvex;
if (!(--G.adjList[j].in))//如果入度为1时 自减后进入循环 如果入度不为1,自减后 相当于边的数目减1
{
Q.data[++top] = j;
}
}
}
if (count < G.numVer)
{
cout << "不是一个网图" << endl;
return NULL;
}
else
cout << "是一个网图"<< endl;
return 0;
return 0;
}
int main()
{
GraphAdjList G;
CreatGraph(G);
SqStack Q;
TOpologicalSort(G, Q);
system("pause");
return 0;
}
출력해야하는 경우 큐를 추가하여 ++ top에 요소를 저장하십시오.