7.4 Storage Structure
The graph cannot represent the relationship between elements by the physical location of data elements in the memory. The following provides different storage structures for the five graphs.
7.4.1 The adjacent matrix is also called array Notation)
Considering that a graph is composed of a fixed point and an edge or an arc, it is difficult to get together. Therefore, we naturally consider two structures for separate storage. It is a good choice to store vertices in a one-dimensional array, regardless of their size and priority. Because edges or arcs are the relationship between vertices and vertices, one-dimensional data may not be fixed. Therefore, a two-dimensional array is used for storage. So our scheme of the adjacent matrix was born.
Two arrays are used to represent the graph. A one-dimensional array stores vertex information in the graph, and a two-dimensional array is called the adjacent matrix) Stores edge or arc information in the graph.
If graph GV, E) Has n vertices, the adjacent matrix is a matrix of n x n, which is defined:
650) this. width = 650; "title =" QQ20130909165658.bmp "src =" http://www.bkjia.com/uploads/allimg/131228/2015155555-0.jpg "/>
The edge array of an undirected graph is a symmetric matrix.
1) it is easy to determine whether any two vertices have boundless edges. You only need to check whether the arc [I] [j] In the edge array is equal to 1.
2) knowing the degree of a vertex is actually the sum of the elements of the vertex Vi in the I row of the adjacent matrix.
3) Finding All adjacent points of vertex Vi is to scan the elements in the I-th line of the adjacent matrix. If arc [I] [j] is 1, it is the adjacent point.
Let's look at a directed graph example.
650) this. width = 650; "title =" QQ20130909165859.bmp "src =" http://www.bkjia.com/uploads/allimg/131228/201515IX-1.jpg "/>
Directed Graphs focus on inbound and outbound degrees. The entry level of vertex V1 is the sum of the elements in the 1st columns. The outdegree of vertex V1 is the sum of all elements in 1st rows.
650) this. width = 650; "title =" QQ20130909170151.bmp "src =" http://www.bkjia.com/uploads/allimg/131228/2015151X3-2.jpg "/>
650) this. width = 650; "title =" QQ20130909170231.bmp "src =" http://www.bkjia.com/uploads/allimg/131228/2015152A6-3.jpg "/>
Storage Structure of the adjacent matrix
# Define MAXVEX 100
# Define INFINITY 65535
Typedef struct
{
Char vexs [MAXVEX]; // vertex table, storing vertices
Int arc [MAXVEX] [MAXVEX]; // The adjacent matrix stores edge information.
Int numVertexes; // Number of vertices in the graph
Int numEdges; // Number of edges in the graph
} MGraph;
7.4.2 adjacent table
This structure is a waste of storage space for graphs with relatively few variables. Now another storage method is considered: the adjacent table.
1. the vertex in the graph is stored in a one-dimensional array. Of course, the vertex can be stored in a single-chain table, but the array is easier to read vertex information and more convenient. In addition, for the vertex array, each element also needs to store a pointer pointing to the first adjacent vertex to find the vertex edge information.
2. In the graph, all the neighboring vertices of each vertex Vi form a linear table. Due to the number of adjacent points, the edge table of vertex Vi is stored in a single linked list. an undirected graph is called the edge table of vertex Vi, directed Graphs are called Vi as the output edge table at the end of the arc.
650) this. width = 650; "title =" QQ20130909171415.bmp "src =" http://www.bkjia.com/uploads/allimg/131228/201515M54-4.jpg "/>
For such a structure:
1) To know the degree of a vertex, search for the number of nodes in the edge table of the vertex.
2) To determine whether edge exists between vertex Vi and Vj, you only need to test whether adjvex in the edge table of vertex Vi has subscripts j of node Vj.
3) If all adjacent points of a vertex are obtained, the edge table of the vertex is traversed. The vertex corresponding to the adjvex domain is the adjacent point.
If it is a directed graph, we store edge tables at the end of the vertex as the arc, so that we can easily obtain the degree of exit for each vertex, however, to facilitate the determination of the vertex's ingoing or the arc with the vertex as the arc header, we can create a directed graph's inverse adjacent table. As shown in:
650) this. width = 650; "title =" QQ20130909174837.jpg "src =" http://www.bkjia.com/uploads/allimg/131228/2015155509-5.jpg "/>
You can add a weight data field in the edge table node definition to store the weight information.
650) this. width = 650; "title =" QQ20130909173417.bmp "src =" http://www.bkjia.com/uploads/allimg/131228/20151530b-6.jpg "/>
7.4.3 cross linked list
For a directed graph, the adjacent table cares about the outbound degree. To understand the inbound degree, you must traverse the entire graph to know whether the adjacent table and the inverse adjacent table can be combined, that is, the cross linked list.
7.4.4 multiple adjacent tables
7.4.5 Number of edge Sets
650) this. width = 650; "title =" QQ20130909173850.bmp "src =" http://www.bkjia.com/uploads/allimg/131228/2015153264-7.jpg "/>
This article is from "Li Haichuan" blog, please be sure to keep this source http://lihaichuan.blog.51cto.com/498079/1293276