标签:
伪代码:
TopLogical(G) call DFS to compute finishtime as each vertex finished , insert it onto the front of a linked list return the linked list of vertices
实现如下:
#include <iostream> #include <vector> #include <memory> using namespace std; vector<vector<int>> graphic; vector<int> res; bool *visit; int length; void init() { visit = new bool[length]; cout<<"init:"<<length<<endl; for(int i = 0 ; i < length ; ++i) { visit[i] = false; } } void Dfs(int cur) { if(visit[cur] ) { return ; } visit[cur] = true; for(int i = 0 ; i < graphic[cur].size() ; ++i) { Dfs(graphic[cur][i]); } cout<<"dfs:"<<cur<<endl; res.push_back(cur); } ///void SecondDfs() //{ //} int main() { length = 7; vector<int> ve; init(); for(int i = 0 ; i < length ; ++i) { graphic.push_back(ve); } graphic[0].push_back(1); graphic[0].push_back(3); graphic[1].push_back(2); graphic[1].push_back(3); graphic[2].push_back(6); graphic[4].push_back(2); graphic[4].push_back(5); graphic[5].push_back(6); for(int i = 0 ; i < length ; ++i) { Dfs(i); } for(int i = res.size()-1 ; i >= 0; --i) { cout<<res[i]<<" "; } return 0; }
标签:
原文地址:http://www.cnblogs.com/fengyuehan/p/4649702.html