The problem of optimal triangular division of Convex Polygon

Source: Internet
Author: User
See the book "algorithm design and analysis" Wang Xiaodong Dynamic Planning 1. problem description (Note: it is the sum of weights of all triangles, not the sum of weights of edges and chords) 2. analysis 3. encoding implementation :/**
* @ Author: Hu Jiawei
* @ Createtime: 12:31:16
* @ Description: optimal triangular division of a convex polygon.
*/

PackageEx2;

Public ClassTriangulation {

Private IntN; // n Polygon
Private Int[] [] Weight; // edge weight array

PublicTriangulation (IntN ){
This. N = N;
This. Weight =New Int[N] [N];
}

Public Static VoidMain (string [] ARGs ){
Triangulation triangulation =NewTriangulation (6 );
Inittriangulation (Triangulation );
IntN = triangulation. getn (); // Number of edges of a convex polygon
Int[] [] T =New Int[N] [N]; // T [I] [J] indicates the weight of the optimal triangle partitioning of a polygon consisting of a vertex {Vi-1, vi... VJ}
Int[] [] S =New Int[N] [N]; // s [I] [J] represents the position of the third vertex of the triangle together with the Vi-1 and vj
Triangulation. minweighttriangulation2 (triangulation. getn ()-1, t, s );
System. Out. println (T [1] [5]);
}

// Initialize weight array information
Public Static VoidInittriangulation (triangulation 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 );
}

// Obtain the optimal triangle partitioning. n is the total number of edges-1.
Public VoidMinweighttriangulation (IntN,Int[] [] T,Int[] [] S ){
// Initialize all the two-vertex polygon with a weight of 0
For(IntI = 1; I <= N; I ++ ){
T [I] [I] = 0;
}
// Cyclically solving T [I] [J]
For(IntR = 2; r <= N; r ++) {// (J-I) range [2, N]
// When r = 2, the loop is actually assigning edge values to T, that is, the weights of two adjacent vertices, such as t [1] [2], T [2] [3]...
For(IntI = 1; I <= N-R + 1; I ++) {// I range [1, N + 1-r], here I want to ensure I + r <= N
IntJ = I + R-1;
T [I] [J] = T [I + 1] [J] + getweight (I-1, I, j); // here actually K = I
// T [I] [J] = T [I] [I] + T [I + 1] [J] + getweight (I-1, I, j)
S [I] [J] = I;
// I-1, I, j
// Loop K, range is [I + 1, J-1], find the smallest T [I] [J]
For(IntK = I + 1; k <j; k ++) {// K is the intermediate vertex between I and j
IntU = T [I] [k] + T [k + 1] [J] + getweight (I-1, K, J ); // use K as the weight.
If(U <t [I] [J]) {// if the weight is smaller, update T [I] [J] And s [I] [J] at the same time.
T [I] [J] = u;
S [I] [J] = K;
}
}
}
}
}

// My writing method is different in the second loop. There is no difference, but it is easy for me to understand.
Public VoidMinweighttriangulation2 (IntN,Int[] [] T,Int[] [] S ){
// Initialize all the two-vertex polygon with a weight of 0
For(IntI = 1; I <= N; I ++ ){
T [I] [I] = 0;
}
// Cyclically solving T [I] [J]
For(IntR = 1; r <= N; r ++) {// R = (J-I) range [1, N]
// When r = 1, the loop is actually assigning edge values to T, that is, the weights of two adjacent vertices, such as t [1] [2], T [2] [3], t [3] [4]...
For(IntI = 1; I <= N-R; I ++) {// I range [1, n-R], here I want to ensure that J = I + r <= N
IntJ = I + R;
T [I] [J] = T [I + 1] [J] + getweight (I-1, I, j); // here actually K = I
// T [I] [J] = T [I] [I] + T [I + 1] [J] + getweight (I-1, I, j)
S [I] [J] = I; // I-1, I, j
// Loop K, range is [I + 1, J-1], find the smallest T [I] [J]
For(IntK = I + 1; k <j; k ++) {// K is the intermediate vertex between I and j
IntU = T [I] [k] + T [k + 1] [J] + getweight (I-1, K, J ); // use K as the weight.
If(U <t [I] [J]) {// if the weight is smaller, update T [I] [J] And s [I] [J] at the same time.
T [I] [J] = u;
S [I] [J] = K;
}
}
}
}
}

// Calculate the sum of the weights of a triangle
Public IntGetweight (IntI,IntJ,IntK ){
ReturnWeight [I] [J] + weight [J] [k] + weight [I] [k];
}

Public IntGetn (){
ReturnN;
}

Public VoidSetn (IntN ){
This. N = N;
}

Public Int[] [] Getweight (){
ReturnWeight;
}

Public VoidSetweight (Int[] [] Weight ){
This. Weight = weight;
}

}

Data: Result: 24

Publish via wiz

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.