Excerpt: storage structure of Graphs

Source: Internet
Author: User
. Adjacency Matrix storage Representation

   

Let figure A = (V, E) be a graph with n vertices. The adjacent matrix of the graph is a two-dimensional array. edge [N] [N] is used to store vertex information and edge or arc information. Defined:

     
(1) The adjacent matrix of an undirected graph is symmetric. The adjacent matrix of a directed graph may be asymmetric.

(2) In a directed graph, calculate the number of rows I 1 to obtain the degree of output of vertex I, and count the number

Number can be used to obtain the inbound degree of vertex J. In an undirected graph, the degree of vertex I can be obtained by counting the number of I rows (columns) 1.



Storage representation of the graph's Adjacent matrix:


# DefineInfinity int_max // maximum ∞

# DefineMax_vertex_num 20 // maximum number of vertices

TypedefEnum {DG, dn, AG, an} graphkind; // {directed graph, directed graph, undirected graph, undirected network}

Typedef structArccell {

Vrtype adj; // vrtype is the vertex link type. For the no-Permission graph, 1 or 0 indicates the adjacent no;

// The weighted graph is of the weight type.

Infotype * Info; // pointer to the arc-related information

} Arccell, adjmatrix [max_vertex_num] [max_vertex_num];

Typedef struct{

Vertextype vexs [max_vertex_num]; // vertex Vector

Adjmatrix arcs; // The adjacent matrix.

IntVexnum, arcnum; // Number of vertices and edges in the graph

Graphkind kind; // The type of the graph.

} Mgraph;

The time complexity of constructing a undirected network with n vertices and e-edge is O (n2 + E * n), where O (n2) is used for adjacent

Matrix initialization.



2. adjacency list storage Representation

An adjacent table is a chained storage structure of a graph. It creates a single-chain table for each vertex in the graph, the knots in the I-th single-chain table indicate the edges attached to vertex VI (the directed graph is an arc ending with vertex VI). Each node is composed of three fields: adjvex) indicates the position of the vertex adjacent to vertex VI in the graph. The chain domain (nextarc) indicates the node of the next edge or arc, and the data domain (Info) stores edge or arc-related information (such as weights ). Each linked list is attached with a header node, which includes data and firstarc pointing to the first node in the linked list. These header nodes are usually stored in a sequential structure,

To randomly access the linked list of any vertex.

  
In an undirected graph's adjacent table, the degree of vertex VI is equal to the number of knots in the I-th linked list. In a directed graph's adjacent table, the degree of exit of vertex VI is equal to the number of knots in the I-th linked list, the entire adjacent table must be traversed to evaluate the input degree of VI. In order to facilitate the evaluation of VI, a directed graph inverse adjacent table (an adjacent table created by the arc with the vertex VI as the header) must be created ).

Storage representation of the graph's adjacent table:

# DefineMax_vertex_num 20

Typedef structArcnode {

IntAdjvex; // The Position of the vertex pointed to by the Arc

StructArcnode*Nextarc; // pointer to the next arc

Infotype*Info; // pointer to the arc-related information

} Arcnode;

Typedef structVnode {

Vertextype data; // vertex Information

Arcnode*Firstarc; // point to the first arc attached to the vertex

} Vnode, adjlist [max_vertex_num];

Typedef struct{

Adjlist vertices;

IntVexnum, arcnum; // Number of vertices and arcs in the graph

IntKind; // The type of the graph

} Algraph;



3. Cross-linked list storage representation of Directed Graphs

Orthogonal list is another chained storage structure of Directed Graphs. It can be seen as a linked list created by combining the adjacent table of Directed Graphs with the inverse list.

# DefineMax_vertex_num 20

Typedef structArcbox {

IntTailvex, headvex; // the tail of the arc and the position of the header Vertex

StructArcbox*HLINK,*Tlink; // point to the pointer fields of the same arc header and the same arc tail respectively.

Infotype* Info;// Pointer to the arc-related information

} Arcbox;

Typedef structVexnode {

Vertextype data;

Arcbox * firstin, * firstout; // point to the first input and output arc of the vertex respectively.

} Vexnode;

Typedef struct{

Vexnode xlist [max_vertex_num]; // header Vector

IntVexnum, arcnum; // number of current vertices and arcs in The Directed Graph

} Olgraph;

 

4. Storage and representation of undirected joined multiple tables



# Defin
E max_vertex_num 20

TypedefEmnu {unvisited, visited} visitif;

Typedef structEBox {

Visitif mark; // access tag

IntIvex, jvex; // the position of the two vertices attached to the edge

StructEBox*Ilink,*Jlink; // point to the next edge attached to the two vertices respectively.

Infotype* Info; // Indicates the edge information pointer.

} EBox;

Typedef structVexbox {

Vertextype data;

EBox*Firstedge; // point to the edge of the first vertex.

} Vexbox;

Typedef struct{

Vexbox adjmulist [max_vertex_num];

IntVexnum, edgenum; // Number of vertices and edges in an undirected graph

} Amlgraph;



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.