Iterating over all edges in a graph yields different time complexities

289 Views Asked by At

A quite simple question regarding time complexity: Let $G = (V,E)$ be a directed Graph. Suppose I want to iterate over all Edges $e \in E(G)$.

One way would be:
for $e \in E(G)$ do
$\qquad$ output $e$
end for
This has obviously a runtime of $\mathcal O(|E(G)|)$.

Another way would be:
for $v \in V(G)$ do
$\qquad$ for $e \in \delta^+(v)$ do
$\qquad\qquad$ output $e$
$\qquad$ end for
end for
But this has runtime $\mathcal O(|V(G)| + |E(G)|)$, since we will definitely encounter every edge, but also every vertex in the outer loop. This becomes very prominent for sparse graphs, e.g. an empty graph with no edges.

And there are probably many more ways to do the same thing, iterating over all edges.

What would be the "correct" runtime in any algorithm that iterates over edges? I know that the above problem boils down to data structures (list vs. incidence list), but what do e.g. authors in papers assume for their complexity analysis?