0% found this document useful (0 votes)
0 views

03_Supplementary_notes_Time_complexity_analysis

The document explains how to determine the running time complexities of code based on different types of statements, including sequences, if-then-else statements, for loops, and nested loops. It outlines that the total time for sequences is the sum of individual times, while for if-then-else, it is the maximum of the two possible sequences. Additionally, it details how for loops and nested loops can lead to complexities of O(N) and O(N^2) respectively, depending on their structure and iteration counts.

Uploaded by

podrilogy7753
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

03_Supplementary_notes_Time_complexity_analysis

The document explains how to determine the running time complexities of code based on different types of statements, including sequences, if-then-else statements, for loops, and nested loops. It outlines that the total time for sequences is the sum of individual times, while for if-then-else, it is the maximum of the two possible sequences. Additionally, it details how for loops and nested loops can lead to complexities of O(N) and O(N^2) respectively, depending on their structure and iteration counts.

Uploaded by

podrilogy7753
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

How to Determine Complexities

In general, how can you determine the running time of a piece of code? The answer is that it depends on
what kinds of statements are used.

1. Sequence of statements
1. statement 1;
2. statement 2;
3. ...
4. statement k;

The total time is found by adding the times for all statements:

Total time T = T(statement 1) + T(statement 2) + ... + T(statement k)

If each statement is "simple" (only involves basic operations) then the time for each statement is
constant and the total time is also constant: O(1).

In the following examples, assume the statements are simple unless noted otherwise.

2. if-then-else statements
1. if (condition) {
2. sequence of statements 1
3. }
4. else {
5. sequence of statements 2
6. }

Here, either sequence 1 will execute, or sequence 2 will execute. Therefore, the worst-case time is
the slowest of the two possibilities: max(time(sequence 1), time(sequence 2)). For example, if
sequence 1 is O(N) and sequence 2 is O(1) the worst-case time for the whole if-then-else statement
would be O(N).

3. for loops
1. for (i = 0; i < N; i++) {
2. sequence of statements
3. }

The loop executes N times, so the sequence of statements also executes N times. Since we assume
the statements are O(1), the total time for the for loop is N * O(1), which is O(N) overall.
4. Nested loops

First we'll consider loops where the number of iterations of the inner loop is independent of the
value of the outer loop's index. For example:

for (i = 0; i < N; i++) {


for (j = 0; j < M; j++) {
sequence of statements
}
}

The outer loop executes N times. Every time the outer loop executes, the inner loop executes M
times. As a result, the statements in the inner loop execute a total of N * M times. Thus, the
complexity is O(N * M). In a common special case where the stopping condition of the inner
loop is j < N instead of j < M (i.e., the inner loop also executes N times), the total complexity
for the two loops is O(N2).

Now let's consider nested loops where the number of iterations of the inner loop depends on
the value of the outer loop's index. For example:

for (i = 0; i < N; i++) {


for (j = i+1; j < N; j++) {
sequence of statements
}
}

Now we can't just multiply the number of iterations of the outer loop times the number of
iterations of the inner loop, because the inner loop has a different number of iterations each
time. So let's think about how many iterations that inner loop has. That information is given in
the following table:

Value of i Number of iterations of inner loop


0 N
1 N-1
2 N-2
... ...
N-2 2
N-1 1

So we can see that the total number of times the sequence of statements executes is:

N + (N-1) + (N-2) + ... + 3 + 2 + 1

We've seen that formula before . The total is O(N2).

You might also like