In the first half of 2021, software designers' real questions and answer analysis in the afternoon (4)

Read the following instructions and C codes, answer questions 1 and 2, and fill in the answers in the corresponding columns on the answer sheet.
[Description]
A convex polygon means that the line connecting any two points of the polygon falls on the boundary or interior of the polygon. Adjacent point lines fall on the sides of the polygon, called edges, and non-adjacent point lines fall inside the polygon. called strings. Assuming that there are weights on any two-point connection, the optimal three-partition problem of a convex polygon is defined as: Find a partition scheme that divides a convex polygon into a set of disjoint triangles and the sum of the weights of each triangle is the smallest. The weight of each triangle is the sum of the three edge weights.
Assume that the convex polygon point numbers of N points are V1, V2,..., VN, if the original convex polygon is divided into a triangle V1VkVN at VK, two sub-polygons V1, V2,..., Vk and Vk, Vk+1, ...VN, to get an optimal subdivision scheme, then the optimal subdivision scheme should include the optimal subdivision schemes of these two sub-convex shapes. Use m[i][j] to represent the weight of the optimal subdivision scheme for the convex polygon formed by Vi-1, Vi,...Vj, and S[i][j] to record the k value for subdividing the convex polygon. then
insert image description here
where:

Wj,i-1 are the weights of the three sides of the triangle respectively. Solve the optimal subdivision scheme of convex polygons, that is, find the weight of the minimum subdivision and the corresponding triangle set.

#include<stdio.h>
#define N 6 //凸多边形规模
int m[N+1] [N+1]; //m[i][j]表示多边形Vi-1到Vj最优三角剖分的权值
int S[N+1] [N+1]; //S[i][j]记录多边形Vi-1到Vj最优三角剖分的k值
int W[N+1] [N+1]; //凸多边形的权重矩阵,在main函数中输入
/*三角形的权重a,b,c,三角形的顶点下标*/
int get_ triangle_weight(int a,int b,int c)

{
    
    
     return W[a][b]+W[b][c]+W[c][a];
}
/*求解最优值*/
void triangle_partition(){
    
    
int i,r,k,j;
int temp;
/*初始化*/
for(i=1;i<=N;i++){
    
    
m[i][i]=0;
}
/*自底向上计算m,S*/
for(r=2;1;r++)

{
    
         /*r为子问题规模*/
       for(i=1;k<=N-r+1;i++)

     {
    
    2;
         m[i][j]= m[i][j]+m[i+1][j]+get_triangle_weight(i-1,i,j); /*k=j*/
         S[i][j]=i;
         for(k=j+1;k<j;k++)

          {
    
         /*计算 [i][j]的最小代价*/
                 temp=m[i][k]+m[k+1][j]+ge_triangle_ weight(i-1,k,j);
                if((3))

                   {
    
             /*判断是否最小值*/ 
                               m[i][j]=temp;
                               S[i][j]=k;
                   }
          }
     }
}
}
/*输出剖分的三角形i,j:凸多边形的起始点下标*/
void print_triangle(int i,int j){
    
    
if(i==j) return;
print_triangle(i,S[i][j]);
print_ triangle((4));
print(“V%d- -V%d- -V%d\n“,i-1,S[i][j],j);
}

[Question 1] (8 points)
According to the instructions and the C code, fill in the blanks (1) ~ (4) in the C code.

(1)i<=N
(2)j=i+r-1
(3)temp<m[i][j]
(4)s[i][j]+1,j

[Question 2] (7 points)
According to the description and C code, the design strategy adopted by the algorithm is (5), the time complexity of the algorithm is (6), and the space complexity is (7) (indicated by O).

(5) Dynamic programming method
(6) O(n3)
(7) O(n2)

Answer analysis:
The algorithm of this question is quite difficult. Without understanding the algorithm process, you can first partially fill in the blanks based on relevant information.
First of all, according to the description of the question, the scale of the problem is truncated from k. At this time, it is actually the term "optimal substructure", and there is a recursive application in this question, which is a typical application of dynamic programming.
According to the code in the title, there are three layers of nested for loops. At this time, the time complexity of the code is O(n3).
The intermediate solution of the auxiliary space record used in this question has two arrays m[i][j] and S[i][j], both of which are two-dimensional arrays, and the order of space complexity is O(n2).
Finally, analyze the code to fill in the blanks.
In the first blank, r represents the scale of the sub-problem. It is known that the scale division starts from r=2, and the maximum sub-problem should be able to take N. Therefore, fill in r<=N or its equivalent representation in this blank.
What is missing in (2) is the initialization assignment of j, which is more difficult in this case. The code calculates the value of the rear boundary of the chain whose front boundary is i and the chain length is r, and the result is i+r-1, that is, fill in j=i+r-1 or its equivalent expression in this question.
(3) Judgment condition for empty and missing. At this time, the comment clearly states that the minimum value is judged here. After the judgment, the value of m[i][j] is modified and changed to temp, which means that m[i][j] is here When the record is not the optimal solution (minimum value), it needs to be corrected to change to the minimum, that is, fill in temp<m[i][j] or its equivalent expression (if a value is smaller than the minimum value, modify the minimum value ).
(4) What is missing is the printing parameter, which is analyzed in combination with the code context. The above prints print_triangle(i,S[i][j]); that is, the number of the previous part of the truncation, and the following print_triangle((4)); prints should be the latter part of the truncation, that is, fill in s[i][j]+1,j.

Guess you like

Origin blog.csdn.net/johnWcheung/article/details/127697560