15.1 For Loops
Count, count, count...
Last updated
Count, count, count...
Last updated
Now that we've seen some runtime analysis, let's work through some more difficult examples. Our goal is to get some practice with the patterns and methods involved in runtime analysis. This can be a tricky idea to get a handle on, so the more practice the better.
Last time, we saw the function dup1, that checks for the first time any entry is duplicated in a list:
We have two ways of approaching our runtime analysis:
Counting number of operations
Geometric visualization
Since the main repeating operation is the comparator, we will count the number of "==" operations that must occur.
We can also approach this from a geometric view.
Let's look at a more involved example next. Consider the following function, with similar nested for loops:
The outer loop advances by multiplying i
by 2 each time. The inner loop runs from 0 to the current value of i
. The two operations inside the loop are both constant time, so let's approach this by asking "how many times does this print out "hello" for a given value of N?"
Our visualization tool from above helped us see dup1's runtime, so let's use a similar approach here. We'll lay out the grid for the nested for loops, and then track the total number of print statements needed for a given N below.
If N is 1, then i
only reaches 1, and j
is only 0, since 0 < 1. So there is only one print statement:
We can keep filling out our diagram to get a fuller picture. Here it is up to N = 18:
What we see, if we add up all the counts at each stage of the loops, is that the number of print statements is:
(if N is a power of 2).
Again, we can think of this in two ways. Since we're already on a graphical roll, let's start there.
We can obtain the same result by solving our equation from above with the power of mathematics:
Again if N is a power of 2.
Unfortunately, they're not. And we know this because we just did two nested for loop examples above, each with different runtimes.
In the end, there is no shortcut to doing runtime analysis. It requires careful thought. But there are a few useful techniques and things to know:
Find exact sum
Write out examples
Draw pictures
We used each of these techniques above.
Also used in the examples above are two important sums you'll see very often:
You saw both of these above, and they'll return again and again in runtime analysis.
The first time through the outer loop, the inner loop will run times. The second time, it will run times. Then , , .... all the way till running the inner loop exactly time when i = . In the worst case, we have to go through every entry, and the outer loop runs times.
Then, let = total number of "==" operations that have occurred. The number of comparisons is:
where is part of the family.
Since "==" is a constant time operation, the overall runtime in the worst case is .
Let's draw out when we use == operations in the grid of combinations:
We see that the number of == operations is the same as the area of a right triangle with a side length of . Since area is in the family, we see again that the overall runtime is .
If N is 2, the next time through the loop i
will be and j
can reach 1. The total number of print statements will be 3 = 1 (from first loop) + 2 (from second loop).
After the second loop, , which is greater than , so the outer loop does not continue, and ends after i = 2
, just like N = 2 did.
If we graph the trajectory of 0.5 N (lower dashed line), and 4N (upper dashed line), and itself (the red staircase line), we see that C(N) is fully bounded between those two dashed lines.
Therefore, the runtime (by definition) must also be linear: .
For example, if :
And by removing lesser terms and multiplicative constants, we know that is in the linear family, so the runtime is .
We now get a more exact value to the red-staircase line plotted below, which is .
It would be really nice if there were some magic way to look at an algorithm and just know its runtime. And it would be even nicer if all nested for loops have a runtime of .
Sum of First Natural Numbers:
Sum of First Powers of 2: