Note that there are some explanatory texts on larger screens.

plurals
  1. PORunning time of minimum spanning tree? ( Prim method )
    primarykey
    data
    text
    <p>I have written a code that solves MST using Prim method. I read that this kind of implementation(using priority queue) should have O(E + VlogV) = O(VlogV) where E is the number of edges and V number of Edges but when I look at my code it simply doesn't look that way.I would appreciate it if someone could clear this up for me.</p> <p>To me it seems the running time is this:</p> <p>The while loop takes O(E) times(until we go through all the edges) Inside that loop we extract an element from the Q which takes O(logE) time. And the second inner loop takes O(V) time(although we dont run this loop everytime it is clear that it will be ran V times since we have to add all the vertices )</p> <p>My conclusion would be that the running time is: O( E(logE+V) ) = O( E*V ).</p> <p>This is my code:</p> <pre><code>#define p_int pair &lt; int, int &gt; int N, M; //N - nmb of vertices, M - nmb of edges int graph[100][100] = { 0 }; //adj. matrix bool in_tree[100] = { false }; //if a node if in the mst priority_queue&lt; p_int, vector &lt; p_int &gt;, greater &lt; p_int &gt; &gt; Q; /* keeps track of what is the smallest edge connecting a node in the mst tree and a node outside the tree. First part of pair is the weight of the edge and the second is the node. We dont remember the parent node beaceuse we dont need it :-) */ int mst_prim() { Q.push( make_pair( 0, 0 ) ); int nconnected = 0; int mst_cost = 0; while( nconnected &lt; N ) { p_int node = Q.top(); Q.pop(); if( in_tree[ node.second ] == false ) { mst_cost += node.first; in_tree[ node.second ] = true; for( int i = 0; i &lt; N; ++i ) if( graph[ node.second ][i] &gt; 0 &amp;&amp; in_tree[i]== false ) Q.push( make_pair( graph[ node.second ][i], i ) ); nconnected++; } } return mst_cost; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload