# Big-O notation for nested loops that might skip iterations

When you have an algorithm that may skip a lot of iterations due to a hash table lookup, do you still count the iterations that are exited immediately?

Hypothetical example:

var n = input.length; //eg, [1,4,6,2,0,8]

for (var i = 0; i < n; i++) {
visited[i] = true;

for (var j = 0; j < n; j++) {
if (visited[j] === true) {
continue;
}
}
}


Another example:

function dfs(node) {
if (visited[node]) {
return 0;
}

visited[node] = true;

var edges = edges[node];

for (var i = 0; i < edges.length; i++) {
dfs(edges[i]);
}
}

//dfs(graph_that_has_cycles);

• Can you construct an example where immediate exit matters to big $O$ analysis? Can you construct another example where immediate exit does not matter to big $O$ analysis? If you can do both, you can write an answer on your own. If not, I would like to see your explanation why in the question. – Apass.Jack Nov 23 '18 at 1:36
• Depends on what you want to count, obviously. What's your cost model? – Raphael Nov 23 '18 at 15:24
• While the standard "replace loop with sum" method doesn't work as well for such loops, the "count globally" method. The specifics depend on the loop at hand. In your first example, it's obvious how often the condition is checked in the inner loop, and how often it evaluates to true. In dfs, a very similar argument applies. In more complicated cases you might need to perform amortized analyses. – Raphael Nov 23 '18 at 15:27
• This is like asking, "If you're computing the total cost of your groceries, do you have to include the cost of items that you picked up but put back on the shelf?" Don't think in terms of learning recipes and rules to follow; rather, figure out what you're trying to compute and then figure out how to compute that thing. – David Richerby Nov 23 '18 at 16:32

If the average number of times the inner loop is $$O(f(n))$$ then the total complexity is $$O(n \times f(n))$$.

Finding that average can be tricky when the condition is not trivial. however assuming that it never drops a loop will give you a worst case. This worst case will also be valid when the amount of inner loops skipped is a constant factor.

However in the first example the loop can be simplified to

for (var i = 0; i < n; i++) {
visited[i] = true;

for (var j = i+1; j < n; j++) {
//...
}
}


This is $$O(n^2)$$. Because $$\Sigma_{i=0}^n i = \frac{n(n+1)}{2} = O(n^2)$$