Minimal Spanning 9 5 Trees 14 7 8 Chapter 9 15 10 3 CPTR - - PDF document

minimal spanning
SMART_READER_LITE
LIVE PREVIEW

Minimal Spanning 9 5 Trees 14 7 8 Chapter 9 15 10 3 CPTR - - PDF document

4/18/2013 12 6 Minimal Spanning 9 5 Trees 14 7 8 Chapter 9 15 10 3 CPTR 318 Prims Algorithm Initialization 6 12 // Initialization 6 for ( each vertex v in V ) 9 5 { 5 7 9 v.value = ; // v 14 7 v.prev = null; //


slide-1
SLIDE 1

4/18/2013 1

Minimal Spanning Trees

Chapter 9

CPTR 318 6 3 7 14 12 10 15 9 5 8 15 6 3 7 14 12 15 9 5 8 9 7 6 5 3 8 10

Prim’s Algorithm Initialization

// Initialization for ( each vertex v in V ) { v.value = ∞; // v v.prev = null; // v has no predecessor v.open = true; // v is open } Q = V; // Insert all vertices into priority queue MST = Ø; // MST empty v = RandomVertex(); // Select arbitrary vertex v.value = 0;

Prim’s Algorithm

// Initialization while ( Q ≠ Ø ) // While the priority queue is nonempty { u = Q.delete_min(); u.open = false; for ( each v adjacent to u ) { if ( v.open and weight(u,v) < v.value ) { v.value = weight(u,v); v.prev = u; Q.decrease_key(v); // value changed } } } for ( each v in V ) MST = MST.insert((v, v.prev));

Prim’s Algorithm Initialization

// Initialization for ( each vertex v in V ) { v.value = ∞; // v v.prev = null; // v has no predecessor v.open = true; // v is open } Q = V; // Insert all vertices into priority queue MST = Ø; // MST empty v = RandomVertex(); // Select arbitrary vertex v.value = 0;

O(|V|) + O(|V| log |V|) + O(1) = O(|V| log |V|)

slide-2
SLIDE 2

4/18/2013 2

Prim’s Algorithm

// Initialization while ( Q ≠ Ø ) // While the priority queue is nonempty { u = Q.delete_min(); for ( each v adjacent to u ) { if ( v.open and weight(u,v) < v.value ) { v.value = weight(u,v); v.prev = u; Q.decrease_key(v); // value changed } } } for ( each v in V ) MST = MST.insert((v, v.prev));

O(|E| log |V|) + O(|V|) = O(|E| log |V|) Prim’s Algorithm O(|V|) + O(|V| log |V|) + O(1) = O(|V| log |V|) O(|E| log |V|) + O(|V|) = O(|E| log |V|)

O(|E| log |V|) O(|V|2) without a heap, optimal for dense graphs

9

A different algorithm...

6 3 7 14 12 10 15 9 5 8 6 3 7 14 12 10 15 9 5 8 6 3 7 14 12 10 15 9 5 8

slide-3
SLIDE 3

4/18/2013 3

Kruskal’s Algorithm

// Initialization MST = Ø; // Empty MST // Make a forest of |V| trees // each containing a vertex in V for ( each vertex v in V ) create a set containing v; // Build priority queue of edges for ( each edge e in E ) Q.insert(e); while ( Q ≠ Ø ) // While the priority queue is nonempty { (u,v) = Q.delete_min(); // Select smallest if ( find(u) ≠ find(v) ) { // edge that union(u, v); // connects two MST.insert((u,v)); // disjoint sets } }

Kruskal’s Algorithm

// Initialization MST = Ø; // Empty MST // Make a forest of |V| trees // each containing a vertex in V for ( each vertex v in V ) create a set containing v; // Build priority queue of edges for ( each edge e in E ) Q.insert(e); while ( Q ≠ Ø ) // While the priority queue is nonempty { (u,v) = Q.delete_min(); // Select smallest if ( find(u) ≠ find(v) ) { // edge that union(u, v); // connects two MST.insert((u,v)); // disjoint sets } }

O(|E| log |E|)

O(1) + O(|V|) + O(|E| log |E|) + O(|E| log |E|) O(1) + O(|V|) + O(|E| log |E|) + O(|E| log |E|)

= O(|E| log |E|)

O(1) + O(|V|) + O(|E| log |E|) + O(|E| log |E|)

= O(|E| log |E|) O(|E| log |E|) is O(|E| log |V|) ?