Calculating number of steps in multiple loop piece of code

Click For Summary

Discussion Overview

The discussion revolves around calculating the time complexity T(n) of a pseudocode algorithm that creates a 2D array from a 1D array. Participants explore the implications of nested loops and the number of steps involved in array operations, focusing on theoretical and mathematical reasoning.

Discussion Character

  • Homework-related
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant suggests that the outer loop should iterate from 1 to n-1 instead of 1 to n, arguing that the last iteration does not contribute to the inner loop.
  • Another participant expresses uncertainty about how to account for the steps involved in summing array entries and proposes that the total number of steps could be modeled using the Gaussian summation formula.
  • Conflicting definitions of T(n) are noted, with one participant referencing a Wikipedia article that defines T(n) as O(f(n)), while another emphasizes the need for an exact count of steps for this specific exercise.
  • Some participants discuss the complexity of counting steps for operations on different processors, raising questions about what constitutes a step in various contexts.
  • A later reply introduces the idea of a third nested loop for summing array entries, suggesting that this could lead to a cubic equation for the number of times the inner loop is called.
  • There is mention of the need to derive coefficients for a cubic equation based on specific values of n to understand the overall complexity better.

Areas of Agreement / Disagreement

Participants express differing views on the definition of T(n) and the correct approach to calculating the number of steps in the algorithm. No consensus is reached regarding the exact formulation of T(n) or the implications of the nested loops.

Contextual Notes

Participants note that the definition of what counts as a step may vary based on processor architecture, which complicates the analysis of time complexity. Additionally, the discussion highlights the potential for different interpretations of the algorithm's structure and its implications for performance.

DrAlexMV
Messages
24
Reaction score
0

Homework Statement



The following pseudocode demonstrates an algorithm to create a 2D array from a 1D array by adding elements into certain rows:

For i = 1, 2, ..., n
For j = i + 1, i + 2, ..., n​
Add up array entries A through A[j]
Store the result in B[i, j]

Endfor​
Endfor

I am supposed to create a T(n) expression for the number of steps but I am not sure how to go about the second loop which depends on the first one. I am also not sure how to do so for the "Add up array entries ... "

Homework Equations






The Attempt at a Solution



Well, my idea was that since the number of steps on the loops themselves follow the Gaussian summation formula for the outer loops to model it this way:

T(n) = n(n - 1)/2

That does not account for the array summation which is O(n) but I am not sure what its T(n) is
 
Last edited:
Physics news on Phys.org
Shouldn't the first for loop be:

For i = 1, 2, ..., n-1 ?
 
It should, but this algorithm comes from the book.
 
rcgldr said:
Shouldn't the first for loop be:

For i = 1, 2, ..., n-1 ?
Other than a wasted step, it doesn't matter if the upper limit is n or n-1. With an upper lint of n, the inner loop does nothing on that final step when i=n.
 
So how should I approach this problem? I need to know the T(n) so I can derive the best-case estimate for this algorithm.
 
The T(n) for the stackoverflow post is the one for this particular exercise. It does not relate to the asymptotic bounds, but to the exact number of steps. With this T(n) you can calculate any asymptotic bound.
 
DrAlexMV said:
The T(n) for the stackoverflow post is the one for this particular exercise. It does not relate to the asymptotic bounds, but to the exact number of steps. With this T(n) you can calculate any asymptotic bound.
In that case you'd need to write out some example code. Each of these operations counts as a step:

load
store
index
increment
decrement
any math operation

In some cases, it's not clear what counts as a step, considering the differences in processors at the machine language level. How many steps is "i += 4", on a processor like X86 that has an add immediate to memory instruction? How many steps is a scaled index on a processor that can shift an index by 1, 2, 4, or 8 bits when used with an operand of size 8, 16, 32, or 64 bits? What about pre / post decrement or increment on processors that support those addressing modes (ARM, 68000 series, ... ).
 
This is much more abstract than that.

For i = 1, 2, ..., n
For j = i + 1, i + 2, ..., n
Add up array entries A through A[j]
Store the result in B[i, j]

Endfor

Endfor

For example, the outer loop:
For i = 1, 2, ..., n​
Runs n times

The second loop:
For j = i + 1, i + 2, ..., n​
Runs n - 1 times in the first outer loop iteration (when i = 1), then it runs n - 2 times (when i = 2)

So if we just look at the first two loops, and we assume that array addition is a constant operation that only takes 1 step, then the number of steps looks something like this for the first iteration of the outer loop:
n + 1​

Where the + 1 comes from the array addition and storing the result

The overall pattern looks like:

n + 1, n, n - 1, n - 2, ...

All of that happens n times (according to the outer loop)

That looks a lot like the Gaussian summation (not quite because of the + 1 in every term).

The problem is that I do not know how to account for the non-constant array summation in that equation.
 
Last edited:
  • #10
DrAlexMV said:
assume that array addition is a constant operation that only takes 1 step.
That would be different than the example shown at stack overflow. In that example, sum = sum + A takes 5 steps: 1 - load index i, 2 - load A, 3 - load sum, 4 - add sum + A, 5 - store back into sum.

Also in the final first loop, where i = n, the second loop does nothing, since j = i + 1 would be j = n+1, so it would loop zero times. This is why I mentioned the first loop should range from 1 to n-1.
 
  • #11
DrAlexMV said:
Add up array entries A through A[j].
I forgot to include this in my last post. This would be a third nested loop:

for k = i, i+1, i+2, ... j
... sum += A[k]

The formula for the number of times the inner loop is called will be a cubic equation, a n^3 + b n^2 + c n + d, where "d" is usually zero. You can solve for the coefficients using linear math by generating equations for 4 values of n, such as n = 1, n = 2, n = 3, n = 4. You could write a program to generate counts for the 4 values of n, then solve for the coefficients of the cubic equation.
 
Last edited:
  • Like
Likes   Reactions: 1 person
  • #12
rcgldr said:
I forgot to include this in my last post. This would be a third nested loop:
for k = i, i+1, i+2, ... j
... sum += A[k]
The formula for the number of times the inner loop is called will be a cubic equation, a n^3 + b n^2 + c n + d, where "d" is usually zero.
Since the op stopped responding to this thread, I'll post the formula in order to archive it:

1/6 n^3 + 3/6 n^2 - 4/6 n
 

Similar threads

  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K