圖論-帶權圖的最小產生樹(Prim)演算法__演算法

來源:互聯網
上載者:User

演算法設計:Prim演算法從頂點開始著手。

從一個頂點開始,然後放入到樹的集合中,然後重複做如下事情:

    (1)、找最新的頂點到其他頂點的所有邊,這些頂點不能在樹的集合中,把這些放入優先順序隊列。

    (2)、找到權值最小的邊把它和它所到達的頂點放入樹的集合中。

重複上述操作直到所有的頂點都在樹中,程式結束。


Graph_mstw.java

package com.mapbar.structure;/** *  * Class Graph_mstw.java *  * Description 帶權圖的最小產生樹演算法 * 總原則:選擇從已經處理過的點到未處理過的點的費用最低的邊。 * Company mapbar *  * author Chenll E-mail:  *  * Version 1.0 *  * Date 2012-10-9 上午11:25:06 */// 定義邊class Edge {public int srcVert; // 源點public int destVert; // 目的點public int distance; // 距離public Edge(int sv, int dv, int d) {this.srcVert = sv;this.destVert = dv;this.distance = d;}}// 定義節點class Vertex {public char label;public boolean isInTree;public Vertex(char l) {this.label = l;isInTree = false;}}//定義優先順序隊列,//找到新的頂點到其它頂點所有邊,這些頂點不能在樹的集合中,放入優先順序隊列,找到費用最少的,把它和它所到達的頂點放入樹的集合中class PriorityQ{private final int SIZE = 20;private Edge[] queArray;private int size;public PriorityQ(){queArray = new Edge[SIZE];size = 0;}//新增public void insert(Edge item){int j;for(j = 0; j<size; j++){if(item.distance>=queArray[j].distance){//從大到小排序break;}}//後移for(int i = size-1; i>=j; i--){queArray[i+1] = queArray[i];}queArray[j] = item;size ++;}//刪除最小的public Edge removeMin(){return queArray[--size];}//刪除第N個public void removeN(int n){//前移for(int i = n; i<size-1; i++){queArray[i] = queArray[i+1];}size --;}public int size(){return size;}public boolean isEmpty(){return size == 0;}//擷取第N個邊public Edge peekN(int n){return queArray[n];}//尋找到達指定頂點的邊public int find(int vIndex){for(int i = 0; i<size; i++){if(queArray[i].destVert == vIndex){return i;}}return -1;}}class Graph {private final int MAX_VERTX = 20; // 節點數private final int INFINITY = 1000000; // 最遠距離private Vertex vertexList[];private int adjMat[][]; //鄰接矩陣private int nVerts; //記錄頂點的個數private int nTree; //記錄樹中頂點的個數private int currentVert; //當前頂點private PriorityQ thePQ; //優先順序隊列public Graph() {vertexList = new Vertex[MAX_VERTX];adjMat = new int[MAX_VERTX][MAX_VERTX];nVerts = 0;// 初始化矩陣for (int i = 0; i < MAX_VERTX; i++) {for (int j = 0; j < MAX_VERTX; j++) {adjMat[i][j] = INFINITY;}}thePQ = new PriorityQ(); }// 添加頂點public void addVertex(char label) {vertexList[nVerts++] = new Vertex(label);}// 添加邊public void addEdge(int sv, int dv, int dis) {adjMat[sv][dv] = dis;adjMat[dv][sv] = dis;}//最小產生樹public void mstw(){currentVert = 0;while(nTree < nVerts-1) {vertexList[currentVert].isInTree = true;nTree ++;for(int i = 0; i< nVerts; i++){//源點和終點相同if(i == currentVert) continue;//終點已經在樹裡面if(vertexList[i].isInTree) continue;//源點和終點沒有邊int dis = adjMat[currentVert][i];if(dis == INFINITY) continue;//加入當前點和終點的邊到隊列putInPQ(i, dis);}if(thePQ.size() == 0){return;}Edge theEdge = thePQ.removeMin();int sVert = theEdge.srcVert;currentVert = theEdge.destVert;System.out.print(vertexList[sVert].label);System.out.print(vertexList[currentVert].label);System.out.print(" ");}//重新設定for(int j = 0; j<nVerts; j++){vertexList[j].isInTree = false;}}public void putInPQ(int vertex,int dis){int index = thePQ.find(vertex);if(index!=-1){//檢查老邊是否有比新邊更小的成本,如果新邊成本比老邊少,需要刪除老邊,把新邊放入Edge tempEdge = thePQ.peekN(index);int oldDis = tempEdge.distance;if(oldDis > dis){ thePQ.removeN(index);Edge theEdge = new Edge(currentVert,vertex,dis);thePQ.insert(theEdge);}} else {Edge theEdge = new Edge(currentVert,vertex,dis);thePQ.insert(theEdge);} }}public class Graph_mstw {public static void main(String[] args){Graph g = new Graph();g.addVertex('A');g.addVertex('B');g.addVertex('C');g.addVertex('D');g.addVertex('E');g.addVertex('F');g.addEdge(0, 1, 6);g.addEdge(0, 3, 4);g.addEdge(1, 2, 10);g.addEdge(1, 3, 7);g.addEdge(1, 4, 7);g.addEdge(2, 3, 8);g.addEdge(2, 4, 5);g.addEdge(2, 5, 6);g.addEdge(3, 4, 12);g.addEdge(4, 5, 7);g.mstw();}}

輸出結果:AD AB BE EC CF 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.