Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time complexity of adjacency list representation?

I am going through this link for adjacency list representation.

http://www.geeksforgeeks.org/graph-and-its-representations/

I have a simple doubt in some part of a code as follows :

// A utility function to print the adjacenncy list representation of graph
void printGraph(struct Graph* graph)
{
    int v;
    for (v = 0; v < graph->V; ++v)
    {
        struct AdjListNode* pCrawl = graph->array[v].head;
        printf("\n Adjacency list of vertex %d\n head ", v);
        while (pCrawl)
        {
            printf("-> %d", pCrawl->dest);
            pCrawl = pCrawl->next;
        }
        printf("\n");
    }
}

Since, here for every V while loop is executed say d times where d is the degree of each vertex.

So, I think time complexity is like

d0 + d1 + d2 + d3 ....... +dV where di is the degree of each vertex.

All this sums up O(E), but the link says that time complexity is O(|V|+|E|)


Not sure what is the problem with the understanding. Some help needed here

like image 437
Garrick Avatar asked Jul 08 '17 06:07

Garrick


People also ask

Why is space complexity of adjacency list?

In general, the space complexity of an adjacency list is O ( V + E ) O(V + E) O(V+E), and in the worst case, it is O ( V 2 ) O(V^{2}) O(V2) when every node is connected to all the other nodes.

Which is faster adjacency list or matrix?

An adjacency list is the more common representation because it is the more efficient than adjacency matrix.

What is the time complexity of DFS if the adjacency list is used?

The site http://web.eecs.utk.edu/~huangj/CS302S04/notes/graph-searching.html describes that when an adjacency list is used then, DFS and BFS have complexity O(V+E), and if an adjacency matrix is used, the complexity is O(V2).

What is the time complexity of BFS is when adjacency list is used?

The Time complexity of BFS is O(V + E) when Adjacency List is used and O(V^2) when Adjacency Matrix is used, where V stands for vertices and E stands for edges.


1 Answers

The important thing here is that for the time complexity to be valid, we need to cover every possible situation:

  • The outer loop is executed O(|V|) regardless of the graph structure.
    • Even if we had no edges at all, for every iteration of the outer loop, we would have to do a constant number of operations (O(1))
    • The inner loop is executed once for every edge, thus O(deg(v)) times, where deg(v) is the degree of the current node.
    • Thus the runtime of a single iteration of the outer loop is O(1 + deg(v)). Note that we cannot leave out the 1, because deg(v) might be 0 but we still need to do some work in that iteration
  • Summing it all up, we get a runtime of O(|V| * 1 + deg(v1) + deg(v2) + ...) = O(|V| + |E|).

As mentioned before, |E| could be rather small such that we still need to account for the work done exclusively in the outer loop. Thus, we cannot simply remove the |V| term.

like image 79
Tobias Ribizel Avatar answered Oct 17 '22 23:10

Tobias Ribizel