Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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..</p> <p>Maybe the <a href="http://en.wikipedia.org/wiki/Kruskal%27s_algorithm" rel="nofollow noreferrer">Kruskal algorithm</a> 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:</p> <ul> <li>Insert all edges into an array and sort them by weight</li> <li>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.</li> </ul> <p>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:</p> <pre><code>int T[MAX_#_OF_NODES] int getParent(int a) { if (T[a]==-1)return a; return T[a]=getParent(T[a]); } void Unite(int a,int b) { if (rand()&amp;1) T[a]=b; else T[b]=a; } </code></pre> <p>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 <code>getParent(A)==getParent(B)</code>). When you are inserting the edge to MST, make sure to update the Union-Find with <code>Unite(getParent(A),getParent(B))</code>.</p> <p>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).</p> <p>That is it ;-)</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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