Search code examples
algorithmtime-complexitypseudocode

Optimal binary search Tree - TimeComplexity


enter image description here

So what really made me stumble here is that when i were to try and calculate the time complexity of this algorithm i was confused by the fact that there are 3 loops which would lead me to believe the operations are O(n^3) but the thing is the middle loop decreases as the outer loop increases and the most inner loop increases as the middle loop decreases. I would pretty much guess it's an O(n^2) overall algorithm but it still seems to be O(n^3) because of the 3 nested loops. When counting the number of operations while running the code i get a counting of somewhere inbetween O(n^2) and O(n^3) which makes it all more frustrating...


Solution

  • I tried something, I would like to hear some fixes it's been some time since my algorithm course :)

    Time complexity solving

    The Sigma is for every loop. notice how it's becoming a multiply when there is no dependency on the variable