Graph (NET) storage structure (array storage representation i.e. adjacency matrix, adjacency table)

Source: Internet
Author: User

Graph (graph) is a nonlinear structure

The characteristics of the graph (many-to-many), the relationship between the vertices is arbitrary, the graph can be related to any two vertices, the apex of the predecessor and the number of successors is unlimited.

Figure : Data structures with many-to-many relationships between data elements, together with an abstract data type that consists of a set of basic operations.

Basic terminology of graphs

Vertex : The data element in the diagram.

Arc: if <v, W>∈VR, then <v, w> represents an arc from V to W , and called V is the arc tail, called W is the arc Head , at this time the figure is called the graph.

G1 = (V1, A1) V1 = {v1, v2, v3, v4}

A1 = {< v1, v2>, < v1, v3>, < V3, v4>, < V4, V1>}

Edge: If <v, W>∈VR will have <w, V>∈VR, then the order pairs (V, W) represent the two ordered pairs, representing a side between V and W , at this time the graph is called the non-direction diagram .

G2 = (V2, E2) V2 = {v1, v2, v3, v4, v5}

E2 = {(V1, v2), (v1, v4), (v2, V3), (v2, V5), (v3, v4), (V3, V5)}

The value range of the edges in the non-direction graph : 0≤e≤n (n-1)/2. (n indicates the number of vertices in the graph, with E for the number of edges.) And does not consider the vertex to its own edge. )

full picture : an n-1 (i.e., an edge between every two vertices) with n (2) edges is called a full graph.

The range of values for arcs in the direction graph : 0≤e≤n (n-1). (n indicates the number of vertices in the graph, and E represents the number of arcs.) And does not take into account the arc of the vertex to itself. )

Directed full graph: a directed graph with N (n-1) arcs (i.e., two arcs with opposite directions between each of the two vertices) is called a directed full graph.

Sparse Graph: A graph that contains very few edges or arcs.

Dense graph: A graph of nearly complete graphs that contain many edges or arcs.

Right : The number of edges or arcs associated with the graph, which can represent the distance or expense from one vertex to another.

Net : a graph with weights.

Sub-graph : If the figure G = (V, E) and g´= (V´, E´) are satisfied: V´ is contained in V and E´ is contained in E, then G´ is a sub-graph of G.

adjacency Point : if (V, V´) is an edge, the vertex V and V´ are mutually contiguous points, or V and V´ are adjacent, and the Edge (V, V´) is attached to Vertex v and V´, or (V, V´) associated with vertex V and V´. If <v, V´> is an arc, the vertex v is adjacent to the V´, and the vertex V´ adjacent to the vertex v. and called Arc <v, v´> is associated with vertex V and V´.

degrees : the degree of vertex v in the non-graph is the number of edges associated with V, recorded as: TD (v).

in: The number of arcs with Vertex v as the head of the direction graph is called the degree of V, which is recorded as: ID (v).

out : The number of arcs with vertex v in the direction graph is called the Out degree of V, which is recorded as: OD (v).

degrees : the sum of the degrees and degrees of penetration, namely: TD (v) = ID (v) + OD (v).

If the degree of Vertex VI is TD (vi), then a graph with an e-edge (ARC) of n vertices satisfies the following relationship:

path : The path from vertex v to V´ is a sequence of vertices. For a graph, the path is also forward.

path length : The number of the top or arc of the path.

loop (Loop): the same path as the first vertex and the last vertex.

Simple path : A path in a sequence where vertices do not recur.

Connectivity : There are paths from Vertex v to V´, which means that V and V´ are connected.

Connected graphs : any of the two vertices in the graph are connected.

Connected Components : The maximal connected sub-graph of undirected graphs (the sub-graph is a connected sub-graph, and a vertex in G is not connected, minus one edge is not great); The connected component of any connected graph is only one, that is itself; a non-connected graph has multiple connected components (each connected part of a non-connected graph).

strongly connected graphs : A graph of all two vertices that are connected to each other.

strongly connected Components : A strongly connected sub-graph of a forward graph (the sub-graph is a strong connected sub-graph, which is not connected by adding a vertex, and then minus one edge is not great); The strong connected component of any strongly connected graph has only one, that is, its own; a non-strong connected graph has multiple strongly connected components.

spanning Tree of connected graphs: A minimal connected sub-graph containing all vertices of undirected graph G, which is a connected sub-graph of G, in which any edge is removed and the sub-graph is no longer connected; by adding an edge, the sub-graph must have a ring. A graph can have many different spanning trees.

all spanning trees have the following common characteristics:1, the number of vertices of spanning tree is the same as the number of vertices of graphs;

2. Spanning tree is a minimal connected sub-graph of graphs;

3. The spanning tree of a connected graph with n vertices has a n-1 edge;

4, the path between any two vertices in the spanning tree is unique;

5, in the spanning tree and add an edge will inevitably form a loop.

6. A graph with n vertices n-1 edges is not necessarily a spanning tree.

Generate forest : consists of several spanning trees, containing all the vertices in the graph, but only enough to form the edges of several disjoint spanning trees.

there is a direction to the tree: if a direction graph has a vertex in the degree of 0, the remaining vertices in the degree of 1, is a tree.

The generated forest of a direction graph: consists of a number of tree-trees, containing all the vertices in the graph, but only enough to form several disjoint arcs of the tree.

Storage structure of graphs

The storage structure of the graph holds two types of information:

1) Vertex data

2) The relationship between vertices

Sequential storage: Any two vertices may be associated, and the relationship between elements cannot be represented by the physical location of the element in the store.

Multi-linked list: Similar to a tree, wasting storage units, inconvenient to operate.

Array notation (adjacency matrix notation)

A graph with n vertices that can be stored in two arrays. One of the one-dimensional arrays stores information about the data element (vertex), and the other two-dimensional array (adjacency matrix) stores information about the relationship (edge or arc) between data elements.

adjacency Matrix: Set g = (V, VR) is a graph with n vertices, in order of the vertices {v1, v2, ..., vn}, the adjacency matrix of G is an n-order phalanx with the following properties:

Like what:

Using adjacency matrix Storage

Another example

Using adjacency matrices

features :

1, the adjacency matrix of the graph is symmetric, compressible storage; n-vertex-free graphs require n (n-1)/2 storage space.

2, there are n vertices of the graph of the required storage space for n², for sparse graph space wasted serious. The occupied storage space is only related to the number of vertices, independent of the number of edges, and is suitable for dense graphs.

3. The degree TD (vi) of the vertex vi in the graph is the number of line 1 in the adjacency matrix.

4, the direction of the diagram:The degree of Vertex VI is the number of line 1 in the adjacency matrix. The entry of Vertex VI is the number of column 1 in the adjacency matrix.

The adjacency matrix of a net can be defined as:

Using the adjacency matrix representation

The code is as follows:

//array Storage representation of graphs#defineInfinty Int_max//Maximum Value#defineMax_vertex_num 20;//maximum number of verticestypedefenum{    //a map of the directionDestinationgraphic,//have to netDestinationnet,//graph without DirectionUndestinationgraphic,//non-direction networkUndestinationnet} Graphkind;typedefstructarecell{//the relationship type of the vertex, for the no-right graph, with 1 or the limit of the adjacent no, with weighted graph (net) with weights or infinity to indicate adjacent no    intadj; //pointers to arcs (edges) related information    int*info;} Arecell, Adjmartix[max_vertex_num][max_vertex_num];typedefstruct{    //vertex Vector    intVexs[max_vertex_num]; //adjacency MatrixAdjmartix arcs; //the current number of vertices and the number of arcs in the graph    intVexnum, Arcnum; //the type of the graph symbolGraphkind Kind;} Mgraph;

adjacency table (tree-like child linked table notation)

The adjacency point field that holds the position of the vertex adjacent to the VI in the table header array.

A chain field that indicates the next edge or arc.

The linked list after the header node represents the vertex of the vertex of the graph that represents the node of the header, where the number of the data field represents the subscript of the table header array, which is the vertex.

If there are n vertices and e edges in the graph, then the adjacency table needs n head nodes and 2e table nodes. Suitable for storing sparse graphs. The degree of Vertex VI in the non-graph is the number of nodes in the I single-linked list. establishing a non-contiguous adjacency table Thought: How to assign a value to a storage structure 1. Create a vertex array. Reads the vertex data Vextex and assigns the link field null. 2. Establish adjacency tables for each vertex.

Reads the vertex pair <i,j>, generates two nodes, and inserts the head of the adjacency table of the vertex j,i, respectively. Until all edges are processed.

Time complexity O (n+e) 

Graph (NET) storage structure (array storage representation i.e. adjacency matrix, adjacency table)

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: 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.