Typical data structures - graphs, graph storage, basic operations and traversal

picture

Quoted from: "Data Structure Tutorial".

concept

Graphs can make relationships between elements many-to-many. There may be a connection relationship between any two data elements in the graph. As a data structure, graphs can express more complex relationships that widely exist between data elements. Among many applications, such as electronic circuit analysis, engineering plan analysis, finding the shortest path, etc., graphs are a very natural model for describing such relationships. Graph theory is one of the main contents of discrete mathematics. Here we only introduce some concepts and storage methods.

  1. Directed graph/undirected graph: If every edge in the graph has no direction, it is an undirected graph. If every edge in the graph has a direction, it is called a directed graph.

    assets/10.jpg

  2. Usually the vertices in the graph need to be numbered in a sequence. If the position of a vertex in a sequence is i, then the vertex is vertex i.

  3. Right/Net: The data information related to the edge is called a right. In specific applications, the weight can have practical meaning, such as the length or grade of the line, the resistance current or voltage value between two end points in the circuit, etc. A graph with a weight on each edge is called a network, or net. The actual meanings of vertices and edges are different depending on the field to which the problem belongs.

  4. Degree: The degree of a vertex refers to the number of edges connected to a vertex v, recorded as TD(v). For a directed graph, the in-degree of a vertex v refers to the number of arcs with vertex v as the end point, denoted as ID(v). The out-degree of a certain vertex v refers to the number of arcs with vertex v as the starting point, denoted as OD( v), we have TD(v) = ID(v) + OD(v). If n is used to represent the number of vertices in the graph, e is used to represent the number of edges or arcs, and TD(vi) is used to represent the degree of vertex vi, then there is the following relationship: 2e = Accumulate TD(vi) from i = 1 to i = n . It can be seen from this relationship that an undirected graph with n vertices has at most n(n - 1)/2. Such a graph is called a complete graph. A directed graph with n vertices has at most n(n - 1) edges. , such a directed graph is called a directed complete graph. If a graph is close to a complete graph, it is called a dense graph; if the number of edges or arcs is small, it is a sparse graph.

  5. Path/ring (loop)/directed graph: In an undirected graph, the sequence of vertices between two vertices can connect a path between the two vertices, that is, a path. The number of edges contained on this path is called the length of the path. For directed graphs, the path is also phased. For a weighted graph, the path length refers to the sum of the weights of all edges on the path. If the starting vertex and the ending vertex are the same vertex, the path is a loop or ring. In a directed graph, if there is a vertex such that a path starting from this vertex can reach all other vertices in the graph, then the directed graph is called a rooted graph, and the vertex is the root of the directed graph.

  6. Subgraph: A subset of a graph, including a portion of the graph's vertices and edges.

  7. Connectivity of graphs: For undirected graphs, if there is a path from fixed point vi to vertex vj (i ≠ j), it is said that vi to vj are connected. If any two vertices in an undirected graph are connected, the undirected graph is called a connected graph. The maximal connected subgraph in an undirected graph is called the connected component of the graph. Obviously, there is only one for a graph. If any two vertices in a directed graph are connected, the directed graph is called a strongly connected graph, and the maximum strongly connected subgraph in the directed graph is called a strongly connected component of the directed graph.

  8. Spanning tree: A minimal connected subgraph of a connected graph is called a spanning tree of the graph. There are no loops in the spanning tree. Adding any edge to the spanning tree will inevitably create a loop. Removing any edge from the spanning tree will definitely make it non-connected.

  9. Spanning forest: If a directed graph has exactly one vertex with an in-degree of 0 and the other vertices with an in-degree of 1, then it is a directed tree. The spanning forest of a directed graph consists of several directed trees.

Basic operations on graphs

Graph storage

  1. Adjacency matrix method (not suitable for sparse graphs because of low space cost-effectiveness).

    12

  2. Adjacency list method.

  1. Cross linked list method for directed graphs. slightly.

Graph traversal

Starting from any specified vertex in a given graph, systematically visit other vertices in the graph according to a certain principle. Each vertex is visited only once to obtain a sequence composed of vertices in the graph. This process is called graph traversal. .

Graph traversal is usually performed using depth-first search and breadth-first search. For details, see the "DFS & BFS (Depth/Breadth First Search)" section below this article.

The main idea of ​​Depth First Search is to first use an unvisited vertex as the starting vertex and walk along the edges of the current vertex to the unvisited vertex. When there are no unvisited vertices, return to the previous vertex and continue to test other vertices until all vertices have been visited. (Quoted from  Depth-First Traversal and Connected Components | Novice Tutorial (runoob.com) )

Breadth-first search, its traversal principle is to start from the specified vertex in the graph, visit the vertex and then visit the unvisited adjacent points of the vertex in sequence, and then start from these adjacent points and visit their unvisited points in sequence according to the same principle. The visited adjacent points, and so on, until the critical points of all visited vertices in the graph are visited. If there are still unvisited vertices in the graph at this time, start from another unvisited vertex. Start and continue the above process until all vertices in the graph have been visited. Breadth-first search is different from depth-first search. It first visits the specified starting point, then sequentially visits all unvisited adjacent points of the vertex, and then visits the unvisited adjacent points of the adjacent points, and so on. Implementation This process requires setting up a queue structure.

Differences in the storage method of the graph, the starting point of the traversal, the method of traversal, etc. will make the traversed sequences different.

Follow-up concepts

Minimum spanning tree, shortest path, AOV network and topology sorting, AOE network and critical path, etc.

Graph theory basics and representation | Novice Tutorial (runoob.com) .

Guess you like

Origin blog.csdn.net/Staokgo/article/details/132922499