博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【数据结构周周练】023 将图的邻接表表示转化为邻接矩阵表示的算法
阅读量:4075 次
发布时间:2019-05-25

本文共 1289 字,大约阅读时间需要 4 分钟。

一、图的存储结构

昨天给大家讲了图的存储结构,一共有四种,邻接表,邻接矩阵,十字链表以及邻接多重表,每个表都有自己的特色以及用途。

今天要给大家分享的是将一个用邻接表表示的图转为邻接矩阵表示,我们知道,邻接矩阵中,存储形式比较简单,普通的邻接矩阵只有0和1,0表示两个节点之间没有边,1表示有边。所以我们要遍历邻接表的每一个结点,得到它的边表,通过其next指针域获得与之相邻的所有边,并在矩阵中的对应位置赋值为1,剩余位置均赋值为0即可。

当然为了方便起见,我们将邻接矩阵的每个边初始化为0,在转化过程中,直接寻找存在的边,赋值为1即可。

二、代码

#define MaxVertexNum 100  //Maximum value of the vertex number#define M 5typedef char VertexType; //the type of vertextypedef int EdgeType; // the type of wight on the edge in weighted graph 带权图中边上的权值的类型// the graph are storaged by adjacency matrix 邻接矩阵存储图typedef struct {	VertexType Vex[MaxVertexNum]; //vertex list	EdgeType Edge[MaxVertexNum][MaxVertexNum];  // adjacency matrix	int vexNum, arcNum;  //current vertex number and arc of graph}MGraph;//the graph are storaged by adjacency listtypedef struct ArcNode {	int adjvex;  // the location of the vertex which was pointed by arc	struct ArcNode *next;}ArcNode;typedef struct VNode {	VertexType data;	ArcNode *first;}VNode, AdjList[MaxVertexNum];typedef struct {	AdjList vertices;  // adjacency list	int vexNum, arcNum;  // current vertex number and arc of graph}*ALGraph;void ConvertGraph(ALGraph &G, EdgeType Edge[M][M]){	for (int i = 0; i < G->vexNum; i++)	{		ArcNode *p = G->vertices[i].first;		while (!p)		{			Edge[i][p->adjvex] = 1;			p = p->next;		}	}}

 

转载地址:http://hdyni.baihongyu.com/

你可能感兴趣的文章
db db2_monitorTool IBM Rational Performace Tester
查看>>
OS + Unix Aix telnet
查看>>
IBM Lotus
查看>>
Linux +Win LAMPP Tools XAMPP 1.7.3 / 5.6.3
查看>>
my read_university
查看>>
network manager
查看>>
OS + Linux Disk disk lvm / disk partition / disk mount / disk io
查看>>
RedHat + OS CPU、MEM、DISK
查看>>
net TCP/IP / TIME_WAIT / tcpip / iperf / cain
查看>>
webServer kzserver/1.0.0
查看>>
OS + Unix IBM Aix basic / topas / nmon / filemon / vmstat / iostat / sysstat/sar
查看>>
my ReadMap subway / metro / map / ditie / gaotie / traffic / jiaotong
查看>>
OS + Linux DNS Server Bind
查看>>
linux下安装django
查看>>
Android 解决TextView设置文本和富文本SpannableString自动换行留空白问题
查看>>
Android开发中Button按钮绑定监听器的方式完全解析
查看>>
Android自定义View实现商品评价星星评分控件
查看>>
postgresql监控工具pgstatspack的安装及使用
查看>>
postgresql查看表的和索引的情况,判断是否膨胀
查看>>
postgresql中根据oid和filenode去找表的物理文件的位置
查看>>