无向图-基本操作函数(建立,广度遍历,深度遍历,邻接表表示)

基本操作函数:

InitGraph(Graph &G)             初始化函数 参数:图G 作用:初始化图的邻接表等
InsertNode(Graph &G,VexType v)  插入点函数 参数:图G,顶点v 作用:在图G中插入顶点v,即改变顶点表
InsertEdge(Graph &G,VexType v,VexType w) 插入边函数 参数:图G,某边两端点v和w 作用:在图G两点v,w之间加入边,即改变边表
BFS(Graph G, int start) 广度遍历函数 参数:图G,开始结点下标start 作用:广度遍历
DFS(Graph G, int start) 深度遍历函数(递归形式)参数:图G,开始结点下标start 作用:深度遍历

功能实现函数:

CreateGraph(Graph &G) 创建图功能实现函数 参数:图G,调用InsertNode等函数 作用:创建图
BFSTraverse(Graph G)  广度遍历功能实现函数 参数:图G 作用:宽度遍历
DFSTraverse(Graph G)  深度遍历功能实现函数 参数:图G 作用:深度遍历

1.创建图

【分析】

调用InsertNode函数进行点的插入,调用InsertEdge函数进行边的插入。

代码:

//创建图功能实现函数 参数:图G,调用InsertNode等函数 作用:创建图
void CreateGraph(Graph &G)
{
	VexType v, w;
	int vn, an;//顶点数,边数
	cout << "请输入顶点数目:" << endl;
	cin >> vn;
	cout << "请输入边数目:" << endl;
	cin >> an;
	cout << "请输入所有顶点名称:" << endl;
	for (int i = 0;i<vn;i++)
	{
		cin >> v;
		if (InsertNode(G, v)) continue;//插入点
		else {
			cout << "输入错误!" << endl;break;
		}
	}
	cout << "请输入所有边(每行输入边连接的两个顶点):" << endl;
	for (int j = 0;j<an;j++)
	{
		cin >> v >> w;
		if (InsertEdge(G, v, w)) continue;//插入边
		else {
			cout << "输入错误!" << endl;break;
		}
	}
	PrintAdj(G);
}

2.广度遍历

【分析】

使用邻接矩阵时,比较方便,只需要遍历以当前结点的为头结点的单链表即可遍历完它的所有邻接点。邻接表方式减少了很多的空间使用。

代码:

//广度遍历功能实现函数 参数:图G 作用:宽度遍历
void BFSTraverse(Graph G)
{
	for (int i = 0;i<MaxVerNum;i++)//初始化访问标记数组
	{
		visited[i] = false;
	}
	for (int i = 0;i < G.vexnum;i++)//对每个连通分量进行遍历
	{
		if (!visited[i])BFS(G, i);
	}
}

3.深度遍历

【分析】

采用递归方式进行深度遍历。

代码:

//深度遍历功能实现函数 参数:图G 作用:深度遍历
void DFSTraverse(Graph G)
{
	for (int i = 0;i<MaxVerNum;i++)//初始化访问标记数组
	{
		visited[i] = false;
	}
	for (int i = 0;i < G.vexnum;i++)//对每个连通分量进行遍历
	{
		if (!visited[i])
		{
			DFS(G, i);cout << endl;
		}
	}
}

4.全部代码

/*
Project: 图-邻接表表示(无向图)
Date:    2019/02/16
Author:  Frank Yu
基本操作函数:
InitGraph(Graph &G)             初始化函数 参数:图G 作用:初始化图的邻接表等
InsertNode(Graph &G,VexType v)  插入点函数 参数:图G,顶点v 作用:在图G中插入顶点v,即改变顶点表
InsertEdge(Graph &G,VexType v,VexType w) 插入边函数 参数:图G,某边两端点v和w 作用:在图G两点v,w之间加入边,即改变边表
BFS(Graph G, int start) 广度遍历函数 参数:图G,开始结点下标start 作用:广度遍历
DFS(Graph G, int start) 深度遍历函数(递归形式)参数:图G,开始结点下标start 作用:深度遍历
功能实现函数:
CreateGraph(Graph &G) 创建图功能实现函数 参数:图G,调用InsertNode等函数 作用:创建图
BFSTraverse(Graph G)  广度遍历功能实现函数 参数:图G 作用:宽度遍历
DFSTraverse(Graph G)  深度遍历功能实现函数 参数:图G 作用:深度遍历
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<set>
#include<list>
#include<queue>
#include<vector>
#include<map>
#include<iterator>
#include<algorithm>
#include<iostream>
#define MaxVerNum 100 //顶点最大数目值
#define VexType char //顶点数据类型
#define EdgeType int //边数据类型,无向图时邻接矩阵对称,有权值时表示权值,没有时1连0不连
using namespace std;
//边表数据结构
typedef struct ArcNode
{
	int adj;//该弧所指向的顶点下标
	struct ArcNode *next;//指向下一条弧的指针
};
//顶点表数据结构
typedef struct VNode
{
	VexType data;//数据域
	ArcNode *first;//第一条依附该顶点的指针
}AdjList[MaxVerNum];
//图的数据结构
typedef struct Graph
{
	AdjList vertices;//邻接表
	int vexnum, arcnum;//顶点数、边数
}Graph;
//*********************************************基本操作函数*****************************************//
//初始化函数 参数:图G 作用:初始化图的邻接表等
void InitGraph(Graph &G)
{
	for (int i = 0;i < MaxVerNum;i++)//初始化邻接表
	{
		G.vertices[i].data = '#';
		G.vertices[i].first = NULL;
	}
	G.arcnum = G.vexnum = 0;          //初始化顶点数、边数
}
//插入点函数 参数:图G,顶点v 作用:在图G中插入顶点v,即改变顶点表
bool InsertNode(Graph &G, VexType v)
{
	if (G.vexnum < MaxVerNum)
	{
		G.vertices[G.vexnum++].data = v;
		return true;
	}
	return false;
}
//插入边函数 参数:图G,某边两端点v和w 作用:在图G两点v,w之间加入边,即改变边表
bool InsertEdge(Graph &G, VexType v, VexType w)
{
	int p1, p2;//v,w两点下标
	p1 = p2 = -1;//初始化
	for (int i = 0;i<G.vexnum;i++)//寻找顶点下标
	{
		if (G.vertices[i].data == v)p1 = i;
        if (G.vertices[i].data == w)p2 = i;
	}
	if (-1 != p1&&-1 != p2)//两点均可在图中找到
	{
		ArcNode* e1, *e2;
		e1 = (ArcNode*)malloc(sizeof(ArcNode));
		(*e1).adj = p1;
		e2 = (ArcNode*)malloc(sizeof(ArcNode));
		(*e2).adj = p2;
		//头插
		(*e1).next = G.vertices[p2].first;
		G.vertices[p2].first = e1;
		(*e2).next = G.vertices[p1].first;
		G.vertices[p1].first = e2;
		G.arcnum++;
		return true;
	}
	return false;
}
bool visited[MaxVerNum];//访问标记数组,用于遍历时的标记
//广度遍历函数 参数:图G,开始结点下标start 作用:广度遍历
void BFS(Graph G, int start)
{
	queue<int> Q;//辅助队列
	cout << G.vertices[start].data;//访问结点
	visited[start] = true;
	Q.push(start);//入队
	while (!Q.empty())//队列非空
	{
		int v = Q.front();//得到队头元素
		Q.pop();//出队
		ArcNode *e1 = G.vertices[v].first;
		while (e1->next)
		{
			if (!visited[e1->adj]) 
			{
			    cout << "->";
				cout << G.vertices[e1->adj].data;//访问结点
				visited[e1->adj] = true;
				Q.push(e1->adj);//入队
			}
				e1 = e1->next;
		}
	}//while
	cout << endl;
}
//深度遍历函数(递归形式)参数:图G,开始结点下标start 作用:深度遍历
void DFS(Graph G, int start)
{
	cout << G.vertices[start].data;//访问
	visited[start] = true;
	ArcNode *e1 = G.vertices[start].first;
	do
	{
	 if (!visited[e1->adj])//未访问
	 {
		cout << "->";
		DFS(G, e1->adj);//递归深度遍历
	 }
	 e1 = e1->next;
	} while (e1->next);
}
//**********************************************功能实现函数*****************************************//
//打印图的邻接表
void PrintAdj(Graph G)
{
	for (int i = 0;i < G.vexnum;i++)
	{
		cout << i<<" "<<G.vertices[i].data << " ";
		if (G.vertices[i].first)
		{
			ArcNode *p = G.vertices[i].first;
			while (p->next) 
			{

				cout << " "<<p->adj;
				p = p->next;
			}
			cout << " " << p->adj;
		}
		cout << endl;
	}
}
//创建图功能实现函数 参数:图G,调用InsertNode等函数 作用:创建图
void CreateGraph(Graph &G)
{
	VexType v, w;
	int vn, an;//顶点数,边数
	cout << "请输入顶点数目:" << endl;
	cin >> vn;
	cout << "请输入边数目:" << endl;
	cin >> an;
	cout << "请输入所有顶点名称:" << endl;
	for (int i = 0;i<vn;i++)
	{
		cin >> v;
		if (InsertNode(G, v)) continue;//插入点
		else {
			cout << "输入错误!" << endl;break;
		}
	}
	cout << "请输入所有边(每行输入边连接的两个顶点):" << endl;
	for (int j = 0;j<an;j++)
	{
		cin >> v >> w;
		if (InsertEdge(G, v, w)) continue;//插入边
		else {
			cout << "输入错误!" << endl;break;
		}
	}
	PrintAdj(G);
}
//广度遍历功能实现函数 参数:图G 作用:宽度遍历
void BFSTraverse(Graph G)
{
	for (int i = 0;i<MaxVerNum;i++)//初始化访问标记数组
	{
		visited[i] = false;
	}
	for (int i = 0;i < G.vexnum;i++)//对每个连通分量进行遍历
	{
		if (!visited[i])BFS(G, i);
	}
}
//深度遍历功能实现函数 参数:图G 作用:深度遍历
void DFSTraverse(Graph G)
{
	for (int i = 0;i<MaxVerNum;i++)//初始化访问标记数组
	{
		visited[i] = false;
	}
	for (int i = 0;i < G.vexnum;i++)//对每个连通分量进行遍历
	{
		if (!visited[i])
		{
			DFS(G, i);cout << endl;
		}
	}
}
//菜单
void menu()
{
	cout << "************1.创建图       2.广度遍历************" << endl;
	cout << "************3.深度遍历     4.退出****************" << endl;
}
//主函数
int main()
{
	int choice = 0;
	Graph G;
	InitGraph(G);
	while (1)
	{
		menu();
		printf("请输入菜单序号:\n");
		scanf("%d", &choice);
		if (4 == choice) break;
		switch (choice)
		{
		case 1:CreateGraph(G);break;
		case 2:BFSTraverse(G);break;
		case 3:DFSTraverse(G);break;
		default:printf("输入错误!!!\n");break;
		}
	}
	return 0;
}

 

 

 

  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lady_killer9

感谢您的打赏,我会加倍努力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值