Text: Step by Step write algorithm (graph structure)
"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "
The graph is an important chapter in the data structure. Through the graph, we can determine whether there is connectivity between the two points, through the graph, we can also calculate the minimum distance between the two points, through the graph, we can also be based on different requirements, to find a different appropriate path. Of course, sometimes in order to calculate the need, we also need to abstract the minimum spanning tree, so that in the traversal of the calculation does not need to continue to determine whether it encountered a loop node. Of course, all of this begins with the representation of the graph.
1) matrix representation
The matrix representation can be said to be the simplest representation, and if there are 5 points in a graph, then we can construct a 5*5 matrix. If there is a connection between the point and the point, then fill 1, otherwise if there is no connection, then you can use 0 to indicate, of course, the dot above the diagonal is meaningless. As shown in the following:
static int Graph[5][5] = {{0, 1, 0, 1, 1},{1, 0, 1, 0, 1},{0, 1, 0, 1, 0},{1, 0, 1, 0, 1},{1, 1, 0, 1, 0}};
If there is a direction between the point and the point, then they are asymmetric about the (x,x) axis of symmetry, so the result may also be:
static int Graph[5][5] = {{0, 0, 0, 0, 0},{1, 0, 0, 0, 0},{0, 1, 0, 0, 0},{1, 0, 1, 0, 0},{1, 1, 0, 1, 0}};
Of course, if the relationship between points and points has some weight, such as distance, then we can use it instead of the original data 1:
static int Graph[5][5] = {{0, 0, 0, 0, 0},{3, 0, 0, 0, 0},{0, 6, 0, 0, 0},{8, 0, 4, 0, 0},{9, 2, 0, 7, 0}};
The graph structure under the matrix representation is very intuitive. However, the matrix has a characteristic, is to compare waste space. Because we have an example of a few vertices here, only 5, but please imagine, if a picture has 10,000 nodes, then 10000*10000 is how much a space ah. The important thing is that most of the points on this 10000*10000 are 0, so the wasted space is quite impressive.
2) array structure
In order to change the characteristic of wasted space of matrix, we can establish a data space composed of vertices and edges. For example, we define a structure like this:
typedef struct _LINE{INT start;int end;int weight;int isdirection;} Line;
The data structure defined above is very concise. The 1th is the starting vertex, the 2nd is the end point, the 3rd is the weight, and the 4th determines whether the current edge has a direction. If there are many sides in the graph, we need to define how many such data. If we put the data of these edges together to form an array, then we can use this array to represent all the information of the graph.
However, we still feel that there is a place of regret. This data structure excessively emphasizes the meaning and importance of the edge, ignoring the meaning of the vertex itself. Because, when we emphasize the edges, we should add the relevant properties of the vertices. Without the support of vertices, the mere edge information is meaningless.
3) Graph representation based on vertex linked list
First, we define the basic structure of the vertices:
typedef struct _LINE{INT end;int weight;struct _line* Next;} line;typedef struct _vectex{int start;int number; line* neighbor;} Vectex;
We use Vectex to record information about vertices, and line represents information about the nodes. If line is a variable in Vectex, then neighbor indicates that the starting point of all current nodes is the start point. If it is a variable in path, then the starting point of next is the front point of the line link, do not know I have made it clear? Here is the definition of path between points and points.
typedef struct _PATH{INT start;int end;int lenth; Line* Next;} PATH;
Where start is the starting point, end is the endpoint, next is the next point of the start link, the lenth is the total length of the path, and of course it can be modified to other weight forms.
Precautions :
1) Arrays and lists are the basis of the graph structure, and friends should have a good grasp of
2) Each data structure has its own application, the key is to understand the ideas and methods
3) The representation of graphs is the basis of graph operation, and mastering them is the basic condition of our further study.
Step-by-Step write algorithm (graph structure)