I have a question about time complexity of the following program below. I know why the first for loop is n + 2 but for the second for loop why is it (n+1)/2?
// sample for loop // this is not a fully coded program just for loop time complexity analysis
for (int i=n; i>=0; i--) //n + 2
for (int j=i; j<n; j++) // (n+2)(n+1)/2
cout << i << “,” << j <<endl; //(n+1)(n)/2
The inner loop runs ni
times; ie it runs 0 times when i==n
, then 1 time when i == n-1
, etc, up to n
times when i == 0
; for a total of n*(n+1)/2
times. (not (n+1)*(n+2)/2
as stated in your comments).
Since the inner loop is initiated n+1
times (not n+2
, as you state), it's fair to say that the average number of times it runs is n/2
.
First your first loop has time complexity of O(n) and nested loop normal has a time complexity of O(n^2), but the inner loop is executed i times, for each value of i. The outer loop is executed n times, thus you see a pattern of execution like this: 1 + 2 + 3 + 4 + ... + n times
Specific Info:
O(1): Time complexity of a function (or set of statements) is considered as O(1) if it doesn't contain loop, recursion and call to any other non-constant time function.
O(n): Time Complexity of a loop is considered as O(n) if the loop variables is incremented / decremented by a constant amount. For example following functions have O(n) time complexity.
O(n^c): Time complexity of nested loops is equal to the number of times the innermost statement is executed.
Source - > Link
Iterating i
from 1 to N
, and j
from 1 to the current value of i
inclusive, is like exploring one half of a rectangle of dimensions N
by N+1
, split diagonally. To appreciate this, draw out the table for yourself on paper: i
on the rows, j
on the columns, and a tick in each cell that the nested loop visits.
The time complexity of the for
loop is O(N^2).
I just answered a similar question on codereview.stackexchange.com .
Here's a simple program that illustrates the time complexity of the loop.
#include <iostream>
using namespace std;
int main()
{
const int N = 10;
int numTotalOps = 0;
for (int i = 0; i < N; ++i)
{
size_t numOps = 0;
for (int j=i; j < N; j++)
{
cout << i << "," << j << endl;
++numTotalOps;
++numOps;
}
cout << "\nNumber of operations for i = " << i << ": " << numOps << endl;
}
cout << "\nNumber of total operations: " << numTotalOps << endl;
return 0;
}
Output:
0,0
0,1
0,2
0,3
0,4
0,5
0,6
0,7
0,8
0,9
Number of operations for i = 0: 10
1,1
1,2
1,3
1,4
1,5
1,6
1,7
1,8
1,9
Number of operations for i = 1: 9
2,2
2,3
2,4
2,5
2,6
2,7
2,8
2,9
Number of operations for i = 2: 8
3,3
3,4
3,5
3,6
3,7
3,8
3,9
Number of operations for i = 3: 7
4,4
4,5
4,6
4,7
4,8
4,9
Number of operations for i = 4: 6
5,5
5,6
5,7
5,8
5,9
Number of operations for i = 5: 5
6,6
6,7
6,8
6,9
Number of operations for i = 6: 4
7,7
7,8
7,9
Number of operations for i = 7: 3
8,8
8,9
Number of operations for i = 8: 2
9,9
Number of operations for i = 9: 1
Number of total operations: 55
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.