每日一题——有向网的邻接矩阵、邻接表、逆邻接表创建、打印及深度、广度遍历

2023-11-18

有向网的三种创建和深度广度遍历

#include <iostream>
#include <iomanip>
using namespace std;
#define MAX_VERTEX 20  //最大顶点个数
#define INFINITY 0 //表示极大值
int visited[MAX_VERTEX] = { 0 };

//邻接矩阵
typedef struct {
	int vertexnum;
	int arcnum;
	int arcs[MAX_VERTEX][MAX_VERTEX];
	char vertex[MAX_VERTEX];
}AdjMatrix;

//边结构
typedef struct ArcNode{
	int adjvex; //顶点
	int weight;  //权值
	ArcNode *next;
}ArcNode;

//顶点结构
typedef struct VertexNode {
	char vexdata;
	ArcNode *head;
}VertexNode;

//邻接表
typedef struct {
	VertexNode vertex[MAX_VERTEX];
	int vertexnum;
	int arcnum;
}AdjList;

//队列结点
typedef struct qNode {
	int data;
	qNode *next;
}QueueNode;

//队列
typedef struct queue {
	QueueNode *front;
	QueueNode *rear;
}Queue;

//初始化队列
Queue* InitQueue() {
	Queue *Q;
	QueueNode *qNode;
	Q = (Queue*)malloc(sizeof(Queue));
	qNode = (QueueNode*)malloc(sizeof(QueueNode));
	qNode->next = NULL;
	Q->front = Q->rear = qNode;
	return Q;
}

//判空
int isEmpty(Queue *Q) {
	if (Q->front == Q->rear)
		return 1;
	return 0;
}

//入队
void InQueue(Queue *Q, int x) {
	QueueNode *temp;
	if (temp = (QueueNode*)malloc(sizeof(QueueNode)))
	{
		temp->data = x;
		temp->next = NULL;
		Q->rear->next = temp;
		Q->rear = temp;
	}
}

//出队
void OutQueue(Queue *Q, int *x) {
	QueueNode *temp;
	if (!isEmpty(Q))
	{
		temp = Q->front->next;
		Q->front->next = temp->next;
		*x = temp->data;
		free(temp);
		if (Q->front->next == NULL)
			Q->rear = Q->front;
	}

}

//获取顶点对应的位置
int LocateVexByAdjMatrix(AdjMatrix *G, char v)
{
	for (int i = 1; i <= G->vertexnum; i++)
	{
		if (G->vertex[i] == v)
			return i;
	}
	return 0;
}

//创建图   G1-->邻接矩阵  G2-->邻接表  G3-->逆邻接表
void creatGragh(AdjMatrix *G1,AdjList *G2, AdjList *G3)
{
	cout << "输入图的顶点数和边数:" << endl;
	cin >> G1->vertexnum >> G1->arcnum;
	G2->vertexnum = G1->vertexnum;
    G2->arcnum = G1->arcnum;
	G3->vertexnum = G1->vertexnum;
	G3->arcnum = G1->arcnum;

	cout << "输入顶点信息:" << endl;
	for (int i = 1; i <= G1->vertexnum; i++)
	{
		cin >> G1->vertex[i]; 
		G2->vertex[i].vexdata = G1->vertex[i];
		G3->vertex[i].vexdata = G1->vertex[i];
	}

	for (int i = 1; i <= G1->vertexnum; i++)
	{//初始化邻接矩阵
		for (int j = 1; j <= G1->vertexnum; j++)
		{
			G1->arcs[i][j] = INFINITY;
		}
	}
	for (int i = 1; i <= G2->vertexnum; i++)
	{
		G2->vertex[i].head = NULL;
		G3->vertex[i].head = NULL;
	}
	ArcNode *cur1, *cur2, *pre1 = NULL, *pre2 = NULL;
	cout << "输入头和尾结点及权值:" << endl;
	for (int i = 1; i <= G1->arcnum; i++)
	{
		char chead; char ctail; int weight;
		cin >> chead >> ctail >> weight;
		int head = LocateVexByAdjMatrix(G1, chead);
		int tail = LocateVexByAdjMatrix(G1, ctail);

		G1->arcs[head][tail] = weight;

		cur1 = (ArcNode*)malloc(sizeof(ArcNode));
		cur1->adjvex = tail;
		cur1->weight = weight;
		pre1 = G2->vertex[head].head;
		if (G2->vertex[head].head == NULL)
		{
			G2->vertex[head].head = cur1;
			pre1 = cur1;
			pre1->next = NULL;

		}
		else {
			pre1->next = cur1;
			pre1 = cur1;
			pre1->next = NULL;
		}

		cur2 = (ArcNode*)malloc(sizeof(ArcNode));
		cur2->adjvex = head;
		cur2->weight = weight;
		pre2 = G3->vertex[tail].head;
		if (G3->vertex[tail].head == NULL)
		{
			G3->vertex[tail].head = cur2;
			pre2 = cur2;
			pre2->next = NULL;

		}
		else {
			pre2->next = cur2;
			pre2 = cur2;
			pre2->next = NULL;
		}
	}
}

//打印邻接表
void showGraghByAdjList(AdjList *G)
{
	for (int i = 1; i <= G->vertexnum; i++)
	{//打印顶点信息
		ArcNode *p = G->vertex[i].head;
		cout << G->vertex[i].vexdata << "->";
		while (p)
		{
			cout << G->vertex[p->adjvex].vexdata << " ";
			p = p->next;
		}
		cout << endl;
	}
}

//打印矩阵图
void showGraghByAdjMatrix(AdjMatrix *G)
{
	cout << setw(8) << endl;
	for (int i = 1; i <= G->vertexnum; i++)
	{//打印顶点信息
		cout << G->vertex[i] << setw(4);
	}
	cout << endl;
	for (int i = 1; i <= G->vertexnum; i++)
	{//打印邻接矩阵
		cout << G->vertex[i] << setw(4);
		for (int j = 1; j <= G->vertexnum; j++)
		{
			cout << G->arcs[i][j] << setw(4);
		}
		cout << endl;
	}
}

//各顶点的入度、出度和度
void degree(AdjMatrix *G)
{
	for (int i = 1; i <= G->vertexnum; i++)
	{
		int count = 0, out_count = 0, in_count = 0;
		for (int j = 1; j <= G->vertexnum; j++)
		{
			if (G->arcs[i][j] != INFINITY)
				out_count++;
			if (G->arcs[j][i] != INFINITY)
				in_count++;
			count = out_count + in_count;
		}
		cout << G->vertex[i] << " " << out_count << " " << in_count <<
			" " << count << endl;
	}
}

//一次深度优先遍历
void DFS(AdjMatrix *G, int start)
{
	cout << G->vertex[start];
	visited[start] = 1;
	for (int i = 1; i <= G->vertexnum; i++)
	{
		if (G->arcs[start][i] != INFINITY && visited[i] == 0) {
			DFS(G, i);
		}
	}
}

//深度遍历
void DFSTraverse(AdjMatrix *G)
{
	int count = 0;
	for (int i = 1; i <= G->vertexnum; i++)
		visited[i] = 0;
	for (int i = 1; i <= G->vertexnum; i++)
	{
		if(!visited[i]) {
			count++;
			DFS(G, i);
		}
	}
}


//一次广度遍历
void BFS(AdjMatrix *G, int start)
{
	cout << G->vertex[start];
	visited[start] = 1;
	Queue *q = InitQueue();
	InQueue(q, start);
	while (!isEmpty(q))
	{
		int top = q->front->data;
		OutQueue(q, &top);
		for (int i = 1; i <= G->vertexnum; i++)
		{
			if (G->arcs[start][i] != INFINITY && visited[i] == 0)
			{
				cout << G->vertex[i];
				visited[i] = 1;
				InQueue(q, i);
			}
		}
	}
}

//广度遍历
void BFSTraverse(AdjMatrix *G)
{
	for (int i = 1; i <= G->vertexnum; i++)
		visited[i] = 0;
	for (int i = 1; i <= G->vertexnum; i++)
		if (!visited[i]) BFS(G, i);
}

//主函数
int main() {
	AdjMatrix *G1;
	G1 = (AdjMatrix*)malloc(sizeof(AdjMatrix));
	AdjList *G2;
	G2 = (AdjList*)malloc(sizeof(AdjList));
	AdjList *G3;
	G3 = (AdjList*)malloc(sizeof(AdjList));
	//创建图
	creatGragh(G1, G2, G3);
	
	cout << "打印邻接矩阵:" << endl;
	showGraghByAdjMatrix(G1);
	
	cout << "打印邻接表:" << endl;
	showGraghByAdjList(G2);
	
	cout << "打印逆邻接表:" << endl;
	showGraghByAdjList(G3);

	//打印度
	cout << "打印出度、入度、度:" << endl;
	degree(G1);

	//深度遍历
	cout << "深度遍历:" << endl;
	DFSTraverse(G1);
	cout << endl;

	//广度遍历
	cout << "广度遍历:" << endl;
	BFSTraverse(G1);
	cout << endl;

	free(G1);
	free(G2);
	free(G3);

	system("pause");
	return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

每日一题——有向网的邻接矩阵、邻接表、逆邻接表创建、打印及深度、广度遍历 的相关文章

  • [1181]linux两台服务器之间传输文件和文件夹

    文章目录 scp 1 从服务器复制文件到本地 2 复制文件到本地并重命名 3 从服务器复制文件夹到本地 4 从本地复制文件到服务器 不包括文件夹本身 5 从本地复制文件夹到服务器 包括文件夹本身 rcp 命令使用 wget rsync 在日

随机推荐

  • Vue3父子组件通信,父子传参

    Vue3父子组件通信 父子传参 前言 Vue2的小伙伴应该该经历过转战Vue3过程的中的抓狂 好多地方使用都不太一样 这期就给大家讲一下近期我也在用vue3开发中遇到到的问题父子组件通信 父传子 在父组件中引入son vue子组件 为子组件
  • sql 2008 R2 修改数据库表编辑行200小技巧

    在使用sql server 2008 R2时 有时候要打开一个表 看里面的数据 发现只能编辑前面200行 如下图 如果我的数据库表的数据 超过200 怎么办呢 其实只要修改下配置 就可以了 如下图 点击选项 进入选项界面 如下图 在sql
  • nginx配置主域名跳转www域名并支持ssl

    server listen 80 listen 443 ssl server name xxxx com return 301 https www xxx com request uri server listen 80 server na
  • 视频转码后有色差要如何处理

    目录 视频转码后有色差要如何处理 KEY COLOR STANDARD KEY COLOR RANGE 视频转码后有色差要如何处理 以下是回答 欢迎大家留言讨论补充 1 色差是如何产生的 1 有损压缩产生的质量损失 解决方法为尽可能的提高码
  • 如何用计算机计算log除法,对数计算器_如何使用计算器计算对数

    如何使用计算器计算对数 示例 使用Windows自带的计算器 这理假设要计算的对数是logaN a 32 N 2 1 打开计算器 快捷键WIN R 输入calc 然后回车 怎样使用科学计算器计算对数 计算机上的log都是默认以10为底的对数
  • c语言实现面向对象编程(const * ,* const)

    c语言实现面向对象编程 面向对象思想 封装 继承 多态 代码实现 函数签名及返回值的约定 const 重载 参考了onlyshi的博客代码 orz传送门 参考了嵌入式实践一些代码 这里就不加了 面向对象思想 面向对象编程 OOP 并不是一种
  • 千年虫及UNIX时间

    转自 http hi baidu com dugucloud blog item b903ba803e5192c59123d99d html 千年虫何来 在上个世纪 许多计算机系统只用二进制7 8位数 足够十进制二位数使用 表示年份 比如说
  • ANR触发机制分析

    ANR是一套监控Android应用程序响应是否及时的机制 可以把发生ANR比作是引爆炸弹 那么整个流程包含三部分组成 埋定时炸弹 system server进程启动倒计时 在规定时间内如果目标应用进程没有干完所有的活 则system ser
  • MySQL的基本语法

    Welcome Huihui s Code World 接下来看看由辉辉所写的关于MySQL的相关操作吧 目录 Welcome Huihui s Code World 一 数据库 建立 查看 使用 删除 建库 查看数据库 使用数据库 删除数
  • decrypt()解密和encrypt()加密

    1 解密函数 select dbo decrypt StName from student 2 加密函数 select dbo encrypt StName from student
  • CxImage的编译及简单使用举例

    1 从http sourceforge net projects cximage 下载最新的CxImage 702源码 2 解压缩后 以管理员身份打开CxImageFull vc10 sln工程 在编译之前先将每个工程属性的Characte
  • Vue中props组件和slot标签的区别

    在 Vue 中 props 和 slot 都是组件之间进行通信的机制 它们的作用和应用场景有一些区别 props 是一种组件的数据传递机制 通过在父组件中以属性的形式向子组件传递数据 子组件接收这些数据 并可以进行相应的处理和渲染 prop
  • 每日一练——day2

    前言 小亭子正在努力的学习编程 接下来将开启编程题的练习 分享的文章都是学习的笔记和感悟 如有不妥之处希望大佬们批评指正 同时如果本文对你有帮助的话 烦请点赞关注支持一波 感激不尽 第一题 题目描述 链接 https www nowcode
  • Jenkins执行python代码

    在ide运行正常 在jenkins运行提示 模块不存在 找了一圈 原来是jenkin运行python的环境没设置 解决方式 在Manage Jenkins System Configuration Configure System中设置全局
  • WSL2连接到宿主Windows程序的网络代理设置

    WSL2想要连上宿主机Windows里设置的网络代理端口很是蛋疼 前置条件 PS C Users overlord gt wsl l v NAME STATE VERSION Ubuntu 20 04 Running 2 获取Host和WS
  • Consul的简介与安装

    1 Consul简介 Consul是一套开源的分布式服务发现和配置管理系统 由HashiCorp公司用Go语言开发 Consul提供了微服务系统中的服务治理 配置中心 控制总线等功能 这些功能中的每一个都可以根据需要单独使用 也可以一起使用
  • wsl配置

    文章目录 1 systemd服务开启 2 固定IP 2 1 官网的方案 2 2 通过WSL2的Linux子系统设置静态IP 2 3 其他方案 3 运行 Linux GUI 应用安装 Chrome 浏览器 此文接我放弃了VMware 1 sy
  • vue(3)调整 App.vue 文件和router路由

    调整 App vue 文件 我们先把默认项目里面没用的东西先删除掉 把代码调整为下面的样子
  • js延迟加载的性能优化

    js的延迟加载有助于提高页面的加载速度 特别是竞价优化站是有一定的好处 今天来说说我是如何优化竞价站打开速度 案例 http yzmb pengchenggroup cn 动态创建DOM方式
  • 每日一题——有向网的邻接矩阵、邻接表、逆邻接表创建、打印及深度、广度遍历

    有向网的三种创建和深度广度遍历 include