Notes on Minimum Spanning Tree problem A Priority
Notes on Minimum Spanning Tree problem A Priority queue trouble Usual theoretical and conceptual descriptions of Prim's (Dijkstra's, etc. ) algorithm say: ". . . store nodes in a priority queue". Technically, this is nearly impossible to do. The graph and the nodes are defined separately and stored elsewhere in the memory. The node has no reference to its position in the queue. The programmer does not know where is the node in the queue. So, how to move a node inside the queue according to the algorithm demands? Standard solution: Do not move a node, enqueue a "copy of a node", possibly more times. When a copy of the node with the smallest value (=highest priority) among all its copies appears at the top of the queue it does its job exactly according to the algorithm prescription. From that moment on, all other copies of the node which are still in the queue become useless and must be ignored. The easiest way to ignore a copy is to check it when it later appears at the top of the queue: If the node is already closed, ignore the copy, pop it and process the next top of the queue. If the node is still open, process it according to the algorithm. 1
30 17 50 y u 8 5 x z 18 INF w Easy 2 10 x. . . u. . . y. . . z. . . w. . . 10. . . 17. . . 30. . . 50. . . INF. . . q. insert(y); q. insert(z); q. insert(w); Difficult 17 u 15 y 8 5 x z w 12 2 10 . . . w. . . z. . . u. . . y. . . . 12. . . 15. . . 17. . . 18. . . Eas y . . w. . . z. . . u. . . y. . . z. . . w. . . 12. . . 15. . . 17. . . 18. . . 30. . . 50. . . INF. . . // push the nodes once more to the queue. The older copies of nodes will get to the top of the queue later than the new copies (which have higher priority). The older copy gets to the top when the node had been processed and closed earlier. Thus: If the node at the top of the queue is closed just pop it and do not process it any more. 2
Example of Prim algorithm implementation using standart library priority queue void MST_Prim(Graph g, int start, final int [] dist, int [] pred ) { // allocate structures int currnode = start, currdist, neigh; int INF = Integer. MAX_VALUE; boolean [] closed = new boolean[g. N]; Priority. Queue <Integer> pq = new Priority. Queue<Integer>( g. N, new Comparator<Integer>() { @Override public int compare(Integer n 1, Integer n 2) { if( dist[n 1] < dist[n 2] ) return -1; if( dist[n 1] > dist[n 2] ) return 1; return 0; } } ); // initialize structures pq. add( start ); for( int i = 0; i < g. N; i++ ) pred[i] = i; Arrays. fill( dist, INF ); Arrays. fill( closed, false ); dist[start] = 0; 3
Example of Prim algorithm implementation using standart library priority queue for( int i = 0; i < g. N; i++ ) { // take the closest node and skip the closed ones while( closed[currnode = pq. poll()] == true ); // and expand the closest node for( int j = 0; j < g. dg[currnode]; j++ ){ neigh = g. edge[currnode][j]; if( !closed[neigh] && ( dist[neigh] > g. w[currnode][j]) ) { dist[neigh] = g. w[currnode][j]; pred[neigh] = currnode; pq. add(neigh); } closed[currnode] = true; } } } A very small change produces Dijkstra's algorithm: if( !closed[neigh] && ( dist[neigh] > g. w[currnode][j] + dist[currnode]) ) { dist[neigh] = g. w[currnode][j] + dist[currnode]; 4
Prim and Dijkstra Algorithms compared void MST_Prim( Graph G, function weight, Node startnode ) for each u in G. V: u. dist = INFINITY; u. parent = NIL startnode. dist = 0; Priority. Queue Q = G. V while !Q. is. Empty() u = Extract-Min(Q) for each v in G. Adj[u] if (v in Q) and v. dist > weight(u, v) v. parent = u v. dist = weight(u, v) void Dijkstra( Graph G, function weight, Node startnode ) for each u in G. V: u. dist = INFINITY; u. parent = NIL startnode. dist = 0; Priority. Queue Q = G. V while !Q. is. Empty() u = Extract-Min(Q) for each v in G. Adj[u] if (v in Q) and v. dist > weight(u, v) + u. dist v. parent = u v. dist = weight(u, v) + u. dist 5
Disjoint-set data structure alias Union-Find structure Only 3 operations are needed: Initialize() Union( representative. A, representative. B ) // merges the two sets represented // by the two representatives Find( node. X ) // returns a representative of the set to which X belongs int [] boss; int [] rank; void UF_init( int n ) boss = new int [n]; rank = new int [n]; for( int i = 0; i < boss[i] = i; // rank[i] = 0; // } } { n; i++ ) { everybody's their own boss necessary? Easy experiment, try it at home: When the end nodes of the inspected edges are chosen uniformly randomly then the average depth of a queried node in the Union-Find forest is less than 2. 6
Union with rank comparison b c b f a d c e a f d e Union(b, f) boss. . . b. . . f. . . node. . . a. . . b. . . c. . . d. . . e. . . f. . . boss. . . b. . . f. . . b. . . rank. . . node. . . a. . . b. . . c. . . d. . . e. . . f. . . ? . . . void UF_union( int root. A, int root. B ) { if( rank[root. B] > rank[root. A] ) boss[root. A] = root. B; else { boss[root. B] = root. A; if( rank[root. B] == rank[ root. A ] ) rank[root. A]++; } } . . . // change rank? 7
Find with path compression a e c b d d e a c b Find(b) node. . . a. . . b. . . c. . . d. . . e. . . boss. . . d. . . c. . . e. . . d. . . a. . . boss. . . d. . . int UF_find( int a ) { int parent = boss[a]; if( parent != a ) boss[a] = UF_find( parent ); // path compression return boss[a]; } int find( int a ) // for C experts { return( boss[a] == a ? a : (boss[a] = find(boss[a])) ); } 8
Kruskal algorithm and Union-Find scheme example 8 0 15 1 18 27 12 6 22 2 14 7 9 21 12 0 13 2 26 17 8 23 4 13 7 3 14 5 3 10 9 24 1 11 4 20 5 10 11 25 16 15 11 6 16 2 3 4 5 6 7 8 9 10 node 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 boss 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 rank 0 0 0 13 0 14 0 15 17 1 0 12 19 0 16 0 17 0 9
Kruskal algorithm and Union-Find scheme example 8 0 15 1 18 27 12 6 22 2 14 7 9 21 12 0 1 2 17 8 4 4 5 1 7 8 10 9 5 20 5 10 11 25 16 15 9 11 4 3 24 14 6 7 3 26 23 13 2 10 11 12 19 6 16 13 14 17 16 17 15 node 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 boss 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 14 16 17 rank 0 0 0 0 1 0 0 0 10
Kruskal algorithm and Union-Find scheme example 8 0 15 1 18 27 12 6 22 2 14 7 12 0 1 13 2 17 23 4 1 6 7 10 9 5 20 5 10 11 25 16 15 9 11 4 3 24 14 5 7 3 26 8 9 21 13 2 10 11 12 19 6 16 13 14 8 17 16 17 15 node 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 boss 0 1 2 3 4 5 6 7 2 9 10 11 12 13 14 14 16 17 rank 0 0 1 0 0 0 1 0 0 0 11
Kruskal algorithm and Union-Find scheme example 8 0 15 1 18 27 12 6 22 2 14 7 9 21 12 0 1 3 14 4 8 10 9 24 1 5 6 5 20 5 10 11 25 16 15 7 11 4 3 17 8 4 7 3 26 23 13 2 9 11 12 19 6 16 13 14 10 17 16 17 15 node 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 boss 0 1 2 3 4 5 6 7 2 9 4 11 12 13 14 14 16 17 rank 0 0 1 0 0 0 12
Kruskal algorithm and Union-Find scheme example 8 0 15 1 18 27 12 6 22 2 14 7 9 21 12 0 1 3 14 4 8 10 9 24 1 5 6 5 20 5 10 11 25 16 15 7 11 4 3 17 8 4 7 3 26 23 13 2 9 11 6 16 12 10 19 14 13 17 16 17 15 node 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 boss 0 1 2 3 4 5 6 7 2 9 4 11 12 14 14 14 16 17 rank 0 0 1 0 0 0 13
Kruskal algorithm and Union-Find scheme example 8 0 15 1 18 27 12 6 22 2 14 7 12 0 1 13 2 17 23 4 3 8 10 9 1 4 5 5 20 5 10 11 25 16 15 6 11 4 3 24 14 11 7 3 26 8 9 21 13 2 7 9 6 16 12 10 19 14 13 17 16 17 15 node 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 boss 0 1 2 3 4 5 6 7 2 9 4 4 12 14 14 14 16 17 rank 0 0 1 0 0 0 14
Kruskal algorithm and Union-Find scheme example 8 0 15 1 18 27 12 6 22 2 14 7 12 0 1 13 2 17 23 4 3 8 10 9 1 4 5 5 20 5 10 11 25 16 15 6 11 4 3 24 14 11 7 3 26 8 9 21 13 2 7 9 6 16 12 10 19 14 13 17 16 15 17 node 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 boss 0 1 2 3 4 5 6 7 2 9 4 4 12 14 14 14 16 16 rank 0 0 1 0 0 0 0 0 1 0 15
Kruskal algorithm and Union-Find scheme example 8 0 15 1 18 27 12 6 22 2 14 7 12 0 1 17 23 4 13 2 3 11 10 9 1 14 5 5 20 5 10 11 25 16 15 6 11 4 3 24 4 8 7 3 26 8 9 21 13 2 7 9 6 16 12 10 19 14 13 17 16 15 17 node 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 boss 0 1 2 4 4 5 6 7 2 9 4 4 12 14 14 14 16 16 rank 0 0 1 0 0 0 0 0 1 0 16
Kruskal algorithm and Union-Find scheme example 8 0 15 1 18 27 12 6 22 2 14 7 12 0 13 2 1 17 23 4 3 11 10 9 1 14 5 5 20 5 10 11 25 16 15 6 11 4 3 24 4 8 7 3 26 8 9 21 13 2 7 9 6 16 12 10 19 14 13 17 16 15 17 node 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 boss 0 0 2 4 4 5 6 7 2 9 4 4 12 14 14 14 16 16 rank 1 0 1 0 0 0 0 0 1 0 17
Kruskal algorithm and Union-Find scheme example 8 0 15 1 18 27 12 6 22 2 14 7 12 0 13 2 1 17 23 4 3 11 1 14 5 11 4 5 3 10 9 24 4 8 7 3 26 8 9 21 13 2 20 5 10 11 25 16 15 6 9 19 6 16 12 14 10 7 13 17 16 15 17 node 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 boss 0 0 2 4 4 5 6 14 2 9 4 4 12 14 14 14 16 16 rank 1 0 1 0 0 0 0 0 1 0 18
Kruskal algorithm and Union-Find scheme example 8 0 15 1 18 27 12 6 22 2 14 7 12 0 13 2 1 14 4 8 3 11 10 9 24 1 10 5 20 5 10 11 25 16 15 5 11 4 3 17 23 4 7 3 26 8 9 21 13 2 6 19 6 16 12 14 9 7 13 17 16 15 17 node 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 boss 0 0 2 4 4 5 6 14 2 4 4 4 12 14 14 14 16 16 rank 1 0 1 0 0 0 0 0 1 0 19
Kruskal algorithm and Union-Find scheme example 8 0 15 1 18 27 12 6 22 2 14 7 12 0 13 2 1 14 3 11 9 20 5 10 16 6 10 5 11 25 15 4 8 10 9 24 1 11 4 3 17 23 4 7 3 26 8 9 21 13 2 19 6 16 12 14 5 7 13 17 16 15 17 node 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 boss 0 0 2 4 4 4 6 14 2 4 4 4 12 14 14 14 16 16 rank 1 0 1 0 0 0 0 0 1 0 20
Kruskal algorithm and Union-Find scheme example 8 0 15 1 18 27 12 6 22 2 14 7 12 0 13 2 1 17 23 4 3 11 10 9 24 1 14 9 5 20 5 10 11 25 16 15 19 6 16 12 10 11 4 3 4 8 7 3 26 8 9 21 13 2 14 5 6 7 13 17 16 15 17 node 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 boss 0 0 2 4 4 4 14 14 2 4 4 4 12 14 14 14 16 16 rank 1 0 1 0 0 0 0 0 1 0 21
Kruskal algorithm and Union-Find scheme example 8 0 15 1 18 27 12 6 22 2 14 7 12 0 17 23 4 13 10 9 24 1 14 11 16 15 9 11 19 6 16 14 6 10 5 10 25 4 3 5 20 12 8 11 4 3 2 1 7 3 26 8 9 21 13 2 7 13 17 16 15 17 5 node 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 boss 0 0 2 4 14 14 2 4 4 4 12 14 14 14 16 16 rank 1 0 2 0 1 0 0 0 0 0 1 0 22
Kruskal algorithm and Union-Find scheme example 8 0 15 1 18 27 12 6 22 2 14 7 12 0 17 23 4 13 7 3 10 24 14 20 5 10 16 15 19 6 16 12 8 4 3 11 11 25 2 1 5 3 9 1 11 4 26 8 9 21 13 2 17 16 14 10 9 5 6 7 17 13 15 node 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 boss 0 0 2 4 14 14 2 4 4 4 12 14 16 16 rank 1 0 2 0 1 0 0 0 1 0 23
Kruskal algorithm and Union-Find scheme example 8 0 15 1 18 27 12 6 22 2 14 7 12 17 23 4 13 7 3 10 24 14 20 5 10 16 15 19 6 16 12 8 1 4 3 11 11 25 2 0 5 3 9 1 11 4 26 8 9 21 13 2 17 16 14 10 9 5 6 7 17 13 15 node 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 boss 2 0 2 4 14 14 2 4 4 4 12 14 16 16 rank 1 0 2 0 1 0 0 0 1 0 24
Kruskal algorithm and Union-Find scheme example 8 0 15 1 18 27 12 6 22 2 14 7 12 17 23 4 13 7 3 10 24 14 20 5 10 16 15 19 6 16 12 8 1 4 3 11 14 10 9 5 11 25 2 0 5 3 9 1 11 4 26 8 9 21 13 2 6 7 13 17 16 15 17 path compression node 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 boss 2 0 2 4 14 14 2 4 4 4 12 14 2 2 16 16 rank 1 0 2 0 1 0 0 0 25
Kruskal algorithm and Union-Find scheme example 8 0 15 1 18 27 12 6 22 2 14 7 12 17 23 4 13 7 3 10 24 14 5 3 9 1 11 4 26 8 9 21 13 2 20 5 10 11 25 16 15 19 6 16 17 2 0 12 8 1 4 3 11 14 10 9 5 6 7 15 16 13 17 node 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 boss 2 0 2 4 14 14 2 4 4 4 12 14 2 2 2 16 rank 1 0 2 0 1 0 0 0 26
Kruskal algorithm and Union-Find scheme example 8 0 15 1 18 27 12 6 22 2 14 7 12 13 14 5 3 10 9 1 11 4 24 20 5 10 11 25 16 15 19 6 16 17 path compression 2 0 17 23 4 7 3 26 8 9 21 13 2 8 1 4 3 11 12 9 14 10 5 6 7 15 16 13 17 node 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 boss 2 0 2 4 14 14 2 2 4 4 12 14 2 2 2 16 rank 1 0 2 0 1 0 0 0 27
Kruskal algorithm and Union-Find scheme example 8 0 15 1 18 27 12 6 22 2 14 7 12 17 23 4 13 14 5 3 10 9 1 11 4 24 20 5 10 11 25 16 15 19 6 16 17 path compression 2 0 7 3 26 8 9 21 13 2 8 1 4 3 11 9 12 6 10 14 5 7 15 16 13 17 node 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 boss 2 0 2 4 2 14 2 2 4 4 12 14 2 2 2 16 rank 1 0 2 0 1 0 0 0 0 1 0 0 28
Kruskal algorithm and Union-Find scheme example 8 0 15 1 18 27 12 6 22 2 14 7 9 21 12 2 0 13 2 17 23 13 10 24 14 5 3 9 1 11 4 26 8 4 7 3 20 5 10 11 25 16 15 19 6 16 17 path compression 8 11 1 4 3 12 9 6 10 14 5 7 15 13 16 17 path compression node 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 boss 2 0 2 4 2 14 2 2 4 2 12 14 2 2 rank 1 0 2 0 1 0 0 0 0 1 0 0 29
Kruskal algorithm and Union-Find scheme example 8 0 15 1 18 27 12 6 22 2 14 7 9 21 12 17 23 4 13 7 3 10 24 14 5 3 9 1 11 4 26 8 20 5 10 11 25 16 15 19 6 16 17 path compression 2 0 13 2 8 11 1 4 3 9 5 12 6 14 10 7 15 16 17 13 node 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 boss 2 0 2 4 2 2 2 14 2 2 4 2 12 14 2 2 rank 1 0 2 0 1 0 0 0 0 1 0 0 30
Kruskal algorithm and Union-Find scheme example 8 0 15 1 18 27 12 6 22 2 14 7 9 21 12 13 2 17 23 13 20 5 10 11 25 16 15 19 6 16 17 path compression 2 0 10 24 14 5 3 9 1 11 4 26 8 4 7 3 8 11 1 4 3 9 5 6 13 10 14 12 15 16 17 7 node 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 boss 2 0 2 4 2 2 2 14 2 2 4 2 12 2 2 rank 1 0 2 0 1 0 0 0 0 0 1 0 31
Kruskal algorithm and Union-Find scheme example 8 0 15 1 18 27 12 6 22 2 14 7 9 21 12 13 2 17 23 13 10 24 14 5 3 9 1 11 4 26 8 4 7 3 20 5 10 11 25 16 15 19 6 16 17 2 0 8 11 1 4 3 9 5 6 13 10 14 15 16 17 12 7 node 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 boss 2 0 2 4 2 2 2 14 2 2 2 2 2 rank 1 0 2 0 1 0 0 0 0 0 1 0 32
Kruskal algorithm running time improvement 33
- Slides: 33