views:

288

answers:

2

Pseudo code which i used:

for all V vertices: visited[n]=0

pick any vertex r from graph and set visited[r]=1

For all edges e incident to r
PQ.insert()

while(PQ is not empty)//line 1
  f=PQ.min()//f(r,s) is such that visited[s]=0
  for all edges e(s,t) incident to s//line 2
     if(visited[t]==0)
        PQ.insert(e);//line 3
     else
        PQ.delete(e);//line 4
  visited[s]=1;
end while;

According to my understanding,

line 1 executes V-1 times.

Line 2 : Sum of Degree of all the vertices times…..that is 2E times

For each line 2: Line 3 and line 4 take log E time because we’re adding/deleting all the edges to/from the PQ one by one.

So total time= V-1+2E.logE = E.log E

But books say it is E.logV

How??? Please help.

Sorry if it sounds silly :(

+1  A: 

for all edges e(s,t) incident to s

How many edges a node s can have at most?
V-1 at most. So, PQ operations have O(logV) time complexity.

Nick D
True...got it.Thank u so much :)
rajya vardhan
+1  A: 

O(log(V)) and O(log(E)) are the same.

  • E is at most V2.
  • log(V2) = 2*log(V)
  • which is an O(log(V))
yairchu
+1: actually E is at most V(V-1)/2. Good answer.
Nick D
@Nick D: right, but as I think in complexities, for me it's the same. E is at most O(V^2) :)
yairchu