[Data Structure]——Graph Short Answer Question Template

Preface

There are a lot of knowledge points about binary trees and graphs, and they are also very targeted in the exam, which requires more thinking and skillful recitation of relevant knowledge points.

1. Definition of graph

1. What is an undirected graph? What is a directed graph?

: According to whether the edges in the graph have directionality, it can be divided into directed graphs and undirected graphs. That is, if there is no difference between the predecessor and successor relationships of a graph, it is an undirected graph; while there is a difference between the predecessor and successor relationships of a graph. It is a directed graph.

2. Connected graphs and strongly connected graphs

(1) Definition of connected graph

1. What is a connected graph? What is a strongly connected graph?

: A connected graph refers to an undirected graph, and a strongly connected graph refers to a directed graph. In an undirected graph, any two nodes are connected (there is a path between any two nodes), then the graph is a connected graph; and in a directed graph, any two different vertices exist The paths between them are called strongly connected graphs.

(2) Connected components and strongly connected components

1. Briefly describe what are connected components and strongly connected components?

: The maximal connected subgraph of an undirected graph is called a connected component. Similarly, the maximal strongly connected subgraph of a directed graph is called a strongly connected component.

(3) Number of edges in a connected graph

1. How many edges does a connected graph with n vertices have? How many edges are there at most?

: An undirected connected graph containing n vertices. When a tree is formed, the number of edges in the graph is the least, with n-1 edges; and when the undirected complete graph is the undirected complete graph, the number of edges in the graph is the most, with n(n-1) /2 edges.

3. Graph traversal

(1) The idea of ​​depth-first traversal of graphs

1. Explain the idea of ​​depth-first traversal algorithm for graphs.

: Depth-first traversal (DFS) of the graph needs to use the stack to traverse:
①First, select a certain vertex vi< in the graph /span> ③If there are still vertices that have not been visited, select another unvisited vertex. As the starting point again, go back to the last unvisited vertex and repeat the above steps until all nodes in the graph are visited. in the graph have been visited; [can be summarized as Starting from the starting vertex, search the graph as deeply as possible along a path until it cannot continue]i , and the vertex has not been visited, the process continues until all vertices connected to vi
② Select any vertex adjacent to v is accessed as the starting point;

(2) The idea of ​​breadth-first traversal of graphs

1. Explain the idea of ​​the breadth-first traversal algorithm of the graph.

: Breadth-first search (BFS) of the graph needs to use the queue to traverse:
①First, select a certain vertex vi in the graph As a starting point, after accessing, enqueue it and mark it as visited (the queue is used to avoid repeated access and store adjacent vertices that have been visited);
② Visit the vertices adjacent to vi in sequence, that is, when the queue is not empty, check all adjacent vertices of the dequeued vertex, and visit the unvisited ones. Adjacent vertices and enqueue them, repeat the process; [can be summarized as starting from the starting vertex, traversing the vertices adjacent to the current vertex layer by layer in breadth-first order to visit them]
③ When the queue is empty, jump out of the loop, that is, all adjacent vertices of the visited vertices have been visited, and the traversal is completed at this time.

(3) Graph traversal thinking to judge connected graphs

1. How to use graph traversal algorithm to determine whether a graph is a connected graph?

: Both traversal algorithms can be used to determine the connectivity of the graph. In depth-first traversal and breadth-first traversal of the graph, if all vertices are visited during the traversal, the graph is connected. Otherwise, the graph is not Connected.

4. Graph storage structure

(1) Definition of the storage structure of the graph

1. What are the two commonly used storage structures for graphs? What other storage structures are there?

: Common storage structures for graphs include adjacency matrices and adjacency lists, as well as cross-linked lists, adjacency multiple lists, etc.

The adjacency matrix of a graph is used to represent the adjacency relationship between vertices, where a one-dimensional array stores the vertices, and a two-dimensional array stores the adjacency relationship between vertices. The adjacency matrix of a graph with n vertices is n× n (n rows and n columns); the adjacency list uses a sequential storage structure and a linked storage structure to store the graph. For each vertex vi in ​​the graph, all the vertices adjacent to vi are connected into a singly linked list, that is, this singly linked list is called is the adjacency list of vertex vi. In addition, the adjacency lists of all vertices need to be put into an array. The space used to store the graph through the adjacency list depends on the number of vertices and edges of the graph. The number of vertices n determines the size of the vertex table. Space size, the number of edges determines the space size of edge table nodes.

(2) Selection of storage structure

1. To find the in-degree and out-degree of each vertex in a directed graph, which storage structure is more convenient? Why?

: Adjacency matrix, because in the adjacency matrix of a directed graph, the number of non-zero elements in the i-th row and the number of non-zero elements in the i-th column correspond to the out-degree OD(vi) and in-degree ID(vi) of the vertex, And since the degree in a directed graph is equal to the sum of out-degree and in-degree, that is, the degree of vertex vi is the sum of the number of non-zero elements in the i-th row and i-th column of its adjacency matrix. If you use an adjacency list, you need to traverse a linked list to find the in-degree and out-degree of each vertex in the directed graph.

(3) Advantages and disadvantages of adjacency matrix

1. Briefly describe the advantages and disadvantages of the graph adjacency matrix storage structure.

: The advantage is that it is easy to determine whether there is an edge between two vertices from the matrix by using a value equal to 0 or 1; in addition, it is also easy to calculate the degree of each vertex. For an undirected graph, the sum of the rows is the degree of the vertex, and For a directed graph, the sum of rows is the out-degree of a vertex, and the sum of columns is the in-degree of a vertex, that is, the degree of a vertex is the sum of the number of non-zero elements in the i-th row and i-th column.

The disadvantage is that the space complexity is high. A directed graph with n vertices requires n2 storage units to store, and for An undirected graph with n vertices only requires n(n-1)/2 storage units to store through compressed storage, and the space complexity of both is O(n2), especially for sparse graphs, which wastes storage space.

(4) Advantages and Disadvantages of Adjacency List

1. Briefly describe the advantages and disadvantages of the graph adjacency list storage structure.

: The advantage is high space utilization. For sparse graphs (graphs with relatively few edges), adjacency lists can effectively save storage space, in which each vertex only needs to store the information of its adjacent vertices; because it is a Chained data structure, so vertices can be easily added or deleted; in addition, its search efficiency is high. Through the chained structure, each vertex has a linked list to store the information of its adjacent vertices, and it can be found by simply traversing the linked list. all adjacent vertices, whereas in an adjacency matrix, one needs to scan the entire row or column to find all adjacent vertices.

The disadvantage is that additional space is needed to store edge information. Each vertex needs to store the information of its adjacent vertices, which requires additional space. Since it needs to traverse all the edges in the adjacency list to determine it, it requires a high time complexity. Therefore, it is impossible to directly determine whether there is an edge between two vertices; in addition, since the adjacent vertex information of each vertex is stored in a linked list, it is necessary to traverse the linked list to calculate the degree of the vertex.

(5) Definition of adjacent multiple tables

1. Briefly describe the definition of adjacency multiple tables of graphs and what problems does this storage structure solve?

: For undirected graphs, this storage structure stores the two vertices of an edge in the edge table node, and connects the edges adjacent to the same vertex in the vertex table node, that is, the adjacency multiple list, which is an adjacency list. improvement methods. This storage method solves the problem of storing the same edge twice when storing undirected graphs in the adjacency list. That is, only one node is needed to record an edge, which avoids repeated storage of the same edge and improves space storage efficiency.

(6) Definition of cross linked list

1. Briefly describe the definition of the cross-linked list of the graph and what problems does this storage structure solve?

: This storage structure is for a directed graph. Each vertex of the graph has two linked lists. One is the arc starting from the vertex, and the other is the arc ending with the vertex. It combines the adjacency list and the inverse adjacency list. It stores the out-degree and in-degree of each vertex at the same time, so that it is easy to find the in-degree and out-degree information of the directed graph vertices.

5. Application of diagrams

(1) Prim’s algorithm (Prim)

1. Briefly describe the basic steps of Prim's algorithm for minimum spanning trees.

: Each time, select a vertex from the graph that is closest to the vertex set in the currently added tree, and add the vertex and corresponding edges to the tree until all vertices are merged into the tree to obtain the minimum spanning tree.

(2) Kruskal algorithm (Kruskal)

1. Briefly describe the idea of ​​Kruskal algorithm to find the minimum spanning tree.

: For a graph with n vertices, the idea of ​​Kruskal's algorithm is to select the weights corresponding to all edges of the graph in order from small to large. If a selected edge forms a loop with the previous tree, then discard Go on and on until all vertices are visited, and when the number of edges in the spanning tree is n-1, the minimum spanning tree can be obtained.

(3) Shortest path

1. What is the difference between Dijkstra's algorithm and Floyd's algorithm for finding the shortest path?

: ①The purpose of solving is different. Dijkstra's algorithm is suitable for the shortest path problem from a certain source point to other vertices in the graph, while Floyd's algorithm is suitable for the shortest path problem between any two points in the graph;< a i=1> ②The time complexity is different. For the graph G=(V, E), where |V| represents the number of vertices in the graph, the time complexity of Dijkstra's algorithm is O(|V|2), while the time complexity of Floyd's algorithm is O(|V|3).

(4) Topological sorting

1. Briefly describe the steps of topological sorting.

: For topological sorting of an AOV network, the steps are as follows:
1. Select a vertex from the network that has no predecessor, that is, the indegree ID(v)=0;
2. Delete the vertex and delete all starting edges (edges out of degree) from the vertex, and output the vertex;
3. Repeat ( Steps 1) and (2) until the AOV network is empty or there is no node without a predecessor;
4. Finally, a sequence is obtained, which is topological sorting.

2. There can be multiple sequences obtained by topological sorting of the AOV network. Is this correct? Why?

: Correct, the topological sorting sequence of the AOV network is more than one, and there can be multiple. This is because when there are multiple vertices with in-degree 0 in the network, topological sorting is performed, and more than one sequence is obtained.

(5) Critical path

1. Briefly describe the process of solving the critical path.

: The steps to solve the critical path are as follows:
1. According to the given graph, find the topological ordered sequence and the inverse topological ordered sequence of the graph;
2. Through the obtained sequence, first find the earliest occurrence time and latest occurrence time of each event;
3. According to the results obtained in (2), Then for the activities, find the earliest occurrence time and the latest occurrence time of each activity;
4. Based on the results obtained in (3), find the activities whose earliest occurrence time and latest occurrence time are the same , the key activities are obtained, and the path connected by the key activities is the critical path.

Guess you like

Origin blog.csdn.net/qq_43085848/article/details/134913521