You analysis is correct for a completely connected graph. However, note that for a completely connected graph the number of edges E
is O(V^2)
itself, so the notation O(V+E)
for the space complexity is still correct too.
However, the real advantage of adjacency lists is that they allow to save space for the graphs that are not really densely connected. If the number of edges is much smaller than V^2
, then adjacency lists will take O(V+E)
, and not O(V^2)
space.
Note that when you talk about O
-notation, you usually have three types of variables (or, well, input data in general). First is the variables dependence on which you are studying; second are those variables that are considered constant; and third are kind of "free" variables, which you usually assume to take the worst-case values. For example, if you talk about sorting an array of N
integers, you usually want to study the dependence of sorting time on N
, so N
is of the first kind. You usually consider the size of integers to be constant (that is, you assume that comparison is done in O(1)
, etc.), and you usually consider the particular array elements to be "free", that is, you study that runtime for the worst possible combination of particular array elements. However, you might want to study the same algorithm from a different point of view, and it will lead to a different expression of complexity.
For graph algorithms, you can, of course, consider the number of vertices V
to be of first kind, and the number of edges to be the third kind, and study the space complexity for given V
and for the worst-case number of edges. Then you indeed get O(V^2)
. But it is also often useful to treat both V
and E
as variables of the first type, thus getting the complexity expression as O(V+E)
.