Matrix chain multiplication table5/24/2023 So we try all combinations of k and calculate m but for that we also need to calculate m and m i.e. This is what dynamic programming is all about. Matrix Ai has dimension p x p for i = 1.n A and A, where p contains the dimensions of the matrices.Ī is a 10 × 30 matrix, B is a 30 × 5 matrix, and C is a 5 × 60 matrix. P*p*p //Final cost of multiplying the two partitions i.e. So the cost of multiplication of A for this partition of k is: m //Minimum cost of multiplication of A So for any k we'll have two partitions: A and A So clearly k should vary from i to j-1 which is reflected in the loop as we try all possible combinations and take the most efficient one. We don't know where it should be, hence we try all possibilities and pick the combination where m is minimized. So at a very high level if i=1 and j=3 and the matrices are : (A*B)*C //We are trying to establish where the outer most parenthesis should be we are trying to compute m or the most efficient way to multiply matrices A and k is the variable. To understand the above stand, first let's establish that i and j are fixed here i.e. prioritize how we chain our matrices so that they are multiplied most efficiently and it's reflected in this line of code: q = m + m + p*p*p The crux of the problem is we are basically trying to 'parenthesise' i.e. I'll try to explain you how I understood the solution. I was just going through the algorithm definition on wikipedia and it's pretty comprehensive there.
0 Comments
Leave a Reply. |