c语言:图的邻接矩阵的建立与广度优先搜索实现

  一、之前写的代码: 很长一段时间没更新博客了,好惭愧啊!有待改进!,只有无向无权图的实现

// 无向图的构建及遍历.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<stdio.h>
using namespace std;
#define Infinity MAX;//邻接矩阵数据结构定义
#define vertex_Max 20
typedef enum{ DG, DN, UDG, UDN }GraphKind;
typedef struct
{
	int vexs[vertex_Max];
	int edges[vertex_Max][vertex_Max];
	int vexnum, edgsnum;
	GraphKind kind;
}MGraph;
int visited[vertex_Max];
typedef struct QNode  //链队列数据结构定义
{
	int data;
	struct QNode *next;
}QNode, *QueuePtr;
typedef struct
{
	QueuePtr front;
	QueuePtr rear;
}LinkQueue;
//函数声明
void GreatUDG(MGraph &G, int n, int e);//构造无向图的邻接矩阵
int Locatvex(MGraph &G, int e);             //定位函数
void DFSTravel(MGraph &G,int i);           //深度优先遍历
void BFSTravel(MGraph &G);           //广度优先遍历
void InitQueue(LinkQueue &Q);        //初始化链队列
void EnQueue(LinkQueue &Q, int e);    //将元素e入队列
void DeQueue(LinkQueue &Q, int e);    //将队头元素出队,并用e返回其值
//函数定义
int Locatvex(MGraph &G,int e)
{
	int i;
	for (i = 1; i <= G.vexnum; i++) 
	{
		if (G.vexs[i] == e) 
		{
			return i;
		}
	}
	return 0;
}

void GreatUDG(MGraph &G, int n, int e)
{
	int v1, v2;
	G.vexnum = n;
	G.edgsnum = e;
	for (int i = 0; i < G.vexnum; i++)
	{
		cin >> G.vexs[i];
	}
	cout << "定点数组构造完成!" << endl;
	for (int i = 0; i<G.vexnum; i++)
	{
		for (int j = 0; j<G.vexnum; j++)
		{
			G.edges[i][j] = G.edges[j][i] = 0;
		}
	}
	for (int k = 0; k<G.edgsnum; k++)
	{
		cin >> v1 >> v2;
		int i = Locatvex(G, v1);
		int j = Locatvex(G, v2);
		while ((i<0) || (i>(G.vexnum-1) ) || (j<0) || (j>(G.vexnum-1)))
		{
			cout << "该边不存在,请重新输入:";
			cin >> v1 >> v2;
			i = Locatvex(G, v1);
			j = Locatvex(G, v2);
		}
		G.edges[i][j] = G.edges[j][i] = 1;
	}
	cout << "该无向图的邻接矩阵构造完成!" << endl;
	cout << "输出创建好的邻接矩阵!" << endl;
	for (int i = 0; i<G.vexnum; i++)
	{
		for (int j = 0; j<G.vexnum; j++)
		{
			cout << G.edges[i][j] << " ";
		}
		cout << endl;
	}
}
void BFSTravel(MGraph &G)//广度优先遍历邻接矩阵
{
	int v = 0;
	int u = 0;
	LinkQueue Q;
	InitQueue(Q);
	for (v = 0; v < G.vexnum; v++)
	{
		if (!visited[v])
		{
			cout << G.vexs[v]<<" ";
			visited[v] = 1;
			EnQueue(Q, v);
			while (Q.front != Q.rear)
			{
				DeQueue(Q, u);
				for (int w = 0; w < G.vexnum; w++)
				{
					if ((G.edges[u][w] != 0) && (visited[w] != 1))
					{
						visited[w] = 1;
						cout << G.vexs[w] << " ";
						EnQueue(Q, w);
					}
				}
			}
		}
	}
	cout << endl;
}
void InitQueue(LinkQueue &Q)        //初始化链队列
{
	Q.front = Q.rear = new QNode;
	Q.front->next = NULL;
}
void EnQueue(LinkQueue &Q, int e)    //将元素e入队列
{
	QNode *p = new QNode;
	p->data = e;
	p->next = NULL;
	Q.rear->next = p;
	Q.rear = p;
}
void DeQueue(LinkQueue &Q, int e)    //将队头元素出队,并用e返回其值
{
	if (Q.front == Q.rear)
		cout << "Queue Empty!" << endl;
	QNode *p = Q.front->next;
	e = p->data;
	Q.front->next = p->next;
	if (Q.rear = p)
		Q.front = Q.rear;
	delete p;
}
int _tmain(int argc, _TCHAR* argv[])
{
	
	MGraph G;
	int n = 6;
	int e = 6;
	GreatUDG(G, n, e);
	for (int v = 0; v < G.vexnum; v++)
		visited[v] = 0;
	cout << "广度优先遍历邻接矩阵如下:" << endl;
	BFSTravel(G);
	return 0;
}

二、改进代码实现图的邻接矩阵实现及其广度优先遍历实现

<pre name="code" class="cpp">// 有向图的邻接矩阵表示及其遍历实现.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"  
#include<iostream>
#include<stdio.h>
#include<malloc.h>
#define  MaxVertexNum 100
#define INFINITY 65535 
typedef int Vertex;     //数组下标表示顶点
typedef int WeightType; //定义权重
typedef char DateType;  //定义数据类型
using namespace std;
//边的定义
typedef struct ENode *PtrToENode;
struct ENode
{
	Vertex V1, V2;      //有向边
	WeightType Weight;  //权重
};
typedef PtrToENode Edge;
//结点定义
typedef struct GNode *PtrToGNode;
struct GNode
{
	int Nv;//顶点数
	int Ne;//边数
	WeightType G[MaxVertexNum][MaxVertexNum]; //邻接矩阵
	DateType Data[MaxVertexNum];//顶点数组
};
typedef PtrToGNode MGraph;

int visited[MaxVertexNum];//标志数组
typedef struct QNode  //链队列数据结构定义  
{
	int data;
	struct QNode *next;
}QNode, *QueuePtr;
typedef struct
{
	QueuePtr front;
	QueuePtr rear;
}LinkQueue;
//函数声明
MGraph CreateGraph(int Vertexnum);         //邻接矩阵初始化函数
void InsertEdge(MGraph Graph, Edge E);     //插入边的信息
MGraph BuildGraph();                       //创建一个邻接矩阵
void DFSTravel(MGraph &G, int i);          //深度优先遍历  
void BFSTravel(MGraph G);                  //广度优先遍历  
void InitQueue(LinkQueue &Q);              //初始化链队列  
void EnQueue(LinkQueue &Q, int e);         //将元素e入队列  
void DeQueue(LinkQueue &Q, int e);         //将队头元素出队,并用e返回其值  
//函数定义  

void InitQueue(LinkQueue &Q)        //初始化链队列  
{
	Q.front = Q.rear = new QNode;
	Q.front->next = NULL;
}
void EnQueue(LinkQueue &Q, int e)    //将元素e入队列  
{
	QNode *p = new QNode;
	p->data = e;
	p->next = NULL;
	Q.rear->next = p;
	Q.rear = p;
}
void DeQueue(LinkQueue &Q, int e)    //将队头元素出队,并用e返回其值  
{
	if (Q.front == Q.rear)
		cout << "Queue Empty!" << endl;
	QNode *p = Q.front->next;
	e = p->data;
	Q.front->next = p->next;
	if (Q.rear = p)
		Q.front = Q.rear;
	delete p;
}
MGraph CreateGraph(int Vertexnum)
{//初始化一个有Vertexnum个顶点但没有变得图
	Vertex V, W;
	MGraph Graph;
	Graph = (MGraph)malloc(sizeof(struct GNode));
	Graph->Nv = Vertexnum;
	Graph->Ne = 0;
	for (V = 0; V<Graph->Nv; V++)
	{
		for (W = 0; W<Graph->Nv; W++)
			Graph->G[V][W] = INFINITY;
		//如果是有权无向图,应该是
		//Graph->G[V][W] =Graph->G[W][V] = INFINITY;
		//如果是无权图有向图,可以初始化为0;
		//Graph->G[V][W] = 0;
		//如果是无权无向图,应该是
		//Graph->G[V][W] =Graph->G[W][V] = 0;
	}
	return Graph;
}

void InsertEdge(MGraph Graph, Edge E)
{//插入边  
	Graph->G[E->V1][E->V2] = E->Weight;
	//若是无向图,则需要插入这条边
	//Graph->G[E->V2][E->V1] = E->Weight;
	//若是无权有向图,
	//Graph->G[E->V1][E->V2]=1;
	//若是无权无向图,
	///Graph->G[E->V2][E->V1] = 1;
}
MGraph BuildGraph()
{
	MGraph Graph;
	Edge E;
	Vertex V;
	int i;
	int Nv;
	cout << "请输入顶点个数:";
	cin >>Nv;
	Graph = CreateGraph(Nv);
	cout << "请输入边的个数:";
	cin >> Graph->Ne;
	if (Graph->Ne != 0)
	{
		E = (Edge)malloc(sizeof(struct ENode));
		for (i = 0; i<Graph->Ne; i++)
		{
			cin >> E->V1 >> E->V2 >> E->Weight;
			//如果是无权图
			//cin>>E->V1>>E->V2;
			InsertEdge(Graph, E);
		}
	}
	for (V = 0; V<Graph->Nv; V++)
		cin >> Graph->Data[V];
	Vertex W;
	cout << "创建好的邻接矩阵如下所示:";
	for (V = 0; V<Graph->Nv; V++)
	{
		for (W = 0; W < Graph->Nv; W++)
			cout << Graph->G[V][W] << "  ";
		cout << endl;
	}
	return Graph;
}
void BFSTravel(MGraph Graph)//广度优先遍历邻接矩阵  
{
	int v = 0;
	int u = 0;
	LinkQueue Q;
	InitQueue(Q);
	for (v = 0; v < Graph->Nv; v++)
	{
		if (!visited[v])
		{
			cout << Graph->Data[v] << " ";
			visited[v] = 1;
			EnQueue(Q, v);
			while (Q.front != Q.rear)
			{
				DeQueue(Q, u);
				for (int w = 0; w < Graph->Nv; w++)
				{
					if ((Graph->G[u][w] != 0) && (visited[w] != 1))
					{
						visited[w] = 1;
						cout << Graph->Data[w] << " ";
						EnQueue(Q, w);
					}
				}
			}
		}
	}
	cout << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
	MGraph Graph;
	Graph = BuildGraph();
	for (int v = 0; v < Graph->Nv; v++)
		visited[v] = 0;
	cout << "广度优先遍历邻接矩阵如下:" << endl;
	BFSTravel(Graph);
	return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值