Convex polygon triangulation analysis of the optimal dynamic programming _

I.  Description of the problem

Polygon triangulation : divide the polygon into a set of triangular cross mutually chord T.

Optimal Analysis : triangulation given a convex polygon, so that the triangulation on all the right of the triangle and the smallest (the value of sum of the weights of all the triangles and, not only the weight of the edges is calculated and the chord value).

Second, the  feasibility analysis and problem-solving ideas selected algorithm strategy

The basic idea : dynamic programming.

Optimal substructure : If the projections (n + 1) polygon p = {V0, V1, ... , VN} optimal triangulation triangle T contains V0VkVn, 1 <= k <n . T is the right part of the right and 3: V0VkVn right triangle, the sub-polygons {V0, V1, ..., Vk } and {Vk, Vk + 1, ... , Vn} , and Right.

Can assert that these two sub-analyzes have more women of Triangle T is determined optimal. Because if a smaller triangle analysis will result in suboptimal triangle T analysis of contradictions

     

Recursive structure : the definition of t [i] [j], 1 <= i <j n {Vi-1, Vi, ..., Vj} triangulation optimal weight function value corresponding to the polygon <= sub-convex, i.e., its optimal value.

Optimal comprising split Vi-1VkVj right triangle, the sub-multi women {Vi-1, Vi, ..., Vk} and {Vk, Vk + 1, ..., Vj} and Right.

 

Optimal weight protrusion (n + 1) of the polygons P value t [1] [n].

T[1][n]=min{t[1][k]+t[k+1][n]+w(V0VkVn)|1<=k<n}

Degradation polygon set {Vi-1, Vi} having a weight of 0, because at the moment is not enough polygon.

Third,  pseudocode description and Complexity Analysis

Pseudo-code :

Public class triangle{

    Initialization of connected points twenty-two weight;

    Initialize all weights two vertices of the polygon is 0;

    // Loop Solution t [i] [j]

    For (beginning and end vertices of the polygon sub-step interval length) {

       For (starting position) {

           This layer corresponds to the cycle starting point position of the end point;

           Solving t [i] [j];

           Recording a third position;

           Find the right position of the minimum value;

       }

    }

}

Complexity analysis :

For the scale of the problem n, the number of bits required to apply the algorithm group two n * n space, so the space complexity is O (n ^ 2). Used to solve the problem for three cycles, so the time complexity is O (n ^ 3).

Fourth,  code implementation

Dynamic programming package; 

public class Triangle { 

  Private n-int; // n-polygonal 
  private int [] [] weight; the right side of the array of values // 

  public Triangle (n-int) { 
    this.n = n-; 
    this.weight new new int = [n-] [n-]; 
  } 

  public static void main (String [] args) { 
	Triangle Triangulation new new Triangle = (. 6); 
    initTriangulation (Triangulation); 
    int = n-triangulation.getN (); // number of sides of the convex polygon 
    int [] [] t = new int [n] [n]; // t [i] [j] denotes the vertex {Vi-1, Vi ... Vj } polygonal split triangles optimal weights 
    int [] [] s = new [ n] int [n]; // s [i] [j] indicates with Vj and Vi-1 position of the triangle constituting the third vertex 
    triangulation.minWeightTriangulation2 (triangulation.getN () -. 1, T, S); 
    System.out.println (T [. 1] [. 5]); 
  } 

  // initialization data array weight
  public static void initTriangulation (Triangle Triangulation) { 
    int [] [] weight = {{0, 2, 2,. 3,. 1,. 4}, {2, 0,. 1,. 5, 2,. 3}, {2,. 1, 0, 2, 1, 4}, 
        {3, 5, 2, 0, 6, 2}, {1, 2, 1, 6, 0, 1}, {4, 3, 4, 2, 1, 0} }; 
    triangulation.setWeight (weight); 
  } 

  // get the best Triangulation, n being the total number of edges -1 
  public void minWeightTriangulation (n-int, int [] [] T, int [] [] S) { 
    / / initialize all of two vertices of a polygon with a weight of 0 
    for (int I =. 1; I <= n-; I ++) { 
      T [I] [I] = 0; 
    } 
    // solving cycle T [I] [J] 
    for ( int r = 2; r <= n; r ++) {// (ji) in the range [2, n-] 
      // when r = 2, is in fact a loop forming edge value t, i.e., two adjacent a weight vertices, for example, T [. 1] [2], T [2] [. 3] ...  
      for (i = int. 1; i <= n--. 1 + R & lt; i ++) {// i ranges [ 1, n + 1-r] , where i to ensure i + r <= n
        int R & lt J = I + -. 1;
        t [i] [j] = t [i + 1] [j] + getWeight (i - 1, i, j); // this is actually I = K 
        // T [I] [J] = T [ I] [I] + T [I +. 1] [J] + getWeight (I -. 1, I, J) 
        S [I] [J] = I; 
        //. 1-I, I, J 
        // cycle k, range [i + 1, j-1 ], to obtain the minimum T [i] [j] 
        for (int i = K +. 1; K <j; K ++) {// K is intermediate between i and j vertex 
          int u = t [i] [ k] + t [k + 1] [j] + getWeight (i - 1, k, j); // divided by k as the weight obtained 
          if (u <t [i ] [j]) {// If the weight is smaller, then simultaneously update t [i] [j] and S [I] [J] 
            t [i] [j] = U; 
            S [I] [J] = K; 
          } 
        } 
      } 
    } 
  } 
 
  // my writing, in a second cycle different here, there is no difference, but I readily appreciated I
  public void minWeightTriangulation2 (n-int, int [] [] T, int [] [] S) { 
    // initialize all two vertices of a polygon with a weight of 0 
    for (int I =. 1; I <= n-; I ++) { 
      T [I] [I] = 0; 
    } 
    // solving cycle T [I] [J] 
    for (int r = 1; R & lt <n-=; R & lt ++) {// r = (JI) in the range [1, n-] 
      // when r = 1, the loop is effectively in the two vertices weights assigned to the edges of the value of t, i.e. adjacent, for example, t [. 1] [2], t [2] [. 3], t [. 3] [. 4] ... 
      for (I int = 1; i <= nr; i ++) {// i the range of [1, nr], where i = i + to ensure R & lt J <= n- 
        int i = J + R & lt; 
        T [i] [J] = t [i + 1] [ j] + getWeight (i - 1, i, j); // this is actually I = K 
        // T [I] [J] = T [I] [I] + T [I +. 1] [J] + getWeight (I -. 1, I, J) 
        S [I] [J] = I; I-//. 1, I, J 
        // cycle k, the range [i + 1, j-1], to obtain the minimum T [i] [j] 
        for (int i = K +. 1; K <j; K ++) {// K is intermediate between the vertices i and j
          int u = t [i] [ k] + t [k + 1] [j] + getWeight (i - 1, k, j); // as the weight divided by k to obtain the  
          if (u <t [i] [j]) {/ / If the weight is smaller, then simultaneously update t [i] [j] and s [i] [j]
            t [i] [j] = U; 
            S [I] [J] = K; 
          } 
        } 
      } 
    } 
  } 

  // compute a sum of weights triangle 
  public int getWeight (int I, J int, int K) { 
    return weight [I] [J] + weight [J] [K] + weight [I] [K]; 
  } 

  public int for getN () { 
    return n-; 
  } 

  public void for setN (n-int) { 
    this.n n-=; 
  } 

  public int [] [] getWeight ( ) { 
    return weight; 
  } 

  public void setWeight (int [] [] weight) { 
    this.weight = weight; 
  } 

}

Fifth, the  code is run results screenshot

 

Guess you like

Origin www.cnblogs.com/LieYanAnYing/p/11817166.html