前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >图的存储方式

图的存储方式

作者头像
zy010101
发布2019-05-25 19:58:39
7160
发布2019-05-25 19:58:39
举报
文章被收录于专栏:程序员程序员

图是多对多的关系,它的存储通常有两种办法。邻接矩阵和邻接表。一般而言,对于稀疏图使用邻接表来存储,对于稠密图使用邻接矩阵来存储。下面给出邻接矩阵实现图的代码。

代码语言:javascript
复制
#include <iostream>
#include <cstdlib>
using namespace std;
#define MAX 100

typedef  struct
{
	char V[MAX];				//顶点集
	int Matrix[MAX][MAX];		//邻接矩阵
	int numV;					//顶点个数
	int numE;					//边的个数
}Graph;
void CreateGraph(Graph *G)
{
	int i, j, k;
	cout << "请输入顶点数和边数:\n";
	cin >> G->numV >> G->numE;
	//getchar();
	cout << "请输入顶点信息:\n";
	for (i = 0; i < G->numV; i++)
		cin >> G->V[i];

	for (i = 0; i < G->numV; i++)
		for (j = 0; j < G->numV; j++)
			G->Matrix[i][j] = 0;

	cout << "请输入边信息:(两个顶点)\n";
	for (k = 0; k < G->numE; k++)
	{
		cin >> i >> j;		//i和j之间有边
		//因为无向图的矩阵是对称的
		G->Matrix[i][j] = 1;
		G->Matrix[j][i] = 1;
		//如果是加权图,那么也应该输入权值。
	}

}
int main()
{
	int i, j;
	Graph *graphHead = (Graph *)malloc(sizeof(Graph));
	CreateGraph(graphHead);
    cout << "邻接表如下所示:\n";
	for (i = 0; i < graphHead->numV; i++)
	{
		for (j = 0; j < graphHead->numV; j++)
			cout << graphHead->Matrix[i][j] << "\t";
		cout << '\n';
	}
	system("pause");
}

测试一个正方形的输入,结果如下图所示。

邻接表的实现方式和散列表(哈希表)比较像,只是不需要散列函数而已。把所有的顶点放在了一个数组中。这样做适合稀疏图。代码实现如下:

代码语言:javascript
复制
#include<cassert>
#include<iostream>
#include<cstdlib>
using namespace std;

typedef struct AdjListNode_
{
	int num;	//顶点编号
	int weight;	//顶点权值
	struct AdjListNode_ *next;	//指向下一个节点
}AdjListNode;

typedef struct AdjList_
{
	AdjListNode * head;		//头指针
}AdjList;

typedef struct Graph_
{
	int numv, nume;		//顶点个数和边个数
	AdjList *array;
}Graph;

/*创建V个顶点的图*/
Graph* CreateGraph(Graph *graph)
{
	int m, n, w;
	cout << "请输入图的顶点数:";
	cin >> graph->numv;
	cout << "请输入图的边数:";
	cin >> graph->nume;

	graph->array = new AdjList[graph->numv];
	for (int i = 0; i < graph->numv; ++i)
	{
		graph->array[i].head = NULL;	//初始化头结点
	}

	for (int i = 0; i < graph->nume; i++)
	{
		cout << "请输入边(起始点到终止点):";
		cin >> m >> n;
		cout << "请输入权值:";
		cin >> w;

		AdjListNode *newNode = new AdjListNode;
		assert(newNode);						//判断是否分配到空间
		newNode->num = n;
		newNode->weight = w;               //begin顶点到end顶点的边的权重
		newNode->next = graph->array[m].head;  //新边插入到链表前面
		graph->array[m].head = newNode;

		//无向图需要在<m,n>和<n,m>都建立链接,所以重复上一步
		newNode = new AdjListNode;
		assert(newNode);
		newNode->num = m;
		newNode->weight = w;
		newNode->next = graph->array[n].head;
		graph->array[n].head = newNode;
	}
	return graph;
}
int main()
{
	Graph *graph = new Graph;
	graph = CreateGraph(graph);
    cout << "邻接表输出如下:\n";
	for (int i = 0; i < graph->numv; i++)
	{
		if (NULL == graph->array[i].head)
		{
			cout << i << endl;
		}
		else
		{
			cout << i ;
			AdjListNode * temp = graph->array[i].head;
			for (int j = 0; temp != NULL; j++)
			{
				cout << "->" << temp->num;
				temp = temp->next;
			}
			cout << "\n";
		}
	}
	system("pause");
}

测试一个这样的正方形输入,四个顶点分别标记为0,1,2,3.

测试的结果如下图所示。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年08月24日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档