本程序主要是参考:算法导论(第三版),分析过程就省略了,其中还给出了深度优先搜索的应用—拓扑排序
grsph.h
#pragma once
#include<iostream>
#include<string>
#include<vector>
using namespace std;
template<typename Comparable>
struct Node
{
Comparable element;//结点的元素
vector<Node<Comparable>*> next; //结点的连接指针,指向多个结点
vector<Node<Comparable>*> father; //父亲结点
string color; //结点的颜色用于区分是否被搜索
int dis; //广义优先搜索时的记录从源结点到该节点的距离
int begin; //深度优先搜索时涂上灰色的时间
int end; //深度优先搜索时涂上黑色的时间
Node(Comparable x,string c,int d,int b,int e)
{
element=x;
color=c;
dis=d;
begin=b;
end=e;
}
};
template<typename Comparable>
class graph
{
public:
void insert(Comparable *a,int *matrix,int n); //a:图中个结点的元素;matrix:邻接矩阵
void BfsGraph(Comparable x); //从元素为x的结点为源结点开始广度优先搜索
void DfsGraph(Comparable x); //深度优先搜索
void Topsort(); //进行拓扑排序,主要针对有向无环图
private:
Node<Comparable>* Findnode(Comparable x);
void DfsGraph(Node<Comparable>* s);
vector<Node<Comparable>*> root; //存储每个结点的地址
int time; //深度优先搜索时,保存时间
};
grsph.cpp
#include "stdafx.h"
#include"grsph.h"
#include<string>
#include<vector>
#include<queue>
#include<iostream>
using namespace std;
template<typename Comparable>
void graph<Comparable>::insert(Comparable *a,int *matrix,int n)
{
for(int i=0;i<n;i++)
{
Node<Comparable>* node=new Node<Comparable>(a[i],"WHITE",0,0,0);
root.push_back(node);
}
Node<Comparable>* temp=NULL;
Node<Comparable>* node=NULL;
for(int i=0;i<n;i++)
{
node=root[i];
for(int j=0;j<n;j++)
{
if(matrix[n*i+j]!=0)
{
temp=root[j];
node->next.push_back(temp);
temp->father.push_back(node);
}
}
}
}
template<typename Comparable>
Node<Comparable>* graph<Comparable>::Findnode(Comparable x)//寻找结点元素是x的结点
{
Node<Comparable>* s=NULL;
int n=root.size();
for(int i=0;i<n;i++)
if(root[i]->element==x)
s=root[i];
return s;
}
template<typename Comparable>
void graph<Comparable>::BfsGraph(Comparable x)
{
Node<Comparable>* s=Findnode(x);
if(s==NULL)
return;
Node<Comparable>* u=NULL;
queue<Node<Comparable>*> Q;
s->color="GRAY";
cout<<s->element<<" ";
s->dis=0;
Q.push(s);
while(!Q.empty())
{
u=Q.front();
Q.pop();
int n=u->next.size();
for(int i=0;i<n;i++)
{
Node<Comparable>* v=u->next[i];
if(v->color=="WHITE")
{
v->color="GRAY";
cout<<v->element<<" ";
v->dis=u->dis+1;
Q.push(v);
}
u->color="BLACK";
}
}
cout<<endl;
}
template<typename Comparable>
void graph<Comparable>::DfsGraph(Comparable x)
{
Node<Comparable>* u=Findnode(x);
if(u==NULL)
return;
time=0;
DfsGraph(u);
int n=root.size();
for(int i=0;i<n;i++)
if(root[i]->color=="WHITE")
DfsGraph(root[i]);
cout<<endl;
}
template<typename Comparable>
void graph<Comparable>::DfsGraph(Node<Comparable>* u)
{
cout<<u->element<<" ";
time=time+1;
u->begin=time;
u->color="GRAY";
Node<Comparable>* temp=NULL;
int n=u->next.size();
for(int i=0;i<n;i++)
{
temp=u->next[i];
if(temp->color=="WHITE")
DfsGraph(temp);
}
u->color="BLACK";
time=time+1;
u->end=time;
}
template<typename Comparable>
void graph<Comparable>::Topsort()//先进行深度优先搜索,然后在将所有结点按完成时间的逆序输出
{
int n=root.size();
Node<Comparable>* temp=NULL;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(root[j]->end>root[i]->end)
{
temp=root[i];
root[i]=root[j];
root[j]=temp;
}
}
}
for(int i=0;i<n;i++)
cout<<"("<<root[i]->element<<","<<root[i]->end<<")"<<" ";
cout<<endl;
}
Alogrithm-graph1.cpp
// Alogrithm-graph1.cpp : 定义控制台应用程序的入口点。
//主要是实现图的广度优先搜索和深度优先搜索
#include "stdafx.h"
#include"grsph.h"
#include"grsph.cpp"
#include<queue>
#include<string>
#include<iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
graph<string> g;
int n;
cout<<"输入图中结点数目:"<<endl;
cin>>n;
int *matrix=new int[n*n];
cout<<"输入邻接矩阵:"<<endl;
for(int i=0;i<n*n;i++)
{
int x;
cin>>x;
matrix[i]=x;
}
string *a=new string[n];
cout<<"输入结点的元素:"<<endl;
for(int i=0;i<n;i++)
{
string x;
cin>>x;
a[i]=x;
}
/* int n=8;
int matrix[64]={0,1,0,0,1,0,0,0,
1,0,0,0,0,1,0,0,
0,0,0,1,0,1,1,0,
0,0,1,0,0,0,1,1,
1,0,0,0,0,0,0,0,
0,1,1,0,0,0,1,0,
0,0,1,1,0,1,0,1,
0,0,0,1,0,0,1,0};
string a[8]={"r","s","t","u","v","w","x","y"};*/ //测试广度优先搜索
/* int n=6;
int matrix[36]={0,1,0,1,0,0,
0,0,0,0,1,0,
0,0,0,0,1,1,
0,1,0,0,0,0,
0,0,0,1,0,0,
0,0,0,0,0,1};
string a[6]={"u","v","w","x","y","z"};*/ //测试深度优先搜索
/* int n=9;
int matrix[91]={0,1,0,0,0,0,0,1,0,
0,0,1,0,0,0,0,1,0,
0,0,0,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,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};
string a[9]={"n","k","y","c","l","j","w","x","s"};*/ //测试拓扑排序
g.insert(a,matrix,n);
//g.BfsGraph("s"); //广度优先搜索
g.DfsGraph("c"); //深度优先搜索
g.Topsort(); //拓扑排序,在进行拓扑排序前,先进行深度优先搜索
return 0;
}