「图」邻接矩阵|边集数组|邻接表 / LeetCode 35|33|81(C++)

概述

我们开始入门图论:图的存储。

图是一种高级数据结构:链表是一个节点由一条边指向下一个节点,二叉树是一个节点由两条边指向下两个节点,而图是由任意多个节点由任意多条边指向任意多个节点

图由节点和边构成,边往往存在边权。边权在不同的问题中往往有不同含义,如在最短路径中表示边的长度,在AOE网中表示任务所需时间。

对于这种复杂的结构,如何存储在计算机的程序语言中呢?

我们先来介绍三种存储结构:邻接矩阵|边集数组|邻接表。


1.邻接矩阵

结构

邻接矩阵由一张二维数组直接构成。

我们称二维数组的每一行为row,整体为adjacency_matrix:

using row=vector<int>;
using adjacency_matrix = vector<vector<int>>;

(using 的用法类似define宏定义) 

你也可以使用最基本的:

int m[100][100];

但失去了vector的动态性。

对于matrix[u][v]=w;表示从u节点到v节点的边权值为w。

形象理解:

复杂度

空间复杂度: O(n²)

n:节点数量

特点:

1.往往用于稠密图(如弗洛伊德最短路径算法的实现),即各点之间存在大量边相互连接。

2.支持按节点访问。

3.不能存储两点之间的多条边。

Code

using row=vector<int>;
using adjacency_matrix = vector<vector<int>>;
void add(adjacency_matrix& m) {
	int n; cin >> n;
	while (n--) {
		int u, v, w; cin >> u >> v >> w;
		m[u][v] = w;
	}
}
void get(const adjacency_matrix& m) {
	for (int i = 0; i < m.size(); i++)
		for (int j = 0; j < m[i].size(); j++)
			if (m[i][j])cout << "       " << m[i][j] << endl << i << "------------->" << j << endl;
}

2.边集数组

结构

边集数组由结构体edge作为元素组成数组。

edge具有成员变量int u,v,w;意为:此边从u出发抵达v,边权为w。

将edge数组称为edges:

struct edge{
	int u;
	int v;
	int w;
}; 
using edges=vector<edge>;

对于edges[i]={u,v,w};表示第i条边从u节点出发抵达v节点,边权为w

(边的序号通常时建立数组读入数据时编排的。)

形象理解:

复杂度

空间复杂度: O(m)

m:边数量

特点:

1.往往用于克鲁斯卡尔最小生成树算法的实现,或作为高级的图储存结构的成员。

2.不支持按节点访问。

3.能存储两点之间的多条边。

Code

struct edge{
	int u;
	int v;
	int w;
}; 
using edges=vector<edge>;
void add(edges& e) {
	int n; cin >> n;
	while (n--) {
		int u, v, w; cin >> u >> v >> w;
		e.push_back({ u,v,w });
	}
}
void get(const edges& e) {
	for(const edge& i:e)
		cout << "       " << i.w << endl << i.u << "------------->" << i.v << endl;
}

3.邻接表

结构

邻接表由结构体out_edge作为元素组成数组。

out_edge只描述节点的出边,而边的起始节点由数组下表提供。

out_edge具有成员变量v,w;意为:抵达v,边权为w。

将out_edge数组称为adjacency_list:

struct out_edge{
	int v;
	int w;
};
using adjacency_list=vector<vector<out_edge>>;

对于adjacency_list[u][i]={v,w};表示从u节点出发的第i条边抵达v节点,边权为w。 

形象理解:

复杂度

空间复杂度: O(n+m)

n:节点数量

m:边数量

复杂度分析:

邻接表数组共有n行,占O(n),而所有行共有m个元素,占O(m)。 

特点:

1.常用于各种图。

2.支持按节点访问。

3.能存储两点之间的多条边。

Code

struct out_edge{
	int v;
	int w;
};
using adjacency_list=vector<vector<out_edge>>;
void add(adjacency_list& l) {
	int n; cin >> n;
	while (n--) {
		int u, v, w; cin >> u >> v >> w;
		if(u>=l.size())l.resize(u + 1);
		l[u].push_back({ v,w });
	}
}
void get(const adjacency_list& l) {
	for (int i=0;i<l.size();i++)
		for(const out_edge j:l[i])
		    cout << "       " << j.w << endl << i << "------------->" << j.v << endl;
}

测试Code

/*
11
1 2 20
2 1 30
2 0 30
4 3 100
8 9 60
9 7 40
3 6 50
5 6 20
7 8 15
2 4 30
1 3 5
以上为测试用例
*/
int main() {
	int test; cin >> test;
	switch (test) {
	case 1: {
		adjacency_matrix m(10, row(10,INT_MAX));
		cout << "------------input------------" << endl;
		add(m);
		cout << "------------output-----------" << endl;
		get(m);
		break;
	}
	case 2: {
		edges e;
		cout << "------------input------------" << endl;
		add(e);
		cout << "------------output-----------" << endl;
		get(e);
		break;
	}
	case 3: {
		adjacency_list l;
		cout << "------------input------------" << endl;
		add(l);
		cout << "------------output-----------" << endl;
		get(l);
		break;
	}
	}
	return 0;
}

总结

以上三种是基本的图储存结构,我们后续会讲解链式邻接表和链式前向星,他们都是基于以上结构的高级存储手段。

  • 14
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值