Don't read the big talk data structure book clearly.

The graph structure is complex, and there may be a connection between any two vertices. Therefore, the relationship between elements cannot be represented by the physical position of data elements in memory, that is, the graph cannot be represented by a simple sequential storage structure. Although the multi linked list can be realized, there are also corresponding problems. Please read the specific book.

There are five storage structures

- Adjacency matrix: less edges will waste space (one-dimensional array + two-dimensional array (staggered array))
- Adjacency list: undirected graph of vertices (one-dimensional array + a linked list) is suitable for attention
- Cross linked list: suitable for directed graph (one-dimensional array + two linked lists))
- Adjacency multiple tables: undirected graphs with edges (one-dimensional array + a linked list) are suitable for attention
- Edge set array: an operation suitable for processing edges in turn (two one-dimensional arrays)

Adjacency matrix: less edges will waste space

The adjacency matrix of a graph is stored in two arrays. A one-dimensional array stores the vertex information in the graph, and a two-dimensional array (called adjacency matrix) stores the edge or arc information in the graph.

/// <summary> ///Figure /// </summary> public class Graph { public int[] vertexs; //vertex array public int[,] arcs; //Edge array public int numVertex; //Number of vertices public int numEdge; //Number of sides public int maxNumVertex;//max vertex }

/// <summary> ///Adjacency matrix /// </summary> public class AdjacentMatrix { /// <summary> ///Create adjacency matrix representation of undirected network graph /// </summary> /// <param name="graph"></param> public Graph CreateGraph(int numVertex, int numEdge, int maxNumVertex, int[] vertexs, int[,] arcs) { //Simply return an object and be responsible for the data and format Graph graph = new Graph() { vertexs = vertexs, arcs = arcs, numVertex = numVertex, numEdge = numEdge, maxNumVertex = maxNumVertex, }; return graph; } }

Adjacency table: suitable for undirected graphs (undirected graphs focusing on vertices)

For graphs with fewer edges than vertices, the adjacency matrix has a great waste of storage space.

Therefore, we consider using chain storage for edges or arcs to avoid space waste.

The storage method of the combination of array and linked list is called adjacency list

/// <summary> ///Adjacency table /// </summary> public class AdjacencyList { /// <summary> ///Edge table node /// </summary> public class EdgeNode { public int adjvex; //The adjacent point field stores the subscript corresponding to the vertex public int weight; //It is used to store weights. It is not required for non network graphs public EdgeNode next; //Chain domain, pointing to the next adjacent node } /// <summary> ///Vertex table node /// </summary> public class VertexNode { public int data; //Vertex domain, storing vertex information public EdgeNode firstEdge; //Edge header pointer } public int numVertexes; //Number of vertices public int numEdges; //Number of sides public VertexNode[] adjacencyArr; //Vertex table /// <summary> ///Note: the required data, including vertex data and edge table data, are not provided according to the book ///Here is just a general idea /// </summary> /// <param name="numVertexes"></param> /// <param name="numEdges"></param> /// <param name="datas"></param> /// <param name="edgeData"></param> public AdjacencyList(int numVertexes, int numEdges, int[] datas, EdgeNode[] edgeData) { this.numEdges = numEdges; this.numVertexes = numVertexes; adjacencyArr = new VertexNode[numVertexes]; for (int i = 0; i < this.numVertexes; i++) //Create vertex table { adjacencyArr[i].data = datas[i]; //vertex data adjacencyArr[i].firstEdge = edgeData[i]; //Create edge table } } }

Cross linked list: suitable for directed graph

For a directed graph, the adjacency table is defective. It is concerned about the out degree problem. If you want to know the in degree, you must traverse the whole graph to know it. On the contrary, the inverse adjacency table solves the problem of in degree but not out degree.

Cross linked list: combine adjacency list and inverse adjacency list

/// <summary> ///Cross linked list /// </summary> public class OrthogonalList { /// <summary> ///Edge table node /// </summary> public class EdgeNode { public int tailIndex; //The arc start point is in the subscript of the vertex table public int headIndex; //The arc end point is in the subscript of the vertex table public int weight; //It is used to store weights. It is not required for non network graphs public EdgeNode headLink; //Enter the pointer field of the edge table and point to the next edge with the same end point public EdgeNode tailLink; //The pointer field of the edge table points to the next edge with the same starting point } /// <summary> ///Vertex table node /// </summary> public class VertexNode { public int data; //Vertex domain, storing vertex information public EdgeNode firstIn; //Edge entry header pointer public EdgeNode firstOut; //Out side header pointer } public int numVertexes; //Number of vertices public int numEdges; //Number of sides public VertexNode[] orthList; //Vertex table /// <summary> ///Note: the required data, including vertex data and edge table data, are not provided according to the book ///Here is just a general idea /// </summary> /// <param name="numVertexes"></param> /// <param name="numEdges"></param> /// <param name="datas"></param> /// <param name="tailEdgeData"></param> public OrthogonalList(int numVertexes, int numEdges, int[] datas, EdgeNode[] tailEdgeData,EdgeNode[] headEdgeData) { this.numEdges = numEdges; this.numVertexes = numVertexes; orthList = new VertexNode[numVertexes]; for (int i = 0; i < this.numVertexes; i++) //Create vertex table { orthList[i].data = datas[i]; //vertex data orthList[i].firstOut = tailEdgeData[i]; //Create edge table orthList[i].firstIn = headEdgeData[i]; //Create edge entry table } } }

Adjacency multiple table: optimization of adjacency table of undirected graph (paying more attention to edge operation)

/// <summary> ///Adjacency multiple table /// </summary> public class AdjacencyMultiList { /// <summary> ///Edge table node /// </summary> public class EdgeNode { public int iVex; //The subscript of the vertex in the vertex table public EdgeNode iLink; //Points to the next edge of the attached vertex iVex public int jVex; //The subscript of the vertex in the vertex table public EdgeNode jLink; //Points to the next edge attached to the vertex jVex public int weight; //It is used to store weights. It is not required for non network graphs } /// <summary> ///Vertex table node /// </summary> public class VertexNode { public int data; //Vertex domain, storing vertex information public EdgeNode firstEdge; //Edge header pointer } public int numVertexes; //Number of vertices public int numEdges; //Number of sides public VertexNode[] adjacencyMultiList; //Vertex table /// <summary> ///Note: the required data, including vertex data and edge table data, are not provided according to the book ///Here is just a general idea /// </summary> /// <param name="numVertexes"></param> /// <param name="numEdges"></param> /// <param name="datas"></param> /// <param name="edgeData"></param> public AdjacencyMultiList(int numVertexes, int numEdges, int[] datas, EdgeNode[] edgeData) { this.numEdges = numEdges; this.numVertexes = numVertexes; adjacencyMultiList = new VertexNode[numVertexes]; for (int i = 0; i < this.numVertexes; i++) //Create vertex table { adjacencyMultiList[i].data = datas[i]; //vertex data adjacencyMultiList[i].firstEdge = edgeData[i]; //Create edge table } } }

Edge set array: it focuses on the set of edges. It is suitable for the operation of edges in turn, but not for the operation of vertices

An edge set array consists of two one-dimensional arrays. One is to store vertex information; The other is to store edge information. Each data element of this edge array is composed of a start subscript, an end subscript and a weight of an edge.

When learning Kruskal algorithm