Learning data structure-Chapter 5: Graphs (the storage method of graphs)

Chapter 5: Graphs (the storage method of graphs)

1. The adjacency matrix method

The following is a representation of an undirected graph. We use a one-dimensional array to store the point set and a two-dimensional array to store the edge set


二维数组表示边: The row number indicates the actual endpoint, the column number indicates the end endpoint, and the value indicates whether the edge exists and the weight of the edge. We call the matrix represented by this two-dimensional array as邻接矩阵

邻接矩阵法

  • The adjacency matrix A of the graph G=(V,E) with the number of nodes n is yes n*n(each row number represents a node, each column number represents a node, n nodes are n*n)
  • Number the vertices of G as V1, V2, V3...Vn (1, 2, 3... array subscript)
  • If <Vi,Vj> exists, then A[i][j]=1, otherwiseA[i][j]=0

Note that the directed edge is different from the undirected edge A[i][j]=1. If the directed edge is Vi---->Vj, it will be stored . If there is no directed edge Vi-------Vj, it will be stored A[i][j]=1、A[j][i]=1.

Adjacency matrix of directed graph:

Undirected graph adjacency matrix: (symmetric about the diagonal)

Weighted graph:


If there is a directed edge Vi---->Vj, store it A[i][j]=Wi,j, if there is no directed edge Vi-------Vj, store it A[i][j]=Wi,j、A[j][i]=Wi,j.

Network to adjacency matrix

Language implementation

#define MaxvertexNum 100 
typedef char VertexType; //每个结点时字符型的(类型可自行调整)
typedef int EdgeType; //可表示便是否存在、边的权重(类型可以自行调整)
typedef struct{
    
     
     VertexType Vex[MaxVertexNum];//点集数组
     EdgeType Edge[MaxVertexNum][MaxVertexNum];//边集数组
     int vexnum,arcnum;//结点的数量和边的数量
}MGraph;

The space complexity of the adjacency matrix method is O(n^2)

2. Properties of adjacency matrix

Property 1 : The space complexity of the adjacency matrix method is O(n^2), which is suitable for dense graphs

Because no matter whether the edge exists or not, we will apply for space for it, so the dense graph is the graph with more edges, the higher its space utilization

Property 2 : The adjacency matrix of the undirected graph is a symmetric matrix

Property 3 : The number of non-zero elements (non-positive infinity) in the i-th row (or the i-th column) of the undirected graph is the degree of the i-th vertex; the i-th row (the i-th example) of the directed graph is non-zero element The number of (non-positive infinity) is the out degree (in degree) of the i-th vertex

Suppose the adjacency matrix of the graph G is A, what does the matrix operation A^n mean? ?

Let’s look A^2at the first two matrices, we knowA^2[2][5]=1*1+0*0+1*1+0*0+0*0=2

First, the first one 1*1: the first 1 represents B->A, the second 1 represents A->E, multiplication represents that there is a B->A->E path for B->E, and the second 1*1: first One 1 represents B->C, the second 1 represents C->E, and multiplication represents that there is a path from B->E: B->C->E.

So: it A^2[2][5]=2means that there are two paths with length 2 from vertex V2 (B) to vertex V5 (E). (The length is 2 because it is the A side, and two because the value is 2)

Now let’s look at it again A^3. First, there are two matrices A^2, one is A and the other is A

Then:, A^3[2][5]=0*0+0*0+1*1+1*0+2*0=1first of all, we know A^2[2][3]=1表示从顶点V2->V5长度为2的路径只有一条, thenA^3[2][5]=1表示从顶点V2到顶 点V5长度为3的路径只有一条

综上我们可知A^n[i][j]表示从顶点vi到顶点Vj长度为n的路径的条数

2. Adjacency matrix table

The adjacency matrix method is explained above. For the sparse graph above, if we use the adjacency matrix method for storage, there will be a lot of waste of space, namely

There will be a lot of space waste in the adjacency matrix method for storing sparse graphs. For sparse graphs, we usually use the adjacency table method to store

邻接表法: Create a singly linked list for each vertex to store the edges adjacent to it

  • Vertex table: adopted 顺序存储, each array element stores the data of the vertex and the head pointer of the edge table
  • Edge table (out-edge table): Adopt 链式存储, store all the edges adjacent to a vertex in a singly linked list, and a linked list node represents an edge from the vertex to the vertex of the linked list node

3. Adjacency list method

Directed graph:

Undirected graph:

Code

#define MaxVertexNum 100 
//边表
typedef struct ArcNode{
    
     
    int adjvex; //下一个结点的下标
    struct ArcNode *next;//指针指向下一个边表结点
    //InfoType info; //表的权重
}ArcNode;
//顶点表
typedef struct VNode{
    
     
    VertexType data;//数据
    ArcNode *first //指向属于它的边表的头指针
}VNOode,AdjList [MaxVertexNum];//数组类型
//邻接表
typedef struct{
    
     
    AdjList vetices;//顶点表
    int vexnum,arcnum;//顶点数量和边的数量
}ALGraph;

'
Features of adjacency list

V represents the number of vertices, E represents the number of edges

  • If G is an undirected graph, the storage space is O(V+2E)
  • If G is a directed graph, the storage space is O(V+E)
  • The adjacency list is more suitable for sparse graphs
  • If G is an undirected graph, the degree of the node is the length of the side table of the node
  • If G is a directed graph, the out-degree of the node is the length of the side table of the node, and the in-degree is calculated by traversing the entire adjacency list
  • The adjacency list is not unique, and the order of the nodes of the side table may be different depending on the algorithm and the input

4. Adjacency matrix VS adjacency table

5. Cross Linked List

· 十字链表: A chain storage structure of directed graph

We can find in the adjacency list that it is very convenient to find the out-degree of a node by traversing the edge table of the node directly, but if you want to find the in-degree of a node, it is more complicated to traverse the entire adjacency list, so We have introduced a cross-linked list, which is relatively simple to query the out-degree and in-degree of the node.

Vertex table node

data: data field
firstin: the head pointer of the
incoming singly linked list firstout: the head pointer of the outgoing singly linked list

Edge table node

tailvex: the end of the arc headvex: the end of
the arc head
hlink: the pointer of the
next arc head tlink: the pointer of the next arc end
info: the weight of the edge

Code

#define MaxVertexNum 100 
typedef struct ArcNode{
    
     
       int tailvex,headvex; 
       struct ArcNode *hlink,*tlink; 
       //InfoType info: 
}ArcNode; 
typedef struct VNode{
    
     
       VertexType data; 
       ArcNode *firstin,*firstout; 
}VNode;
typedef struct{
    
     
       VNode xlist[MaxVertexNum];
       int vexnum, arcnum; 
}GLGraph;

6. Adjacent multiple tables

· Mentioned above 十字链表is 有向图a chain store structure, and 邻接多重表it is for 无向图one kind of linked storage structure.

If we want to delete an undirected edge in the adjacency list, we need to find the corresponding node, then traverse its edge table, find the corresponding edge table node and delete it, so the efficiency of the operation is relatively low. Which leads to邻接多重表

ivex: the first endpoint
of the edge
ilink: the pointer of the edge table node of the next edge adjacent to the first endpoint
jvex: the second endpoint of the edge jlink: the next adjacent to the second endpoint Pointer
info of edge table node : weight (unnecessary)
mark: mark (unnecessary)

#define MaxVertexNum 100 
typedef struct ArcNode{
    
     
       int ivex, jvex; 
       struct ArcNode *ilink, *jlink; 
       //InfoType info; 
       //bool mark: 
}ArcNode;
typedef struct VNode{
    
     
       VertexType data; 
       ArcNode *firstedge; 
)VNode;
typedef struct{
    
     
       VNode adjmulist[MaxVertexNum];
       int vexnum, arcnum; 
}AMLGraph;

7. Rimuke

Knowledge of data structure, the official 理木客account is being updated simultaneously, welcome to follow

Guess you like

Origin blog.csdn.net/qq_41941875/article/details/106643728