The Gateway to Computer Science Excellence
+2 votes
Suppose that all edge weights in a graph are integers in the range from 1 to |V|. How fast can you make Prim's algorithm run? What if the edge weights are integers in the range from 1 to W for some constant W?

Answer given :-

The running time of Prims algorithm is composed of :

• O(V) initialization.

• O(V · time for EXTRACT-MIN).

• O(E · time for DECREASE-KEY).

If the edges are in the range 1, . . . , |V| the Van Emde Boas priority queue can speed up EXTRACT- MIN and DECREASE-KEY to O(lg lg V) thus yielding a total running time of O(V lg lg V +E lg lg V) = O(E lg lg V ). If the edges are in the range from 1 to W we can implement the queue as an array [1...W+1] where the ith slot holds a doubly linked list of the edges with weight i. The (W+1)st slot contains ∞. EXTRACT-MIN now runs in O(W) = O(1) time since we can simply scan for the first nonempty slot and return the first element of that list. DECREASE-KEY runs in O(1) time as well since it can be implemented by moving an element from one slot to another.

Doubt 1 : Incase of range 1, . . . , |V| , how EXTRACT- MIN and DECREASE-KEY speed up to O(lg lg V).

Doubt 2 : why doubly linked list is used for range 1.... |W| ??
in Algorithms by Active (1.2k points) | 99 views

Note that, in any case, the algorithm performs O(1) work and then possibly recurses on a subtree over a universe of size M1/2 (an m/2 bit universe). This gives a recurrence for the running time of T(m)=T(m/2) + O(1), which resolves to O(log m) = O(log log M).


i.e. ans for 1st query


Superb question

1 Answer

+3 votes
Best answer

for clarification of your 1st doubt, you can read


for 2nd doubt, i hope there is no need to use doubly linked list

 array [1...W+1]  ===> already array is sorted by edge weights

where ith slot holds a doubly linked list of the edges with weight i. 

let assume there are 5 edges with weight 15,

they use double linked list due to efficiently usage, if you use single linked list, after extracting one edge from it, you need to update it's previous node, next pointer. ( but no problem with using singly linked list )

but when you decrease the key, let the 3rd edge in weight 15,....

it need to update 2nd edge next pointer, array element points to new edge ( but no problem with using singly linked list )



by Veteran (65.5k points)
selected by
doubly linked list is better


because find next and find prev two things need here
yes mam, using double linked list is better but not mandatory

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,741 questions
57,252 answers
104,697 users