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
| package bbm.graph;
import java.util.HashSet; import java.util.Set;
public class Johnson {
private static int[] bellmanFord(int[][] newWeight) { int[] h = new int[newWeight.length]; h[0] = 0; for (int i = 1; i < h.length; i++) { h[i] = 10000; } for (int k = 0; k < newWeight.length - 1; k++) { for (int i = 0; i < newWeight.length; i++) { for (int j = 0; j < newWeight.length; j++) { h[j] = relax(h[j], h[i], newWeight[i][j]); } } } for (int i = 0; i < newWeight.length; i++) { for (int j = 0; j < newWeight.length; j++) { if (h[i] + newWeight[i][j] < h[j]) { throw new RuntimeException("Has cycle"); } } } return h; }
private static int relax(int dv, int ds, int w) { if (dv > ds + w) { return ds + w; } else { return dv; } }
private static void dijkstra(int index, int[][] weight) { System.out.println("Start from " + (index + 1)); int[] length = new int[weight.length]; length[index] = 0; for (int i = 0; i < length.length; i++) { if (i != index) { length[i] = 10000; } } Set<Integer> handled = new HashSet<>(); while (handled.size() < weight.length) { int min = 10000; int minIndex = -1; for (int i = 0; i < length.length; i++) { if (!handled.contains(i) && length[i] < min) { min = length[i]; minIndex = i; } } handled.add(minIndex); for (int i = 0; i < weight.length; i++) { if (weight[minIndex][i] != 10000 && i != minIndex) { length[i] = relax(length[i], length[minIndex], weight[minIndex][i]); } } } for (int i = 0; i < length.length; i++) { System.out.print(length[i] + " "); } System.out.println(); }
private static void johnson(int[][] weight) { int[][] newWeight = new int[weight.length + 1][weight.length + 1]; for (int i = 0; i < newWeight.length; i++) { newWeight[0][i] = 0; } for (int i = 1; i < newWeight.length; i++) { newWeight[i][0] = 10000; } for (int i = 0; i < weight.length; i++) { for (int j = 0; j < weight.length; j++) { newWeight[i + 1][j + 1] = weight[i][j]; } } System.out.println("New weight:"); for (int i = 0; i < newWeight.length; i++) { for (int j = 0; j < newWeight.length; j++) { System.out.print(newWeight[i][j] + " "); } System.out.println(); } int[] h = bellmanFord(newWeight); System.out.println("H:"); for (int i = 0; i < h.length; i++) { System.out.print(h[i] + " "); } System.out.println(); for (int i = 0; i < weight.length; i++) { for (int j = 0; j < weight.length; j++) { if (weight[i][j] != 10000) { weight[i][j] += h[i+1] - h[j+1]; } } } System.out.println("Weight:"); for (int i = 0; i < weight.length; i++) { for (int j = 0; j < weight.length; j++) { System.out.print(weight[i][j] + " "); } System.out.println(); } for (int i = 0; i < weight.length; i++) { dijkstra(i, weight); } }
public static void main(String[] args) { int[][] weight = new int[][] { {0, 3, 8, 10000, -4}, {10000, 0, 10000, 1, 7}, {10000, 4, 0, 10000, 10000}, {2, 10000, -5, 0, 10000}, {10000, 10000, 10000, 6, 0} }; System.out.println("Weight:"); for (int i = 0; i < weight.length; i++) { for (int j = 0; j < weight.length; j++) { System.out.print(weight[i][j] + " "); } System.out.println(); } johnson(weight); } }
|