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);
true
. Indfs
, a very similar argument applies. In more complicated cases you might need to perform amortized analyses. – Raphael Nov 23 '18 at 15:27