DP8 矩阵链相乘 Matrix Chain Multiplication @geeksforgeeks

思路是在矩阵链的每一个地方分别分开,然后找最小

Given a sequence of matrices, find the most efficient way to multiply these matrices together.The problem is not actually to perform the multiplications, but merely to decide in which order to perform the multiplications.

We have many options to multiply a chain of matrices because matrix multiplication is associative. In other words, no matter how we parenthesize the product, the result will be the same. For example, if we had four matrices A, B, C, and D, we would have:

    (ABC)D = (AB)(CD) = A(BCD) = ....

However, the order in which we parenthesize the product affects the number of simple arithmetic operations needed to compute the product, or the efficiency. For example, suppose A is a 10 × 30 matrix, B is a 30 × 5 matrix, and C is a 5 × 60 matrix. Then,

    (AB)C = (10×30×5) + (10×5×60) = 1500 + 3000 = 4500 operations
    A(BC) = (30×5×60) + (10×30×60) = 9000 + 18000 = 27000 operations.

Clearly the first method is the more efficient.

Given an array p[] which represents the chain of matrices such that the ith matrix Ai is of dimension p[i-1] x p[i]. We need to write a function MatrixChainOrder() that should return the minimum number of multiplications needed to multiply the chain.

  Input: p[] = {40, 20, 30, 10, 30}   
  Output: 26000  
  There are 4 matrices of dimensions 40x20, 20x30, 30x10 and 10x30.
  Let the input 4 matrices be A, B, C and D.  The minimum number of 
  multiplications are obtained by putting parenthesis in following way
  (A(BC))D --> 20*30*10 + 40*20*10 + 40*10*30

  Input: p[] = {10, 20, 30, 40, 30} 
  Output: 30000 
  There are 4 matrices of dimensions 10x20, 20x30, 30x40 and 40x30. 
  Let the input 4 matrices be A, B, C and D.  The minimum number of 
  multiplications are obtained by putting parenthesis in following way
  ((AB)C)D --> 10*20*30 + 10*30*40 + 10*40*30

  Input: p[] = {10, 20, 30}  
  Output: 6000  
  There are only two matrices of dimensions 10x20 and 20x30. So there 
  is only one way to multiply the matrices, cost of which is 10*20*30

1) Optimal Substructure:
A simple solution is to place parenthesis at all possible places, calculate the cost for each placement and return the minimum value. In a chain of matrices of size n, we can place the first set of parenthesis in n-1 ways. For example, if the given chain is of 4 matrices. let the chain be ABCD, then there are 3 way to place first set of parenthesis: A(BCD), (AB)CD and (ABC)D. So when we place a set of parenthesis, we divide the problem into subproblems of smaller size. Therefore, the problem has optimal substructure property and can be easily solved using recursion.

Minimum number of multiplication needed to multiply a chain of size n = Minimum of all n-1 placements (these placements create subproblems of smaller size)

2) Overlapping Subproblems
Following is a recursive implementation that simply follows the above optimal substructure property.



package DP;

public class MatrixChainMultiplication {

	public static void main(String[] args) {
		int[] dm = {1, 2, 3, 4};
		int begin = 1;
		int end = dm.length-1;
		System.out.println(matrixChainOrderRec(dm, begin, end));
		System.out.println(matrixChainOrderDP(dm, dm.length));
	}

	// Matrix Ai has dimension dm[i-1] x dm[i] for i = 1..n
	public static int matrixChainOrderRec(int[] dm, int begin, int end){
		if(begin == end){
			return 0;
		}
		int min = Integer.MAX_VALUE;
		
		// place parenthesis at different places between first and last matrix,
	    // recursively calculate count of multiplications for each parenthesis 
	    // placement and return the minimum count
		for(int k=begin; k<end; k++){
			int count = matrixChainOrderRec(dm, begin, k) +
							 matrixChainOrderRec(dm, k+1, end) +
							 dm[begin-1]*dm[k]*dm[end];
			min = Math.min(min, count);
		}
		return min;
	}
	
	// Matrix Ai has dimension dm[i-1] x dm[i] for i = 1..n
	// Time Complexity: O(n^3), Auxiliary Space: O(n^2)
	public static int matrixChainOrderDP(int[] dm, int n){
		/* For simplicity of the program, one extra row and one extra column are
	       allocated in dp[][].  0th row and 0th column of m[][] are not used */
		int[][] dp = new int[n+1][n+1];
		/* dp[i,j] = Minimum number of scalar multiplications needed to compute
	       the matrix A[i]A[i+1]...A[j] = A[i..j] where dimension of A[i] is p[i-1] x p[i] */
		
		// cost is zero when multiplying one matrix.
		for(int i=1; i<n; i++){
			dp[i][i] = 0;
		}
		
		// L is chain length. L starts from 2
		for(int L=2; L<n; L++){
			for(int begin=1; begin<n-L+1; begin++){
				int end = begin+L-1;
				dp[begin][end] = Integer.MAX_VALUE;
				for(int k=begin; k<=end-1; k++){
					// q = cost/scalar multiplications
					int q = dp[begin][k] + dp[k+1][end] + dm[begin-1]*dm[k]*dm[end];
					dp[begin][end] = Math.min(dp[begin][end], q);
				}
			}
		}
		return dp[1][n-1];
	}
	
}

http://www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/
基于SSM框架的智能家政保洁预约系统,是一个旨在提高家政保洁服务预约效率和管理水平的平台。该系统通过集成现代信息技术,为家政公司、家政服务人员和消费者提供了一个便捷的在线预约和管理系统。 系统的主要功能包括: 1. **用户管理**:允许消费者注册、登录,并管理他们的个人资料和预约历史。 2. **家政人员管理**:家政服务人员可以注册并更新自己的个人信息、服务类别和服务时间。 3. **服务预约**:消费者可以浏览不同的家政服务选项,选择合适的服务人员,并在线预约服务。 4. **订单管理**:系统支持订单的创建、跟踪和管理,包括订单的确认、完成和评价。 5. **评价系统**:消费者可以在家政服务完成后对服务进行评价,帮助提高服务质量和透明度。 6. **后台管理**:管理员可以管理用户、家政人员信息、服务类别、预约订单以及处理用户反馈。 系统采用Java语言开发,使用MySQL数据库进行数据存储,通过B/S架构实现用户与服务的在线交互。系统设计考虑了不同用户角色的需求,包括管理员、家政服务人员和普通用户,每个角色都有相应的权限和功能。此外,系统还采用了软件组件化、精化体系结构、分离逻辑和数据等方法,以便于未来的系统升级和维护。 智能家政保洁预约系统通过提供一个集中的平台,不仅方便了消费者的预约和管理,也为家政服务人员提供了一个展示和推广自己服务的机会。同时,系统的后台管理功能为家政公司提供了强大的数据支持和决策辅助,有助于提高服务质量和管理效率。该系统的设计与实现,标志着家政保洁服务向现代化和网络化的转型,为管理决策和控制提供保障,是行业发展中的重要里程碑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值