拓扑排序

Realize topological sort with adjacent matrix:

 

#include<iostream>
#include<vector>
using namespace std;
int main(int argc,char **argv)
{
	int adjacent[7][7]={{0,1,1,1,0,0,0},
				{0,0,0,1,1,0,0},
				{0,0,0,0,0,1,0},
				{0,0,1,0,0,1,1},
				{0,0,0,1,0,0,1},
				{0,0,0,0,0,0,0},
				{0,0,0,0,0,1,0}};
	vector<int> res;
	int i=0;
	int k=0;
	for(;k<7;k++)
	{
		if(adjacent[0][k]==2)
			continue;
		int j=0;
		for(;j<7;j++)
			if(adjacent[j][k]==1)
				break;
		if(j==7)
		{
			res.push_back(k+1);
			for(int q=0;q<7;q++)
				adjacent[k][q]=0;
			adjacent[0][k]=2;
			if(res.size()==7)
			{
				vector<int>::iterator iter=res.begin();
				for(;iter!=res.end();++iter)
					cout<<*iter<<" ";
				cout<<endl;
				return 0;
			}
			k=0;
		}
	}
	if(k==7)
	{
		cout<<"the graph include circute route!"<<endl;
		return 0;
	}
}



posted @ 2010-10-04 11:49  张朝阳  阅读(293)  评论(0编辑  收藏  举报