通过DFS来遍历图的一个具体形式。
唯一区别的就是要记录其访问次序,每一次访问该节点结束之后记录其终止时间,最后对终止时间进行排序就可以了!时间越大的节点,越应该先做!仅此而已。
程序没有考虑效率,还可以提高,有空在修改吧!
#include <iostream>
#include <stack>
using namespace std;
void topologize(bool** graph,int list[], int n){
int *ft = new int[n]; // finish time;
bool *visited = new bool[n]; // check visisted state;
memset(visited,0,sizeof(bool)*n);
int time = 0;
for(int i = 0 ; i < n; ++i){
stack<int> s;
if(!visited[i]){
s.push(i);
time++;
cout<<"push "<<i <<endl;
}
visited[i] = true;
while(!s.empty()){
int pos = s.top();
int j;
for(j = 0 ; j < n ; ++j){
if(!visited[j] && *((bool*)graph+pos*n+j)){
s.push(j);
cout<<pos<<" push "<<j <<" break"<<endl;
time++;
visited[j] = true;
break;
}
}
if(j==n){
ft[s.top()] = time;
cout<<"pop "<<s.top()<<endl;
time++;
s.pop();
}
}
}
memset(visited,0,sizeof(bool)*n);
for(int i = 0 ;i < n ; ++i){
int min = 10000;
int result = -1;
for(int j = 0; j < n ; ++j){
if(!visited[j]&&ft[j]<min){
min = ft[j];
result = j;
}
}
visited[result] = true;
list[i] = result;
cout<<result<<endl;
}
for(int i = 0 ; i < n ; i++)
cout<<ft[i]<<' ';
cout<<endl;
delete[] visited;
delete[] ft;
}
int main(){
/*const int N = 14;
bool graph[N][N] = {{0,0,0,0,1,1,0,0,0,0,0,1,0,0},
{0,0,1,0,1,0,0,0,1,0,0,1,0,0},
{0,0,0,0,0,1,1,0,0,1,0,1,0,0},
{0,0,1,0,0,0,0,0,0,0,0,0,0,1},
{0,0,0,0,0,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,1,0,0,0,1,0},
{0,0,0,0,0,1,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,1,1,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,1,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},};*/
const int N = 9;
bool graph[N][N] = {{0,0,1,1,0,0,0,0,0},
{0,0,0,1,0,0,0,0,0},
{0,0,0,1,0,1,0,0,0},
{0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,0,0,1},
{0,0,0,0,0,1,0,1,0},
{0,0,0,0,0,0,0,0,1},
{0,0,0,0,0,0,0,0,0}};
int list[N];
topologize((bool**)graph,list,N);
system("PAUSE");
return 0;
}
第二个图输出的结果
push 0
0 push 2 break
2 push 3 break
pop 3
2 push 5 break
5 push 8 break
pop 8
pop 5
pop 2
pop 0
push 1
pop 1
push 4
pop 4
push 6
6 push 7 break
pop 7
pop 6
3 8 5 2 0 1 4 7 6
9 11 8 3 13 7 17 16 6
请按任意键继续. . .
第一个图输出的结果是
push 0
0 push 4 break
4 push 7 break
pop 7
pop 4
0 push 5 break
5 push 8 break
pop 8
5 push 12 break
12 push 9 break
9 push 10 break
10 push 13 break
pop 13
pop 10
9 push 11 break
pop 11
pop 9
pop 12
pop 5
pop 0
push 1
1 push 2 break
2 push 6 break
pop 6
pop 2
pop 1
push 3
pop 3
7 4 8 13 10 11 9 12 5 0 6 2 1 3
19 25 24 27 4 18 23 3 7 16 13 15 17 12
请按任意键继续. . .