【Data Structure】——Related Exercises on Graphs

1. Choose to fill in the blanks

Question 1

1. Undirected graph G=(V, E), where: V={a, b, c, d, e, f}, E={(a, b), (a, c), (a, e ), (b, e), (c, f), (f, d), (e, d)}, take vertex a as the source point to perform depth-first traversal on the graph, and the obtained vertex sequence is correct () .
A, a, b, e, c, d, f
B, a, c, f, e, b, d
C, a, e, b, c, f, d
D, a, e, d, f, c , b

Analysis: (D)
The undirected graph can be drawn as follows:
insert image description here
Depth-first search (DFS) of the graph [similar to the pre-order traversal of the tree, first visit the nodes, and then recursively traverse the outer nodes, search a graph as deep as possible, all backtracking algorithm].
The DFS algorithm is a recursive process, which needs to use the stack to complete the operation. First select a certain vertex vi in ​​the graph, after visiting, randomly select a vertex adjacent to vi, and this vertex has not been visited, ..., continue to repeat the process until all the vertices connected to vi in ​​the graph are visited; If there is still a vertex that has not been visited, another vertex that has not been visited is selected as the starting point again, and the above steps are repeated until all nodes in the graph are visited.

Use vertex a as the source point to perform depth-first traversal on the graph, and then select any vertex adjacent to a. Since the vertices adjacent to a are b, c, e, there are the following three access sequences {a, b, ... ...} or {a, c, ...} or {a, e, ...},
insert image description here
then choose to visit vertex d, and you can also choose a vertex that has not been visited yet. Since the previously selected access sequence is {a, d}, at this time, access the vertex f adjacent to vertex d to obtain the sequence {a, d, f}; then access the vertex c adjacent to vertex f to obtain the sequence {a , d, f, c}; Since the vertices a and f adjacent to c have been visited at this time, go back to f, continue to check d, the vertices adjacent to d have also been visited, ..., and finally until the vertex d It has not been visited yet, visit it, you can get the sequence {a, e, d, f, c, b}.

Question 2

2. The graph shown below performs a breadth-first search traversal starting from vertex 1, and the available vertex access sequence is ().
A, 1, 3, 2, 4, 5, 6, 7
B, 1, 2, 4, 3, 5, 6, 7
C, 1, 2, 3, 4, 5, 7, 6
D, 2, 5 , 1, 4, 7, 3, 6

insert image description here
Analysis: (B)
Breadth-first search (BFS) of graphs [similar to tree-like layer order traversal, layer by layer outward traversal].
First select a starting point vertex vi, enqueue it and mark it as visited after accessing it (use the queue to avoid repeated visits and store adjacent vertices that have been visited); when the queue is not empty, check all vertices that are dequeued Adjacent vertices, access unvisited adjacent vertices and enqueue them, ..., continue to repeat the process until all vertices connected to vi in ​​the graph are visited; jump out of the loop when the queue is empty, then traverse at this time Finish.

Use vertex 1 as the source point to perform breadth-first traversal on the graph, select all vertices adjacent to it for access, there are 2 and 3, and the sequence {1, 2, 3} or {1, 3, 2} can be obtained; then, Visit other vertices adjacent to 2 and 3 until all vertices are visited. For example, choose to visit vertex 2, and then visit 4 adjacent to vertex 2; at this time, to visit the adjacent vertex of vertex 4, since vertex 2 has already been visited, you can choose to visit vertex 3, vertex 5 and vertex 6, select vertex 3, at this time The sequence is {1, 2, 4, 3}; then visit the vertices 5 and 6 adjacent to vertex 4, the sequence is {1, 2, 4, 3, 5, 6}; finally there is vertex 7 that has not been visited yet, The sequence {1, 2, 4, 3, 5, 6, 7} is available.

Question 3

3. A directed complete graph with n vertices has ( ) edges.
A, n(n-1)/2
B, n(n-1)
C, n(n+1)/2
D, n(n+1)

Analysis: (B)
If in a directed graph, if each vertex is connected by two opposite arcs, it is called a directed complete graph. In a directed complete graph containing n vertices, there are n(n-1 ) arcs.

For example, in a directed complete graph with 4 vertices, there are a total of n(n-1)=4×3=12 arcs, as follows:
insert image description here

Question 4

4. In an undirected graph, the degree of all vertices is equal to () times the number of edges; in a directed graph, the sum of in-degrees of all vertices is equal to () times the sum of out-degrees of all vertices.
A, 1/2
B, 2
C, 1
D, 4

Analysis: (B)(C)
In an undirected graph, the sum of the degrees of all vertices is equal to twice the number of edges. For example, for the following graph, the sum of the degrees of the vertices is 2+3+3+2=10, and the edges of the graph are The number is 5, so 10=2×5.
insert image description here
In a directed graph, for a vertex, the degree of the node is equal to the in-degree of the vertex + the out-degree of the vertex, and the number of arc heads of the node is called the in-degree, recorded as ID(v); the arc tail of the node The number is called out-degree, recorded as OD(v), that is, TD(v)=ID(v)+OD(v). For a directed graph with n vertices, the degree of each vertex can be up to 2(n-1), and the sum of the in-degrees of all vertices is equal to the sum of the out-degrees of all vertices. For example, in the directed graph below, the sum of the in-degrees of all vertices is 2+0+2+1=5, and the sum of the out-degrees of all vertices is 0+3+1+1=5, which are equal :
insert image description here

2. Application questions

Question 1

Question type :根据图的邻接表存储结构,画出图以及图的深度/广度优先生成树

1. Let G=(V, E) be stored in an adjacency list, as shown in the figure below, draw the depth-first spanning tree and breadth-first spanning tree of the graph.

insert image description here

Solution: According to the adjacency table, vertex 1 is adjacent to 2, 3, 4, vertex 2 is adjacent to 1, 3, 4, 5, vertex 3 is adjacent to 1, 2, 4, vertex 4 is adjacent to 1, 2, 3, 5 Adjacency, vertex 5 is adjacent to 2, 4, draw this graph: insert image description here
the depth-first traversal sequence steps of the graph:
1. Visit vertex 1 first, and mark it as visited after the visit. At this time, the sequence is {1};
2. Check the singly linked list 1. The first unvisited adjacent vertex is 2, and then continue the deep traversal starting from vertex 2. At this time, the sequence is {1, 2}; 3. Check the
singly linked list 2. Vertex 1 has already been visited, so it has not been visited. The adjacent vertex to be visited is 3, and then continue the deep traversal starting from vertex 3. At this time, the sequence is {1, 2, 3}; 4.
Check the singly linked list 3. Vertex 1 and vertex 2 have been visited, so the unvisited The adjacent vertex is 4, and then continue the deep traversal starting from vertex 4. At this time, the sequence is {
1, 2, 3, 4 }; The unvisited adjacent vertex is 5, and then continue the deep traversal starting from vertex 5. At this time, the sequence is {1, 2, 3, 4, 5};
6. There are no unvisited vertices, and the traversal is complete.

Breadth-first traversal sequence steps of the graph:
1. Visit vertex 1 first, mark it as visited after the visit, make it into the queue, and then delete the current queue head node. At this time, the sequence is {1}; 2. Traverse the singly linked
list 1, Make the unvisited adjacent vertices 2, 3, and 4 enqueue and mark, and the sequence is {1, 2, 3, 4} at this time;
3. Traverse the singly linked list 2, enqueue and mark unvisited adjacent vertex 5 , the sequence at this time is {1, 2, 3, 4, 5};
4. There are no unvisited vertices, and the traversal is complete.

Depth-first spanning tree:
insert image description here
Breadth-first spanning tree:
insert image description here

Guess you like

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