List of adjacent tables 1. List of adjacent tables
An adjacent table is a chained storage structure of a graph.
In the adjacent table, a single-chain table is created for each vertex in the graph. The node in the I-th single-chain table is the edge attached to the vertex VI (the directed graph is an arc ending with vertex VI ).
The structure of the table node and the header node in the adjacent table:
2. undirected graph joining table
Figure 7-5
Iii. Directed Graph joining table and inverse joining table
(1) In the directed graph's adjacent tableIThe link edges of a single-chain table are vertices.ISent edge.
(2) In order to obtain the inbound degree of vertex I, the entire adjacent table needs to be traversed. Therefore, you can create an inverse adjacent table.
(3) In the inverse neighbor table of the directed graphIAll links to a single-linked table are directed to the vertex.I.
(A) adjacent table (B) Inverse adjacent table
Figure 7-6
Iv. Summary of the adjacent table
◆ If there are n vertices and e edges in the graph, then n vertex nodes and 2e table nodes are required to represent an undirected graph in the adjacent table, if you do not consider the inverse adjacent table, you only need n vertex nodes and e edge nodes.
◆ In an undirected graph's adjacent table, the degree of vertex VI is exactly the number of knots in the I-th linked list.
◆ In a directed graph, the number of nodes in the I-th linked list is only the exit degree of vertex VI. The number of nodes in the I-th linked list in the inverse adjacent table is Vi's inbound.
◆ The time complexity of creating an adjacent table is O (n + E ).
Adjacent matrix of an image
The adjacency matrix (adjacency matrix) is used to store vertex information in the graph using a one-dimensional array, and represent the adjacent relations between vertices in the graph using a matrix. Assume that graph G = (V, E) has n definite vertices, that is, V = {v0, V1 ,..., Vn-1}, it indicates that the neighboring relationship between vertices in G is a n × n matrix, the elements of the matrix is:
A [I] [J] =
If G is a network chart, the adjacent matrix can be defined:
A [I] [J] =
Wij indicates the weight of an edge (Vi, vj) or <VI, VJ>. ∞ indicates the number of values allowed by a computer to be greater than all edge values.
Figure 6-7 an undirected graph's Adjacent matrix representation
Figure 6-8 the adjacent matrix of a network chart
The representation is shown in Figure 6-7 in the form of an adjacent matrix.
The network diagram is represented in 6-8 using the adjacent matrix representation.
It is easy to see that this representation has the following features:
(1) The undirected graph's Adjacent matrix must be a symmetric matrix. Therefore, you only need to store the elements of the upper (or lower) Triangle Matrix when storing the adjacent matrix.
(2) For undirected graphs, non-zero elements (or non-∞ elements) in the I row (or column I) of the adjacent matrix) the number is exactly the degree TD (VI) of the I vertex ).
(3) For Directed Graphs, non-zero elements (or non-∞ elements) in row I (or column I) of the adjacent matrix) the number is exactly the outbound OD (vi) (or inbound ID (VI) of vertex I )).
(4) using the adjacent matrix method to store a graph, it is easy to determine whether any two vertices in the graph are connected by edges. However, you must determine the number of edges in the graph, each element must be checked by row or column, which takes a lot of time. This is the limitation of using an adjacent matrix to store graphs.
The following describes the representation of the graph's Adjacent matrix storage.
In addition to using a two-dimensional array to store the adjacent matrix between vertices, we also need a one-dimensional array to store vertex information, in addition, the number of vertices and edges of the graph are displayed. The format is described as follows:
Structure 6-1 Graph
# Define maxvertexnum 100 // set the maximum number of vertices to 100
Typedef char vertextype; // set the vertex type to vertex type
Typedef int edgetype; // The Edge Weight is set to an integer.
Typedef struct
{Vertextype vexs [maxvertexnum]; // vertex table
Edetype edges [maxvertexnum] [maxvertexnum]; // The adjacent matrix, that is, the edge table.
Int N, E; // Number of vertices and edges
} Mgragh; // maragh is the graph type stored in the adjacent matrix.
Algorithm 6-1 creates an image's Adjacent matrix Storage
Void createmgraph (mgraph * g)
{// Establish the storage of the directed graph G's Adjacent matrix
Int I, J, K, W;
Char ch;
Printf ("Enter the number of vertices and the number of edges (input format: vertex number, number of edges): \ n ");
Scanf ("% d, % d", & (g-> N), & (g-> E); // enter the number of vertices and edges.
Printf ("Enter the vertex information (in the format of vertex number <CR>): \ n ");
// Enter vertex information to create a vertex table
For (I = 0; I <G-> N; I ++) scanf ("\ n % C", & (g-> vexs [I]);
For (I = 0; I <G-> N; I ++)
For (j = 0; j <G-> N; j ++) g-> edges [I] [J] = 0; // initialize the adjacent matrix
Printf ("Enter the sequence numbers of the two vertices corresponding to each edge (input format: I, j): \ n ");
For (k = 0; k <G-> E; k ++)
{Scanf ("\ n % d, % d", & I, & J); // enter the e edge, establish the adjacent matrix G-> edges [I] [J] = 1;
// If G-> edges [J] [I] = 1 is added, it is created for the undirected graph's Adjacent matrix storage.
}
}