Data structure final review subjective exercises (answer version)

8-1 Application scenarios of the adjacency list (20 points)

Question and answer: Why use adjacency list to store undirected graphs, why should it be sparse enough to be cost-effective?

答:假设无向图有 n 个点,m 条边,每个点相连 因为邻接表储存无向图的时候,有 n 个顶点就要创建 n 个链表. 每个链表都会存和本顶点相关联的顶点,故每一条边会被存两次。故至少需 m* 2 个节点 而邻接矩阵无论图的稀疏程度,都需要 n*(n+1)/2 的空间存储。

Textbook P158;

8-2 The edge of the adjacency matrix (20 points)

Question and answer: Using adjacency matrix to store undirected graph, how to check whether there is an edge between vertex i and vertex j?

无向图的邻接矩阵 A 是一个对称矩阵,每条边会表示两次,因此矩阵中对应位置 A[i][j] 或 A[j][i]是否为 1,若为 1 则判断有边,为 0 则判断无边。

Textbook P155;

8-3 Binary tree first-order traversal (40 points)

If you use the following graphic method to store a binary tree, please write the corresponding type definition, and write the binary tree traversal algorithm based on your type definition.
Insert picture description here

struct BiTNode{
    
      
	char data; 
	int lchild,rchild;  
}Tree[100]; 

void preTraverse(int i){
    
      
	if(i==-1) return ;  
	printf("%d",Tree[i].data);  
	preTraverse(Tree[i].lchild);  
	preTraverse(Tree[i].rchild);  
} 

int main(){
    
        
	preTraverse(1); 
} 

8-4 Degree of adjacency list (5 points)

Questions and answers: How to find the degree of a specified vertex when using an adjacency list to store a directed graph?

Suppose the adjacency list of the directed graph is as follows: (Textbook P156-158)

#define MVNum  100       //最大顶点个数 
typedef struct ArcNode {
    
       
  int   adjvex;   // 该弧所指向的顶点的位置 
  struct ArcNode  *nextarc;   // 指向下一条弧的指针 
  OtherInfo   info;   // 该弧相关的信息,如权值等,若无信息可缺省 
} ArcNode; 
typedef struct VNode {
    
      
  VerTexType  data;   // 顶点信息 
  ArcNode  *firstarc;   // 指向第一条依附该顶点的弧 
} VNode, AdjList[MVNum]; 
typedef struct {
    
       
     AdjList  vertices; 
     int  vexnum, arcnum;  
} ALGraph; 

Find the degree of a specified vertex

method one:

If the directed graph is ALGraph G, locate and find the specified vertex Vi, and the position is i in the one-dimensional array G.vertices.

First find the out-degree of the specified vertex G.vertices[i]:
take G.vertices[i] in the adjacency list as the head node, traverse the singly linked list pointed to, and traverse each node of the linked list sequentially. The number of nodes is the out-degree of the vertex.

Then find the in-degree of the specified vertex G.vertices[i]:
traverse each singly-linked list in G.vertices[1]~G.vertices[G.vexnum] in turn, and each singly-linked list traverses its nodes in turn at the same time, Judge whether the value of adjvex of the node is equal to i, if it is equal, the counter of the in-degree is incremented by one, and the number of all nodes equal to i in all singly linked lists is counted as the in-degree of the vertex.

Finally, the degree of the vertex is out degree + in degree

8-5 Side 2 of the adjacency matrix (5 points)

Question and answer: Use the adjacency matrix a to store the undirected network. If there is no edge between the vertex i and the vertex j, what is the value of a[i][j] and how did you analyze it?

若 i 号顶点与 j 号顶点之间不存在边,则 a[i][j]和 a[j][i]为∞,∞表示计算机允许的、大于所有边上权值的数 Maxn

8-6 In-order traversal of binary tree (40 points)

If you use the following diagram to store a binary tree, please:
(1) Write the corresponding type definition.
(2) Write a binary tree in-order traversal algorithm based on your type definition .
(3) Write the statement that calls the function.
Insert picture description here

struct BiTNode{
    
      
	char data; 
	int lchild,rchild;  
}Tree[100]; 

void inTraverse(int i){
    
      
	if(i==-1) return ;  
	inTraverse(Tree[i].lchild);  
	printf("%d",Tree[i].data); 
	inTraverse(Tree[i].rchild);  
}  

int main(){
    
        
	inTraverse(1); 
} 

8-7 Connected and disconnected 1 (20 points)

1. For a connected graph, what are its connected components?
1) 在无向图中,如果从顶点 vi 到顶点 vj 有路径,则称 vi 和 vj 连通.如果图中任意两个顶点之间都连通,则称该图为连通图,否则,将其中的极大连通子图称为连通分量。任何连通图的连通分量只有一个,即是其自身。

2. If a depth-first search from any vertex of an undirected graph can visit all vertices, what must the graph be?
2) 连通图

8-8 Connected and disconnected 2 (20 points)

1. For a connected graph, what are its connected components?
1) 在无向图中,如果从顶点 vi 到顶点 vj 有路径,则称 vi 和 vj 连通.如果图中任意两个顶点之间都连通,则称该图为连通图,否则,将其中的极大连通子图称为连通分量。任何连通图的连通分量只有一个,即是其自身。

2. If a breadth-first search from any vertex of an undirected graph can access all vertices, what must the graph be?
2) 连通图

8-9 DFS reading algorithm write operation results 1 (30 points)

With the following algorithm definition:

void DFS_AM(AMGraph G, int v)
{
    
      //图G为邻接矩阵类型 
  cout << v << " ";  //访问第v个顶点
  visited[v] = true;  
  for(w=0; w<G.vexnum; w++) //依次检查邻接矩阵v所在的行  
     if((G.arcs[v][w]!=0)&& (!visited[w]))  
         DFS_AM(G, w); //w是v的邻接点,如果w未访问,则递归调用DFS_AM 
} 

void DFSTraverse(Graph G)
{
    
      // 对图 G 作深度优先遍历
  for(v=0; v<G.vexnum; ++v) 
     visited[v] = FALSE; //访问标志数组初始化
  for (v=G.vexnum-1; v>=0; --v) 
     if (!visited[v])  DFS(G, v);  //对尚未访问的顶点调用DFS
}

Assume that the content of the arcs array in Figure G is as follows:
Insert picture description here
write the running process and output results of DFSTraverse(G).

Note: Write only the output result and not the analysis process, 0 points.

v=7
DFS(G,7), output 7, visited[7]=TRUE
DFS(G,1), output 1, visited[1]=TRUE
DFS(G, 2), output 2, visited[2]= TRUE
DFS(G, 4), output 4, visited[4]=TRUE
DFS(G, 5), output 5, visited[5]=TRUE
DFS(G, 3) out, output 3, visited[3]=TRUE
Back to DFS(G, 5)
DFS(G, 6) out, output 6, visited[6]=TRUE
Back to DFS(G, 5) out
Back to DFS(G, 4) out
Back to DFS (G, 2) out
back to DFS (G, 1) out
back to DFS (G, 7) out
v=6 v=5 v=4 v=3 v=2 v=1 v=0
DFS (G , 0) out, output 0, visited[0]=TRUE
output sequence: 7 1 2 4 5 3 6 0

8-10 DFS reading algorithm write operation results 2 (30 points)

With the following algorithm definition:

void DFS_AM(AMGraph G, int v) 
{
    
      // 图 G 为邻接矩阵类型  
  cout << v << " ";  // 访问第 v 个顶点
 
  visited[v] = true;   
  for(w=G.vexnum-1; w>0; w--) // 依次检查邻接矩阵 v 所在的行   
     if((G.arcs[v][w]!=0)&& (!visited[w]))   
         DFS_AM(G, w); //w 是 v 的邻接点,如果 w 未访问,则递归调用 DFS_AM  
}  
 
void DFSTraverse(Graph G) 
{
    
      // 对图 G 作深度优先遍历
 
  for(v=0; v<G.vexnum; ++v)  
     visited[v] = FALSE; // 访问标志数组初始化
 
  for (v=0; v<G.vexnum; ++v)  
     if (!visited[v])  DFS_AM(G, v);  // 对尚未访问的顶点调用 DFS 
}

Suppose the content of the arcs array in Figure G is as follows:
Insert picture description here
write the running process and output results of DFSTraverse(G). (30 points)
Note: Write only the output results and not the analysis process, 0 points.

Answer: Refer to the answer to the previous question.

8-11 Find the shortest path of a single source (20 points)

Textbook P189 Figure 6.35, try Dijkstra's algorithm to find the shortest path from vertex a to other vertices.
Insert picture description here
(1) Write the contents of the S[], D[], Path[] array after initialization and after each iteration.

(2) Write down the shortest path length between vertex a and other vertices and the corresponding path
Insert picture description here
(1) Insert picture description here
or
Insert picture description here
1-initialization, 2-7 do 6 times
(2)
Insert picture description here
8-12 whether there is an edge between the two vertices (20 minutes )

Write an algorithm to determine whether there is an edge from vertex vi to vertex vj (i!=j) in the directed graph G stored in the adjacency list .

Suppose the adjacency list of the directed graph G is as follows:

#define MVNum  100       //最大顶点个数 

typedef struct ArcNode {
    
       
  int   adjvex;   // 该弧所指向的顶点的位置 
  struct ArcNode  *nextarc;   // 指向下一条弧的指针 
  OtherInfo   info;   // 该弧相关的信息,如权值等,若无信息可缺省 
} ArcNode; 

typedef struct VNode {
    
      
  VerTexType  data;   // 顶点信息 
  ArcNode  *firstarc;   // 指向第一条依附该顶点的弧 
} VNode, AdjList[MVNum]; 

typedef struct {
    
       
     AdjList  vertices; 
     int  vexnum, arcnum;  
} ALGraph; 

Answer: If the directed graph is ALGraph G, the position of the vertex vi in ​​the one-dimensional array G.vertices is i, and the position of the vertex vj is j in the one-dimensional array G.vertices. Then traverse the singly linked list with the vertex G.vertices[i] as the head node, and determine whether the value of adjvex of the current node is equal to j by traversing the edge. If it is equal, there is an edge from vertex vi to vertex vj. Otherwise, there is no edge from vertex vi to vertex vj.

8-13 Construction and Search of Hash Table (25 points)

Set a set of keywords: (10, 16, 32, 17, 31, 30, 20), the hash function is: H(key) = key MOD 11, the table length is 12, and the linear detection method handles conflicts. Try to answer the following questions:

1. Draw a schematic diagram of the hash table;

2. If you search for keyword 20, which keywords need to be compared in turn?

3. If you search for keyword 27, which keywords need to be compared in turn?

4. Assuming that the search probability of each keyword is equal, find the average search length when the search is successful.

5. Find the filling factor.
Insert picture description here
8-14 Prim-minimum spanning tree (10 points)

For the following figure, write the sequence of vertices and edges of the minimum spanning tree using Prim's algorithm, starting from the vertex, 1.
Insert picture description here
Insert picture description here
Vertex sequence: V={1,6,2,3,4,0,5}
Edge sequence: E={(1,6)14, (1,2)16, (2,3)12, (3, 4)22, (6,0)23, (0,5),10)

8-15 Write the breadth-first traversal sequence of the graph according to the adjacency list (10 points)

(1) Write out the adjacency list in the figure below (the sequence number of the edge node is from small to large);

(2) Write the breadth-first search sequence and depth-first search sequence starting from vertex 3, separated by spaces between vertices. It is agreed to visit in priority order of small number of nodes. (The answer can be inserted into the picture)
Insert picture description here
Insert picture description here
8-16 Construction of Binary Sort Tree (5 points)

Insert the key sequence of 12,7,17,11,16,2,13,9,21,4 into an empty binary sort tree in turn. Please draw the resulting binary sort tree.
Insert picture description here

Insert picture description here

8-17 Binary search (5 points)

Given the following ordered list of 11 elements (8,16,19,23,39,52,63,77,81,88,90), draw the decision tree of its binary search, and give the search elements 88 and 17. The binary search process.
Insert picture description here

For the concept of decision tree, see textbook P194;

8-18 Initial heap of heap sort (6 points)

If the key code of a group of records is (46,79,56,38,40,84), please write the initial heap created by the method of heap sorting. Please write the detailed process (3 points) and result (3 points).

Refer to textbook P252 Example 8.6: Start screening from the last non-terminal node;

Insert picture description here
Results: 84, 79, 56, 38, 40, 46;

8-19 Construction of Huffman Tree (9 points)

Assuming that the message used for communication consists of only 8 letters, the frequencies of letters appearing in the message are 0.09, 0.16, 0.02, 0.06, 0.32, 0.03, 0.21, 0.11, respectively.

(1) Try to design a Huffman code for these 8 letters, please write down the detailed process and code of the Huffman tree construction; (4 points)

(2) Design another equal-length coding scheme represented by binary; (2 points)

(3) For the above example, analyze the coding length of the two schemes and analyze the advantages and disadvantages of the two schemes (3 points)

Answer:
(1) ①Initialization: first dynamically apply for 2 8 = 16 units; then loop 2 8-1 = 15 times, starting from unit 1, and sequentially add the parents, left child, and right child in all units from 1 to 15 The subscripts of are initialized to 0; the last cycle is 8 times, and the weight of the leaf nodes in the first 8 units is input;
②Create tree: cycle 8-1 = 7 times, select and delete through n-1 = 7 times And merge to create a Huffman tree. The selection is to select the two tree root nodes s1 and s2 with the parents of 0 and the smallest weight from the current forest; delete means to change the parents of nodes s1 and s2 to non-zero; merge is to change the weights of s1 and s2 The sum as the weight of a new node is sequentially stored in the unit after n+1 = 9 in the array, and the subscript of the left child of this new node is recorded as s1, and the subscript of the right child is s2.
Insert picture description here
Insert picture description here
Coding:
A: 000
B: 110
C: 11100
D: 1111
E: 10
F: 11101
G: 01
H: 001

(2) (3)
Insert picture description here

Guess you like

Origin blog.csdn.net/Jessieeeeeee/article/details/107390119