�������� Design and Analysis of Algorithms ������������������� ������ �����ก��� ������ ��� �������� ������������������� ����������������� ��. ��. ����� �������������ก�� ����������������������������ก������� ������ก�� �����������ก�������������� ��������������� �����������กก��������ก��������� ������ก������������� 2542 http://www.cp.eng.chula.ac.th/faculty/spj Outline Dynamic Programming � Optimal Polygon Triangualation Problem � Polygon Triangulation and Parenthesization � Optimal Polygon Triangulation and Optimal Polygon Triangulation Matrix-Chain Multiplication http://www.cp.eng.chula.ac.th/faculty/spj Optimal Polygon Triangulation Triangulations Input Output http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj
Optimal Polygon Triangulation Triangulation � � Parenthesization v 6 � Input : a convex polygon P = < v 0 , v 0 , ..., v n -1 >, v 0 A 6 v 5 a weight function w defined on triangles formed A 1 by sides and chords of P . v 1 A 5 e.g., w ( � v i v j v k ) = | v i v j | + | v j v k | + | v k v i | v 4 A 2 A 4 � Output : find a triangulation that minimizes the v 2 v 3 A 3 sum of the weights of the triangles in the triangulation. http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Triangulation � � Parenthesization Triangulation � � Parenthesization v 6 v 0 A 6 v 0 A 6 v 5 A 1 A 1 v 1 A 5 v 1 A 5 v 4 v 4 A 2 A 2 A 4 A 4 v 2 v 2 v 3 v 3 A 3 A 3 http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Triangulation � � Parenthesization Triangulation � � Parenthesization v 0 A 6 A 6 A 1 A 1 v 1 A 5 A 5 v 4 v 4 A 2 A 2 A 4 A 4 v 2 v 2 v 3 v 3 A 3 A 3 http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj
Triangulation � � Parenthesization Matrix-Chain Multiplication Matrix-Chain-Order( p, n ) { A 6 for i = 1 to n m[i,i] = 0 A 1 for len = 2 to n A 5 for i = 1 to n - len + 1 j = i + len - 1 A 5 A 6 m[i,j] = � A 2 for k = i to j-1 A 4 A 1 A 2 A 3 A 4 q = m[i,k] + m[k+1,j] + p[i-1]*p[k]*p[j] A 3 if q < m[i,j] then m[i,j] = q ( ( ( A 1 A 2 )( A 3 A 4 ) ) ( A 5 A 6 ) ) s[i,j] = k return s } http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Optimal Polygon Triangulation Polygon_Triangulate( v, n ) { for i = 1 to n m[i,i] = 0 for len = 2 to n for i = 1 to n - len + 1 j = i + len - 1 m[i,j] = � for k = i to j-1 q = m[i,k] + m[k+1,j] + w(v[i-1],v[k],v[j]) if q < m[i,j] then m[i,j] = q s[i,j] = k return s } http://www.cp.eng.chula.ac.th/faculty/spj
Recommend
More recommend