【Data Structure Learning Record 18】——The concept of graph

Zero. Foreword

In the previous section, I learned about linked lists and trees.
In the linked list, the elements have only linear relationships, and each element generally has only one predecessor and successor.
In the tree, there is an obvious hierarchical relationship between elements, and the elements on each level may be related to multiple elements in the next level, but can only be related to a certain element in the previous level.
In the graph, the relationship between our nodes can be arbitrary, just like a tree, the node has only one parent, and the graph can have multiple parents. . . It sounds weird. .

1. Terminology

1. Description

顶点(Vertex): Data element
,: If it Vrepresents a finite and non-empty set of VRvertices (trees and linked lists can be empty); it represents a set of relations between two vertices. If <v,w>∈VR, then <v,w> represents a line starting from v to w .
弧尾: Such as v in the arc definition.
弧头: Such as w in the arc definition. (Somewhat strange, the tail is at the front and the head is at the back, just understand it as an arrow)
有向图(Digraph): A graph composed of vertices and arcs.
: If the ordered pair defined by the arc is <v,w>changed to an unordered pair (v,w), then it is the one starting from v to w at this time .
无向图(Undigragph): A graph composed of vertices and edges.
Insert picture description here

2. Complete graph

Without considering the arc or edge of the vertex back to itself, if there is:
1 2 n (n − 1) \frac{1}{2}n(n-1)21n(n1 )
An undirected graph with edges, called完全图.
And there are:
n (n − 1) n(n-1)n(n1 )
The graph of the edges, called有向完全图
As for how to derive the formula for calculating the edges, that is a junior high school math problem, not much to say.
Insert picture description here

3. Sparse and dense graphs

Generally speaking, there are few edges or arcs:
<nlogn <nlogn<n l o g n
we call it and稀疏图
vice versa稠密图

4. Subgraph

Suppose there are two graphs G=(V,{E}) and G'=(V',{E'}), if:
V ′ ⊆ V, E ′ ⊆ E V'\subseteq V, E'\subseteq EVV ,EE
can say that G'is G's子图
Insert picture description here

5. Adjacency and Degree

If the undirected graph G={V,{E}}, if the edge (v,v') ∈ E, then the vertices v and v'are called mutually 邻接点. Edge (v, v') 依附vertex v and v', or (v, v') and vertex v, v' 相关联.
The vertex v is the number of edges associated with v, denoted as TD(V), for example, the degree of V 3 of this graph is 3 in
Insert picture description here
a directed graph G=(V,{A}), if arc<v,v' >∈A, then vertex v is adjacent to vertex v', and vertex v'is adjacent to vertex v. The number of arcs with vertex v as the head is called v 入度(InDegree)and is denoted as ID(v); in the same way, the number of arcs with vertex v as the tail is called v 出度(OutDegree)and is denoted as OD(v). Of vertices TD (v) = ID (v ) + OD (v)
For example, the FIG V . 1 the TD of = 3, ID = 2, OD = 1
Insert picture description here
In general, a and n vertices, edges or arcs E In the figure, there is the following rule:
e = 1 2 ∑ i = 1 n TD (V i) e = \frac{1}{2} \sum _ {i=1} ^nTD(V_i)e=21i=1nT D ( Vi)

6. Paths and loops

Undirected graph G = (V, {E} ) from the vertex v to vertex v 'is 路径a sequence of vertices (v = v I, 0 , v I,. 1 , ..., v I, m ), where (v I ,j-1 , v i,j ) ∈ E, 1≤j≤m. The same is true for directed graphs. Having said that, it looks complicated, and drawing a graph is intuitive:
Insert picture description here
the path where the first vertex and the last vertex are the same is called an 回路OR .
The path where the sequence vertices do not recur is called 简单路径. Except for the first vertex and the last vertex, the loops where other vertices do not appear repeatedly are called 简单回路or 简单环.

7. Connected graph and connected components

In an undirected graph, there is a path from vertex v to vertex v', then v and v'are 连通true. If for any two vertices in the graph, they are connected, then this graph is 连通图.
连通分量, Also called 极大连通子图: For the connected subgraph G'in the graph G, if there is no other connected subgraph G'', so that G'⫋G'', then G'is the connected component of G. So G should be the collection of all connected components.
Insert picture description here
强连通图, If for each pair of v i , v j ∈ V, v i ≠ v j , then the graph G is a strongly connected graph. With reference to the definition of connected components, directed graphs also exist 强连通分量.

8. Spanning tree

A connected graph 生成树is a minimal connected subgraph, which contains all the vertices in the (sub)graph, and the number of points is n, then it has and only has n-1 edges. If an edge is added to this minimal connected subgraph, then a ring must be formed.
Therefore, a spanning tree with n vertices has one and only n-1 edges. If edges>n-1, then there must be a ring; if <n-1 edges, it is a non-connected graph. But a graph with n-1 edges is not necessarily a spanning tree
Insert picture description here

9. Directed trees and spawning forests

If a directed graph has exactly one vertex with an in-degree of 0 and the remaining vertices with an out-degree of 1, it is one 有向树. A directed graph 生成森林consists of several directed trees, containing all the vertices in the graph, but only enough 不相交arcs to form several directed trees.
Insert picture description here

2. Picture storage structure

The storage structure of the graph will be discussed in detail later, but now let me talk about the common ones:

  1. Array notation
  2. Adjacency list
  3. Cross linked list
  4. Leading multiple tables

3. Graph traversal

This will also be discussed later, the two traversal 深度优先搜索DFSand广度优先搜索BFS

Guess you like

Origin blog.csdn.net/u011017694/article/details/110393817