Adjacency-list scale chart in degrees

I came across this question in which it was necessary to calculate the degree of each of the node graph from the adjacency list view.

for each u
   for each Adj[i] where i!=u
     if (i,u) ∈ E
         in-degree[u]+=1

Now, according to me, its temporal complexity should be O(|V||E|+|V|^2), but instead the solution that I called was equal O(|V||E|).

Please help and tell which one is correct.

+4
source share
1 answer

Instead of O (| V || E |), the complexity of the calculated indices is O (| E |). Consider the following pseudo-code for calculating the indices of each node:

for each u
  indegree[u] = 0;

for each u
  for each v \in Adj[u]
    indegree[v]++;

O (| V |). : v | E | , | V | . O (| V || E |). , , O (| E |).

+7

Source: https://habr.com/ru/post/1535498/


All Articles