c++实现图的邻接矩阵、邻接表、深度优先DFS、广度优先BFS
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "time.h"
#include <iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXVEX 100
#define GRAPH_INFINITY 65535
typedef int Status;
typedef char VertexType;
typedef int EdgeType;
struct queue
{
int front,rear;
int data[MAXVEX];
};
class MGraph
{
public:
MGraph();
void CreateMGraph(void);
void CreateMGraph_example(void);
void show(void);
bool isempty(void);
void EnQueue(int e);
void DeQueue(int *e);
void DFS(int i);
void DFSTraverse(void);
void BFSTraverse(void);
public:
VertexType vexs[MAXVEX];
EdgeType arc[MAXVEX][MAXVEX];
int numNodes,numEdges;
bool visited[MAXVEX];
queue *q=new queue;
};
MGraph::MGraph()
{
numNodes=0;
numEdges=0;
q->front=0;
q->rear=0;
for(int i=0;i<numNodes;i++) q->data[i]=0;
for(int i=0;i<MAXVEX;i++) vexs[i]=0;
for(int i=0;i<MAXVEX;i++) for(int j=0;j<MAXVEX;j++) arc[i][j]=GRAPH_INFINITY;
for(int i=0;i<numNodes;i++) visited[i]=false;
}
void MGraph::CreateMGraph(void)
{
int i,j,k,w;
cout<<"输入顶点数和边数:"<<endl;
cin>>numNodes>>numEdges;
cout<<"读入顶点的值:"<<endl;
for(i=0;i<numNodes;i++) cin>>vexs[i];
for(k=0;k<numEdges;k++)
{
cout<<"输入边(vi,vj)上的下标i,下标j,权w:"<<endl;
cin>>i>>j>>w;
arc[i][j]=w;
arc[j][i]=arc[i][j];
}
}
void MGraph::show(void)
{ if(numNodes==0) cout<<"顶点表为空,无顶点!!!"<<endl;
else
{
cout<<"顶点表为: ";
for(int i=0;i<numNodes;i++) cout<<vexs[i]<<" ";
cout<<endl<<"邻接矩阵为:"<<endl;
for(int i=0;i<numNodes;i++)
{
for(int j=0;j<numNodes;j++) cout<<arc[i][j]<<" ";
cout<<endl;
}
}
}
bool MGraph::isempty(void)
{
if(q->front==q->rear) return true;
else return false;
}
void MGraph::EnQueue(int e)
{
if((q->rear+1)%MAXVEX==q->front)
{
cout<<"队列已满!!!"<<endl;
exit(1);
}
else
{
q->data[q->rear]=e;
q->rear=(q->rear+1)%MAXVEX;
}
}
void MGraph::DeQueue(int *e)
{
if(q->front==q->rear) cout<<"队列为空!!!"<<endl;
else
{
*e=q->data[q->front];
q->front=(q->front+1)%MAXVEX;
}
}
void MGraph::CreateMGraph_example(void)
{
int i, j;
numEdges=15;
numNodes=9;
vexs[0]='A';
vexs[1]='B';
vexs[2]='C';
vexs[3]='D';
vexs[4]='E';
vexs[5]='F';
vexs[6]='G';
vexs[7]='H';
vexs[8]='I';
for (i=0;i<numNodes;i++)
{
for (j=0;j<numNodes;j++)
{
arc[i][j]=0;
}
}
arc[0][1]=1;
arc[0][5]=1;
arc[1][2]=1;
arc[1][8]=1;
arc[1][6]=1;
arc[2][3]=1;
arc[2][8]=1;
arc[3][4]=1;
arc[3][7]=1;
arc[3][6]=1;
arc[3][8]=1;
arc[4][5]=1;
arc[4][7]=1;
arc[5][6]=1;
arc[6][7]=1;
for(i=0;i<numNodes;i++)
{
for(j=i;j<numNodes;j++)
{
arc[j][i]=arc[i][j];
}
}
}
void MGraph::DFS(int i)
{
int j;
visited[i]=true;
cout<<vexs[i]<<" ";
for(j=0;j<numNodes;j++)
{
if(!visited[j]&&arc[i][j]!=0) DFS(j);
}
}
void MGraph::DFSTraverse(void)
{
for(int i=0;i<numNodes;i++)
{
if(!visited[i]) DFS(i);
}
}
void MGraph::BFSTraverse(void)
{
int i,j;
for(i=0;i<numNodes;i++)
{
if(!visited[i])
{
visited[i]=true;
cout<<vexs[i]<<" ";
EnQueue(i);
while(!isempty())
{
DeQueue(&i);
for(j=0;j<numNodes;j++)
{
if(arc[i][j] == 1 && !visited[j])
{
visited[j]=TRUE;
cout<<vexs[j]<<" ";
EnQueue(j);
}
}
}
}
}
}
struct EdgeNode
{
int adjvex;
EdgeType info;
struct EdgeNode* next;
};
struct VertexNode
{
VertexType data;
EdgeNode* firstedge;
int in;
};
class GraphAdjList
{
public:
GraphAdjList();
void CreateALGraph(void);
void CreateALGraph_example(void);
bool isempty(void);
void show(void);
void EnQueue(int e);
void DeQueue(int *e);
void DFS(int i);
void DFSTraverse(void);
void BFSTraverse(void);
public:
VertexNode AdjList[MAXVEX];
int numNodes,numEdges;
queue* q=new queue;
bool visited[MAXVEX];
};
GraphAdjList::GraphAdjList()
{
numNodes=0;
numEdges=0;
for(int i=0;i<MAXVEX;i++)
{
AdjList[i].data=0;
AdjList[i].firstedge=NULL;
}
q->front=q->rear=0;
for(int i=0;i<numNodes;i++) q->data[i]=0;
for(int i=0;i<numNodes;i++) visited[i]=false;
}
void GraphAdjList::CreateALGraph(void)
{
int i,j,k;
EdgeNode* e;
cout<<"输入顶点数和边数:"<<endl;
cin>>numNodes>>numEdges;
for(i=0;i<numNodes;i++)
{
cin>>AdjList[i].data;
AdjList[i].firstedge=NULL;
}
for(k=0;k<numEdges;k++)
{
cout<<"输入边(vi,vj)上的顶点序号: "<<endl;
cin>>i>>j;
e=new EdgeNode;
e->adjvex=j;
e->next=AdjList[i].firstedge;
AdjList[i].firstedge=e;
e=new EdgeNode;
e->adjvex=i;
e->next=AdjList[j].firstedge;
AdjList[j].firstedge=e;
}
}
void GraphAdjList::CreateALGraph_example(void)
{
int i;
EdgeNode *e;
numEdges=15;
numNodes=9;
AdjList[0].data='A';
AdjList[1].data='B';
AdjList[2].data='C';
AdjList[3].data='D';
AdjList[4].data='E';
AdjList[5].data='F';
AdjList[6].data='G';
AdjList[7].data='H';
AdjList[8].data='I';
for(i=0;i<numNodes;i++) AdjList[i].firstedge=NULL;
e=new EdgeNode;
e->adjvex=1;
e->next=AdjList[0].firstedge;
AdjList[0].firstedge=e;
e=new EdgeNode;
e->adjvex=5;
e->next=AdjList[0].firstedge;
AdjList[0].firstedge=e;
e=new EdgeNode;
e->adjvex=2;
e->next=AdjList[1].firstedge;
AdjList[1].firstedge=e;
e=new EdgeNode;
e->adjvex=8;
e->next=AdjList[1].firstedge;
AdjList[1].firstedge=e;
e=new EdgeNode;
e->adjvex=6;
e->next=AdjList[1].firstedge;
AdjList[1].firstedge=e;
e=new EdgeNode;
e->adjvex=3;
e->next=AdjList[2].firstedge;
AdjList[2].firstedge=e;
e=new EdgeNode;
e->adjvex=8;
e->next=AdjList[2].firstedge;
AdjList[2].firstedge=e;
e=new EdgeNode;
e->adjvex=4;
e->next=AdjList[3].firstedge;
AdjList[3].firstedge=e;
e=new EdgeNode;
e->adjvex=7;
e->next=AdjList[3].firstedge;
AdjList[3].firstedge=e;
e=new EdgeNode;
e->adjvex=6;
e->next=AdjList[3].firstedge;
AdjList[3].firstedge=e;
e=new EdgeNode;
e->adjvex=8;
e->next=AdjList[3].firstedge;
AdjList[3].firstedge=e;
e=new EdgeNode;
e->adjvex=5;
e->next=AdjList[4].firstedge;
AdjList[4].firstedge=e;
e=new EdgeNode;
e->adjvex=7;
e->next=AdjList[4].firstedge;
AdjList[4].firstedge=e;
e=new EdgeNode;
e->adjvex=6;
e->next=AdjList[5].firstedge;
AdjList[5].firstedge=e;
e=new EdgeNode;
e->adjvex=7;
e->next=AdjList[6].firstedge;
AdjList[6].firstedge=e;
}
void GraphAdjList::show(void)
{
if(numNodes == 0) cout<<"当前无顶点"<<endl;
else
{
cout<<"各个顶点的值为:";
for(int i=0;i<numNodes;i++) cout<<AdjList[i].data<<" ";
}
}
bool GraphAdjList::isempty(void)
{
if(q->front==q->rear)
{
return true;
}
else
{
return false;
}
}
void GraphAdjList::EnQueue(int e)
{
if((q->rear+1)%MAXVEX == q->front)
{
cout<<"queue is full!!!"<<endl;
exit(1);
}
else
{
q->data[q->rear]=e;
q->rear=(q->rear+1)%MAXVEX;
}
}
void GraphAdjList::DeQueue(int *e)
{
if(q->front==q->rear)
{
cout<<"queue is empty!!!"<<endl;
exit(1);
}
else
{
*e=q->data[q->front];
q->front=(q->front+1)%MAXVEX;
}
}
void GraphAdjList::DFS(int i)
{
EdgeNode* p;
visited[i]=true;
cout<<AdjList[i].data<<" ";
p=AdjList[i].firstedge;
while(p)
{
if(!visited[p->adjvex]) DFS(p->adjvex);
p=p->next;
}
}
void GraphAdjList::DFSTraverse(void)
{
int i;
for(i=0;i<numNodes;i++)
{
if(!visited[i]) DFS(i);
}
}
void GraphAdjList::BFSTraverse(void)
{
int i;
EdgeNode *p;
for(i=0;i<numNodes;i++)
{
if(!visited[i])
{
visited[i]=true;
cout<<AdjList[i].data<<" ";
EnQueue(i);
while(!isempty())
{
DeQueue(&i);
p=AdjList[i].firstedge;
while(p)
{
if(!visited[p->adjvex])
{
visited[p->adjvex]=true;
cout<<AdjList[p->adjvex].data<<" ";
EnQueue(p->adjvex);
}
p=p->next;
}
}
}
}
}
int main()
{
MGraph* graph = new MGraph;
graph->CreateMGraph_example();
graph->show();
cout<<"深度遍历:"<<endl;
graph->DFSTraverse();
for(int i=0;i<graph->numNodes;i++) graph->visited[i]=false;
cout<<endl<<"广度遍历:"<<endl;
graph->BFSTraverse();
cout<<endl;
GraphAdjList* AdjListgraph=new GraphAdjList;
AdjListgraph->CreateALGraph_example();
AdjListgraph->show();
cout<<endl<<"深度遍历: ";
AdjListgraph->DFSTraverse();
cout<<endl<<"广度遍历: ";
for(int i=0;i<AdjListgraph->numNodes;i++) AdjListgraph->visited[i]=false;
AdjListgraph->BFSTraverse();
cout<<endl;
return 0;
}