-------------------------------------------------------------------------- COMP 731 - Data Struc & Algorithms - Lecture 17 - Tuesday, August 8, 2000 -------------------------------------------------------------------------- Today: o Finish Fibonacci heaps o An application: Prim's algorithm The material on F-heaps followed the second half of of Lecture 9 from Dexter Kozen's book "The Design and Analysis of Algorithms", Springer-Verlag, 1992. No further lecture notes will be provided on this. I then went on to describe Prim's algorithm, and to contrast it with Kruskal's algorithm. Prim's algorithm -- high-level description Choose an arbitrary start vertex, s Put s into the tree T repeat Choose the least cost edge {v,w} where v is already in the tree T and w is not yet in the tree T. Add edge {v,w} to the tree T until T is a spanning tree (it has n-1 edges) Prim's algorithm -- implementation Prim (G,length) // G=(V,E) where V={1,...,n} and |E|=m and // length: E -> Reals Fibonacci Heap h // Make an initially empty F-heap for i = 1 to n do d[i] = infinity // the key of every node is its d[]-value d[1] = 0 for i = 1 to n do Insert(h,i) for each edge e in E do T[e] = NO // no edge is yet in the tree for iterations=1 to n-1 do v = Deletemin(h) if v!=1 then T[{v,pi(v)}] = YES // put the edge in the MST for each w in Adj(v) do if inheap(h,w) and length(v,w) < d[w] then Decreasekey(h,w,length(v,w)) return T note: inheap(h,i) just looks up in an arry if the item i in 1..n is currently in the heap. Running time: we do n Insert's at O(1) amortized cost each: O(n) n Deletemin's at O(lg n) amortized each: O(n lg n) 2m Decreasekey's at O(1) amortized each: O(m) ---------- O(m + n lg n) (same as our implementation of Kruskal's algorithm)