Matrix Chain Order
More...
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
|
int | matrixChainOrder (int l, const int *p, int *s) |
| for assert for INT_MAX macro
|
|
void | printSolution (int l, int *s, int i, int j) |
| Recursively prints the solution.
|
|
static void | test () |
| Self-test implementations.
|
|
int | main () |
| Main function.
|
|
Matrix Chain Order
From Wikipedia: Matrix chain multiplication (or the matrix chain ordering problem) is an optimization problem concerning the most efficient way to multiply a given sequence of matrices. The problem is not actually to perform the multiplications, but merely to decide the sequence of the matrix multiplications involved.
- Author
- CascadingCascade
◆ main()
Main function.
- Returns
- 0
116{
118 return 0;
119}
static void test()
Self-test implementations.
Definition matrix_chain_order.c:96
◆ matrixChainOrder()
int matrixChainOrder |
( |
int |
l, |
|
|
const int * |
p, |
|
|
int * |
s |
|
) |
| |
for assert for INT_MAX macro
for IO operations for malloc() and free()
Finds the optimal sequence using the classic O(n^3) algorithm.
- Parameters
-
l | length of cost array |
p | costs of each matrix |
s | location to store results |
- Returns
- number of operations
27{
28
29
30 int **mat =
malloc(l *
sizeof(
int *));
31 for (int i = 0; i < l; ++i)
32 {
33 mat[i] =
malloc(l *
sizeof(
int));
34 }
35
36 for (int i = 0; i < l; ++i)
37 {
38 mat[i][i] = 0;
39 }
40
41
42 for (int cl = 1; cl < l; ++cl)
43 {
44 for (int i = 0; i < l - cl; ++i)
45 {
46 int j = i + cl;
47 mat[i][j] = INT_MAX;
48 for (int div = i; div < j; ++div)
49 {
50 int q = mat[i][div] + mat[div + 1][j] + p[i] * p[div] * p[j];
51 if (q < mat[i][j])
52 {
53 mat[i][j] = q;
54 s[i * l + j] = div;
55 }
56 }
57 }
58 }
59 int result = mat[0][l - 1];
60
61
62 for (int i = 0; i < l; ++i)
63 {
65 }
67
68 return result;
69}
#define malloc(bytes)
This macro replace the standard malloc function with malloc_dbg.
Definition malloc_dbg.h:18
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition malloc_dbg.h:26
◆ printSolution()
void printSolution |
( |
int |
l, |
|
|
int * |
s, |
|
|
int |
i, |
|
|
int |
j |
|
) |
| |
Recursively prints the solution.
- Parameters
-
l | dimension of the solutions array |
s | solutions |
i | starting index |
j | ending index |
- Returns
- void
80{
81 if (i == j)
82 {
83 printf("A%d", i);
84 return;
85 }
86 putchar('(');
89 putchar(')');
90}
void printSolution(int l, int *s, int i, int j)
Recursively prints the solution.
Definition matrix_chain_order.c:79
◆ test()
static void test |
( |
void |
| ) |
|
|
static |
Self-test implementations.
- Returns
- void
97{
98 int sizes[] = {35, 15, 5, 10, 20, 25};
99 int len = 6;
100 int *sol =
malloc(len * len *
sizeof(
int));
102 assert(r == 18625);
103 printf("Result : %d\n", r);
104 printf("Optimal ordering : ");
107
108 printf("\n");
109}
int matrixChainOrder(int l, const int *p, int *s)
for assert for INT_MAX macro
Definition matrix_chain_order.c:26