1、SolutionsHW5参考答案Homework FiveDesign and Analysis of Algorithms2010, 北京1 Let G = (V, E) be a connected weighted graph, where |V| = n, |E| = m. Every edge has a weight which is an integer between 1 and W, where W is a constant. Design an O(m) algorithm to compute the minimum spanning tree (MST).Soluti
2、on: The key idea is how can we efficiently implement the priority Q to support the Extract-Min (Q) and update the value keyv in the Q.Since the keyv is in 0, W we define a set for each value: Lk = u | keyu = k, 0 k W, and LW + 1 = u | keyu = .We use a linked list to chain all vertices in each set. O
3、bviously, u Lkeyu. We initialize L0 = s, LW + 1 = V s, and Lk = for k 1, 2, , W. These indexed sets, Lk 0 k W+1, form the priority queue Q. Each time we need to Extract-Min (Q), we search the lists from L0 until a non-empty list is found. A vertex in the first non-empty list is the vertex we need. W
4、hen a vertex is updated, it will be moved to a corresponding set. Using linked lists, deleting and inserting an element in a list can be done in O(1) time.Now, we are ready to present the algorithm.Modified Prims algorithm (G, W, r)1 for each v VG2 do keyv 3 v nil4 5 keyr 06 S 7 for k = 1 to W8 do L
5、k .9 L0 s 10 LW + 1 = V s11 while S V12 do i 013 while Li = 14 do i i + 115 u Extract an element from Li16 S S u17 for each v Adju18 do if v S and w(u, v) w(y). Then, the weight of spanning tree (T-x)y is W(T) - w(x) + w(y), and W(T) - w(x) + w(y) W(T), which contradicts to the fact that T is a minimum spanning tree.
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1