图的创建与dfs,bfs遍历

#include<iostream>
#include<list>
#include<fstream>
#include<queue>
#include<iostream>
#include<list>
#include<fstream>
const double MAXCOST=10000;
using namespace std;

struct EdgeNode
{
int dest;
double cost;
operator int(){return dest;}
EdgeNode(int dd,int cc)
{
dest=dd;
cost=cc;
}
};

template<class T>
class Graph
{
T *VA;
list<EdgeNode>* HL;
int sizeV,sizeE;
int maxV;
double GetCost(int si,int dj)const;
void BFS(list<T>& L,int pos,bool* visited)const;
public:
Graph(int n=100):sizeV(0),sizeE(0),maxV(n)
{
HL=new list<EdgeNode>[n];
VA=new T[n];
}
~Graph(){delete []VA;delete []HL;}
int Empty()const{return(sizeV==0);}
int Full()const{return(sizeV==maxV);}
int SizeV()const{return(sizeV);}
int SizeE()const{return(sizeE);}
double GetCost(const T& v1,const T& v2)const;
int FindNode(const T& v)const;
bool FindNode(T& v,int pos)const;
bool InsertV(const T& v);
bool InsertE(const T& v1,const T& v2,double w);
bool DeleteV(const T& v);
bool DeleteE(const T& v1,const T& v2);
void ReadGraph(const char* filename);
void WriteGraph(const char* filename);
void BFS(list<T>& L,const T& v)const;
void BFS(list<T>& L)const;
void DFS(list<T>& T,int pos,bool *visited)const;
void DFS(list<T>& L,const T& v)const;
friend ostream& operator<<(ostream& ostr,const Graph<T>& g)
{
for(int i=0;i<g.sizeV;i++)
{
ostr<<i<<'-'<<g.VA[i]<<':';
list<EdgeNode>::iterator first=g.HL[i].begin(),last=g.HL[i].end();
for(;first!=last;++first)
ostr<<'('<<(*first).dest<<','<<(*first).cost<<')'<<' ';
ostr<<endl;
}
return(ostr);
}

friend istream& operator>>(istream& istr,Graph<T>& g)
{
char s[50]; int n; double w;
T v1,v2;
istr>>s>>n;
for(int i=1;i<=n;++i)
{
istr>>v1;
g.InsertV(v1);
}
istr>>s>>n;
for(i=1;i<=n;++i)
{
istr>>v1>>v2>>w;
g.InsertE(v1,v2,w);
}
return(istr);
}
};
template<class T>
int Graph<T>::FindNode(const T& v)const
{
for(int i=0;i<sizeV;i++)
if(VA[i]==v)
return(i);
return(-1);
}
template<class T>
bool Graph<T>::FindNode(T& v,int pos)const
{
if(pos<0||pos>=sizeV)
return(0);
v=VA[pos];
return(1);
}
template<class T>
bool Graph<T>::InsertV(const T& v)
{
if(sizeV==maxV)
return(0);
VA[sizeV]=v;
sizeV++;
return(1);
}
template<class T>
bool Graph<T>::InsertE(const T& v1,const T& v2,double w)
{
int si=FindNode(v1),dj=FindNode(v2);
if(si==-1||dj==-1||si==dj)
return(0);
HL[si].insert(HL[si].end(),EdgeNode(dj,w));
sizeE++;
return(1);
}

template<class T>
void Graph<T>::ReadGraph(const char* filename)
{
char str[50];
int n;
double w;
T v1,v2;
ifstream fin;
fin.open(filename,ios::in);
if(!fin)
{
cout<<"cannot open"<<filename<<endl;
exit(1);
}
fin>>str>>n;
for(int i=1;i<=n;++i)
{
fin>>v1;
InsertV(v1);
}
fin>>str>>n;
for(i=1;i<=n;++i)
{
fin>>v1>>v2>>w;
InsertE(v1,v2,w);
}
}
template<class T>
void Graph<T>::WriteGraph(const char* filename)
{
ofstream fout;
fout.open(filename,ios::out);
if(!fout)
{
cout<<"cannot open "<<filename<<endl;
exit(1);
}
for(int i=0;i<sizeV;i++)
{
fout<<i<<'-'<<VA[i]<<':';
list<Graph<T>::EdgeNode>::iterator first=HL[i].begin(),last=HL[i].end();
for(;first!=last;++first)
fout<<'('<<(*first).dest<<','<<(*first).cost<<')'<<' ';
fout<<endl;
}
}

template<class T>
bool Graph<T>::DeleteE(const T& v1,const T& v2)
{
int si=FindNode(v1),dj=FindNode(v2);
if(si==-1||dj==-1||si==dj)
return(0);
list<EdgeNode>::iterator first=HL[si].begin(),last=HL[si].end();
for(;first!=last;++first)
if((*first).dest==dj)
{
HL[si].Erase(first);
sizeE--;
return(1);
}
return(0);
}

template<class T>
bool Graph<T>::DeleteV(const T& v)
{
int si=FindNode(v),i;
if(si==-1)
return(0);
int size=HL[si].size();
for(i=si;i<sizeV-1;i++)
{
VA[i]=VA[i+1];
HL[i]=HL[i+1];
}
HL[sizeV-1].clear();
sizeV--;
sizeE=sizeE-size;

list<EdgeNode>::iterator first,last;
for(i=0;i<sizeV;i++)
{
first=HL[i].begin(),last=HL[i].end();
for(;first!=last;++first)
if((*first).dest==si)
{
HL[i].erase(first);
sizeE--;
break;
}
}
for(i=0;i<sizeV;i++)
{
first=HL[i].begin(),last=HL[i].end();
for(;first!=last;++first)
if((*first).dest>si)
(*first).dest--;
}
return(1);
}


template<class T>
double Graph<T>::GetCost(int si,int dj)const
{
if(si<0||si>=sizeV||dj<0||dj>=sizeV||si==dj)
return(0);
list<EdgeNode>::const_iterator first=HL[si].begin(),last=HL[si].end();
for(;first!=last;++first)
if((*first).dest==dj)
return((*first).cost);
return(0);
}

template<class T>
double Graph<T>::GetCost(const T& v1,const T& v2)const
{
return(GetCost(FindNode(v1),FindNode(v2)));
}

template <class T>
void Graph<T>::BFS(list<T>& L,const T& v)const
{
int pos=FindNode(v);
if(pos==-1)
return;
bool *visited=new bool[sizeV];
for(int i=0;i<sizeV;i++)
{
visited[i]=0;
}
queue<int> Q;
Q.push(pos);
visited[pos]=1;
list<EdgeNode>::const_iterator first,last;
while(!Q.empty())
{
pos=Q.front();
Q.pop();
L.push_back(VA[pos]);
first=HL[pos].begin();
last=HL[pos].end();
for(;first!=last;first++)
{
if(visited[(*first).dest]==0)
{
Q.push((*first).dest);
visited[(*first).dest]=1;
}
}
}
delete[]visited;
}

template<class T>
void Graph<T>::BFS(list<T>& L)const
{
int i;
bool *visited=new bool[sizeV];
for(i=0;i<sizeV;i++)
visited[i]=0;
for(i=0;i<sizeV;i++)
if(visited[i]==0)
BFS(L,i,visited);
delete[]visited;
}

template<class T>
void Graph<T>::BFS(list<T>& L,int pos,bool* visited)const
{
if(pos<0||pos>=sizeV)
{
return ;
}
queue<int> Q;
Q.push(pos);
visited[pos]=1;
list<EdgeNode>::const_iterator first,last;
while(!Q.empty())
{
pos=Q.front();
Q.pop();
L.push_back(VA[pos]);
first=HL[pos].begin();
last=HL[pos].end();
for(;first!=last;++first)
{
if(visited[(*first).dest]==0)
{
Q.push((*first).dest);
visited[(*first).dest]=1;
}
}
}
}

template<class T>
void Graph<T>::DFS(list<T>& L,int pos,bool *visited)const
{
if(pos<0||pos>=sizeV)
{
return ;
}
L.push_back(VA[pos]);
visited[pos]=1;
list<EdgeNode>::const_iterator first,last;
first=HL[pos].begin();
last=HL[pos].end();
for(;first!=last;first++)
{
if(visited[(*first).dest]==0)
DFS(L,(*first).dest,visited);
}
}

template<class T>
void Graph<T>::DFS(list<T>& L,const T& v)const
{
int pos=FindNode(v);
if(pos==-1)
{
return ;
}
bool *visited=new bool[sizeV];
for(int i=0;i<sizeV;i++)
{
visited[i]=0;
}
DFS(L,pos,visited);
}


int main()
{
ios::sync_with_stdio(false);
Graph<char> G(20);
//cin>>G;
ifstream fin;
fin.open("graphin.txt",ios::in);
if(!fin)
{
cout<<"cannot open input file!"<<endl;
exit(1);
}
fin>>G;
list<char> l;
G.BFS(l);
cout<<"BFS序列:"<<endl;
list<char>::iterator it;
for(it=l.begin();it!=l.end();it++)
{
cout<<*it<<' ';
}
cout<<endl;
l.clear();
G.DFS(l,'A');
cout<<"DFS序列:"<<endl;
for(it=l.begin();it!=l.end();it++)
{
cout<<*it<<' ';
}
cout<<endl;
cout<<G;

/*ofstream fout;
fout.open("graphout.txt",ios::out);
if(!fout)
{
cout<<"cannot open output file!"<<endl;
exit(1);
}
fout<<G;*/

return 0;
}

这个程序采用的文件的输入输出方式,因为重载了>>和<<所以也能够用cin和cout的输入输出方式

格式如下即可:

<Vertices> 5
A B C D E
<Edges> 8
A B 5
B A 6
B C 7
B E 7
C B 11
C D 20
D A 2
E D 8

posted on 2018-11-16 20:17  Numb_lzw  阅读(345)  评论(0编辑  收藏  举报

导航