Concept and storage structure of [PHP data structure] diagram


With the deepening of learning, our knowledge is constantly expanding and enriching. Does the tree structure make everyone circle? Trust me, after learning the graph, you will find that the binary tree is simply too simple to say. In fact, what we call a tree is also a special form of a graph.

Concept of graph

Remember the picture about trees we saw when we learned the first article about trees?

At that time, we said that graph C is not a tree, but a graph. Why? From the definition of a tree, we can see that a tree can only have one root node, there can be no connection between peers, and there can be multiple child nodes. The characteristic of graph is that nodes can be connected with each other. For example, the following pictures are all pictures.

In the figure drawn above, figure B has an arrow, while the connecting line of figure a has no arrow. A graph with a clear direction is called a directed graph. A graph without an arrow, that is, a graph without direction, is called an undirected graph.

Let’s first turn our attention to figure A-1. In fact, it just rotates figure a. Can you see? If the connection between nodes 4 and 1 is ignored, it is a tree. Is it consistent with the concept of graph C in our tree graph above.

The more formal official definition of a graph is:

Graph G consists of two sets V and E, denoted as g = (V, e), where V is the finite nonempty set of vertices, e is the finite set of vertices in V, and these vertices are even symmetric as edges.

In a directed graph, the line segment connecting two points from the starting node to the pointing node can be recorded as 。andAre two different edges, also known as arcs. According to figure a, we can see that there are < 1,2 >, < 2,1 >, < 1,3 > ❤️, 1> 、 <1, 4> 、 <4, 1> 、 ❤️, 4> And < 4,3 >. In graph B, because it is a directed graph, its edges only have < 1,2 >, < 1,3 > ❤️, 4> And < 4,1 >.

Does it feel clear when you look at the picture above? When you look at this definition, you look confused? Like this definition, if you are a classmate who needs an exam, you still have to memorize it. If you just want to learn, apply or understand together, you don’t have to memorize. V is the node, e is the relationship between these nodes, and the relationship between two vertices, that is, the segments connecting nodes on the graph are edges.

OK, after understanding the three most basic concepts, we will continue to learn other cart terms related to the diagram!

Related terms of figure

First, we use n to represent the number of vertices in the graph and e to represent the number of edges. Remember these two codes.

  • (1) Subgraph: suppose there are two graphs g = (V, e) and G=(V, E)If VIncluded in V and eIncluded in E, it is called GSubgraph of G

The subgraphs on the right in the figure above are all subgraphs belonging to the original graph. It can be seen that subgraphs can produce many forms, and directed graphs are the same concept. However, compared with undirected graphs, directed graphs can generate fewer subgraphs because their edges have directions.

  • (2) Undirected complete graph and directed complete graph: for an undirected graph, if it has n (n-1) / 2 edges, it is an undirected complete graph. For a directed graph, if it has n (n-1) solitary bars, it is called a directed complete graph. (refer to complete binary tree)

In fact, the concept of complete graph is that all adjacent nodes in the graph have edges that can be connected together.

For a directed graph, although the edges have directions, of course, we can also define a back and forth direction, such as < 1,2 > and < 2,1 >. In a directed graph, we need to draw two arrows in opposite directions, which can be from 1 to 2 or from 2 to 1. In an undirected graph, one edge is used to replace these two edges. The edge without arrow direction is bidirectional.

  • (3) Sparse and dense graphs: having few edges or solitary (e.g. E2n) is called sparse graph, and vice versa is called dense graph

  • (4) Weight and net: in practical application, each edge or arc can be marked with a value with a certain meaning, just like the distance on the map. These values are called weight. A weighted graph can be called a net

In the top picture, the numbers on the edges of figure A-2 and figure B-1 represent the weight. These two graphs can be called net graphs. We will learn the concept of weight later when talking about relevant algorithms. From these two diagrams, we can clearly see that if you want to go from node 1 to node 4, you don’t go directly to the edge < 1,4 > but to go to < 1,3 > ❤️, 4> This route is faster.

  • (5) Adjacency point: two nodes with edges are adjacency points

  • (6) Degree, in degree and out degree: the degree of vertex v refers to the number of edges associated with V. For a directed graph, the arrow pointing to other nodes is the out degree, and the arrow pointing to itself is the in degree

Let’s continue with figure B. Node 1 has two out degrees and one in degree. There seems to be no need to explain this too much.

  • (7) Path and path length: all vertices passing from one vertex to another are paths. If it is a directed graph, its path is in the direction of the arrow. Path length is the number of edges or orphans that a path passes through

  • (8) Loop or ring: the same path between the first vertex and the last vertex is called a loop or ring

  • (9) Connectivity, connected graph and connected component: if there is a path between two nodes, they are said to be connected. If all nodes in the whole graph can be connected to each other, the graph is a connected graph. The connected component is a polar connected subgraph in an undirected graph and a non connected graph.

The following three concepts are also given in this figure. In an undirected graph, the connected component is equal to the polar connected subgraph. In this graph, we have two connected components.

  • (10) Polar connected subgraph: the maximum number of nodes that a connected subgraph can contain. If another node is added, the subgraph is not a connected graph

  • (11) Spanning tree: a minimal connected subgraph, which contains all the vertices in the graph, but only enough n-1 edges to form a tree. Such a connected subgraph is the spanning tree of a connected graph

In fact, all nodes in the graph can be connected in series through one path. In the graph of connected components, we generate two minimum spanning trees according to two connected components. The nodes of the spanning tree of their connected component 1 do not necessarily have this structure. We can make node 4 under node 2, which depends on how we traverse to generate this minimum spanning tree.

At the top, the minimum spanning tree of our figure a can actually be figure A-1. Remove the red dotted line. Of course, node 4 can also be under node 1, which also depends on how our program traverses the graph to generate what kind of tree.

  • (12) Spanning forest: in a non connected graph, each connected component can generate a connected spanning tree, which constitutes the spanning forest of the whole non connected graph

Did you feel dizzy after reading it? It doesn’t matter. We will often use these terms in our later study, and this is not the most comprehensive. You can learn and understand the related terms of the map more deeply according to the bibliography and other learning materials.


The concept of figure is almost introduced. You can digest it and continue to learn the following content. This is just the beginning. Many students will not think that this thing has improved a lot compared with the tree structure at once. Don’t be afraid. After learning the following knowledge, even if you don’t understand the contents related to the diagram for the time being, you must have a deeper understanding of the tree structure. Why? Trees are actually graphs without loops. Their traversal is almost realized through depth or breadth, but the graph is a little more complex. Does it feel like the future is still a little hopeful? Learning is often a gradual process. Current knowledge and past knowledge will always be related. Don’t think too much first. Go down step by step!

reference material:

Data structure, Second Edition, Yan Weimin

Data structure, Second Edition, Chen Yue

High score notes on data structure, 2020 edition, tianqin postgraduate entrance examination

Official account: hard core project manager

Add wechat / QQ friends: [xiaoyuezigonggong / 149844827] get free PHP and project management learning materials

Tiktok, official account, voice, headline search, hard core project manager.

Station B ID: 482780532