There are three kinds of relationships between data, namely "one-to-one", "one-to-many" and "many-to-many". The data of the first two relations can be stored in a linear table and a tree structure respectively , with "many-to-many" "The structure of logical relational data-diagram.
1. Definition
Definition: The graph is composed of some points (vertex) and the lines between these points (edge); where, points are usually called "vertex (vertex)", and the connection between points It is called an "edge or arc" (edege). It is usually written as G = (V, E).
2 Classification of figures
Basis for classification: whether there are directions.
Two categories: undirected graph, directed graph.
-
Undirected graph
Figure 1 -
Directed graph
Figure II
Simply put, the difference between a directed graph and an undirected graph is whether there is only directivity from vertex to another vertex. For example, the directed graph above is a flight graph, that is, there are flights from V3 to V4, but from V4 to V3 can only be V4-> V1-> V3.
Unlike linked lists, each data element stored in the graph is called a vertex (not a node)
In the graph storage structure, it is customary to use Vi to represent the vertices in the graph, and the set of all vertices is usually represented by V. As shown in Figure 1, the set of vertices is V = {V1, V2, V3, V4}.
3 Basic properties of the figure
- Arc head and arc tail
In a directed graph, the vertex without the arrow is usually called the "initial point" or "arc tail", and the vertex of the arrow straight line is called the "end point" or "arc head".
- In and out
For a vertex V in a directed graph, the number of arcs with arrows pointing to V is V in degrees (InDegree, denoted as ID (V)); the number of arcs with arrows away from V is V out degrees (OutDegree, Record as OD (V)). Take the vertex V1 in Figure 2 as an example. The vertex has an in degree of 1 and an out degree of 2 (the degree of the vertex is 3).
- The difference between (V1, V2) and <V1, V2>
The undirected graph describes the relationship between two vertices (V1 and V2) can be expressed by (V1, V2), while the directed graph describes the "unidirectional" relationship from V1 to V2 by <V1, V2> .
Since the relationship between vertices in the graph storage structure is represented by lines, (V1, V2) can also be used to represent the lines connecting V1 and V2 in undirected graphs, also known as edges; similarly, <V1, V2 > It can also be used to indicate the line from V1 to V2 in the directed graph, also known as arc.
- The meaning of collection VR
In addition, VR is used to represent the set of relationships between all vertices in the graph. For example, the set of undirected graphs in Figure 1 VR = {(v1, v2), (v1, v4), (v1, v3), (v3, v4)}, and the set of directed graphs in Figure 2 VR = {< v1, v2>, <v1, v3>, <v3, v4>, <v4, v1>}.
- Paths and loops
Whether it is an undirected graph or a directed graph, a sequence of all vertices (including these two vertices) from one vertex to another is called a path. If the first vertex and the last vertex in the path are the same, the path is called a "loop" (or "ring").
And, if the vertices in the path are not repeated, this path is also called "simple path"; similarly, if the vertices in the loop do not overlap each other, this loop is called "simple loop" (or simple loop).
Taking Figure 1, for example, there is a path from V1 that can return to V1. This path is {V1, V3, V4, V1}, which is a loop (loop), and it is also a simple loop (simple loop).
In a directed graph, each path or loop is directional.
- The meaning of weights and nets
In some practical scenarios, each edge (or arc) in the graph will be given a real number to express a certain meaning. This real number matching the edge (or arc) is called "weight" , And weighted graphs are often called nets. As shown in Figure 3, it is a network structure:
Figure 3. Weighted graph storage structure
In the article on learning "Huffman Tree", we have already understood the concept of "weight". The power of this picture is similar.
Subgraph: refers to a graph composed of a part of vertices and edges in the graph, and is called a subgraph of the original graph.
4. The structure of the graph
According to different characteristics, we divide the graph into complete graph, connected graph, sparse graph and dense graph.
- Complete graph In
other words, every node is connected to all nodes except itself.
There are two types of complete graphs, undirected and directed.
For a complete graph with n vertices, the number of edges in the graph is n (n-1) / 2; for a directed complete graph with n vertices, the number of arcs in the graph is n (n-1). - Connected graph In
simple terms, any connected vertex in the graph can find each other through a certain path.
Connected graph:
unconnected graph:
- Sparse graph and dense graph The
sparse and dense judgment condition is:
e <nlogn, where e represents the number of edges (or arcs) in the graph, and n represents the number of vertices in the graph. If the formula holds, it is a sparse graph; otherwise it is a dense graph.
5 storage types
5.1 Sequential storage structure
Simply put, it uses an array to store the graph. Use a one-dimensional array to store nodes, and use another two-dimensional array to store the relationship between vertices.
5.2 Adjacent matrix storage structure
It is said that the matrix is used to represent the logical relationship of the graph. As shown below
- Undirected graph G1
- G1's adjoining matrix
Mapping relationship: There is no arc or edge between a vertex and another vertex, it is recorded as 0, otherwise it is recorded as 1.
Explanation: - If it is a directed graph, then if A–> B, then AB is 1, and BA is 0.
- As mentioned above, a one-dimensional array is used to store vertex information, and a two-dimensional array is used to store edge information. From the figure, we can see that there are a large number of 0 elements, which leads to the disadvantage of the adjacency matrix and consumes space .
5.3 Storage structure of adjacency list
In order to solve the shortcomings of space consumption of the adjacency matrix . We introduced the adjacency list. The so-called adjacency list is simply to record the subscripts of the nodes that are connected to the current node. The details are as follows:
Take section 5.2, undirected graph G1 as an example. Its adjacency list is:
5.4 Cross-linked list storage structure
to be continued……
Disclaimer: The content of this article is collated from online content, and adds a little personal understanding.
Reference content: