Figure (2) - storage structure of the graph

Generally speaking, the storage structure of the graph should be designed according to the requirements of the specific problem. Commonly used storage structures are adjacency matrix, adjacency list, adjacency multilist and cross-linked list.
1. Adjacency matrix

In the adjacency matrix representation of the graph, in addition to the vertex table that records the information of each vertex, there is also a matrix that represents the relationship between the various vertices, which is called the adjacency matrix.
As shown in the figure above, the adjacency matrices of undirected graph, directed graph and directed network are given respectively.

The adjacency matrix of w undirected graph is symmetric,
The adjacency matrix of a directed graph of w may be asymmetric.
w In an undirected graph, the degree of node i can be obtained by counting the number of 1 in the i-th row (column).
w In a directed graph, the out-degree of node i can be obtained by counting the number of 1s in the i-th row, and the in-degree of node j can be obtained by counting the number of 1s in the j-th column.
w is a static storage method. Not easy to expand.

w is related to the number of nodes and has nothing to do with edges (arcs). A sparse matrix will appear.

2. Adjacency list
An adjacency list is an improvement on an adjacency matrix. When the number of edges in the graph is small, there will be a large number of zero elements in the adjacency matrix. In order to store these zero elements, a lot of storage space will be consumed. To this end, each row of the adjacency matrix can be changed to a singly linked list.

The adjacency list representations of undirected and directed graphs are given in Fig. Among them, in the adjacency list representation of the directed graph, the number of arc nodes in the arc linked list of the vertex is counted, and the out-degree of the node is obtained. If you want to get its in-degree, you can make its inverse adjacency list, As shown below:

For a weighted graph, a domain for storing the weights on the edge must be added to the edge node of the adjacency list, as shown in the following figure:


Degree of n nodes:

      Undirected graph: is the number of nodes in the singly linked list of this node.
      Directed graph: out-degree: is the number of nodes in the singly linked list of this node.
                   In-degree: Scan the singly linked list of the entire adjacency list, which is the number of times the node appears or scan the inverse adjacency list.

3. Adjacent multiple table
In the adjacency list of an undirected graph, each edge is stored twice, for simplicity, resulting in an adjacency multi-list. In the adjacency multilist of an undirected graph, there are two vertex fields adjvex1 and adjvex2, respectively indicating that the two vertices attached to the edge are in the sequence number nexttarc1 field in the graph is a link pointer, pointing to the next edge attached to the vertex adjvex1, nextarc2 points to the next edge attached to vertex adjvex2
4. Cross-linked list

Similar to undirected graphs, directed graphs also have another chain storage structure, called a cross-linked list. According to needs, for directed graphs, sometimes both adjacency lists and inverse adjacency lists are used. These two lists can be combined into one and represented by a cross-linked list.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324647395&siteId=291194637