Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
|
|
@ -0,0 +1,4 @@
|
|||
package mat_chain is
|
||||
type Vector is array (Natural range <>) of Integer;
|
||||
procedure Chain_Multiplication (Dims : Vector);
|
||||
end mat_chain;
|
||||
|
|
@ -0,0 +1,91 @@
|
|||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
|
||||
|
||||
package body mat_chain is
|
||||
type Result_Matrix is
|
||||
array (Positive range <>, Positive range <>) of Integer;
|
||||
|
||||
--------------------------
|
||||
-- Chain_Multiplication --
|
||||
--------------------------
|
||||
|
||||
procedure Chain_Multiplication (Dims : Vector) is
|
||||
n : Natural := Dims'Length - 1;
|
||||
S : Result_Matrix (1 .. n, 1 .. n);
|
||||
m : Result_Matrix (1 .. n, 1 .. n);
|
||||
procedure Print (Item : Vector) is
|
||||
begin
|
||||
Put ("Array Dimension = (");
|
||||
for I in Item'Range loop
|
||||
Put (Item (I)'Image);
|
||||
if I < Item'Last then
|
||||
Put (",");
|
||||
else
|
||||
Put (")");
|
||||
end if;
|
||||
end loop;
|
||||
New_Line;
|
||||
end Print;
|
||||
|
||||
procedure Chain_Order (Item : Vector) is
|
||||
J : Natural;
|
||||
Cost : Natural;
|
||||
Temp : Natural;
|
||||
|
||||
begin
|
||||
for idx in 1 .. n loop
|
||||
m (idx, idx) := 0;
|
||||
end loop;
|
||||
|
||||
for Len in 2 .. n loop
|
||||
for I in 1 .. n - Len + 1 loop
|
||||
J := I + Len - 1;
|
||||
m (I, J) := Integer'Last;
|
||||
for K in I .. J - 1 loop
|
||||
Temp := Item (I - 1) * Item (K) * Item (J);
|
||||
Cost := m (I, K) + m (K + 1, J) + Temp;
|
||||
if Cost < m (I, J) then
|
||||
m (I, J) := Cost;
|
||||
S (I, J) := K;
|
||||
end if;
|
||||
end loop;
|
||||
end loop;
|
||||
end loop;
|
||||
end Chain_Order;
|
||||
|
||||
function Optimal_Parens return String is
|
||||
function Construct
|
||||
(S : Result_Matrix; I : Natural; J : Natural)
|
||||
return Unbounded_String
|
||||
is
|
||||
Us : Unbounded_String := Null_Unbounded_String;
|
||||
Char_Order : Character;
|
||||
begin
|
||||
if I = J then
|
||||
Char_Order := Character'Val (I + 64);
|
||||
Append (Source => Us, New_Item => Char_Order);
|
||||
return Us;
|
||||
else
|
||||
Append (Source => Us, New_Item => '(');
|
||||
Append (Source => Us, New_Item => Construct (S, I, S (I, J)));
|
||||
Append (Source => Us, New_Item => '*');
|
||||
Append
|
||||
(Source => Us, New_Item => Construct (S, S (I, J) + 1, J));
|
||||
Append (Source => Us, New_Item => ')');
|
||||
return Us;
|
||||
end if;
|
||||
end Construct;
|
||||
|
||||
begin
|
||||
return To_String (Construct (S, 1, n));
|
||||
|
||||
end Optimal_Parens;
|
||||
|
||||
begin
|
||||
Chain_Order (Dims);
|
||||
Print (Dims);
|
||||
Put_Line ("Cost = " & Integer'Image (m (1, n)));
|
||||
Put_Line ("Optimal Multiply = " & Optimal_Parens);
|
||||
end Chain_Multiplication;
|
||||
|
||||
end mat_chain;
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
with Mat_Chain; use Mat_Chain;
|
||||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
|
||||
procedure chain_main is
|
||||
V1 : Vector := (5, 6, 3, 1);
|
||||
V2 : Vector := (1, 5, 25, 30, 100, 70, 2, 1, 100, 250, 1, 1000, 2);
|
||||
V3 : Vector := (1000, 1, 500, 12, 1, 700, 2500, 3, 2, 5, 14, 10);
|
||||
begin
|
||||
Chain_Multiplication(V1);
|
||||
New_Line;
|
||||
Chain_Multiplication(V2);
|
||||
New_Line;
|
||||
Chain_Multiplication(V3);
|
||||
end chain_main;
|
||||
|
|
@ -4,8 +4,8 @@ using OffsetArrays
|
|||
|
||||
function optim(a)
|
||||
n = length(a) - 1
|
||||
u = fill!(OffsetArray{Int}(0:n, 0:n), 0)
|
||||
v = fill!(OffsetArray{Int}(0:n, 0:n), typemax(Int))
|
||||
u = fill!(OffsetArray{Int}(undef, 0:n, 0:n), 0)
|
||||
v = fill!(OffsetArray{Int}(undef, 0:n, 0:n), typemax(Int))
|
||||
u[:, 1] .= -1
|
||||
v[:, 1] .= 0
|
||||
for j in 2:n, i in 1:n-j+1, k in 1:j-1
|
||||
|
|
|
|||
|
|
@ -0,0 +1,58 @@
|
|||
const int_max = max_int // builtin const
|
||||
|
||||
struct MatrixChain {
|
||||
mut:
|
||||
may [][]int
|
||||
say [][]int
|
||||
dim []int
|
||||
nir int
|
||||
}
|
||||
|
||||
fn (mut mc MatrixChain) mat_chain_order() {
|
||||
mc.may = [][]int{len: mc.nir, init: []int{len: mc.nir, init: 0}}
|
||||
mc.say = [][]int{len: mc.nir, init: []int{len: mc.nir, init: 0}}
|
||||
for ial in 0 .. mc.nir {
|
||||
mc.may[ial][ial] = 0
|
||||
}
|
||||
for length in 2 .. mc.nir + 1 {
|
||||
for ial in 0 .. mc.nir - length + 1 {
|
||||
jir := ial + length - 1
|
||||
mc.may[ial][jir] = int_max
|
||||
for kal in ial .. jir {
|
||||
cost := mc.may[ial][kal] + mc.may[kal + 1][jir]
|
||||
+ mc.dim[ial] * mc.dim[kal + 1] * mc.dim[jir + 1]
|
||||
if cost < mc.may[ial][jir] {
|
||||
mc.may[ial][jir] = cost
|
||||
mc.say[ial][jir] = kal
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn (mut mc MatrixChain) path(air int, bir int) string {
|
||||
if air == bir { return (rune(65 + air)).str() }
|
||||
return "(" + mc.path(air, mc.say[air][bir]) + mc.path(mc.say[air][bir] + 1, bir) + ")"
|
||||
}
|
||||
|
||||
fn (mut mc MatrixChain) pr_chain_order() {
|
||||
println("Order : " + mc.path(0, mc.nir - 1))
|
||||
}
|
||||
|
||||
fn main() {
|
||||
dims_2d := [
|
||||
[5, 6, 3, 1],
|
||||
[1, 5, 25, 30, 100, 70, 2, 1, 100, 250, 1, 1000, 2],
|
||||
[1000, 1, 500, 12, 1, 700, 2500, 3, 2, 5, 14, 10],
|
||||
]
|
||||
mut mc := &MatrixChain{}
|
||||
for dims in dims_2d {
|
||||
mc.dim = dims
|
||||
mc.nir = dims.len - 1
|
||||
println("Dims : $dims")
|
||||
mc.mat_chain_order()
|
||||
mc.pr_chain_order()
|
||||
println("Cost : ${mc.may[0][mc.nir - 1]}")
|
||||
println("")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue