Giving infinite as result when using matrix chain multiplication to find the efficient cost

I am trying to understand Matrix Chain Multiplication. Particularly the problem Given a sequence of matrices, find the most efficient way to multiply these matrices together.

I tried the following but it prints some infinite value for the result. What is it that I am doing incorrectly?

const p = [1, 2, 3, 4, 3];

function mcm(m, i, j) {
 if (i >= j) return 0;
 else {
    let ans = Number.MAX_VALUE;
    let temp;
    for (let k = i; k < j - 1; k++) {
       temp = mcm(m, i, k) + mcm(m, k + 1, j) + m[i - 1] * m[k] * m[j];
       if (ans > temp) ans = temp;
    }
    return ans;
 }
}

console.log(mcm(p, 1, p.length - 1));

133 thoughts on “Giving infinite as result when using matrix chain multiplication to find the efficient cost”

  1. I have been browsing online more than 4 hours today, yet I never found
    any interesting article like yours. It is pretty worth enough for me.

    In my view, if all website owners and bloggers made good content as you did, the
    web will be a lot more useful than ever before.

    Reply
  2. It’s perfect time to make some plans for the future and it’s
    time to be happy. I’ve read this post and if I could
    I wish to suggest you few interesting things or advice.
    Maybe you can write next articles referring to this article.
    I want to read more things about it!

    Reply
  3. I have been surfing online more than three hours today,
    yet I never found any interesting article like
    yours. It’s pretty worth enough for me. In my opinion, if all
    web owners and bloggers made good content as you did, the net will be
    much more useful than ever before.

    Reply
  4. Hi! I could have sworn I’ve visited your blog before but after
    looking at some of the articles I realized it’s new to
    me. Anyways, I’m definitely pleased I found it and I’ll be bookmarking it and checking back regularly!

    Reply
  5. I’ve been browsing online more than 4 hours today, yet I never found any interesting article like yours.
    It is pretty worth enough for me. In my view, if all webmasters and bloggers made good content
    as you did, the internet will be much more useful than ever before.

    Reply
  6. I’ll right away snatch your rss as I can not in finding your e-mail subscription link or newsletter service.
    Do you’ve any? Kindly let me recognise so that I could subscribe.
    Thanks.

    Reply
  7. Thanks for your article. It is extremely unfortunate that over the last several years, the travel industry has already been able to to fight terrorism, SARS, tsunamis, bird flu virus, swine flu, plus the first ever entire global economic collapse. Through everthing the industry has really proven to be solid, resilient and also dynamic, obtaining new strategies to deal with trouble. There are generally fresh complications and chance to which the field must once more adapt and reply.

    Reply
  8. I have observed that of all sorts of insurance, health care insurance is the most controversial because of the clash between the insurance plan company’s necessity to remain afloat and the buyer’s need to have insurance coverage. Insurance companies’ revenue on overall health plans are incredibly low, consequently some firms struggle to make money. Thanks for the tips you write about through this blog.

    Reply
  9. Hi there would you mind letting me know which webhost you’re utilizing?
    I’ve loaded your blog in 3 completely different browsers and I must
    say this blog loads a lot faster then most. Can you suggest a good hosting provider at a honest price?
    Many thanks, I appreciate it!

    Reply
  10. It’s appropriate time to make some plans for the future and it’s time to
    be happy. I’ve read this post and if I could I want to suggest
    you few interesting things or advice. Maybe you can write next articles referring to
    this article. I desire to read even more things about
    it!

    Reply

Leave a Comment