Question

5. The in-degree of a vertex in a directed graph is the number of edges directed into it. Here is an algorithm for labeling e
0 0
Add a comment Improve this question Transcribed image text
Answer #1

For simplicity, I will represent theta with 'O'

Number of vertices = n

Number of edges = m

Now for the first for loop
for each vertex i: ---> O(n)
i.indegrees = 0 ---> O(1)

and for the second for loop
for each vertex i: ----> O(n)
for each neighbor j of i: ----> O(2m)
j,indegree = j.indegree + 1 ----> O(1)

Why O(2m) ?
It's because we will traverse each edge twice. Let's say there are two nodes (- Node1 and Node2) and one edge (Edge1-2).
For i = Node1
edge = Edge1-2
For i = Node2
edge = Edge2-1
As you can see we traverse 2 nodes once and one edge twice(one for each side of the node connected by the edge)

Thus for the first for loop ---> O(n), and for the second for loop ---> O(n*2m) = O(2nm) = O(nm)

Finally ---> O(n + nm) = O(nm) since nm >>> n

Add a comment
Know the answer?
Add Answer to:
5. The in-degree of a vertex in a directed graph is the number of edges directed...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT