Primitive operations and running time [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
For the given pseudo code I have to determine primitive operations and running time:
for( i=0; i<n; i++) ->1+n+n=2n+1
a[i] = 0; ->?
for( i=0; i<n; i++ ) ->2n+1
for( j=0; j<n; j++ ) ->n(2n+1)
a[i] += a[j] + i + j; ->?
Anyone to help me if it's correct and how to do the rows which I have put ?. please

As far as I understand, the for loop instruction going from 0 to n times would be executed n+1 times (once extra when loop index equals n itself, after which it breaks), so it would be n+1 and not 2n+1. Its contents would run for n times. Assigning values doesn't add additional complexity in terms of input size (n):
for(i = 0; i < n; i++) // 1+n
a[i] = 0; // runs for n times
Likewise, for the nested loops, the inner-loop statement will run for n+1 times, multiplied by n, as it in itself is the content of the outer loop, making the product of n(n+1). The contents would run for n*n times.
for(i = 0; i < n; i++) // n+1
for(j = 0; j < n; j++) // n(n+1) => n*n+n
a[i] += a[j] + i + j; // runs for n*n, for the dominant factor
With the innermost statements running for n*n times, the overall asymptotic time complexity would be O(n2).

Related

Do different/older processors run c++ code differently? [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 3 years ago.
Improve this question
I was coding a function that loops through a 2d array, if the current element in the array is less than the element next to it, then I would add 1 to my int counter variable. The issue is, when I run the code on my windows based machine with a AMD ryzen 7 processor, it works correctly and the counter gets to 20 (on a 5x5 array). But when I run it on my 2014 Macbook with a intel core i5 processor, on both MacOS and Windows 10 (bootcamp) the counter only gets to 19 using the same exact block of code. Here is my loop
int counter = 0;
for(int i=0; i < ROWS; i++){
for(int j=0; j < COLUMNS; j++){
if(board[i][j] < board[i][j + 1]){
counter += 1;
}
}
}
I would think this would work in any situation.
Well it would be nice to have a minimal reproducible example like others said, I think I can see it from here.
using j+1 will let the index go out of bounds causing undefined behaviour.
Try :
for(int i=0; i < ROWS; i++){
for(int j=0; j < COLUMNS - 1; j++){
if(board[i][j] < board[i][j + 1]){
counter += 1;
}
}
}
This should avoid that problem by simply stopping one sooner

Finding the time complexity function of these nested for loops

Keep in mind that the following pseudo code is similar to c++, so i will use a c++ tag
void matrixmult (int n, const number A[][], const number B[][], number C[][])
{
index i, j, k;
for(i = 1; i <= n; i++) //the i for loop will run n + 1 times
for(j = 1; j <=n; j++) //the j for loop will run n(n+1) times
C[i][j] = 0 //this will run (n-1)n times
for(k = 1; k <=n; k++) //the k for loop will run (n-1)(n+1) times
C[i][j] = C[i][j]+ A[i][k] * B[k][j]; //this will run n((n-1)(n+1))
I was instructed by my professor to find the time complexity function of the very last line of code above
I believe that the time complexity function is T(n) = n(n-1)(n+1)
I need someone to double check my work, did i make a mistake somewhere? did i even get the correct time complexity here?
any help will be appreciated
You have three nested loops, looping n steps each, so it's n^3.
Getting more detailed. Depending on the model of computation, you could instead count the number of assignments, comparisons, multiplications and even memory accesses.

What is the upper bound of the inner for loop? [duplicate]

This question already has answers here:
How can I find the time complexity of an algorithm?
(10 answers)
Running time complexity of double for-loops
(3 answers)
Closed 5 years ago.
I understand that the upper bound (i.e. for (int i = 0; i < n; i++) for a non-nested/single for loop is the worst case time complexity. Basically, n is the maximum number of times the for loop will iterate. With this piece of information in mind, here is pseudo code that I have written.
for (i = 1; i <= n; i++)
for (j = n; j >= 1; j--)
cout << "hi";
From this piece of code, it's obvious that the time complexity for the upper bound of the outer for loop is O(n).
However, what would the time complexity for the inner for loop be?
The statement in the inner loop gets executed n*n times what results in the complexity of O(n^2).
for (i = 1; i <= n; i++)// O(n)
for (j = n; j >= 1; j--)// O(n)
cout << "hi";// O(1)
Total: O(n)*O(n)*O(1)=O(n^2)
for (j = n; j >= 1; j--) and for(j = 0; j <= n; j++) are equivalent statements as far as how many times they will be executed, one just counts from the upper bound to 0 and the other counts from zero to the upper bound

How to break inner loop but the loop still increment [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
for (int j=1; j<path.size(); j++) {
//do something
for (int z=0; z<path.size(); z++) {
//do something
}
break;
}
The inner loop can loop from 0 until it hits the end of the path size, but the outer loop just keeps giving me j=1 instead of doing the increment like inter loop does.
How can both loop do the increment at same time? I was trying to delete the break; but z loop through path.size()'s times.
The inner loop executes completely for every iteration of the outer loop, that's why you see z going looping path.size() times. After this your loop stops due to break so it won't iterate a second time over outer loop. So even if condition of outer loop is still true it won't even check it since you're breaking out of your loop.
Try running this test program with and without the break
for (int j=1; j<5; j++) {
printf("j=%d\n",j);
for (int z=0; z<5; z++) {
printf("z=%d\n",z);
}
break; // next time try to remove this break
}
and you might get enlightened.
If you want j and z to be counted up in parallel the you might want something like this
for (int j=1, z= 1; j<5; j++, z++) {
printf("j=%d\n",j);
printf("z=%d\n",z);
}
for(int j = 1 ; j < path.size() ; j++){
do something
for (int z = 0 ; z < j ; z++){
do something
}
I think that's what you're looking for

How do I clean up these nested for loops? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
NOTE: Bad code ahead. You've been warned.
This code iterates through all elements in all 3x3 squares in a Sudoku puzzle. The way shown below is that it gets the coordinate of the upper-left-hand element for each square then iterates through each element in that square. This means that there will be a total of 4 "for" loops needed to just access the correct index, and then another "for" loop to do the correct action.
This code will work (if the retracted code were put in), but it looks very messy and is very hard to read. Is there a better way to do it that eliminates these nested "for" loops?
Thanks in advance.
void Sudoku::updateSquares(int grid[9][9], int possibleSolutions[9][9][10])
{
for (int i = 0; i < 9; i += 3)
{
for (int j = 0; j < 9; j += 3) //for every square:
{
//Other code
//...
//Other code
//updates the possibleSolutions array
for (int k = 0; k < 3; k++)
{
for (int l = 0; l < 3; l++) //for every element in every square:
{
if(grid[i+k][j+l] != 0)
continue;
for (int n = 0; n < 10; n++)
{
if(possibleSolutions[i+k][j+l][n] != 0 && numbers[n] == 0)
{
possibleSolutions[i+k][j+l][n] = 0;
possibleSolutions[i+k][j+l][0] -= 1; //reduce the size, which is held in [][][0]
}
}
}
}
}
}
}
You have implemented something called as "Exhaustive Searching" which is essentially trying out every possible combination of squares.
Have you heard about something called loop unwinding ?
--> Instead of 5 nested For loops use fewer nested loops multiple times; something like 2 nested loops .
Use a dynamic programming approach which is probably O(n^2)
Top Coder DP example.