这个算法思想很简单,每次挑出入度为0的节点,放到栈中去,然后删掉以他为起点的边,当然相应的要更新与他相连的边的入度,也就是减1.
需要说明的是,下面给出的算法使用的数据结构是关系矩阵,这里只是我偷懒,其实更好的选择应该是连接表,因为AOV一般点与点之间的关系并不是很多,也就是边的数量一般不是很多,所以链接矩阵相应的会更加节省空间吧~~~
还有就是栈采用了一个隐式方法,不好细说,具体就看代码吧,也不是很难理解...
参考代码:
#include<iostream>
#include<fstream>
using namespace std;
#define max 1000
int graph[101][101];
int count[101];
int num;
void topoOrder()
{
for(int i=0;i<num;i++)
{
for(int j=0;j<num;j++)
{
if(graph[i][j]!=0&&graph[i][j]<max)
{
count[j]++;
}
}
}
int top=-1;
for(int i=0;i<num;i++)
{
if(count[i]==0)
{
count[i]=top;
top=i;
}
}
cout<<"Begin"<<endl;
for(int i=0;i<num;i++)
{
if(top==-1)
{
cout