zoukankan      html  css  js  c++  java
  • 最小生成树的prim算法

    代码1:

    转自:http://blog.csdn.net/jlhnxly/article/details/6402020

     1 /**
    2 * 最小生成树的prim算法
    3 * @author liuy
    4 */
    5 public class Prim {
    6
    7 public static void prim(int num, float[][] weight) { //num为顶点数,weight为权
    8 float[] lowcost = new float[num + 1]; //到新集合的最小权
    9
    10 int[] closest = new int[num + 1]; //代表与s集合相连的最小权边的点
    11
    12 boolean[] s = new boolean[num + 1]; //s[i] == true代表i点在s集合中
    13
    14 s[1] = true; //将第一个点放入s集合
    15
    16 for(int i = 2; i <= num; i++) { //初始化辅助数组
    17 lowcost[i] = weight[1][i];
    18 closest[i] = 1;
    19 s[i] = false;
    20 }
    21
    22 for(int i = 1; i < num; i++) {
    23 float min = Float.MAX_VALUE;
    24 int j = 1;
    25 for(int k = 2; k <= num; k++) {
    26 if((lowcost[k] < min) && (!s[k])) {//根据最小权加入新点
    27 min = lowcost[k];
    28 j = k;
    29 }
    30 }
    31
    32 System.out.println("加入点" + j + ". " + j + "---" + closest[j]);//新加入点的j和与j相连的点
    33
    34 s[j] = true;//加入新点j
    35
    36 for(int k = 2; k <= num; k++) {
    37 if((weight[j][k] < lowcost[k]) && !s[k]) {//根据新加入的点j,求得最小权
    38 lowcost[k] = weight[j][k];
    39 closest[k] = j;
    40 }
    41 }
    42 }
    43 }
    44
    45 public static void main(String[] args) {
    46 //
    47 // / | /
    48 // 6 1 5
    49 // / | /
    50 // ②-5--③--5--④
    51 // / // /
    52 // 3 6 4 2
    53 //////
    54 // ⑤--6-⑥
    55 //最小生成树为:
    56 //
    57 // |
    58 // 1
    59 // |
    60 // ②-5--③ ④
    61 // / / /
    62 // 3 4 2
    63 // / //
    64 // ⑤ ⑥
    65 //
    66 float m = Float.MAX_VALUE;
    67 float[][] weight = {{0, 0, 0, 0, 0, 0, 0},
    68 {0, m, 6, 1, 5, m, m},
    69 {0, 6, m, 5, m, 3, m},
    70 {0, 1, 5, m, 5, 6, 4},
    71 {0, 5, m, 5, m, m, 2},
    72 {0, m, 3, 6, m, m, 6},
    73 {0, m, m, 4, 2, 6, m}};//上图的矩阵
    74 prim(weight.length - 1, weight);
    75 //加入点3. 3---1
    76 //加入点6. 6---3
    77 //加入点4. 4---6
    78 //加入点2. 2---3
    79 //加入点5. 5---2
    80 }
    81 }


    代码2:

    View Code
     1 public class MST {
    2
    3 public MST() {
    4 }
    5
    6 /** The adjacency matrix of the graph */
    7 private double[][] adjM;
    8
    9 public double[][] buildMST(double[][] adjM) {
    10
    11 this.adjM = adjM;
    12
    13 // Marks nodes that belong to MST. Initial MST has only one node.
    14 boolean[] allV = new boolean[adjM.length];
    15 allV[0] = true;
    16
    17 // Adjacency matrix defining MST
    18 double[][] mst = new double[adjM.length][adjM.length];
    19 for(int i = 0, n = mst.length; i < n; i++) {
    20 for(int j = 0; j < n; j++) {
    21 /*
    22 * Using -1 to indicate that there is no edge between nodes i and j.
    23 * Can't use 0 because it is a valid distance.
    24 */
    25 mst[i][j] = -1;
    26 }
    27 }
    28
    29 Edge e = null;
    30 while((e = findMinimumEdge(allV)) != null ) {
    31 allV[e.getJ()] = true;
    32 mst[e.getI()][e.getJ()] = e.getW();
    33 mst[e.getJ()][e.getI()] = e.getW();
    34 }
    35
    36 return mst;
    37 }
    38
    39 private Edge findMinimumEdge(boolean[] mstV) {
    40 Edge e = null;
    41 double minW = Double.POSITIVE_INFINITY;
    42 int minI = -1;
    43 int minJ = -1;
    44
    45 for( int i = 0, n = adjM.length; i < n; i++ ) {
    46 // part of MST
    47 if( mstV[i] == true ) {
    48 for(int j = 0, k = adjM.length; j < k; j++) {
    49 // not part of MST
    50 if( mstV[j] == false ) {
    51 if( minW > adjM[i][j]) {
    52 minW = adjM[i][j];
    53 minI = i;
    54 minJ = j;
    55 }
    56 }
    57 }
    58 }
    59 }
    60
    61 if( minI > -1 ) {
    62 e = new Edge(minI, minJ, minW);
    63 }
    64
    65 return e;
    66 }
    67
    68 class Edge {
    69
    70 private int i;
    71 private int j;
    72 private double w;
    73
    74 Edge(int i, int j, double w) {
    75 this.i = i;
    76 this.j = j;
    77 this.w = w;
    78 }
    79
    80 public int getI() {
    81 return i;
    82 }
    83
    84 public int getJ() {
    85 return j;
    86 }
    87
    88 public double getW() {
    89 return w;
    90 }
    91
    92 }
    93 }



  • 相关阅读:
    51 Nod 1068 Bash游戏v3
    51 Nod Bash 游戏v2
    51 Nod 1073 约瑟夫环
    UVA 12063 Zeros and ones 一道需要好好体会的好题
    51 Nod 1161 Partial sums
    2018中国大学生程序设计竞赛
    UVA 11971 Polygon
    UVA 10900 So do you want to be a 2^n-aire?
    UVA 11346 Possibility
    python with as 的用法
  • 原文地址:https://www.cnblogs.com/paulbai/p/prim.html
Copyright © 2011-2022 走看看