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 <stdio.h>
#include <limits.h>
#include <stdlib.h>
Include dependency graph for matrix_chain_order.c:

Functions

int matrixChainOrder (int l, const int *p, int *s)
 for assert
 
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
88 {
89 test(); // run self-test implementations
90 return 0;
91}
static void test()
Self-test implementations.
Definition: matrix_chain_order.c:70
Here is the call graph for this function:

◆ matrixChainOrder()

int matrixChainOrder ( int  l,
const int *  p,
int *  s 
)

for assert

for IO operations for INT_MAX macro 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
24 {
25 // mat stores the cost for a chain that starts at i and ends on j (inclusive on both ends)
26 int mat[l][l];
27 for (int i = 0; i < l; ++i) {
28 mat[i][i] = 0;
29 }
30 // cl denotes the difference between start / end indices, cl + 1 would be chain length.
31 for (int cl = 1; cl < l; ++cl) {
32 for (int i = 0; i < l - cl; ++i) {
33 int j = i + cl;
34 mat[i][j] = INT_MAX;
35 for (int div = i; div < j; ++div) {
36 int q = mat[i][div] + mat[div + 1][j] + p[i] * p[div] * p[j];
37 if (q < mat[i][j]) {
38 mat[i][j] = q;
39 s[i * l + j] = div;
40 }
41 }
42 }
43 }
44 return mat[0][l - 1];
45}

◆ 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
55 {
56 if(i == j) {
57 printf("A%d",i);
58 return
59 }
60 putchar('(');
61 printSolution(l,s,i,s[i * l + j]);
62 printSolution(l,s,s[i * l + j] + 1,j);
63 putchar(')');
64}
void printSolution(int l, int *s, int i, int j)
Recursively prints the solution.
Definition: matrix_chain_order.c:55
Here is the call graph for this function:

◆ test()

static void test ( void  )
static

Self-test implementations.

Returns
void
70 {
71 int sizes[] = {35,15,5,10,20,25};
72 int len = 6;
73 int *sol = malloc(len * len * sizeof(int));
74 int r = matrixChainOrder(len,sizes,sol);
75 assert(r == 18625);
76 printf("Result : %d\n",r);
77 printf("Optimal ordering : ");
78 printSolution(len,sol,0,5);
79 free(sol);
80
81 printf("\n");
82}
#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
int matrixChainOrder(int l, const int *p, int *s)
for assert
Definition: matrix_chain_order.c:24
Here is the call graph for this function: