貪心演算法 – 最小產生樹 Prim演算法

來源:互聯網
上載者:User

一個無向帶權圖G=(V,E),其中n個頂點Vertex,以及串連各個頂點之間的邊Edge,可能有些頂點之間沒有邊,每條邊上的權值都是非負值。

產生樹:

G的一個子圖,包含了所有的Vertex,和部分的Edge。

最小產生樹:

所有的產生樹中,各條Edge上的權值總和最小的一個。

例子:設計通訊網路時,各個城市之間鋪設線路,最經濟的方案。

最小產生樹性質:

G=(V,E),

S是V的真子集,

如果u在S中,v在V-S中,且(u,v)是圖的一條邊,稱之為特殊邊,且(u,v)是所有特殊邊中最短的,

那麼,(u,v)這條邊一定在最小產生樹中。


Prim演算法:

任意指定一個頂點作為起始點,放在S中。

每一步將最短的特殊邊放入S中,需要n-1步,即可把所有的其他的點放入S中。演算法結束。


對於這個圖,Prim演算法的過程為:



代碼實現如下:

/** * 最小產生樹 minimum spanning tree * @author xuefeng * */public class MST {public static final int NOT_REACHED = -1;/** * @param E n*n */public static void prim(int[][] E) {int n = E.length;boolean[] S = new boolean[n];int[] dist = new int[n];int[] prev = new int[n];S[0] = true; // 選取頂點1作為起始點for (int i = 1; i < n; i++) {dist[i] = E[0][i];prev[i] = 0;S[i] = false;}int min = -1, minV = -1;for (int i = 1; i < n; i++) {min = -1;minV = -1;// 選擇離S中頂點最近的點for (int j = 1; j < n; j++) {if (!S[j] && dist[j] != -1 && (min == -1 || dist[j] < min)) {min = dist[j];minV = j;}}if (minV == -1)continue;S[minV] = true;// S中多了個點,需要改變S離外面的點的最短距離for (int j = 1; j < n; j++) {if (!S[j] && isReachable(E, minV, j)&& (dist[j] == -1 || E[minV][j] < dist[j])) {dist[j] = E[minV][j];prev[j] = minV;}}// 輸出測試結果for (int j = 0; j < n; j++) {if (S[j]) {System.out.print((j + 1) + " ");}}System.out.println();}}private static boolean isReachable(int[][] E, int v1, int v2) {return E[v1][v2] != NOT_REACHED;}public static void main(String[] args) {int[][] E = { { -1, 6, 1, 5, -1, -1 }, { 6, -1, 5, -1, 3, -1 },{ 1, 5, -1, 5, 6, 4 }, { 5, -1, 5, -1, -1, 2 },{ -1, 3, 6, -1, -1, 6 }, { -1, -1, 4, 2, 6, -1 } };prim(E);}}

輸出為:

1 3 1 3 6 1 3 4 6 1 2 3 4 6 1 2 3 4 5 6 

可由prev數組構造出最小產生樹。

演算法複雜度為O(n^2)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.