const int MAXSIZE = 50; //顶点最大数目
#include <vector>
using namespace std;
template<typename T>
class CGraph
{
public:
CGraph(void);
~CGraph(void);
private:
vector<T> vecNodes;//顶点列表
int edge[MAXSIZE][MAXSIZE];//边表
int numVertexs;//顶点数
int numEdges;//边数
bool visited[MAXSIZE];//用于图的遍历
int FindVertex(const T& vertex,const vector<T> &lst);
void ClearVisitFlag();
vector<T>& GraphDepthFirstSearch(const T& beginVertex);//深度遍历图
vector<T>& GraphBreadthFirstSearch();//广度遍历
public:
bool GraphEmpty(void)const;
bool GraphFull(void)const;
int NumberOfVertices(void)const;//获取顶点数
int NumberOfEdges(void)const;//获取边数
int GetWeight(const T&vertex1,const T& vertex2);//获取指定两个顶点间的权值
vector<T>& GetNeighbors(const T& vertex);//获取指定顶点的邻接顶点
void CreateGraph();//创建图
int GetVertexPos(const T& vertex);//获取指定顶点的位置
int InsertVertex(const T& vertex);//插入顶点
void InsertEdge(const T& vertex1,const T& vertex2,int weight);//插入边
void DeleteVertex(const T& vertex);//删除顶点
void DeleteEdge(const T& vertex1,const T& vertex2);//删除边
//int MinimumPath(const T& sVertex,const T& desVertex);最短路径
void DepthFirstSearch();//深度遍历图
void BreadthFirstSearch();//广度遍历图
};
图的实现代码
#include "Graph.h"
#include <stack>
#include <queue>
#include <algorithm>
#include <iostream>
using namespace std;
template<typename T>
CGraph<T>::CGraph(void)
{
for (int i=0;i<MAXSIZE;++i)
{
for (int j=0;j<MAXSIZE;++j)
{
this->edge[i][j] = 0;
}
}
this->numVertexs = 0;
this->numEdges = 0;
}
template<typename T>
CGraph<T>::~CGraph(void)
{
this->vecNodes.clear();
for (int i=0;i<MAXSIZE;++i)
{
for (int j=0;j<MAXSIZE;++j)
{
this->edge[i][j] = 0;
}
}
this->numVertexs = 0;
this->numEdges = 0;
}
template<typename T>
int CGraph<T>::NumberOfEdges()const
{
return this->numEdges;
}
template<typename T>
int CGraph<T>::NumberOfVertices()const
{
return this->numVertexs;
}
template<typename T>
int CGraph<T>::GetWeight(const T&vertex1,const T& vertex2)
{
int pos1,pos2;
pos1 = this->GetVertexPos(vertex1);
pos2 = this->GetVertexPos(vertex2);
return this->edge[pos1][pos2];
}
template<typename T>
bool CGraph<T>::GraphFull()const
{
return this->numVertexs>=MAXSIZE;
}
template<typename T>
bool CGraph<T>::GraphEmpty()const
{
return this->numVertexs==0;
}
template<typename T>
int CGraph<T>::InsertVertex(const T& vertex)
{//插入顶点,返回插入位置
int result=-1;
if (!this->GraphFull())
{
this->vecNodes.push_back(vertex);
result = this->numVertexs;
this->numVertexs++;
}
}
template<typename T>
void CGraph<T>::InsertEdge(const T& vertex1,const T& vertex2,int weight)
{//插入边
int pos1,pos2;
pos1 = this->GetVertexPos(vertex1);
pos2 = this->GetVertexPos(vertex2);
if (pos1==-1&&pos2==-1)
{//两个都是新顶点
pos1 = this->InsertVertex(vertex1);
pos2 = this->InsertVertex(vertex2);
}
else if (pos1==-1)
{//一个是新顶点
pos1 = this->InsertVertex(vertex1);
}
else if (pos2==-1)
{
pos2 = this->InsertVertex(vertex2);
}
this->edge[pos1][pos2] = weight;
this->edge[pos2][pos1] = weight;
}
template<typename T>
void CGraph<T>::DeleteVertex(const T& vertex)
{//删除顶点
int pos = this->GetVertexPos(vertex);
if (pos!=-1)
{
this->vecNodes.erase(remove(this->vecNodes.begin(),this->vecNodes.end(),vertex),this->vecNodes.end());//删除顶点
for (int i=0;i<this->numVertexs;++i)
{
if(this->edge[pos][i]>0)
this->edge[pos][i] = 0;
if (this->edge[i][pos]>0)
this->edge[i][pos] = 0;
}
this->numVertexs--;
this->numEdges--;
}
}
template<typename T>
void CGraph<T>::DeleteEdge(const T& vertex1,const T& vertex2)
{//删除边
int pos1,pos2;
pos1 = this->GetVertexPos(vertex1);
pos2 = this->GetVertexPos(vertex2);
if (pos1!=-1&&pos2!=-1)
{
if (this->edge[pos1][pos2]>0)
{
this->edge[pos1][pos2] = 0;
this->edge[pos2][pos1] = 0;
this->numEdges--;
}
}
}
template<typename T>
void CGraph<T>::CreateGraph()
{//创建图
cout<<"输入顶点数,边数: ";
cin>>this->numVertexs;
cin>>this->numEdges;
int i;
T item;
cout<<"输入顶点: ";
for (i=0;i<this->numVertexs;++i)
{
cout<<"第"<<i+1<<"个顶点: ";
cin>>item;
this->vecNodes.push_back(item);
}
T vert1,vert2;
int pos1,pos2,weight;
for (i=0;i<this->numEdges;++i)
{
cout<<"输入第"<<i+1<<"条边(尾,头,权值): ";
cin>>vert1>>vert2>>weight;
pos1 = this->GetVertexPos(vert1);
pos2 = this->GetVertexPos(vert2);
this->edge[pos1][pos2] = weight;
this->edge[pos2][pos1] = weight;
}
}
template<typename T>
int CGraph<T>::GetVertexPos(const T& vertex)
{//获取顶点位置
return this->FindVertex(vertex,this->vecNodes);
}
template<typename T>
int CGraph<T>::FindVertex(const T &vertex,const vector<T> &lst)
{//在顶点列表中找指定数据的下标
int pos = -1;
for (pos = lst.size()-1;pos>=0;--pos)
{
if (lst[pos]==vertex)
{
break;
}
}
return pos;
}
template<typename T>
vector<T>& CGraph<T>::GraphDepthFirstSearch(const T& beginVertex)
{//深度遍历
vector<T> *result = new vector<T>();
vector<T> adjLst;
stack<T> s;
s.push(beginVertex);
int pos;
T vertex;
vector<T>::iterator iter;
while (!s.empty())
{
vertex = s.top();
s.pop();
if (this->FindVertex(vertex,*result)==-1)
{
pos = this->GetVertexPos(vertex);
visited[pos] = true;
(*result).push_back(vertex);
adjLst = this->GetNeighbors(vertex);
for ( iter= adjLst.begin();iter != adjLst.end();++iter)
{
if (this->FindVertex(*iter,*result)==-1)
{
s.push(*iter);
}
}
}
}
return *result;
}
template<typename T>
void CGraph<T>::ClearVisitFlag()
{
for (int i=0;i<this->numEdges;++i)
{
this->visited[i] = false;
}
}
template<typename T>
void CGraph<T>::BreadthFirstSearch()
{
vector<T>::const_iterator iter;
vector<int> vec;
this->ClearVisitFlag();
vec = this->GraphBreadthFirstSearch();
for (iter = vec.begin();iter!=vec.end();++iter)
{
cout<<*iter<<" ";
}
this->ClearVisitFlag();
}
template<typename T>
vector<T>& CGraph<T>::GraphBreadthFirstSearch()
{//广度遍历
vector<T> *result = new vector<T>();
vector<T> adjLst;
vector<T>::iterator iter;
queue<T> q;
T item;
int pos;
this->ClearVisitFlag();
for (int i=0;i<this->numVertexs;++i)
{
if (!this->visited[i])
{
visited[i] = true;
result->push_back(this->vecNodes[i]);
q.push(this->vecNodes[i]);
while (!q.empty())
{
item = q.front();
q.pop();
adjLst = this->GetNeighbors(item);
for ( iter= adjLst.begin();iter != adjLst.end();++iter)
{
if (this->FindVertex(*iter,*result)==-1)
{
result->push_back(*iter);
pos = this->GetVertexPos(*iter);
visited[pos] = true;
}
}
}
}
}
return *result;
}
template<typename T>
vector<T>& CGraph<T>::GetNeighbors(const T& vertex)
{//获取邻接顶点
vector<T> *result;
result = new vector<T>();
int pos = this->GetVertexPos(vertex);
if (pos==-1)
{
cerr<<"顶点不在图中"<<endl;
return *result;
}
for (int i=0;i<this->numVertexs;++i)
{
if (edge[pos][i]>0)
{
result->push_back(this->vecNodes[i]);
}
}
return *result;
}
template<typename T>
void CGraph<T>::DepthFirstSearch()
{
vector<T>::const_iterator iter,iter2;
int pos;
vector<int> vec1;
this->ClearVisitFlag();
for (iter = this->vecNodes.begin();iter!=this->vecNodes.end();++iter)
{
pos = this->GetVertexPos(*iter);
if (!visited[pos])
{//还未访问,从这点开始
vec1 = this->GraphDepthFirstSearch(*iter);
for (iter2 = vec1.begin();iter2!=vec1.end();++iter2)
{
cout<<*iter2<<" ";
}
}
}
this->ClearVisitFlag();
}
测试程序:
#include "Graph.cpp"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
CGraph<int> *graph1 = new CGraph<int>();
graph1->CreateGraph();
graph1->DepthFirstSearch();
graph1->BreadthFirstSearch();
system("pause");
return 0;
}