retagged by
6,352 views

1 Answer

Best answer
9 votes
9 votes
Given an adjacency-list representation Adj of a directed graph, the out-degree of a vertex $u$ is equal to the length of Adj[u], and the sum of the lengths of all the adjacency lists in Adj is $|E|.$ Thus the time to compute the out-degree of every vertex is

$Θ(|V| + |E|).$

The in-degree of a vertex $u$ is equal to the number of times it appears in all the lists in Adj. If we search all the lists for each vertex, the time to compute the in-degree of every vertex is $Θ (|V||E|).$

Alternatively, we can allocate an array $T$ of size $|V|$ and initialize its entries to zero. Then we only need to scan the lists in Adj once, incrementing $T [u]$ when we see $u$ in the lists.

The values in $T$ will be the in-degrees of every vertex. This can be done in $Θ (|V| + |E|)$ time with $Θ (|V|)$ additional storage.
edited by

Related questions

0 votes
0 votes
1 answer
2
8 votes
8 votes
2 answers
3