Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
matrix_chain_order.c File Reference

Matrix Chain Order More...

#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
Include dependency graph for matrix_chain_order.c:

Functions

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.
 

Detailed Description

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

Function Documentation

◆ main()

int main ( void  )

Main function.

Returns
0
116{
117 test(); // run self-test implementations
118 return 0;
119}
static void test()
Self-test implementations.
Definition matrix_chain_order.c:96
Here is the call graph for this function:

◆ 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
llength of cost array
pcosts of each matrix
slocation to store results
Returns
number of operations
27{
28 // mat stores the cost for a chain that starts at i and ends on j (inclusive
29 // on both ends)
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 // cl denotes the difference between start / end indices, cl + 1 would be
41 // chain length.
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 // Free dynamically allocated memory
62 for (int i = 0; i < l; ++i)
63 {
64 free(mat[i]);
65 }
66 free(mat);
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
ldimension of the solutions array
ssolutions
istarting index
jending index
Returns
void
80{
81 if (i == j)
82 {
83 printf("A%d", i);
84 return;
85 }
86 putchar('(');
87 printSolution(l, s, i, s[i * l + j]);
88 printSolution(l, s, s[i * l + j] + 1, j);
89 putchar(')');
90}
void printSolution(int l, int *s, int i, int j)
Recursively prints the solution.
Definition matrix_chain_order.c:79
Here is the call graph for this function:

◆ 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));
101 int r = matrixChainOrder(len, sizes, sol);
102 assert(r == 18625);
103 printf("Result : %d\n", r);
104 printf("Optimal ordering : ");
105 printSolution(len, sol, 0, 5);
106 free(sol);
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
Here is the call graph for this function: