图的创建和遍历

#include<stdio.h>
#include<iostream.h>
#include<stdlib.h>
#define MaxInt 32767
#define MAXQSIZE 100
#define MVNum 100
#define OK 1
#define ERROR 0
#define OVERFLOW -2
bool visited[MVNum];
typedef struct
{/*存储表示*/
	char vexs[MVNum];
	int arcs[MVNum][MVNum];
	int vexnum,arcnum;
 }AMGraph; 
int LocateVex(AMGraph G, char v)
{/*v在G中的位置*/
	int i;
	for(i=0;i<G.vexnum;++i)
		if(G.vexs[i]==v)
			return i;
   return -1;
}
int CreateUDN(AMGraph &G)
{/*创建无向图*/
	int i,j,k;
	char v1,v2;
	cout<<"总顶点数,总边数:";
	cin>>G.vexnum>>G.arcnum;
	cout<<endl;
	cout<<"请输入各点:"<<endl;
	for(i=0;i<G.vexnum;++i)
	{
		cout<<"第"<<(i+1)<<"个点:";
		cin>>G.vexs[i];
	}
	for(i=0;i<G.vexnum;++i)
		for(j=0;j<G.arcnum;++j)
			G.arcs[i][j]=MaxInt;
	cout<<endl;
	cout<<"请输入边依附的顶点: "<< endl;
	for(k=0;k<G.arcnum;++k)
	{
		cout<<"第"<<(k + 1)<<"条边依附的顶点:";
		cin>>v1>>v2;
		i=LocateVex(G,v1);
		j=LocateVex(G,v2);
		G.arcs[i][j]=1;
		G.arcs[j][i]=G.arcs[i][j];
	}
	return OK;
}
int FirstAdjVex(AMGraph G,int v)
{/*v的第一个邻接点*/
	int i;
	for(i=0;i<G.vexnum;++i)
	{
		if(G.arcs[v][i]==1&&visited[i]==false)
			return i;
	}
	return -1;
}
int NextAdjVex(AMGraph G,int v,int w)
{/*v相对于w的下一个邻接点*/
	int i;
	for(i=w;i<G.vexnum;++i)
	{
		if(G.arcs[v][i]==1&&visited[i]==false)
			return i;
	}
	return -1;
}

void DFS(AMGraph G,int v)
{/*从第v个顶点出发递归*/
	int w;
	cout << G.vexs[v] << "    ";
	visited[v]=true;
	for(w=FirstAdjVex(G,v);w>=0;w=NextAdjVex(G,v,w))  
		if(!visited[w])
			DFS(G,w); 
}
void DFSTraverse(AMGraph G)
{ /*深度优先搜索遍历连通图*/
	int v;
	for(v=0;v<G.vexnum;++v)
		visited[v]=false;       //访问标志数组初始化 
	for(v=0;v<G.vexnum;++v)
        if(!visited[v])
			DFS(G,v);
}
typedef struct
{/*深度优先遍历遍历非连通图*/
	int *base;
	int front;
	int rear;
}sqQueue;
void InitQueue(sqQueue &Q)
{
	Q.base = new int[MAXQSIZE];
	if(!Q.base)
		exit(1);
	Q.front = Q.rear = 0;
}

void EnQueue(sqQueue &Q, int e)
{/*插入元素e为Q的新的队尾元素*/
	if((Q.rear+1)%MAXQSIZE==Q.front)
		return;
	Q.base[Q.rear]=e;
	Q.rear=(Q.rear+1)%MAXQSIZE;
}

int QueueEmpty(sqQueue Q)
{/*判断是否为空队*/	
	if(Q.rear==Q.front)
		return true;
	return false;
}

void DeQueue(sqQueue &Q,int &u)
{/*队头元素出队并置为u*/
	u=Q.base[Q.front];
	Q.front=(Q.front+1)%MAXQSIZE;
}
void BFS(AMGraph G, int v)
{ /*按广度优先非递归遍历连通图G*/
	sqQueue Q;
	int u,w;
    cout<<G.vexs[v]<<"    ";
    visited[v]=true;
    InitQueue(Q);
    EnQueue(Q,v);
    while(!QueueEmpty(Q))
	{
		DeQueue(Q,u);
		for(w=FirstAdjVex(G,u);w>=0;w=NextAdjVex(G,u,w))
			if(!visited[w])
			{
				cout<<G.vexs[w]<<"    ";
				visited[w]=true;
				EnQueue(Q,w);
			}
    }
} 
int main()
{
	AMGraph G;
    int i;
	char c;
	CreateUDN(G);		
	cout<<"遍历连通图的起始点为:";
	cin>>c;
	for(i=0;i<G.vexnum;++i)
	{
		if(c==G.vexs[i])
			break;
	}
	cout<<endl;
	cout<<"广度优先搜索遍历结果为:"<<endl;
	BFS(G,i);
	cout<<endl;	
	cout<<"深度优先搜索遍历结果为:"<<endl;
	DFSTraverse(G);
	cout<<endl;
	return 0;
}

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值