Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
Java/DynamicProgramming/MatrixChainMultiplication.java at master · songallna/Java · GitHub
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Small and medium teams
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
songallna
Java
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Java
/
DynamicProgramming
/
MatrixChainMultiplication.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
135 lines (115 loc) · 3.09 KB
master
Breadcrumbs
Java
/
DynamicProgramming
/
MatrixChainMultiplication.java
Copy path
Top
File metadata and controls
Code
Blame
135 lines (115 loc) · 3.09 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package
DynamicProgramming
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
Arrays
;
import
java
.
util
.
Scanner
;
public
class
MatrixChainMultiplication
{
private
static
Scanner
scan
=
new
Scanner
(
System
.
in
);
private
static
ArrayList
<
Matrix
>
mArray
=
new
ArrayList
<>();
private
static
int
size
;
private
static
int
[][]
m
;
private
static
int
[][]
s
;
private
static
int
[]
p
;
public
static
void
main
(
String
[]
args
) {
int
count
=
1
;
while
(
true
) {
String
[]
mSize
=
input
(
"input size of matrix A("
+
count
+
") ( ex. 10 20 ) : "
);
int
col
=
Integer
.
parseInt
(
mSize
[
0
]);
if
(
col
==
0
)
break
;
int
row
=
Integer
.
parseInt
(
mSize
[
1
]);
Matrix
matrix
=
new
Matrix
(
count
,
col
,
row
);
mArray
.
add
(
matrix
);
count
++;
}
for
(
Matrix
m
:
mArray
) {
System
.
out
.
format
(
"A(%d) = %2d x %2d%n"
,
m
.
count
(),
m
.
col
(),
m
.
row
());
}
size
=
mArray
.
size
();
m
=
new
int
[
size
+
1
][
size
+
1
];
s
=
new
int
[
size
+
1
][
size
+
1
];
p
=
new
int
[
size
+
1
];
for
(
int
i
=
0
;
i
<
size
+
1
;
i
++) {
Arrays
.
fill
(
m
[
i
], -
1
);
Arrays
.
fill
(
s
[
i
], -
1
);
}
for
(
int
i
=
0
;
i
<
p
.
length
;
i
++) {
p
[
i
] =
i
==
0
?
mArray
.
get
(
i
).
col
() :
mArray
.
get
(
i
-
1
).
row
();
}
matrixChainOrder
();
for
(
int
i
=
0
;
i
<
size
;
i
++) {
System
.
out
.
print
(
"-------"
);
}
System
.
out
.
println
();
printArray
(
m
);
for
(
int
i
=
0
;
i
<
size
;
i
++) {
System
.
out
.
print
(
"-------"
);
}
System
.
out
.
println
();
printArray
(
s
);
for
(
int
i
=
0
;
i
<
size
;
i
++) {
System
.
out
.
print
(
"-------"
);
}
System
.
out
.
println
();
System
.
out
.
println
(
"Optimal solution : "
+
m
[
1
][
size
]);
System
.
out
.
print
(
"Optimal parens : "
);
printOptimalParens
(
1
,
size
);
}
private
static
void
printOptimalParens
(
int
i
,
int
j
) {
if
(
i
==
j
) {
System
.
out
.
print
(
"A"
+
i
);
}
else
{
System
.
out
.
print
(
"("
);
printOptimalParens
(
i
,
s
[
i
][
j
]);
printOptimalParens
(
s
[
i
][
j
] +
1
,
j
);
System
.
out
.
print
(
")"
);
}
}
private
static
void
printArray
(
int
[][]
array
) {
for
(
int
i
=
1
;
i
<
size
+
1
;
i
++) {
for
(
int
j
=
1
;
j
<
size
+
1
;
j
++) {
System
.
out
.
print
(
String
.
format
(
"%7d"
,
array
[
i
][
j
]));
}
System
.
out
.
println
();
}
}
private
static
void
matrixChainOrder
() {
for
(
int
i
=
1
;
i
<
size
+
1
;
i
++) {
m
[
i
][
i
] =
0
;
}
for
(
int
l
=
2
;
l
<
size
+
1
;
l
++) {
for
(
int
i
=
1
;
i
<
size
-
l
+
2
;
i
++) {
int
j
=
i
+
l
-
1
;
m
[
i
][
j
] =
Integer
.
MAX_VALUE
;
for
(
int
k
=
i
;
k
<
j
;
k
++) {
int
q
=
m
[
i
][
k
] +
m
[
k
+
1
][
j
] +
p
[
i
-
1
] *
p
[
k
] *
p
[
j
];
if
(
q
<
m
[
i
][
j
]) {
m
[
i
][
j
] =
q
;
s
[
i
][
j
] =
k
;
}
}
}
}
}
private
static
String
[]
input
(
String
string
) {
System
.
out
.
print
(
string
);
return
(
scan
.
nextLine
().
split
(
" "
));
}
}
class
Matrix
{
private
int
count
;
private
int
col
;
private
int
row
;
Matrix
(
int
count
,
int
col
,
int
row
) {
this
.
count
=
count
;
this
.
col
=
col
;
this
.
row
=
row
;
}
int
count
() {
return
count
;
}
int
col
() {
return
col
;
}
int
row
() {
return
row
;
}
}
You can’t perform that action at this time.