数据结构 图 邻接矩阵表示法

图 邻接矩阵表示法

图的邻接表表示法看这里

基本结构

enum GraphKind {
    DG, DN, UDG, UDN };

template<typename VRType, typename InfoType>
struct ArcCell
{
   
	VRType adj;
	InfoType* info;
};

template<typename VRType, typename InfoType>
using AdjMatrix = ArcCell<VRType, InfoType>[MAX_VERTEX_NUM][MAX_VERTEX_NUM];

template<typename VertexType, typename VRType, typename InfoType>
struct MGraph
{
   
	VertexType vexs[MAX_VERTEX_NUM];
	AdjMatrix<VRType, InfoType> arcs;
	int vexnum, arcnum;
	GraphKind kind;
};

测试用的图
7 10
北京 西安 郑州 徐州 成都 广州 上海
北京 西安 2553
北京 郑州 695
北京 徐州 704
徐州 上海 651
郑州 徐州 349
西安 郑州 511
西安 成都 812
成都 广州 2368
广州 郑州 1579
广州 上海 1385
数据结构 图 邻接矩阵表示法_第1张图片

基本操作

初始化
初始化图的数据,并根据图的类型给邻接矩阵赋相应的权值

template<typename VertexType, typename VRType, typename InfoType>
Status initMGraph(MGraph<VertexType, VRType, InfoType>& MG, GraphKind kind)//初始化
{
   
	MG.kind = kind;
	MG.arcnum = 0;
	MG.vexnum = 0;
	if (MG.kind == DG || MG.kind == UDG)//判断图的类型初始化相应权值
	{
   
		for (int i = 0; i < MAX_VERTEX_NUM; i++)
		{
   
			for (int j = 0; j < MAX_VERTEX_NUM; j++)
				MG.arcs[i][j].adj = 0;
		}
	}
	if (MG.kind == DN || MG.kind == UDN)
	{
   
		for (int i = 0; i < MAX_VERTEX_NUM; i++)
		{
   
			for (int j = 0; j < MAX_VERTEX_NUM; j++)
				MG.arcs[i][j].a

你可能感兴趣的:(数据结构,数据结构,有向图,c++)