图的邻接矩阵和邻接表存储

目录

邻接矩阵存储法

简介

​编辑

邻接矩阵举例

无向图邻接矩阵

有向图邻接矩阵

当各条边带有权值时

邻接矩阵算法实现

结构体定义和函数声明

函数的实现

邻接表存储法

简介

邻接表的算法实现

结构体定义和函数声明

函数的实现

邻接矩阵和邻接表的差别


邻接矩阵存储法

简介

邻接矩阵是表示顶点之间邻接关系的矩阵,图的邻接矩阵存储使用两个数组来 表示图,一个一维数组存储图中的顶点信息,一个二维数组存储图中的边的信息。


邻接矩阵举例

无向图邻接矩阵

有向图邻接矩阵

当各条边带有权值时

邻接矩阵算法实现

结构体定义和函数声明

cpp 复制代码
#define GRAPHMATRIXUTIL_H_

//图的邻接矩阵表示

typedef struct GRAPHMATRIX_STRU
{
	int size;//图中结点个数
	int** graph;//二维数组保存图

}GraphMatrix;

//初始化图
GraphMatrix* InitGraph(int num);//num  图中结点个数   用邻接矩阵表示图

//将数据读入图
void ReadGraph(GraphMatrix* graphMatrix);//graphMatrix 图

//将图的结构显示
void WriteGraph(GraphMatrix* graphMatrix);

函数的实现

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include "graphmatrixutil.h"
#include<stdlib.h>
#include<stdio.h>

GraphMatrix* InitGraph(int num)
{
	int i;
	int j;
	GraphMatrix* graphMatrix = (GraphMatrix*)malloc(sizeof(GraphMatrix));
	graphMatrix->size = num;//图中结点个数

	//给图分配空间
	graphMatrix->graph = (int**)malloc(sizeof(int*) * graphMatrix->size);
	for (i = 0; i < graphMatrix->size; i++)
	{
		graphMatrix->graph[i] = (int*)malloc(sizeof(int) * graphMatrix->size);
	}

	//给图中所有元素设置初值
	for (i = 0; i < graphMatrix->size; i++)
	{
		for (j = 0; j < graphMatrix->size; j++)
		{
			graphMatrix->graph[i][j] = INT_MAX;//INT_MAX是C语言中的常量,表示最大的整数
		}
	}
	return graphMatrix;
}

//将数据读入图,输入方式:点 点 权值,如果权值为0,则输入结束
void ReadGraph(GraphMatrix* graphMatrix)
{
	int vex1, vex2, weight;
	printf("请输入,输入方式:点 点 权值,如果权值为0,则输入结束");
	scanf("%d%d%d", &vex1, &vex2, &weight);
	while (weight != 0)
	{
		graphMatrix->graph[vex1][vex2] = weight;
		scanf("%d%d%d", &vex1, &vex2, &weight);
	}
}

//将图的结构显示出来,输出方式为:点,点,权值
void WriteGraph(GraphMatrix* graphMatrix)
{
	int i, j;
	printf("图的结构如下:输出方式为:点,点,权值\n");
	for (i = 0; i < graphMatrix->size; i++)
	{
		for (j = 0; j < graphMatrix->size; j++)
		{
			if (graphMatrix->graph[i][j] < INT_MAX)
			{
				printf("%d %d %d\n", i, j, graphMatrix->graph[i][j]);
			}
		}
	}
}

邻接表存储法

简介

邻接表的算法实现

结构体定义和函数声明

cpp 复制代码
#define GRAPHLISTUTIL_H

typedef struct GRAPHLISTNODE_STRU
{
	int nodeno;//图中结点的编号
	struct GRAPHLISTNODE_STRU* next;//指向下一个结点的指针
}GraphListNode;

typedef struct GRAPHLIST_STRU
{
	int size;//图中结点的个数
	GraphListNode* graphListArray;//图的邻接表
}GraphList;

//初始化图
GraphList* InitGraph(int num);

//读图
void ReadGraph(GraphList* graphList);

void WriteGraph(GraphList* graphList);


函数的实现

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS

#include "graphlistutil.h"
#include<stdlib.h>
#include<stdio.h>

//初始化
GraphList* InitGraph(int num)
{
	int i;
	GraphList* graphList = (GraphList*)malloc(sizeof(GraphList));

	graphList->size = num;
	graphList->graphListArray = (GraphListNode*)malloc(sizeof(GraphListNode) * num);

	for (i = 0; i < graphList->size; i++)
	{
		graphList->graphListArray[i].next = NULL;
		graphList->graphListArray[i].nodeno = i;
	}

	return graphList;
}

//将数据读入图,输入方式:点 点,点为-1结束
void ReadGraph(GraphList* graphList)
{
	int vex1, vex2;
	GraphListNode* tempNode = NULL;
	printf("请输入:输入方式:点 点,点为-1结束\n");
	scanf("%d%d", &vex1, &vex2);

	while (vex1 >= 0 && vex2 >= 0)
	{
		tempNode = (GraphListNode*)malloc(sizeof(GraphListNode));
		tempNode->nodeno = vex2;
		tempNode->next = NULL;

		//寻找到要插入结点的地方,为了方便放在头部
		tempNode->next = graphList->graphListArray[vex1].next;
		graphList->graphListArray[vex1].next = tempNode;
		scanf("%d%d", &vex1, &vex2);

	}
}

//图结构显示
void WriteGraph(GraphList* graphList)
{
	int i;
	GraphListNode* tempNode = NULL;
	for (i = 0; i < graphList->size; i++)
	{
		tempNode = graphList->graphListArray[i].next;
		while (tempNode != NULL)
		{
			printf("结点%d和%d相连\n", i, tempNode->nodeno);
			tempNode = tempNode->next;
		}
	}
}

邻接矩阵和邻接表的差别


相关推荐
heimeiyingwang9 天前
【深度学习加速探秘】Winograd 卷积算法:让计算效率 “飞” 起来
人工智能·深度学习·算法
时空自由民.9 天前
C++ 不同线程之间传值
开发语言·c++·算法
ai小鬼头9 天前
AIStarter开发者熊哥分享|低成本部署AI项目的实战经验
后端·算法·架构
小白菜3336669 天前
DAY 37 早停策略和模型权重的保存
人工智能·深度学习·算法
zeroporn9 天前
以玄幻小说方式打开深度学习词嵌入算法!! 使用Skip-gram来完成 Word2Vec 词嵌入(Embedding)
人工智能·深度学习·算法·自然语言处理·embedding·word2vec·skip-gram
亮亮爱刷题9 天前
飞往大厂梦之算法提升-7
数据结构·算法·leetcode·动态规划
_周游9 天前
【数据结构】_二叉树OJ第二弹(返回数组的遍历专题)
数据结构·算法
双叶8369 天前
(C语言)Map数组的实现(数据结构)(链表)(指针)
c语言·数据结构·c++·算法·链表·哈希算法
安全系统学习9 天前
【网络安全】DNS 域原理、危害及防御
算法·安全·web安全·网络安全·哈希算法
Cyrus_柯9 天前
C++(面向对象编程——继承)
开发语言·c++·算法·面向对象