1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
| package com.atguigu.kruskal;
import java.util.Arrays;
public class KruskalCase {
private int edgeNum; private char[] vertexs; private int[][] matrix; private static final int INF = Integer.MAX_VALUE; public static void main(String[] args) { char[] vertexs = {'A', 'B', 'C', 'D', 'E', 'F', 'G'}; int matrix[][] = { { 0, 12, INF, INF, INF, 16, 14}, { 12, 0, 10, INF, INF, 7, INF}, { INF, 10, 0, 3, 5, 6, INF}, { INF, INF, 3, 0, 4, INF, INF}, { INF, INF, 5, 4, 0, 2, 8}, { 16, 7, 6, INF, 2, 0, 9}, { 14, INF, INF, INF, 8, 9, 0} }; KruskalCase kruskalCase = new KruskalCase(vertexs, matrix); kruskalCase.print(); kruskalCase.kruskal(); } public KruskalCase(char[] vertexs, int[][] matrix) { int vlen = vertexs.length; this.vertexs = new char[vlen]; for(int i = 0; i < vertexs.length; i++) { this.vertexs[i] = vertexs[i]; } this.matrix = new int[vlen][vlen]; for(int i = 0; i < vlen; i++) { for(int j= 0; j < vlen; j++) { this.matrix[i][j] = matrix[i][j]; } }
for(int i =0; i < vlen; i++) { for(int j = i+1; j < vlen; j++) { if(this.matrix[i][j] != INF) { edgeNum++; } } } } public void kruskal() { int index = 0; int[] ends = new int[edgeNum]; EData[] rets = new EData[edgeNum]; EData[] edges = getEdges(); System.out.println("图的边的集合=" + Arrays.toString(edges) + "共"+ edges.length); sortEdges(edges); for(int i=0; i < edgeNum; i++) { int p1 = getPosition(edges[i].start); int p2 = getPosition(edges[i].end); int m = getEnd(ends, p1); int n = getEnd(ends, p2); if(m != n) { ends[m] = n; rets[index++] = edges[i]; } } System.out.println("×îСÉú³ÉÊ÷Ϊ"); for(int i = 0; i < index; i++) { System.out.println(rets[i]); } }
public void print() { System.out.println("邻接矩阵: \n"); for(int i = 0; i < vertexs.length; i++) { for(int j=0; j < vertexs.length; j++) { System.out.printf("%12d", matrix[i][j]); } System.out.println(); } }
private void sortEdges(EData[] edges) { for(int i = 0; i < edges.length - 1; i++) { for(int j = 0; j < edges.length - 1 - i; j++) { if(edges[j].weight > edges[j+1].weight) { EData tmp = edges[j]; edges[j] = edges[j+1]; edges[j+1] = tmp; } } } } private int getPosition(char ch) { for(int i = 0; i < vertexs.length; i++) { if(vertexs[i] == ch) { return i; } } return -1; } private EData[] getEdges() { int index = 0; EData[] edges = new EData[edgeNum]; for(int i = 0; i < vertexs.length; i++) { for(int j=i+1; j <vertexs.length; j++) { if(matrix[i][j] != INF) { edges[index++] = new EData(vertexs[i], vertexs[j], matrix[i][j]); } } } return edges; } private int getEnd(int[] ends, int i) { ] while(ends[i] != 0) { i = ends[i]; } return i; } }
class EData { char start; char end; int weight; public EData(char start, char end, int weight) { this.start = start; this.end = end; this.weight = weight; }
@Override public String toString() { return "EData [<" + start + ", " + end + ">= " + weight + "]"; } }
|