【Data structure】Graph storage structure (adjacency matrix, adjacency list, cross-linked list, adjacency multiple list) and implementation (C language)

1. Adjacency matrix notation

1.1 Adjacency matrix of graph

Adjacency Matrix representation of graph is also called array representation. It uses two arrays to represent the graph: one is a one-dimensional array used to store vertex information, and the other is a two-dimensional array used to store the relationship between vertices in the graph. This relationship array is called an adjacency matrix .
adjacency matrix of a graphThe adjacency matrix of a directed graph can indicate the direction of the arc; the adjacency matrix of an undirected graph has no direction, so it is a symmetric matrix; the adjacency
matrix is ​​not suitable for storing a sparse graph, which will cause a waste of space.

1.2 Create the adjacency matrix of the directed network

# include<stdio.h>
# define MAX_VERTEX_NUM 20			//最多顶点个数
# define INFINITY 32768				//表示极大值,即∞

/*图的邻接矩阵表示法*/
typedef int AdjType;
typedef char VertexData;
typedef struct ArcNode {
    
    
	AdjType adj;							//无权图用1或0表示是否相邻,带权图则为权值类型
}ArcNode;
typedef struct {
    
    
	VertexData vertex[MAX_VERTEX_NUM];		//顶点向量
	ArcNode arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM];//邻接矩阵
	int vexnum, arcnum;						//图的顶点数和弧数
}AdjMatrix;

/*采用邻接矩阵表示法创建有向网*/
/*求顶点位置*/
int LocateVertex(AdjMatrix* G, VertexData v) {
    
    
	int k;
	for (k = 0; k < G->vexnum; k++) {
    
    
		if (G->vertex[k] == v)
			break;
	}
	return k;
}

/*创建有向网*/
int CreateDN(AdjMatrix* G) {
    
    
	int i, j, k, weight;
	VertexData v1, v2;
	printf("输入图的顶点数和弧数:");			//输入图的顶点数和弧数
	scanf("%d%d", &G->vexnum, &G->arcnum);
	for (i = 0; i < G->vexnum; i++) {
    
    		//初始化邻接矩阵
		for (j = 0; j < G->vexnum; j++)
			G->arcs[i][j].adj = INFINITY;
	}
	printf("输入图的顶点:");
	for (i = 0; i < G->vexnum; i++)			//输入图的顶点
		scanf(" %c", &G->vertex[i]);
	for (k = 0; k < G->arcnum; k++) {
    
    
		printf("输入第%d条弧的两个顶点及权值:", k + 1);
		scanf(" %c %c %d", &v1, &v2, &weight);	//输入一条弧的两个顶点及权值
		i = LocateVertex(G, v1);
		j = LocateVertex(G, v2);
		G->arcs[i][j].adj = weight;			//建立弧
	}
}

/*邻接矩阵的输出*/
void AdjMatrixDisplay(AdjMatrix* G) {
    
    
	printf("图的邻接矩阵输出:\n");
	int i, j;
	for (i = 0; i < G->vexnum; i++) {
    
    
		printf("\t%c", G->vertex[i]);
	}
	printf("\n");
	for (i = 0; i < G->vexnum; i++) {
    
    		//输出邻接矩阵
		printf("%c\t", G->vertex[i]);
		for (j = 0; j < G->vexnum; j++) {
    
    
			if (G->arcs[i][j].adj == INFINITY)
				printf("∞\t");
			else
				printf("%d\t", G->arcs[i][j].adj);
		}
		printf("\n");
	}
}

int main() {
    
    
	AdjMatrix G;
	CreateDN(&G);
	AdjMatrixDisplay(&G);
	return 0;
}

operation result
operation result

2. Adjacency list notation

2.1 Adjacency list storage structure of graph

The adjacency list (Adjacency List) representation is a chained storage structure of a graph, which only stores edge information that exists in the graph. In the adjacency list, an edge list with a head node is established for each vertex in the graph, and the head node of each edge list forms a head node list. In this way, the adjacency list representation of a graph with n vertices consists of two parts: the head node and the edge list.
Adjacency List Node StructureHeader node table : All header nodes are stored in the form of a sequential structure (vector), so that the edge list of any vertex can be randomly accessed. The header node is composed of two parts, where the data field vexdata is used to store the name of the vertex or other related information; the chain field firstarc is used to point to the first vertex in the linked list.
②Edge table : It consists of n edge linked lists representing the adjacency relationship between vertices in the graph . The arc node structure of the edge list is composed of three parts, the adjacent point field adjvex is used to store the position of the vertex adjacent to the vertex vi in ​​the graph; the chain field nextarc is used to point to the next edge or arc associated with the vertex vi nodes; the data field info is used to store information related to edges or arcs (such as the weight of each edge or arc in the weighted graph).
graph adjacency list

# define MAX_VERTEX_NUM 20					//最多顶点个数

/*图的邻接表表示法*/
typedef char VertexData;
//弧结点结构
typedef struct ArcNode {
    
    
	int adjvex;								//该弧指向顶点的位置
	struct ArcNode* nextarc;				//指向下一条弧的指针
	int info;								//与弧相关的信息
}ArcNode;
//表头结点结构
typedef struct VertexNode {
    
    
	VertexData data;						//顶点数据
	ArcNode* firstarc;						//指向该顶点的第一条弧的指针
}VertexNode;
//邻接表结构
typedef struct {
    
    
	VertexNode vertex[MAX_VERTEX_NUM];
	int vexnum, arcnum;						//图的顶点数和弧数
}AdjList;

2.2 Create an adjacency list of a directed graph

Adjacency list creation process

/*采用邻接表表示法创建有向图*/
/*求顶点位置*/
int LocateVertex(AdjList* G, VertexData v) {
    
    
	int k;
	for (k = 0; k < G->vexnum; k++) {
    
    
		if (G->vertex[k].data == v)
			break;
	}
	return k;
}

/*创建有向图*/
int CreateAdjList(AdjList* G) {
    
    
	int i, j, k;
	VertexData v1, v2;
	ArcNode* p;
	printf("输入图的顶点数和弧数:");			//输入图的顶点数和弧数
	scanf("%d%d", &G->vexnum, &G->arcnum);
	printf("输入图的顶点:");
	for (i = 0; i < G->vexnum; i++) {
    
    		//输入图的顶点,初始化顶点结点
		scanf(" %c", &(G->vertex[i].data));
		G->vertex[i].firstarc = NULL;
	}
	for (k = 0; k < G->arcnum; k++) {
    
    
		printf("输入第%d条弧的两个顶点:", k + 1);
		scanf(" %c %c", &v1, &v2);			//输入一条弧的两个顶点
		i = LocateVertex(G, v1);
		j = LocateVertex(G, v2);
		p = (ArcNode*)malloc(sizeof(ArcNode));	//申请新弧结点
		p->adjvex = j;
		p->nextarc = G->vertex[i].firstarc;
		G->vertex[i].firstarc = p;
	}
}

3. Cross linked list notation

3.1 Cross linked list storage structure of graph

Orthogonal List is another linked storage structure of directed graph . Each arc in the directed graph corresponds to an arc node in the cross-linked list, and each vertex in the directed graph corresponds to a node in the cross-linked list, which is called a vertex node.
Vertex node
data : Used to store information related to vertices, such as the name of the vertices.
firstin : used to point to the first arc vertex with this vertex as the arc head.
firstout : used to point to the first arc vertex with this vertex as the arc tail.

arc nodetailvex : Indicates the position of the arc tail vertex in the graph.
headvex : Indicates the position of the arc head vertex in the graph.
hlink : point to the next arc with the same arc head as this arc.
tlink : points to the next arc with the same arc tail as this arc.
info : point to the relevant information of the arc, such as weight, etc.
cross linked list of graph

# define MAX_VERTEX_NUM 20					//最多顶点个数

/*十字链表表示法*/
typedef char VertexData;
//弧结点结构
typedef struct ArcNode {
    
    
	int tailvex, headvex;
	struct ArcNode* hlink, * tlink;
}ArcNode;
//顶点结构
typedef struct VertexNode {
    
    
	VertexData data;						//顶点信息
	ArcNode* firstin, * firstout;
}VertexNode;
//十字链表结构
typedef struct {
    
    
	VertexNode vertex[MAX_VERTEX_NUM];
	int vexnum, arcnum;						//图的顶点数和弧数
}OrthList;

3.2 Create a cross-linked list of directed graphs

/*创建有向图的十字链表*/
/*求顶点位置*/
int LocateVertex(OrthList* G, VertexData v) {
    
    
	int k;
	for (k = 0; k < G->vexnum; k++) {
    
    
		if (G->vertex[k].data == v)
			break;
	}
	return k;
}

/*创建有向图的十字链表*/
void CreateOrthList(OrthList* G) {
    
    
	int i, j, k;
	VertexData vt, vh;
	ArcNode* p;
	printf("输入图的顶点数和弧数:");
	scanf("%d%d", &G->vexnum, &G->arcnum);	//输入图的顶点数和弧数
	printf("输入图的顶点:");
	for (i = 0; i < G->vexnum; i++) {
    
    		//输入图的顶点,初始化顶点结点
		scanf(" %c", &(G->vertex[i].data));
		G->vertex[i].firstin = NULL;
		G->vertex[i].firstout = NULL;
	}
	for (k = 0; k < G->arcnum; k++) {
    
    
		printf("输入第%d条弧的弧尾和弧头:", k + 1);
		scanf(" %c %c", &vt, &vh);
		i = LocateVertex(G, vt);
		j = LocateVertex(G, vh);
		p = (ArcNode*)malloc(sizeof(ArcNode));	//申请新弧结点
		p->tailvex = i;
		p->headvex = j;
		p->tlink = G->vertex[i].firstout;
		G->vertex[i].firstout = p;
		p->hlink = G->vertex[j].firstin;
		G->vertex[j].firstin = p;
	}
}

4. Adjacency to multiple tables

4.1 Storage structure of adjacency multitable

The adjacency multi-list (Adjacency Multi-list) is a storage structure of an undirected graph , which can provide more convenient edge processing information.
Vertex Node Structuredata : Used to store information related to the vertex, such as the name of the vertex.
firstedge : used to point to the first edge attached to the vertex.
Edge node structuremark : mark field, used to mark whether this edge has been searched.
ivex / jvex : The positions in the graph of the two vertices attached to this edge.
ilink : used to point to the next edge attached to the vertex ivex.
jlink : used to point to the next edge attached to the vertex jvex.
adjacency multiple table

# define MAX_VERTEX_NUM 20					//最多顶点个数

/*邻接多重表表示法*/
typedef char VertexData;
//边结点结构
typedef struct EdgeNode {
    
    
	int mark, ivex, jvex;
	struct EdgeNode* ilink, * jlink;
}EdgeNode;
//顶点结点结构
typedef struct {
    
    
	VertexData data;
	EdgeNode* firstedge;
}VertexNode;
//邻接多重表结构
typedef struct {
    
    
	VertexNode vertex[MAX_VERTEX_NUM];
	int vexnum, arcnum;						//图的顶点数和弧数
}AdjMultiList;

4.2 Create an adjacency multitable for an undirected graph

/*创建无向图的邻接多重表*/
/*求顶点位置*/
int LocateVertex(AdjMultiList* G, VertexData v) {
    
    
	int k;
	for (k = 0; k < G->vexnum; k++) {
    
    
		if (G->vertex[k].data == v)
			break;
	}
	return k;
}

/*创建无向图的邻接多重表*/
void CreateAdjMultiList(AdjMultiList* G) {
    
    
	int i, j, k;
	VertexData v1, v2;
	EdgeNode* p;
	printf("输入图的顶点数和弧数:");
	scanf("%d%d", &G->vexnum, &G->arcnum);	//输入图的顶点数和弧数
	printf("输入图的顶点:");
	for (i = 0; i < G->vexnum; i++) {
    
    		//输入图的顶点,初始化顶点结点
		scanf(" %c", &(G->vertex[i].data));
		G->vertex[i].firstedge = NULL;
	}
	for (k = 0; k < G->arcnum; k++) {
    
    
		printf("输入第%d条弧的两个顶点:", k + 1);
		scanf(" %c %c", &v1, &v2);			//输入一条弧的两个顶点
		i = LocateVertex(G, v1);
		j = LocateVertex(G, v2);
		p = (EdgeNode*)malloc(sizeof(EdgeNode));
		p->ivex = i;
		p->jvex = j;
		p->mark = 0;

		p->ilink = G->vertex[i].firstedge;
		p->jlink = G->vertex[j].firstedge;
		G->vertex[i].firstedge = p;
		G->vertex[j].firstedge = p;
	}
}

Reference: Geng Guohua "Data Structure - Described in C Language (Second Edition)"

For more data structure content, follow my "Data Structure" column : https://blog.csdn.net/weixin_51450101/category_11514538.html?spm=1001.2014.3001.5482

Guess you like

Origin blog.csdn.net/weixin_51450101/article/details/122912327