The algorithm
- Put the starting vertex in S and push every edge at it onto a heap keyed by weight.
- Pop the lightest entry. If both its ends are already in S, discard it and pop again.
- Otherwise take the edge, put its far end u into S, and push every edge at u whose far end is not yet in S.
- Stop when V - 1 edges have been taken, or when the heap empties first — in which case the graph was not connected.
Correctness is the cut property applied once per step, with S as the side. The edge taken is by construction a lightest edge crossing that cut, so it lies in some minimum spanning tree extending what has been built. That is the same induction the sorted method uses; only the choice of cut differs.
Example · Seven vertices, seven pops, one of them staleThe lab opens on 1-2 4, 1-3 3, 2-3 2, 2-4 5, 3-4 7, 4-5 1, 5-6 6, 5-7 8, 6-7 2, starting at vertex 1. The pops come out 1-3 at 3 adding vertex 3, 2-3 at 2 adding vertex 2, then 1-2 at 4 with both ends already in S — discarded — then 2-4 at 5 adding 4, 4-5 at 1 adding 5, 5-6 at 6 adding 6, and 6-7 at 2 adding 7. Six edges, weight 19, the same tree the sorted method built. The counters read 9 pushes, 7 pops and 12 key comparisons.
The pop order is worth a second look, because it is not sorted. 1-3 at weight 3 comes out before 2-3 at weight 2, and 2-4 at 5 before 4-5 at 1. The heap returns the lightest entry it HOLDS, and an edge is only held once its tail has joined the tree — 4-5 was not a candidate until 4 was in. That is the whole difference from the sorted method, which considers every edge in one global order.
Stale entries, and why they are tolerated
Nine edges were pushed and seven popped. The two never popped are the two the loop stopped before reaching, and one of the seven was stale. A heap has no cheap way to find and remove an entry that is no longer wanted, so the standard answer is to leave it and test on the way out. The cost is that the heap may hold up to E entries rather than V, which is where the E log V term in the usual bound comes from.
The lab's lazy preset was built to fill the heap with such entries, and measuring it is instructive: on 1-2 1, 1-3 1, 1-4 1, 2-3 9, 2-4 9, 3-4 9, 1-5 2, 2-5 3, 3-5 4 the run pushes 9 entries, pops 4, and discards none of them. The heavy edges do become stale, but the four light edges at vertex 1 are enough to finish the tree, and the loop stops before any stale entry reaches the top. Staleness costs memory here and no pops at all — which is a different thing from what the preset's name suggests, and the count is what settles it.
The reference column, and what may not be read off it
The panel prints a column labelled as E times log₂ V for reference. It is computed as the number of edges times the integer part of the base-two logarithm of the number of vertices, so on the opening graph it is 9 times 2, which is 18. The measured comparison count is 12. Those two numbers are not the same kind of thing and the panel says so: the first is a count of something that happened on one graph from one starting vertex, and the second is a scale for what the heap operations would cost in the worst case.
Specifically, 12 being below 18 is not evidence that the algorithm beats its bound, and 24 from vertex 4 being above it is not evidence that it fails one. The reference is a number to compare growth against across several graphs, and the lab's other presets are there to be swept: a star on six vertices gives 12 comparisons against a reference of 10, and the nine-edge graph on five vertices gives 18 against 18. No verdict is printed beside the column, deliberately.
Against the other algorithm
Both algorithms are run on whatever graph you type and the two weights are printed together. On the opening graph both give 19. On the ties preset 1-2 2, 1-3 2, 2-3 2, 3-4 1 both give 5, and here they take the same three edges in different orders: one starts with the edge of weight 1 because it sorted first, the other reaches it last because its tail only joined the tree at the end. Equal weight, equal edge set, different schedule — and on a graph with a genuine choice between tied edges, the edge sets can differ too.
The measured counts, beside the bound. From vertex 1: 9 pushes, 7 pops, 12 comparisons, weight 19. From vertex 4: 9 pushes, 7 pops, 24 comparisons, weight 19. The bound is O(E log V) on every graph from every start, and it comes from at most E pushes and at most E pops, each costing O(log V) comparisons on a heap holding at most E < V² entries. Four starting vertices on one graph is not a bound, and the spread between 12 and 24 is exactly the reason to say so.