Calculating number of steps in multiple loop piece of code

In summary, the algorithm described in this pseudocode creates a two dimensional array from a one dimensional array by adding elements into certain rows. The algorithm is modeled after the Gaussian summation for the outer loops and it does not account for the array summation which is O(n). The first for loop should be range from 1 to n-1 to account for the non-constant array summation.
  • #1
DrAlexMV
25
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
  • #2
Shouldn't the first for loop be:

For i = 1, 2, ..., n-1 ?
 
  • #3
It should, but this algorithm comes from the book.
 
  • #4
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.
 
  • #5
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.
 
  • #7
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.
 
  • #8
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, ... ).
 
  • #9
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 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
 

FAQ: Calculating number of steps in multiple loop piece of code

How do you calculate the number of steps in a multiple loop piece of code?

To calculate the number of steps in a multiple loop piece of code, you need to determine the complexity of the loops. This can be done by identifying the number of nested loops and the number of iterations each loop performs. The number of steps will then be equal to the product of the number of iterations in each loop.

Why is it important to calculate the number of steps in a multiple loop piece of code?

Calculating the number of steps in a multiple loop piece of code helps in evaluating the efficiency and performance of the code. It allows you to identify potential bottlenecks and optimize the code for better performance.

What is the difference between time complexity and space complexity when calculating the number of steps?

Time complexity refers to the amount of time it takes for a program to run as the input size increases. Space complexity refers to the amount of memory required for a program to run as the input size increases. When calculating the number of steps in a multiple loop piece of code, we are mainly concerned with time complexity.

Can the number of steps in a multiple loop piece of code be reduced?

Yes, the number of steps in a multiple loop piece of code can be reduced by optimizing the code. This can be done by identifying and eliminating unnecessary loops, using more efficient data structures, or finding alternative algorithms with a lower time complexity.

Are there any tools or methods that can help in calculating the number of steps in a multiple loop piece of code?

Yes, there are various tools and methods that can help in calculating the number of steps in a multiple loop piece of code. Some programming languages have built-in functions for measuring time complexity, and there are also online calculators and software programs that can analyze the code and provide the number of steps. Additionally, manually counting the number of iterations in each loop can also help in calculating the number of steps.

Similar threads

Replies
21
Views
2K
Replies
4
Views
4K
Replies
3
Views
1K
Replies
2
Views
1K
Replies
5
Views
2K
Replies
1
Views
1K
Replies
1
Views
1K
Replies
1
Views
1K
Replies
15
Views
4K
Back
Top