使用java實現最小生成樹prim算法

實體類:java

package com.caicai.prim.entity; /** * * @author caizy * */ public class ShortEdge { private int lowCost;//到連通樹的最小權值 private int adjvex;//使它到連通樹的最小權值的那個節點編號 public int getLowCost() { return lowCost; } public void setLowCost(int lowCost) { this.lowCost = lowCost; } public int getAdjvex() { return adjvex; } public void setAdjvex(int adjvex) { this.adjvex = adjvex; } }
package com.caicai.prim.entity; public class Graph<T> { private int vertexNum;//頂點數 private T vertex[];//存放頂點信息的數組 private int arcNum;//存放邊數; private int arc[][];//存放邊關係和邊的權值:當爲0時不存在邊,大於零時有邊且邊爲權值。 public int getVertexNum() { return vertexNum; } public void setVertexNum(int vertexNum) { this.vertexNum = vertexNum; } public T[] getVertex() { return vertex; } public void setVertex(T[] vertex) { this.vertex = vertex; } public int getArcNum() { return arcNum; } public void setArcNum(int arcNum) { this.arcNum = arcNum; } public int[][] getArc() { return arc; } public void setArc(int[][] arc) { this.arc = arc; } }

prim算法實現:c++

package com.caicai.prim; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import com.caicai.prim.entity.Graph; import com.caicai.prim.entity.ShortEdge; /** * * @author caizy *prim算法實現 */ public class Prim<T> { private Map<Integer,ShortEdge> map = new HashMap<Integer, ShortEdge>(); private Graph<T> graph; public Graph<T> getGraph() { return graph; } public void setGraph(Graph<T> graph) { this.graph = graph; } public Map<Integer,ShortEdge> prim() { initial(); int arc[][]=graph.getArc(); Map<Integer,ShortEdge> resultMap=new LinkedHashMap<Integer,ShortEdge>(); while(map.size()>0) { int k = minEdge(map); resultMap.put(k,map.get(k) ); map.remove(k); if(map.size()==0) break; for (int key:map.keySet()) { if (arc[key][k] < map.get(key).getLowCost()) map.get(key).setLowCost(arc[k][key]); map.get(key).setAdjvex(k); } } return resultMap; } private void initial() { int arc[][]=graph.getArc(); for(int i = 1; i < graph.getVertexNum(); i++) { ShortEdge shortEdge = new ShortEdge(); shortEdge.setAdjvex(0);//開始的時候集合中只有一個頂點0 if(arc[0][i]!=0) shortEdge.setLowCost(arc[0][i]); else shortEdge.setLowCost(Integer.MAX_VALUE); map.put(i,shortEdge); } } private int minEdge(Map<Integer,ShortEdge> map) { int min=Integer.MAX_VALUE; int found =0; for(Integer key: map.keySet()) { if(map.get(key).getLowCost()<min) { min=map.get(key).getLowCost(); found=key; } } return found; } }

測試類算法

package prim.test; import java.util.Map; import com.caicai.prim.Prim; import com.caicai.prim.entity.Graph; import com.caicai.prim.entity.ShortEdge; public class PrimTest { public static void main(String args[]) { Graph<String> graph = new Graph<String>(); graph.setVertexNum(4); graph.setArcNum(5); int arc[][] = new int[5][5]; arc[0][1]=19; arc[1][0]=19; arc[1][2]=25; arc[2][1]=25; arc[1][3]=25; arc[3][1]=25; arc[2][3]=17; arc[3][2]=17; arc[2][0]=46; arc[0][2]=46; graph.setArc(arc); String vertex[]=new String[4]; for (int i= 0;i<4;i++) { vertex[i]="頂點"+i; } graph.setVertex(vertex); Prim<String> prim =new Prim<String>(); prim.setGraph(graph); Map<Integer,ShortEdge> resultMap=prim.prim(); for(int key:resultMap.keySet()) { System.out.format("邊爲%d"+"-"+"%d,"+"權值爲:%d\n",key,resultMap.get(key).getAdjvex(), resultMap.get(key).getLowCost()); } } }

 測試結果:數組

參考書籍:數據結構(c++版)第2版 王紅梅等編著數據結構

相關文章
相關標籤/搜索