ECS 110: Data Structures and Programming Discussion Section Notes -- Week 9 Ted Krovetz (tdkrovetz@ucdavis.edu) PRIM & ASSIGNMENT 4 HELP ======================== This week we will examine Prim's algorithm for finding a minimum spanning tree (using F-Heaps). Also, we will look at the reduced cost matrix method for finding a good lower bound for your cost estimation function in your TSP program. 1. PRIM'S ALGORITHM ------------------- 1.1 MST Property R. C. Prim invented an algorithm to find a minimum spanning tree based on the following property, known as the minimum-cost spanning tree property (Taken from Aho, Hopcroft, and Ullman; 1983). Let G = (V,E) be a connected graph with a cost function defined on its edges. Let U be some proper subset of the set of vertices V. If (u,v) is an edge of lowest cost such that u is an element of U and v is an element of V - U, then there is a minimum cost spanning tree that includes (u,v) as an edge. The proof that every minimum-cost spanning tree satisfies the MST property is not hard. Suppose to the contrary that there is no minimum-cost spanning tree for G that includes (u,v). Let T be any minimum-cost spanning tree for G. Adding (u,v) to T must introduce a cycle, since T was connected and acyclic. This cycle involves edge (u,v). Thus, there must be another edge (u',v') in T such that u' is an element of U and v' is an element of V - U (as illustrated in section). If not, there would be no way for the cycle to get from u to v without following the edge (u,v) a second time. Deleting the edge (u',v') breaks the cycle and yields a spanning tree T' whose cost is certainly no higher that the cost of T since by assumption c(u,v) <= c(u',v'). Thus, T' contradicts our assumption that there is no minimum-cost spanning tree that includes (u,v). 1.2 The algorithm Prim's algorithm uses this property by starting with a connected acyclic graph of a single vertex and repeatedly adds the vertex which is closest to the growing tree. This is repeated until all vertices have been added to the MST. Some pseudo-code: void prim(graph G, set_of_edges &T) { set_of_vertices U; vertex u,v; T = { }; U = { 1 }; while (U != V) { (u,v) = lowest cost edge with u in U and v in (V - U); T += (u,v); U += v; } } [Do an example in section] How might we implement such an algorithm. Implicit in this pseudo-code are several key operations. At each iteration we choose the edge of least cost, which implies some sort of data structure to keep track of all the edges. But also, as the tree grows, the vertices which haven't been added to the growing tree will have their "closeness" to the tree change as the tree grows nearer. Fibonacci heaps do delete-min() and decrease-key() quite quickly, so seems to be a natural for this problem. struct node_info { int in_tree; int dist; int closest; }; void prim2(int C[n,n]) // accepts a cost matrix { Fib_Heap fh; node_info node[1..n]; vertex u; for (int i = 2; i <= n; i++) { node[i].dist = C[1,i]; node[i].in_tree = 0; node[i].closest = 1; fh.insert(node[i]); } for (int i = 2; i <= n; i++) { u = fh.delete_min(); cout << "edge: " << u << node[u].closest << endl; node[i].in_tree = 1; for (j = 2; j <= n; j++) { if (( ! node[j].in_tree) && (C[u,j] < node[j].dist)) { fh.decrease_key(node[j], C[u,j]); node[j].closest = j; } } } 1.3 Analysis As you may remember, Kruskal's algorithm sorted the edges by weight and then repeatedly selected the lowest cost edge n times. This yielded a running time of O(e log e + n), where e is the number of edges, and n is the number of nodes. This implementation of Prim's algorithm maintains a list of nodes in updating them n times. Thus, O(n^2). A more careful implementation (only checking adjacent nodes in the third for loop), could yield O(e + n log n). Therefore, the ratio of edges to nodes is an important factor in choosing which algorithm to use. 2 MORE TSP ---------- I will demonstrate the reduced cost matrix method of calculating the cost estimation function of the TSP. The example I will follow is that which is included in the assignment handout. I hope to have a question and answer session as well.