A Matrix Chain Multiplication algorithm in Javascript - javascript

I am trying to adapt an established dynamic programming Matrix Chain Multiplication algorithm to compare Javascript's performance to other languages.
This is the Java version found on Wikipedia from which I am adapting to Javascript:
void matrixChainOrder(int[] p) {
int n = p.length - 1;
m = new int[n][n];
s = new int[n][n];
for (int L = 1; L < n; L++) {
for (int i = 0; i < n - L; i++) {
int j = i + L;
m[i][j] = Integer.MAX_VALUE;
for (int k = i; k < j; k++) {
int q = m[i][k] + m[k+1][j] + p[i]*p[k+1]*p[j+1];
if (q < m[i][j]) {
m[i][j] = q;
s[i][j] = k;
}
}
}
}
}
Below is the version I am adapting and it is the same as far as I can see, but the console logs the following error message: TypeError: m[(k + 1)] is undefined
var matrixChainOrder = function(p) {
var n = p.length - 1;
var m = [[,]];
var s = [[,]];
for (var L = 1; L < n; L++) {
for (var i = 0; i < n - L; i++) {
var j = i + L;
m[i][j] = Number.MAX_SAFE_INTEGER;
for (var k = i; k < j; k++) {
var q = m[i][k] + m[k+1][j] + p[i]*p[k+1]*p[j+1];
if (q < m[i][j]) {
m[i][j] = q;
s[i][j] = k;
}
}
}
}
}
var array = [15, 12, 16, 17, 19];
var d = new Date();
var start = d.getMilliseconds();
matrixChainOrder(array);
var end = d.getMilliseconds();
console.log(end - start);
I don't understand why a TypeError is being thrown... hoping someone else can spot the problem and help me out.

Related

NaN out of matrix multiplication?

I have the code below for matrix formation out of an array and its multiplication.
But when I try to multiply two matrices like mtp(matrix(2,2,[1,2,3,4]),matrix(2,2,[1,0,0,1])) it returns NaN in all places.
Please help me out
function matrix(m, n, arr) {
var result = {};
for (t = 1; t <= m; t++) {
result[t] = {};
};
for (i = 1; i <= m; i++)
for (j = 1; j <= n; j++) result[i][j] = arr[m * (i - 1) + j - 1];
return {
"result": result,
"m": m,
"n": n
};
}
function mtp(a, b) {
if (parseInt(a.n) != parseInt(b.m)) {
return;
} else {
var result = [];
var m = parseInt(a.m);
var n = parseInt(b.n);
var k = parseInt(a.n);
for (i = 1; i <= m; i++) {
for (j = 1; j <= n; j++) {
for (p = 1; p <= k; p++) {
result[m * (i - 1) + j - 1] += (parseInt(a.result[i][p]) * parseInt(b.result[p][j]));
console.log(parseInt(a.result[i][p]) * parseInt(b.result[p][j]))
}
}
}
}
console.log(result, matrix(m, n, result).result);
}
mtp(matrix(2,2,[1,2,3,4]),matrix(2,2,[1,0,0,1]));
When you define result it is an array of zero elements
var result = [];
When you try to add a number to an element of the array, that element is not defined, Adding any number to undefined gives you NaN. There are 2 ways to solve this, either initialize your array with the right length of zeros, or default it to zero during the sum. I've chosen the latter below,
result[m * (i - 1) + j - 1] = (result[m * (i - 1) + j - 1]||0) + (a.result[i][p] * b.result[p][j]);
// Note here ---------------------------------------------^
I've got rid of all the unnecessary parseInt calls.
function matrix(m, n, arr) {
var result = {};
for (t = 1; t <= m; t++) {
result[t] = {};
};
for (i = 1; i <= m; i++)
for (j = 1; j <= n; j++) result[i][j] = arr[m * (i - 1) + j - 1];
return {
"result": result,
"m": m,
"n": n
};
}
function mtp(a, b) {
if (a.n != b.m) {
return;
} else {
var result = [];
var m = a.m;
var n = b.n;
var k = a.n;
for (i = 1; i <= m; i++) {
for (j = 1; j <= n; j++) {
for (p = 1; p <= k; p++) {
result[m * (i - 1) + j - 1] = (result[m * (i - 1) + j - 1]||0) + (a.result[i][p] * b.result[p][j]);
console.log(a.result[i][p] * b.result[p][j])
}
}
}
}
console.log(result, matrix(m, n, result).result);
}
mtp(matrix(2,2,[1,2,3,4]),matrix(2,2,[1,0,0,1]));
In your mtp() function the values of the elements inside your result array are uninitialized (that is, you've defined an array, but that array has no actual values or even a determined length), so in your inner loop when you +=, you're referencing a newly created array element which has no default value (and therefore is undefined), and adding some number to that undefined value.
An undefined value plus a number is not a number (NaN). Try preinitializing your result array.

How do I traverse an array diagonally in javascript [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have an array with strings that I would like to traverse diagonally.
Assumptions:
Each string is the same length.
Arrays could be square or rectangular, horizontally or vertically.
The matrix looks like this:
A B C D
E F G H
I J K L
I Would like to get (from top left to bottom right):
A
EB
IFC
JGD
KH
L
and (from the bottom left to top right):
I
JE
KFA
LGB
HC
D
I already have a piece of code that works 3/4 of the way, but i cant seem to figure out what I am doing (wrong).
//the array
var TheArray = ['ABCD','EFGH','IJKL'];
//amount of rows
var RowLength = TheArray.length;
//amount of colums
var ColumnLength = TheArray[0].length;
The code I have chops up the diagonals into 4 of these loops to get all the diagonals. It looks as 2 for loops with an if to not loop over unbound values. The pseudo code looks a bit like this:
for(loop rows){
var outputarray = [];
for(loop columns){
if(delimit for out of bound){
var temprow = TheArray[something?];
var tempvalue = temprow[something?];
outputarray.push(tempvalue);
}
}
//use values
document.getElementById("theDiv").innerHTML += outputarray.join("")+"<br>";
}
I hope somebody can help me with this.
From top left to bottom right
var theArray = ["ABCD","EFGH","IJKL"];
var length = { "x" : theArray[0].length, "y" : theArray.length };
length.max = Math.max(length.x, length.y);
var temp, k, x, y;
for (k = 0; k <= 2 * (length.max - 1); ++k) {
for (temp = [], y = length.y - 1; (x = k - y), y >= 0; --y) {
x >= 0 && x < length.x && temp.push(theArray[y][x]);
}
temp.length > 0 && (document.body.innerHTML += temp.join('') + '<br>');
}
(see also this Fiddle)
From the bottom left to top right
var theArray = ["ABCD","EFGH","IJKL"];
var length = { "x" : theArray[0].length, "y" : theArray.length };
length.max = Math.max(length.x, length.y);
var temp, k, x, y;
for (k = 0; k <= 2 * (length.max - 1); ++k) {
for (temp = [], y = length.y - 1; (x = k + y - length.y), y >= 0; --y) {
x >= 0 && x < length.x && temp.push(theArray[y][x]);
}
temp.length > 0 && (document.body.innerHTML += temp.join('') + '<br>');
}
(see also this Fiddle)
Combined
As there's but a single line of difference between both, you can easily combine them in a single function :
var theArray = ["ABCD","EFGH","IJKL"];
function diagonal(data, fromBottom) {
var length = { "x" : data[0].length, "y" : data.length };
length.max = Math.max(length.x, length.y);
var temp, k, x, y;
var returnMe = [];
for (k = 0; k <= 2 * (length.max - 1); ++k) {
for (temp = [], y = length.y - 1; y >= 0; --y) {
x = k - (fromBottom ? length.y - y : y);
x >= 0 && x < length.x && temp.push(data[y][x]);
}
temp.length > 0 && returnMe.push(temp.join(''));
}
return returnMe;
}
document.body.innerHTML = diagonal(theArray).join('<br>') +
'<br><br><br>' +
diagonal(theArray, true).join('<br>');
(see also this Fiddle)
This does the trick, and outputs the desired results to the screen:
var array = ['ABCD','EFGH','IJKL'];
var rows = array.length;
var cols = array[0].length;
for (var n = 0; n < cols + rows - 1; n += 1)
{
var r = n;
var c = 0;
var str = '';
while (r >= 0 && c < cols)
{
if (r < rows)
str += array[r][c];
r -= 1;
c += 1;
}
document.write(str+"<br>");
}
Result:
A
EB
IFC
JGD
KH
L
Yet another solution:
function getAllDiagonal(array) {
function row(offset) {
var i = array.length, a = '';
while (i--) {
a += array[i][j + (offset ? offset - i : i)] || '';
}
return a;
}
var result = [[], []], j;
for (j = 1 - array.length; j < array[0].length; j++) {
result[0].push(row(0));
result[1].push(row(array.length - 1));
}
return result;
}
var array = ['ABCD', 'EFGH', 'IJKL'];
document.write('<pre>' + JSON.stringify(getAllDiagonal(array), 0, 4) + '</pre>');
Use indices:
[i][j-i]
Where i goes from 0 to M-1
j goes from 0 to i
While j++ < N
for the matrix
type Array[M][N]
However this may miss a few at the bottom right if the matrix is rectangular, and you might need a second nested for loop with i and j to capture those.
Try this
var TheArray = ['ABCD', 'EFGH', 'IJKL'];
//amount of rows
var RowLength = TheArray.length;
//amount of colums
var ColumnLength = TheArray[0].length;
var totalNoComb = RowLength + ColumnLength - 1;
var combArr = new Array(totalNoComb);
for (var i = 0; i < totalNoComb; i++) {
combArr[i] = "";
for (var j = RowLength-1; j >-1; j--) {
if (i - j > -1 && i - j < ColumnLength)
combArr[i] += TheArray[j][i-j];
}
}
alert(combArr);
for (var i = 0; i < totalNoComb; i++) {
combArr[i] = "";
for (var j = 0; j < RowLength; j++) {
if (i - j > -1 && i - j < ColumnLength)
combArr[i] += TheArray[ RowLength -1-j][i - j];
}
}
alert(combArr);
This should work even for rectangular matrices:
var array = ["ABCD", "EFGH", "IJKL"];
var arrOfArr = [];
var resultArray = [];
for (var i = 0; i < array.length; ++i) {
arrOfArr.push(array[i].split(''));
}
var rows = arrOfArr.length;
var columns = arrOfArr[0].length;
var index = 0;
for (var i = 0; i < rows; ++i) {
var k = 0;
resultArray[index] = new Array();
for (var j = i; j >= 0; --j) {
resultArray[index].push(arrOfArr[j][k]);
++k;
if ( k === columns) {
break;
}
}
resultArray[index] = resultArray[index].join('');
++index;
}
for (var j = 1; j < columns; ++j) {
var k = rows - 1;
resultArray[index] = new Array();
for (var i = j; i < columns; ++i) {
resultArray[index].push(arrOfArr[k][i]);
--k;
if ( k === -1) {
break;
}
}
resultArray[index] = resultArray[index].join('');
++index;
}
console.log(JSON.stringify(resultArray));
Note: This assumes that all strings are the same size, or at least are as large as the first string.
In a 2D array (or in this case, an array of strings), a diagonal's indexes add up to the diagonal's number (like a row-number). 00, 01 10, 02 11 20, etc.
Using this method, the number of diagonal "rows" (starting at zero) is equal to the sum of the largest indexes, or the sum of (columnlength+rowlength-2).
Therefore, my solution is to iterate through the diagonal row numbers and print all index pairs whose sum is equal to the current diagonal row.
var TheArray = ["ABCD","EFGH","IJKL"];
//amount of rows
var RowLength = TheArray.length;
//amount of colums
var ColumnLength = TheArray[0].length;
var text = ''
for (i = 0; i <= (RowLength+ColumnLength-2); i++){
for (x = 0; x<=i; x++){
if (TheArray[i-x] && TheArray[i-x][x]){
text += TheArray[i-x][x];
}
}
text += "<br/>";
}
document.getElementById('text').innerHTML = text;
JSFiddle Link
Here is my try for 'from top left to bottom right':
for (i=0; i<nbRows; i++) {
x = 0; y = i;
while (x < nbColumns && y >= 0) {
print(array[x, y]);
x++; y--;
}
print("\n");
}
for (i=1; i<nbColumns; i++) {
x = i; y = nbRows - 1;
while (x < nbColumns && y >=0) {
print(array[x, y]);
x++; y--;
}
}
Needs a few adaptations to fit JavaScript syntax.
Full solution for both diagonals:
var TheArray = ['ABCD', 'EFGH', 'IJKL'];
var RowLength = TheArray.length;
var ColumnLength = TheArray[0].length;
// Diagonals
var diagonal = [[], []];
for (var i = 0; i < Math.min(RowLength, ColumnLength); i++) {
diagonal[0].push({'row': 0-i, 'col': i});
diagonal[1].push({'row': 0-i, 'col': 0-i});
}
// Entry points
// 1///
// 2///
// 3456
var points = [[], []];
for (var y = 0; y < RowLength; y++) {
points[0].push({'row': y, 'col': 0});
}
for (var x = 1; x < ColumnLength; x++) {
points[0].push({'row': RowLength - 1, 'col': x});
}
// Entry points
// \\\6
// \\\5
// 1234
for (var x = 0; x < ColumnLength; x++) {
points[1].push({'row': RowLength - 1, 'col': x});
}
for (var y = RowLength - 2; y >= 0; y--) {
points[1].push({'row': y, 'col': ColumnLength - 1});
}
var strings = [[], []];
for (var line = 0; line < diagonal.length; line++) {
for (var point = 0; point < points[line].length; point++) {
var inside = true;
var index = 0;
var string = '';
while (inside && index < diagonal[line].length) {
var row = points[line][point]['row'] + diagonal[line][index]['row'];
var col = points[line][point]['col'] + diagonal[line][index]['col'];
if (row >= 0 && row < RowLength && col >= 0 && col < ColumnLength) {
string += TheArray[row][col];
index++;
} else {
inside = false;
}
}
strings[line].push(string);
}
}
console.log(strings);

How to get every n from a number based on a known value?

The title might be a bit confusing, here is want i want to accomplish.
giving n = 51 & m = 24 i want to get:
[
{start:0, end:24},
{start:24, end:48},
{start:48, end:51}
]
so far i have this, but i need to offset the j and end
var n = 51;
var m = 24;
var arr = [];
for (var j = 0; j < n; j += m) {
var end = (j + j) >= n ? n : j;
console.log(j, end);
if (j > 0) arr.push({
start: j,
end: end
});
}
any ideas?
The problem is in your end calculation, if j+ m is greater than n then you want end to be n
var n = 51;
var m = 24;
var arr = [];
for (var j = 0; j < n; j += m) {
var end = (j + m);
arr.push({
start: j,
end: end > n ? n : end
});
}
result.innerHTML = JSON.stringify(arr, null, 2)
<pre id="result"></pre>
var n = 51;
var m = 21;
var arr = [];
for (var j = 0; j < n; j += m) {
var end = j + m > n ? n : j + m;
arr.push({
start: j,
end: end
});
}
alert(JSON.stringify(arr));

Universal function for getting all unique pairs, trebles etc from an array in javascript

I am looking to create a function in javascript, which would allow me to pass a long array, together with one argument.
what I'm looking for is something like this:
var ar = [1,2,3,4];
var pairs = superAwesomeFunction(ar,2) //=> [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]];
var trebles = superAwesomeFunction(ar,3) //=> [[1,2,3],[1,2,4],[1,3,4],[2,3,4]
ideally, the function would have no limit on the folding argument.
I wrote a piece of code that looks like this, which is working fine, but it's not really useful as it isn't universal, and I would need a lot of such functions, which seems silly.
getAll2Folds: function (ar) {
var combinations = [],
numOdds = ar.length;
for (var i = 0; i < numOdds; i++) {
for (var j = i + 1; j < numOdds; j++) {
combinations.push([ar[i], ar[j]]);
}
}
return combinations;
},
getAll3Folds: function (ar) {
var combinations = [],
numOdds = ar.length;
for (var i = 0; i < numOdds; i++) {
for (var j = i + 1; j < numOdds; j++) {
for (var k = j + 1; k < numOdds; k++) {
combinations.push([ar[i], ar[j], ar[k]]);
}
}
}
return combinations;
};
},
... (not so great :|)
getAll8Folds: function (ar) {
var combinations = [],
numOdds = ar.length;
for (var i = 0; i < numOdds; i++) {
for (var j = i + 1; j < numOdds; j++) {
for (var k = j + 1; k < numOdds; k++) {
for (var l = k + 1; l < numOdds; l++) {
for (var m = l + 1; m < numOdds; m++) {
for (var n = m + 1; n < numOdds; n++) {
for (var o = n + 1; o < numOdds; o++) {
for (var p = o + 1; p < numOdds; p++) {
combinations.push([ar[i], ar[j], ar[k], ar[l], ar[m], ar[n], ar[o], ar[p]]);
}
}
}
}
}
}
}
}
return combinations;
}
I'm free to use underscore, jquery or whatever tool i want, but can't find an elegant solution, which would also be performant. ideas?
Thanks
Basically combine() takes an array with the values to combine and a size of the wanted combination results sets.
The inner function c() takes an array of previously made combinations and a start value as index of the original array for combination. The return is an array with all made combinations.
The first call is allways c([], 0), because of an empty result array and a start index of 0.
var arr = [1, 2, 3, 4, 5, 6, 7];
function combine(a, size) {
function c(part, start) {
var result = [], i, l, p;
for (i = start, l = arr.length; i < l; i++) {
// get a copy of part
p = part.slice(0);
// add the iterated element to p
p.push(a[i]);
// test if recursion can go on
if (p.length < size) {
// true: call c again and concat it to the result
result = result.concat(c(p, i + 1));
} else {
// false: push p to the result, stop recursion
result.push(p);
}
}
return result;
}
return c([], 0);
}
out(JSON.stringify(combine(arr, 2), null, 4), true);
out(JSON.stringify(combine(arr, 3), null, 4), true);
out(JSON.stringify(combine(arr, 4), null, 4), true);
out(JSON.stringify(combine(arr, 5), null, 4), true);
out(JSON.stringify(combine(arr, 6), null, 4), true);
// just for displaying the result
function out(s, pre) {
var descriptionNode = document.createElement('div');
descriptionNode.className = 'description';
if (pre) {
var preNode = document.createElement('pre');
preNode.innerHTML = s + '<br>';
descriptionNode.appendChild(preNode);
} else {
descriptionNode.innerHTML = s + '<br>';
}
document.getElementById('out').appendChild(descriptionNode);
}
<div id="out"></div>

bruteforce string matching javascript

function search(pattern, text) {
var M = pattern.length;
var N = text.length;
for (var i = 0; i < N - M; i++) {
var j =0;
while (j < M) {
if (text.charAt(i + j) != pattern.charAt(j)) {break;}
}
if (j == M) {return i;}
}
return -1;
}
console.log(search("rf", "jdsrfan"));
I want to make an brute-force string matching algorithm in JavaScript. Can anyone tell me whats wrong with above code?
I did fixed it myself fixed code as follows:
// return offset of first match or -1 if no match
function bruteForcePatternSearch(sPattern, sText) {
var M = sPattern.length,
N = sText.length;
for (var i = 0; i <= N - M; i++) {
var j=0;
while (j < M) {
if (sText.charAt(i+j) !=sPattern.charAt(j)){
break;
}
j++;
}
if (j == M) {return i;} // found at offset i
}
return -1; // not found
}
bruteForcePatternSearch("abracadabra","abacadabrabracabracadabrabrabracad");
You're never incrementing j to start with. Hence the infinite loop.
Then, as Claudio commented, i < N - M is wrong. Should be i <= N - M.
Spoiler: here the fixed function. But I advise you not to take it as-is, but to try doing it yourself instead.
function search(pattern, text) {
var M = pattern.length;
var N = text.length;
for (var i = 0; i <= N - M; ++i) {
var matched = true;
for (var j = 0; j < M; ++j) {
if (text.charAt(i + j) != pattern.charAt(j)) {
matched = false;
break;
}
}
if (matched) {
return i;
}
}
return -1;
}
i guess this will work
for (var i = 0; i < M; i++) {
var j =0;
while (j < N) {
if (text.charAt(j) != pattern.charAt(i)) {
break;
}
j++
}
if (j == M) {return i;}
}
here is the explanation
on each pattern
match each character of text

Categories

Resources