Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
matrix_chain_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
31
37# include <gtest/gtest.h>
38
39# include <limits>
40# include <random>
41
42# include <Matrix_Chain.H>
43
44using namespace Aleph;
45
46namespace
47{
48 size_t brute_cost(const Array<size_t> & dims, const size_t i, const size_t j)
49 {
50 if (i == j)
51 return 0;
52
53 size_t best = std::numeric_limits<size_t>::max();
54 for (size_t k = i; k < j; ++k)
55 {
56 const size_t left = brute_cost(dims, i, k);
57 const size_t right = brute_cost(dims, k + 1, j);
58 const size_t here = dims[i] * dims[k + 1] * dims[j + 1];
59 const size_t total = left + right + here;
60 if (total < best)
61 best = total;
62 }
63 return best;
64 }
65
66 size_t split_cost(const Array<size_t> & dims,
67 const Array<Array<size_t>> & split,
68 const size_t i, const size_t j)
69 {
70 if (i == j)
71 return 0;
72 const size_t k = split[i][j];
73 return split_cost(dims, split, i, k)
74 + split_cost(dims, split, k + 1, j)
75 + dims[i] * dims[k + 1] * dims[j + 1];
76 }
77} // namespace
78
79
81{
82 // CLRS classic: dims = {30, 35, 15, 5, 10, 20, 25} -> 15125
83 Array<size_t> dims = {30, 35, 15, 5, 10, 20, 25};
85 EXPECT_EQ(r.min_multiplications, 15125u);
86 EXPECT_FALSE(r.parenthesization.empty());
87}
88
90{
91 Array<size_t> dims = {10, 20};
93 EXPECT_EQ(r.min_multiplications, 0u);
94 EXPECT_EQ(r.parenthesization, "A1");
95}
96
98{
99 Array<size_t> dims = {10, 20, 30};
100 auto r = matrix_chain_order(dims);
101 EXPECT_EQ(r.min_multiplications, 6000u); // 10*20*30
102}
103
105{
106 // A1: 10x30, A2: 30x5, A3: 5x60
107 // (A1 A2) A3: 10*30*5 + 10*5*60 = 1500 + 3000 = 4500
108 // A1 (A2 A3): 30*5*60 + 10*30*60 = 9000 + 18000 = 27000
109 Array<size_t> dims = {10, 30, 5, 60};
110 auto r = matrix_chain_order(dims);
111 EXPECT_EQ(r.min_multiplications, 4500u);
112}
113
115{
116 Array<size_t> dims = {30, 35, 15, 5, 10, 20, 25};
118}
119
121{
123 EXPECT_THROW(matrix_chain_order(too_short), std::domain_error);
124
125 Array<size_t> empty;
126 EXPECT_THROW(matrix_chain_order(empty), std::domain_error);
127
128 Array<size_t> zero_dim = {10, 0, 5};
129 EXPECT_THROW(matrix_chain_order(zero_dim), std::domain_error);
130}
131
133{
134 Array<size_t> dims = {30, 35, 15, 5, 10, 20, 25};
135 auto r = matrix_chain_order(dims);
136
137 // Count that parenthesization contains A1..A6
138 for (int i = 1; i <= 6; ++i)
139 {
140 std::string name = "A" + std::to_string(i);
141 EXPECT_NE(r.parenthesization.find(name), std::string::npos)
142 << "Missing " << name << " in: " << r.parenthesization;
143 }
144
145 // Count balanced parentheses
146 int depth = 0;
147 for (char c : r.parenthesization)
148 {
149 if (c == '(') ++depth;
150 else if (c == ')') --depth;
151 EXPECT_GE(depth, 0);
152 }
153 EXPECT_EQ(depth, 0);
154}
155
157{
158 // All square matrices of same size: any order is equally optimal
159 Array<size_t> dims = {5, 5, 5, 5, 5};
160 auto r = matrix_chain_order(dims);
161 // 4 matrices, each 5x5. Any parenthesization costs (n-1)*5^3 = 3*125 = 375
162 EXPECT_EQ(r.min_multiplications, 375u);
163}
164
166{
167 Array<size_t> dims = {9, 4, 8, 3, 6, 7};
168 auto r = matrix_chain_order(dims);
169 const size_t n = dims.size() - 1;
170 EXPECT_EQ(split_cost(dims, r.split, 0, n - 1), r.min_multiplications);
171}
172
174{
175 std::mt19937 rng(20260226);
176 for (int trial = 0; trial < 120; ++trial)
177 {
178 const size_t n = 2 + rng() % 6; // 2..7 matrices
180 dims.reserve(n + 1);
181 for (size_t i = 0; i <= n; ++i)
182 dims.append(1 + static_cast<size_t>(rng() % 25));
183
184 const auto r = matrix_chain_order(dims);
185 const size_t brute = brute_cost(dims, 0, n - 1);
186 EXPECT_EQ(r.min_multiplications, brute);
187 EXPECT_EQ(split_cost(dims, r.split, 0, n - 1), r.min_multiplications);
188 }
189}
190
192{
193 const size_t M = std::numeric_limits<size_t>::max();
194 Array<size_t> dims = {M, M, M};
195 EXPECT_THROW(matrix_chain_order(dims), std::runtime_error);
196}
Matrix-chain multiplication ordering via interval DP.
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:139
void reserve(size_t cap)
Reserves cap cells into the array.
Definition tpl_array.H:315
#define TEST(name)
static mt19937 rng
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Matrix_Chain_Result matrix_chain_order(const Array< size_t > &dims)
Compute optimal matrix-chain multiplication order.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
size_t matrix_chain_min_cost(const Array< size_t > &dims)
Compute only the minimum multiplication cost.
std::vector< std::string > & split(const std::string &s, const char delim, std::vector< std::string > &elems)
Split a std::string by a single delimiter character.
static int * k
gsl_rng * r