Learning data structure-Chapter 5: Graphs (basic knowledge of graphs)

Chapter 5: Graphs (basic knowledge of graphs)

1. Basic concepts of graphs

The graph G is composed of a vertex set V and an edge set E, denoted as G=(V,E), where V(G) represents the finite non-empty set of vertices in the graph G ; E(G) represents the distance between the vertices in the graph G Relation (edge) set

|V| represents the number of vertices in the graph G, also called the order of the graph G; |E| represents the number of edges in the graph G (| | represents the absolute value)


注意: Both linear tables and trees can be empty, but the graph cannot be empty

2. Undirected graph & directed graph

The graph we explained above is an undirected graph. Its edge set is a set of parentheses. Why are the parentheses used? In fact, such parentheses indicate undirected edges .

无向边: Indicates that this edge has no direction, and it does not distinguish between the start and the end, so we can use this 无序对(V,W) = (W,V)to represent it, or it can be said that V and W are adjacent points of each other

无向图: All edges in this graph are无向边

有向图: All edges in this graph are有向边

有向边: The edge has a direction, it distinguishes the start position and the end position, V------->W, we also call such a directed edge , the start position is the arc head, and the end position is the arc end . We use it 有序对<V,W>to mean that at this time we have to distinguish between <V,W> and <W,V>. We also call V adjacent to W or W adjacent to V

3. Simple graph & multiple graph

简单图: There are no repeated edges, or there is no edge from the node to itself.

多重图: There is a repetitive edge, or there is an edge from a node to itself.

注意: Although the figure below shows that A points to B and B points to A, this is not a repeated edge, not a multigraph. When we talked about directed graphs above, we mentioned that <A,B> and <B,A> are representative Different edges, so it is not a multigraph.

4. Complete graph

完全图: Divided into 无向完全图and有向完全图

无向完全图

  • There are edges between any two vertices
  • n vertices have n(n-1)/2 edges

有向完全图

  • There are arcs in opposite directions between any two vertices
  • n vertices have n(n-1) edges

5. Subgraph

FIGS provided with two G=(V,E)and G'=(V',E'), if the V‘are Va subset of, and E’is Ea subset, called G‘as Gsub-graphs, if V(G) = V(G')called G'for the Ggeneration of subgraphs





6. Undirected graph & directed graph

无向图: Only 连通: if there 路径exists from vertex V to vertex W , then it is said that V and W are connected

连通图: Any two nodes are connected

有向图: Only 强连图: if there are both from vertex V to vertex W and from vertex W to vertex V 路径, then it is said that V and W are strongly connected

强连通图: Any two nodes are strongly connected

How many edges does a connected graph with n vertices (strongly connected graph) have at least?

Connected graph: n-1 [a vertex and each remaining vertex (n-1) have an edge]

Strongly connected graph: n

Undirected graph 连通分量: maximally connected subgraph

Directed graph 强连通分量: maximally strongly connected subgraphs

G for a (strong) connected subgraph G'·, if there is no Gother (strong) connected subgraph G", such that G属于G", called G'as G(strong) connected components (that is, the connected component that must be the largest, the absence of additional The connected subgraph or strongly connected subgraph can include it, that is, to ensure that it is connected or strongly connected, it can contain more vertices and edges)

Let’s look at a few examples: find the connected components of an undirected graph

First draw several connected subgraphs of it, and find the connected components (maximal connected subgraphs)


We can see that the following two are connected components (maximal connected subgraphs), because other connected subgraphs cannot contain them.

Find the connected components of the directed graph



The above undirected graph has two connected components, while the directed graph has only one connected component. In fact, we can draw one 结论: if a graph is a connected graph or a strongly connected graph, its connected component or strongly connected component is himself, and if a graph is not a connected graph or a strongly connected graph, then its connected component or There will be many strongly connected components.

7. Spanning trees and forests

极小连通子图: Connected subgraphs with the least edges

Minimize edges when

生成树connected: the connected graph contains a minimal connected subgraph with all vertices

First, the original graph must be a connected, and then a minimal connected subgraph containing all vertices


There are n-1 spanning trees for a graph with n vertices

(Connected graphs can only generate spanning trees, and unconnected graphs can only generate spanning forests)

生成森林: Spanning trees of all connected components of a non-connected graph form a forest

8. Degree of vertices

顶点的度: The number of edges with this vertex as a breakpoint

Undirected graph : the degree of vertex V is recorded as TD(V) with V as the number of breakpoints and edges

The total number of degrees in the undirected graph with n vertices and e edges is: 2e

Directed graph :

  • Out degree: the number of directed edges starting from V, denoted as OD(V)
  • In-degree: the number of directed edges with V as the end point, denoted as ID(V)

The out-degree and in-degree totals of the directed graph with n vertices and e edges are: e :

9. Net

Give weights to edges (in actual situations it may take time, etc.)

10. Dense graph & sparse graph


Sparse and dense definition: |E|<|V|log|V|

When the variable is less than the number of nodes multiplied by the number of log nodes, it is called a sparse graph

11.Directed tree

有向树: The in-degree of one vertex is 0, and the in-degree of the other vertices are all 1.有向图

12. Path

路径: A vertex sequence from vertex V to vertex W on the way, the path that does not repeat the vertices in the sequence is called a simple path


路径长度: The number of the upper side of the path, if the path is the shortest, it is called距离

回路: The same path for the first vertex and the last vertex (ring structure)

No knowledge of public data structure processing wood off the synchronous update, the next time will explain: memory map and basic operation and look forward to everyone's attention

Guess you like

Origin blog.csdn.net/qq_41941875/article/details/106601284