Question
This is only one question, I explained the question in the detailed formated with instructions and example, you only have to edit the given code
This is only one question, I explained the question in the detailed formated with instructions and example, you only have to edit the given code to get the similar output in the example. Please help answer it in step by step format. Thank you!!
Dynamic programming algorithms are an important class of algorithms where a complex optimization problem is divided into subproblems, and the subproblems are tackled recursively. They have many applications throughout computer science such as computer networks and also in genome sequencing.
Here we will study how dynamic programming is used to optimally multiply a chain of matrices such that the fewest number of multiplication operations are needed.Links to an external site. The matrix chain multiplication problem is to multiply at least three matrices, such as A*B*C*D. Because matrix multiplication is associative, one can perform this multiplication in one of several orders of operation:
A(B(CD))
A((BC)D)
(AB)(CD)
(A(BC))D
((AB)C)D
Depending on the dimensions of the matrices A, B, C, and D, the number of multiplications needed may vary greatly depending on how you parenthesize the matrix chain multiplication problem.
The dynamic programming approach to find the parenthesization that needs the fewest multiplication operations is to break the problem into subproblems, recursively find the cost of each subproblem, and then select among the subproblems for the most optimal approach. To be more concrete, suppose we want to perform that matrix chain multiplication of ABCD. For the first decision we would select among three ways to partition the problem:
A(BCD)
(AB)(CD)
(ABC)D
Now, the minimum cost of calculating BCD cannot be determined directly, so we have to recursively determine the cost of calculating B(CD) vs. (BC)D in order to determine the true cost of option 1.
Likewise, the minimum cost of calculating ABC cannot be determined directly, so we have to recursively determine the cost of calculating A(BC) vs. (AB)C in order to determine the true cost of option 3.
Multiply a chain of matrices with an optimal number of multiplication operations.
Input Example:
3 3 2 0 2 0 2 1 1 2 1 1 1 1 2 2 3
**1. The first line is the number matrices in the chain.
2. The second line is the row and column dimensions of the first matrix, separated by a space.
3. The subsequent several lines are the contents of the first matrix, one row per line, and each column element separated by a space in each line.
4. After the first matrix is complete, the second matrix begins with a single line describing the row and column dimensions of the second matrix, and so on.
Output:
12 4 6 4 6 4 6
Write a C program that takes as a command line input the path to the test case file:
./matChainMul tests/test0.txt
Then, you should use a dynamic programming algorithm to calculate the product while using as few multiplication operations as possible.
matMul.h code:
#include | |
#include | |
#include | |
#include | |
size_t mulOpCount = 0; | |
bool set = false; | |
void printLog ( void ) { | |
FILE *fp = fopen("mul_op_count.txt", "w"); | |
fprintf(fp, "%ld", mulOpCount); | |
fclose(fp); | |
} | |
unsigned int mul ( | |
unsigned int multiplier, | |
unsigned int multiplicand | |
) { | |
if (!set) { | |
atexit(printLog); | |
set = true; | |
} | |
mulOpCount++; | |
return multiplier * multiplicand; | |
} | |
void matMul ( | |
unsigned int l, | |
unsigned int m, | |
unsigned int n, | |
int** matrix_a, | |
int** matrix_b, | |
int** matMulProduct | |
) { | |
// printf("l=%d ", l); | |
// printf("m=%d ", m); | |
// printf("n=%d ", n); | |
for ( unsigned int i=0; i | |
// printf("i=%d ", i); | |
for ( unsigned int k=0; k | |
// printf("k=%d ", k); | |
matMulProduct[i][k] = 0; | |
for ( unsigned int j=0; j | |
matMulProduct[i][k] += mul ( matrix_a[i][j], matrix_b[j][k] ); | |
} | |
} | |
} | |
} |
Edit the code given below:
#include
#include
#include
#include
unsigned int cost (
unsigned int matrixCount,
unsigned int* rowSizes,
unsigned int* colSizes
) {
if ( matrixCount==1 ) { // Base case.
return 0; // No multplication to be done.
} else {
unsigned int numPossibleSplits = matrixCount-1; // Think: if there are two matrices to multiply, there is one way to split.
// AB: (A)(B)
// ABC: (A)(BC) and (AB)(C)
unsigned int costs[numPossibleSplits];
for ( unsigned int split=0; split unsigned int l = rowSizes[0]; assert ( colSizes[split] == rowSizes[split+1] ); unsigned int m = colSizes[split]; unsigned int n = colSizes[matrixCount-1]; costs[split] = cost( split+1, rowSizes, colSizes ) + // cost of left subchain l * m * n + // cost of multiplying the two chains cost( matrixCount-split-1, rowSizes+split+1, colSizes+split+1 ); // cost of right subchain } unsigned int minCost = costs[0]; for ( unsigned int split=1; split if ( costs[split] minCost = costs[split]; } } return minCost; } } int main(int argc, char* argv[]) { unsigned int matrixCount; unsigned int* rowSizes; unsigned int* colSizes; int*** matrices; FILE* fp = fopen(argv[1], "r"); if (!fp) { perror("fopen failed"); exit(EXIT_FAILURE); } if (!fscanf(fp, "%d ", &matrixCount)) { perror("reading the number of matrices failed"); exit(EXIT_FAILURE); } rowSizes = calloc( matrixCount, sizeof(int) ); colSizes = calloc( matrixCount, sizeof(int) ); matrices = calloc( matrixCount, sizeof(int**) ); for (unsigned int matIndex=0; matIndex unsigned int rows, cols; if (!fscanf(fp, "%d %d ", &rows, &cols)) { perror("reading the dimensions of matrix failed"); exit(EXIT_FAILURE); } rowSizes[matIndex] = rows; colSizes[matIndex] = cols; matrices[matIndex] = calloc( rows, sizeof(int*) ); for ( unsigned int i=0; i matrices[matIndex][i] = calloc( cols, sizeof(int) ); for ( unsigned int k=0; k int element; if (!fscanf(fp, "%d ", &element)) { perror("reading the element of matrix failed"); exit(EXIT_FAILURE); } matrices[matIndex][i][k] = element; } } } unsigned int mul_op_count = cost ( matrixCount, rowSizes, colSizes ); printf("%d ", mul_op_count ); for (unsigned int matIndex=0; matIndex int rows = rowSizes[matIndex]; for ( unsigned int i=0; i free(matrices[matIndex][i]); } free(matrices[matIndex]); } free(matrices); free(colSizes); free(rowSizes); fclose(fp); exit(EXIT_SUCCESS); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started