You can use adjacency lists to speed your solution up (but not for dense graphs), but even then, you are not going to get O(V log V) without a Fibonacci heap.
You can use adjacency lists to speed your solution up (but not for dense graphs), but even then, you are not going to get O(V log V) without a Fibonacci heap.. Maybe the Kruskal algorithm would be simpler for you to understand. It features no priority queue, you only have to sort an array of edges once. It goes like this basically: Insert all edges into an array and sort them by weight Iterate over the sorted edges, and for each edge connecting nodes I and j, check if I and j are connected.
If they are, skip the edge, else add the edge into the MST. The only catch is to be quickly able to say if two nodes are connected. For this you use the Union-Find data structure, which goes like this: int TMAX_#_OF_NODES int getParent(int a) { if (Ta==-1)return a; return Ta=getParent(Ta); } void Unite(int a,int b) { if (rand()&1) Ta=b; else Tb=a; } In the beginning, just initialize T to all -1, and then every time you want to find out if nodes A and B are connected, just compare their parents - if they are the same, they are connected (like this getParent(A)==getParent(B)).
When you are inserting the edge to MST, make sure to update the Union-Find with Unite(getParent(A),getParent(B)). The analysis is simple, you sort the edges and iterate over using the UF that takes O(1). So it is O(E logE + E ) which equals O(E log E).
That is it ;-).
I know Kruskals algorithm, but I wanted to understand this one also :-). If I use adjacency lists for Prim then I get: The while + for loop loops over all edges and inserts them into a heap. This should be then E*log(E).
Is this the best complexity you can get with this approach(using a heap, not Fibbonaci heap)? – sklitzz Jan 10 '10 at 12:40 2 Yeah, you check each edge at most twice (from both nodes) and the queue has E edges at maximum, which results in O(E log E). But we do not write it like that, because constant factors are irrelevant in O() notation.So O(E log E) = O(E log (V^2)) = O(E * 2 log V) = O(E log V).
– supo Jan 10 '10 at 13:14 This(the comment above) is the answer that I wanted, thank you I understand now :-) – sklitzz Jan 11 '10 at 20:52.
I did not have to deal with the algorithm before, but what you have implemented does not match the algorithm as explained on Wikipedia. The algorithm there works as follows. But all vertices into the queue.
O(V) While the queue is not empty... O(V) Take the edge with the minimum weight from the queue. O(log(V)) Update the weights of adjacent vertices. O(E / V), this is the average number of adjacent vertices.
Reestablish the queue structure. O(log(V)) This gives O(V) + O(V) * (O(log(V)) + O(V/E)) = O(V) + O(V) * O(log(V)) + O(V) * O(E / V) = O(V) + O(V * log(V)) + O(E) = O(V * log(V)) + O(E) exactly what one expects.
– sklitzz Nov 19 '09 at 18:40 The priority of each vertex is the cost of connecting the vertex with the current spanning tree - this is the minimum weight of all edges connecting the vertex with the current tree or infinity if there is no edge. All this values are initialized with infinity and updated each time a vertex is moved from the queue to the tree. – Daniel Brückner Nov 20 '09 at 11:07.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.