Storage Structure Code

Source: Internet
Author: User

Storage Structure Code

I. Graph Storage Structure

1.1 adjacent matrix

Two arrays are used to represent the graph. A one-dimensional array stores vertex information in the graph, and a two-dimensional array (Adjacent matrix) Stores edge or arc information in the graph.

If graph G has n vertices, the adjacent matrix is a matrix of n x n, which is defined:


Looking at an instance, the left is an undirected graph.


As shown above, the edge array of an undirected graph is a symmetric matrix. The so-called symmetric matrix is that the element of the n-level matrix satisfies aij = aji <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Response + response/response + zcrHwdq907Xjo7s8L3A + response/yzby9sr6/yOu2yLrNs/response + CjwvcD4KPHA + response = "http://www.2cto.com/uploadfile/Collfiles/20141117/20141117090117363.png" width = "362" height = "71" alt = "\">

Here, wij indicates the weight value on (vi, vj. An infinite number indicates the value allowed by a computer greater than the weight on all sides, that is, an impossible limit value. The left graph is a directed network graph, and the right graph is its adjacent matrix.


Then how does an adjacent matrix create a graph? The Code is as follows. (Directed Graph)

# Include
 
  
Typedef char VertexType; // vertex type typedef int EdgeType; // edge weight type # define INF 65535 // use 65535 to represent infinity typedef struct {VertexType vexs [MAXVEX]; // vertex table EdgeType arc [MAXVEX] [MAXVEX]; // The adjacent matrix, which can be seen as an edge int numVertexes or numEdges; // The number of vertices and edges in the Graph} Graph; void CreateGraph (Graph * G) {int I, j, k, w; printf ("Number of input vertices and edges: \ n "); scanf ("% d", & G-> numVertexes, & G-> numEdges); getchar (); printf ("Enter % d vertex symbols: \ n ", g-> numVertexes); for (I = 0; I
  
   
NumVertexes; I ++) scanf ("% c", & G-> vexs [I]); getchar (); for (I = 0; I
   
    
NumVertexes; I ++) for (j = 0; j
    
     
NumVertexes; j ++) G-> arc [I] [j] = INF; for (k = 0; k <2 * G-> numEdges; k ++) // about the number of cycles undirected graph G-> numEdges times, directed graph G-> numEdges * 2 times {subscript I on printf ("input edge (vi, vj, j and weight w: "); // if it is a directed graph, input the subscript scanf (" % d ", & I, & j, & w) according to the direction ); g-> arc [I] [j] = w; G-> arc [j] [I] = G-> arc [I] [j]; // remove this sentence from the directed Graph} // print the void printGraph (Graph g) {int I, j; for (I = 0; I <g. numVertexes; I ++) {for (j = 0; j <g. numVertexes; j ++) {printf ("% d", g. arc [I] [j]);} printf ("\ n");} int main () {Graph g; // CreateGraph (& g) of the adjacent matrix ); printGraph (g); return 0 ;}
    
   
  
 

Test data:

4 6
ABCD
0 1 8
0 2 7
0 3 65535
1 0 65535
1 2 4
1 3 65535
2 0 65535
2 1 65535
2 3 2
3 0 5
3 1 3
3 2 65535

Output:


1.2 adjacent table

The adjacent matrix is a good graph storage structure. However, for graphs with fewer edge numbers than those with smaller vertices, this structure is a great waste of storage space. Therefore, finding a storage method that combines arrays and linked lists is called an adjacent table.

The Processing Method of the adjacent table is as follows:

(1) The vertex in the graph is stored in a one-dimensional array. Of course, the vertex can also be stored in a single-chain table. However, the array can easily read vertex information, making it easier.

(2) In the graph, all the adjacent points of each vertex vi form a linear table. because the number of adjacent points is not fixed, it is stored in a single-chain table. an undirected graph is called an edge table of vertex vi, directed Graphs are called vertex vi as the output edge table at the end of the arc.

For example, it is the structure of an undirected graph adjacent table.


It can be seen that each node in the vertex table is represented by two fields: data and firstedge. data is the data field and stores vertex information. firstedge is the pointer field and points to the first node in the edge table, that is, the first adjacent point of the vertex. Edge table nodes are composed of adjvex and next fields. Adjvex is an adjacent vertex domain. It stores the subscript of the adjacent vertex of a vertex in the vertex table, And next stores the pointer pointing to the next node in the edge table.

For a network chart with weights, you can add a weight data field in the edge table node definition to store the weights. As shown in.


For the structure of the adjacent table, the code for creating the graph is as follows. (Undirected graph)

# Include
 
  
# Define MAXVEX 1000 // maximum number of vertices typedef char VertexType; // vertex type typedef int EdgeType; // edge weight type typedef struct EdgeNode // edge table node {int adjvex; // The adjacent vertex field stores the corresponding subscript EdgeType weigth of the vertex. // It is used to store the weight value. For non-net graphs, you do not need struct EdgeNode * next; // The Link field, pointing to the next adjacent vertex} EdgeNode; typedef struct VertexNode // vertex Table Structure {VertexType data; // vertex field, storing vertex information EdgeNode * firstedge; // edge header pointer} VertexNode, adjList [MAXVEX]; typedef struct {AdjList adjList; int numVertexes, numEdges; // The number of vertices and edges in the graph} GraphList; // create the graph's adjacent table structure void CreateGraph (GraphList * g) {int I, j, k; EdgeNode * e; printf ("Number of input vertices and edges: \ n "); scanf (" % d ", & g-> numVertexes, & g-> numEdges); getchar (); for (I = 0; I
  
   
NumVertexes; I ++) {printf ("Please input a vertex at a time % d: \ n", I); scanf ("% c ", & g-> adjList [I]. data); // enter the vertex information getchar (); g-> adjList [I]. firstedge = NULL; // set the edge table to an empty table} g-> adjList [I]. firstedge = NULL; // create an edge table for (k = 0; k <g-> numEdges; k ++) // The number of cycles in the adjacent table the undirected graph and the directed graph are both g-> numEdges count {the vertex numbers and weights on the printf ("input undirected graph edge (vi, vj: \ n "); int w; scanf (" % d ", & I, & j, & w); e = new EdgeNode; e-> adjvex = j; // The adjacent serial number is j e-> weigth = w; // edge
   
    
Value e-> next = g-> adjList [I]. firstedge; // point the e pointer to the structure directed to the current vertex g-> adjList [I]. firstedge = e; // point the pointer of the current vertex to e = new EdgeNode; e-> adjvex = I; e-> weigth = w; // edge
    
     
Value e-> next = g-> adjList [j]. firstedge; g-> adjList [j]. firstedge = e ;}} void printGraph (GraphList * g) {int I = 0; while (g-> adjList [I]. firstedge! = NULL & I <MAXVEX) {printf ("vertex: % c \ n", g-> adjList [I]. data); EdgeNode * e = NULL; e = g-> adjList [I]. firstedge; while (e! = NULL) {if (e-> adjvex! = I) printf ("neighbor Subscript: % d edge: <% c, % c> weigth: % d \ n", e-> adjvex, g-> adjList [I]. data, g-> adjList [e-> adjvex]. data, e-> weigth); e = e-> next;} I ++; printf ("\ n") ;}} int main (int argc, char ** argv) {GraphList g; CreateGraph (& g); printGraph (& g); return 0 ;}
    
   
  
 

Test data:

4 6
A
B
C
D
0 2 5
0 3 7
1 0 8
2 1 3
2 3 2
3 1 4

Output:


1.3 cross linked list

For directed graphs, the adjacent table is defective. If you are concerned about the degree of parallelism, you must traverse the entire graph to understand the level of parallelism. On the contrary, the inverse adjacent table does not solve the degree of parallelism. The following describes the storage method of this directed graph: The cross linked list combines the adjacent table and the inverse adjacent table.

Redefines the vertex table node structure, as shown below.


Firstin indicates the inbound header pointer, pointing to the first node of the vertex's inbound table, firstout indicates the outbound header pointer, pointing to the first node of the vertex's outbound table.

Redefine the edge table structure, as shown below.


Here, tailvex refers to the following table where the arc start point is in the vertex table, headvex refers to the subscript of the arc end point in the vertex table, headlink refers to the pointer field of the inbound edge table, pointing to the next edge with the same end point, taillink refers to the edge table pointer field pointing to the next edge with the same starting point. If it is a network, you can add a weight domain to store the weights.

For example, the vertex is still saved into a one-dimensional array, and the graph of the Real-line arrow pointer is exactly the same as that of the adjacent table. In terms of vertex v0, firstout points to the first node v3 in the output edge table. Therefore, the v0 edge table node hearvex = 3, and tailvex is actually the subscript 0 of the current vertex v0. Because v0 only has one outbound vertex, all headlinks and taillink are empty.


It is important to explain the meaning of the dotted arrow. It is actually the representation of the inverse adjacent table of the graph. For v0, it has two indegree vertices v1 and v2. Therefore, firstin points to the node where headvex is 0 in the edge table node of vertex v1, such as circle 1. Then, the headlink of the edge node points to the next edge vertex v2, such as circle 2. For vertex v1, it has an inbound vertex v2, so its firstin points to the node with headvex 1 in the edge table node of vertex v2, such as circle 3.

The benefit of the Cross-linked list is that it is easier to find the arc ending with v and the arc starting with v because the adjacent table and the inverse adjacent table are integrated, therefore, it is easier to obtain the vertex's outbound and inbound degrees.

In addition to the complexity of the structure, the time complexity of creating the graph algorithm is the same as that of the adjacent table. Therefore, in the directed graph application, the cross linked list is a very good data structure model.

Here we will introduce the above three storage structures. Apart from the third storage structure, the other two storage structures are relatively simple.

For more information, see the big data structure !!!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.