Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
matrix_chain_example.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 <iostream>
38# include <iomanip>
39
40# include <Matrix_Chain.H>
41# include <print_rule.H>
42
43using namespace Aleph;
44
45namespace
46{
55 void print_dims(const Array<size_t> & dims)
56 {
57 std::cout << "Matrices: ";
58 for (size_t i = 0; i + 1 < dims.size(); ++i)
59 {
60 if (i > 0)
61 std::cout << " ";
62 std::cout << "A" << (i + 1)
63 << "(" << dims[i] << "x" << dims[i + 1] << ")";
64 }
65 std::cout << "\n";
66 }
67
79 void run_case(const Array<size_t> & dims, const char * title)
80 {
81 std::cout << title << "\n";
82 print_rule();
84 const auto r = matrix_chain_order(dims);
85 std::cout << std::setw(26) << std::left << "Minimum multiplications"
86 << ": " << r.min_multiplications << "\n";
87 std::cout << std::setw(26) << std::left << "Optimal parenthesization"
88 << ": " << r.parenthesization << "\n";
89 print_rule();
90 std::cout << "\n";
91 }
92}
93
105int main()
106{
107 std::cout << "\n=== Matrix-Chain Dynamic Programming ===\n\n";
108
109 run_case({30, 35, 15, 5, 10, 20, 25}, "Scenario A: CLRS reference chain");
110 run_case({10, 20, 30}, "Scenario B: Two matrices");
111 run_case({10, 30, 5, 60}, "Scenario C: Three matrices (high asymmetry)");
112 run_case({5, 5, 5, 5, 5}, "Scenario D: Uniform square matrices");
113
114 std::cout << "Scenario E: Cost-only helper\n";
115 print_rule();
116 Array<size_t> dims = {40, 20, 30, 10, 30};
118 std::cout << std::setw(26) << std::left << "matrix_chain_min_cost"
119 << ": " << matrix_chain_min_cost(dims) << "\n";
120 print_rule();
121
122 std::cout << "\nDone.\n";
123 return 0;
124}
Matrix-chain multiplication ordering via interval DP.
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:139
int main()
Example driver demonstrating matrix-chain dynamic programming routines.
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.
void print_rule()
Prints a horizontal rule for example output separation.
Definition print_rule.H:39
size_t matrix_chain_min_cost(const Array< size_t > &dims)
Compute only the minimum multiplication cost.
gsl_rng * r