Abstract data type definition for graphs

`图是一种数据结构，加上一组基本操作就构成了图的抽象数据类型。图的抽象数据类型定义如下：ADT Graph{数据对象V：具有相同特性的数据元素的集合，称为顶点集。数据关系R：R={VR}VR={<v,w>|<v,w>| v,w?V∧p(v,w) ，<v,w>表示 从v到w的弧，P(v,w)定义了弧<v,w>的信息 }`

`基本操作PCreate_Graph() ： 图的创建操作。初始条件：无。 操作结果：生成一个没有顶点的空图G。GetVex(G, v)DFStraver(G,V)：从v出发对图G深度优先遍历。 初始条件：图G存在。 操作结果：对图G深度优先遍历，每个顶点访问且只访问一次。 ? ?BFStraver(G,V)：从v出发对图G广度优先遍历。 初始条件：图G存在。 `

Storage structure of graphs

` 图的存储结构比较复杂，其复杂性主要表现在：`

There may be a connection between any vertex, and it is not possible to represent the relationship between elements in the physical location of the data element in the store, so it cannot be represented by a simple sequential storage structure.

The degree of the vertices in the graph is different, some may vary greatly, if the maximum degree of the vertex design structure, it will waste a lot of storage units, and vice versa, each vertex of its own degree of design of the structure, will affect the operation.

The common storage structures of graphs are: adjacency matrix, adjacency linked list, cross linked list, adjacency multiple table and edge table.

Adjacency matrix (Array) notation

` 图的邻接矩阵基本思想：用两个数组来表示图：一个一维数组存储顶点信息，一个二维数组存储边或弧的信息。该二维数组称为邻接矩阵。`

Characteristics of adjacency matrix of unauthorized graphs

Properties of adjacency matrix of non-graph graphs

The adjacency matrix is a symmetric phalanx;

For Vertex VI, its degree is the number of non-0 elements of line I;

The number of edges of the graph is the number of non-0 elements in the upper (or lower) triangular matrix.

Non-graph adjacency matrix non-0-element number = number of non-direction graph edges

The properties of a graph adjacency matrix

For vertex VI, the number of non-0 elements of line I is its out OD (vi), and the number of non-0 elements in column I is its entry id (vi).

The number of non-0 elements in the adjacency matrix is the sum of the arcs of the graph.

3 creation of adjacency matrices for graphs

The adjacency matrix of graphs is easy to implement, defining two arrays to store the information of vertex information (data Element) and edge or arc (the relationship between data elements). The storage structure form is defined as follows:

` #define MAXVEX 100/* Maximum number of vertices, which should be user-defined */ #define INFINITY 65535 typedef char Vertextype; /* vertex type should be user defined * /typedef int edgetype; /* edge of "hljs-bullet" should be defined by user * /typedef struct{vertextype Vexs[maxvex]; /* vertex table * /Edgetype arc[maxvex ][maxvex ]; /* adjacency matrix, can be regarded as side table * /int numvertexes, numedges; /* the current number of vertices and the number of edges in the graph * /}mgraph; `

`(1) 图的创建/* 建立无向网图的邻接矩阵表示 */void CreateMGraph(MGraph *G){ 输入顶点数numVertexes和边数numEdges ； for0;i <G-> numVertexes;i++) 读入顶点信息,建立顶点表 for0;i <G-> numVertexes;i++) for0;j <G-> numVertexes;j++) G->arc[i][j]=INFINITY;/* 邻接矩阵初始化 */ for0;k <G->numEdges;k++) 读入numEdges条边，建立邻接矩阵 }`

` 其他操作`

(2) vertex positioning of graphs

Determining the position (subscript) of a vertex in a vexs array is exactly the same as finding a data element in a linear table stored sequentially.

The vertex information is read according to the subscript.

(3) Adding vertices to the graph

A similar data element is added to the end of a linear table stored sequentially.

(4) Add an arc to the diagram

Modifies the corresponding array element in the adjacency matrix based on the vertex to which the given arc or edge is attached.

Adjacency Linked List method

Basic idea: Arrays are combined with linked lists.

Specific measures:

Vertices are stored in a one-dimensional array, and each data element also needs to store pointers to the first adjacency point.

All adjacency points of each vertex vi form a single-linked list, and the non-graph is called the Edge (chain) Table of Vertex VI, and the forward graph is called the Vertex VI as the edge table of the arc tail (or as an edge table for the arc head).

Related Operations for adjacency tables

To find the degree of a node, simply look for the number of nodes in the edge table of the vertex (or simply add an additional item to the node's structure to store the number of its adjacency points);

Judge whether the Vertex VI to VJ exists edge, check the edge table of vertex VI if there is a node VJ subscript J can be;

All adjacent points of the vertex are required to traverse the edge table of the vertex.

Note: In the adjacency table of the graph, the number of edge table nodes = 2 * Number of edges

2 Characteristics of adjacency table method

In the condition of sparse edge or arc, the storage space is saved by adjacency matrix representation.

In the graph without direction, the degree of Vertex VI is the node number of the first list;

A positive adjacency table or inverse adjacency table can be established for a forward graph. A positive adjacency table is an adjacency table established with Vertex VI as the exit (i.e. the starting point of the arc), and the inverse adjacency table is an adjacency table based on the vertex VI (i.e. the end point of the ARC);

In the graph, the node number in the list I list is the out (or into) degree of Vertex VI, and the degree of (or out) is required to traverse the entire adjacency table;

`3 node and its type definition # define Max_vex 30 /* maximum number of vertices */ typed EF char vertextype; /* vertex type should be user-defined */ typedef int Edgetype; /* edge of "hljs-comment" should be defined by user */ typedef struct Edgenode /* Side Table node */ {int Adjvex; /* adjacency point field, storing the subscript corresponding to the vertex */ Edgetype info; /* for storing weights, no need for non-grid */ struct Edgenode *next; /* chain field, point to next adjacency point */} Edgenode; `

`typedefstruct/* 顶点表结点 */{ VertexType data; /* 顶点域,存储顶点信息 */ /* int indegree ; //顶点的度, 有向图是入度或出度 (亦可不要) */ EdgeNode *firstarc; /* 边表头指针 */}VertexNode, AdjList[MAX_VEX];typedefstruct{ AdjList adjList; //顶点数组（亦成为头结点向量） int/* 图中当前顶点数和边数 */}GraphAdjList;`

Using the storage structure described above, it is easy to implement the basic operation of the diagram.

`(1)/* Create adjacency table structure for diagram */void Createalgraph(graphadjlist*G) {Enter the number of top pointsG->numnodes and number of sidesG->numedges for (i =0; I <G->numnodes;i++) Read-in vertex informationG->adjlist[i]. data, G->adjlist[i].firstedge)/* Set the edge table to empty table */for (k =0; k <G->numedges;k++)/* Create a vertex number on the edge table */{input edge (VI,VJ); Request space to memory, generate edge table nodes, insert newly generated nodes into a linked list with vertex VI as the head node; (Is it convenient to insert by head insertion method or tail insertion method?) Note here: If there is no direction graph, one edge corresponds to two vertices, so we should also revise the list with VI as Head node and VJ as head node. }}`

Other operations:

(2) vertex positioning of graphs

The vertex positioning of a graph is actually the data field content of an element that determines a vertex in the adjlist array.

(3) Adding vertices to the graph

Adds a vertex to the graph and adds a data element at the end of the adjlist array.

(4) Add an arc to the diagram

Modifies a single-linked list based on the vertex attached to a given arc or edge: An unmodified graph modifies two single-linked lists; a graph modifies a single-linked list.

Cross-linked list method

The cross-linked list (orthogonal list) is another chained storage structure of the forward graph, which is a kind of linked list which combines the positive adjacency table and the inverse adjacency table of the forward graph.

In this structure, the Arc Head node and the Arc tail node of each arc are stored in the linked list, and are organized into a linked list with the arc-tail node as the head (vertex) node and the head node as the vertex node.

`◆ data域：存储和顶点相关的信息；Info域：指向该弧的相关信息，如网的权值`

`Node type definition#define INFINITY max_val/* Maximum value ∞*/#define Max_vex //maximum number of vertices typedef structarcnode{intTailvex, Headvex;//Tail node and head node subscript in the vertex table;InfoType info;//ARC-related information, such as weightsstructArcnode *hlink, *tlink; }arcnode;/ * ARC node type definition * /typedef structvexnode{Vextype data;//Vertex informationArcnode *firstin, *firstout;} Vexnode;/ * Vertex node type definition * /typedef struct{intVexnum; Vexnode Xlist[max_vex];} Olgraph;/ * Type definition for graph * /`

Adjacency multiple table (adjacency multilist)

` 邻接多重表(Adjacency Multilist)是无向图的另一种链式存储结构。 在无向图的邻接表中，一条边(v,w)的两个表结点分别被选在以v和w为头结点的链表中。如果关注的重点是顶点，则邻接表是不错的选择，但在涉及到边的操作会带来不便。 邻接多重表的结构和十字链表类似，每条边用一个结点表示；邻接多重表中的顶点结点与邻接表中的完全相同`

Data domain: Storage and vertex-related information;

Firstedge: The table node corresponding to the first edge attached to the vertex;

Mark: To identify whether the edge has been accessed;

Ivex and Jvex: Save the subscript of the two vertices attached to the edge in the vertex table respectively;

Info field: Holds information about the edge;

Ilink: Point to the next edge attached to the vertex Ivex;

Jlink: Point to the next edge attached to the vertex Jvex;

```
node type definition # Define INFINITY 65535/* Maximum value ∞*/ #define
Max_vex 30/* Maximum number of vertices */ typedef EMNU {unvisited, visited} visitting; typedef struct edgenode{visitting mark; //access tag int Ivex, Jvex; //the position of the two nodes attached to the edge in the figure InfoType info; //edge-related information, such as weights struct Edgenode *ilink, *jlink; //points to the next edge attached to both vertices } Edgenode; /* arc Edge node type definition */
```

`typedefstruct VexNode{ VexType data; // 顶点信息ArcNode *firsedge ; //指向依附于该顶点的第一条边}VexNode ; /* 顶点结点类型定义 */typedefstruct{ int vexnum ;VexNode mullist[MAX_VEX] ;}AMGraph ;`

Edge table storage Structure of graphs

In some applications, the weights of the edges in the graph and the two vertices that are attached are sometimes mainly examined, that is, the structure of the graph is mainly represented by edges, called the edge table storage structure.

The edge table structure is stored sequentially and consists of 2 one-dimensional arrays, one storing vertex information and one storage edge information. Each element of an edge array consists of three parts:

The beginning subscript of the Edge

The end of the edge subscript

Weighted value of the edge

`The form of the side table storage structure is described as follows:#Define INFINITY max_val/* Max ∞*/#Define Max_vex 30 */MAX Vertex number * /#define Max_edge 100/* Maximum number of sides * /typedefstructenode{intBegin, end;/ * Two vertices attached to a side * /Weighttype weight;/ * Weight of the edge * /}enode;/ * Edge TABLE element type definition * /typedefstruct{intVexnum, Edgenum;/* Vertex count and number of edges */Vextype Vexs[max_vex];/ * Vertex table * /Enode Edges[max_edge];/ * Side table * /}elgraph;`

Data structure-the storage structure of graphs