图的表示(邻接矩阵和邻接表)

本文介绍了图的两种表示方法——邻接矩阵和邻接表,并通过代码展示了它们的实现。邻接矩阵适用于存储稠密图,邻接表则更适合稀疏图。
摘要由CSDN通过智能技术生成

图的表示有邻接矩阵、关联矩阵、邻接表等方式。

下面用代码实现邻接矩阵和邻接表。

#include<stdio.h>
#include<stdlib.h>
#define MAX 50//maximum number of vertices
typedef char VertexType;

typedef enum{DG,UDG}GraphType;//0 for directed and 1 undirected

typedef struct MyGraph{
	GraphType type;
	VertexType v[MAX];//vertices
	int e[MAX][MAX];//edges
	int nv;//number of vertices
	int ne;//number of edges
}Graph;

int get_index_of_vertex(char v,Graph* p){
	int i;
	for(i=0;i<p->nv;i++){
		if(p->v[i]==v)
			return i;
	}
	return 0;
}

void print(Graph* p){
	int i,j;
	if(p->type==0)
		printf("this is a directed graph\n");
	else
		printf("this is an undirected graph\n");
	printf("vertex set:\n");
	for(i=0;i<p->nv;i++)
		printf("%c ",p->v[i]);
	printf("\n");
	printf("adjacent matrix:\n");
	for(i=0;i<p->nv;i++){
		for(j=0;j<p->nv;j+
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值