From the Matrix Chain Multiplication page on Wikipedia, there is this fragment of Java code:
public void matrixChainOrder(int[] p) {
int n = p.length - 1;
m = new int[n][n];
s = new int[n][n];
for (int i = 0; i < n; i++) {
m[i] = new int[n];
m[i][i] = 0;
s[i] = new int[n];
}
...
Isn't m = new int[n][n];
already allocating memory space of size n
in both its dimensions so this step in the loop m[i] = new int[n];
is actually redundant because all it does is reallocate the second dimension again?
Yes, it is.
m[i] = new int[n];
is absolutely superfluous. And it seems that this line is heritage from c
-style psedocode, where such initilization was nessecary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With