|
| 1 | +package hw06; |
| 2 | + |
| 3 | +public class Dijkstra2 { |
| 4 | + private final int MAX_VALUE = Integer.MAX_VALUE; |
| 5 | + private int[][] adjacencyMatrix ; |
| 6 | + private int dist[]; |
| 7 | + private boolean visited[]; |
| 8 | + |
| 9 | + public Dijkstra2(){ |
| 10 | + this.adjacencyMatrix = new int[][] { |
| 11 | + { 0, 10, 3, MAX_VALUE, MAX_VALUE }, |
| 12 | + { MAX_VALUE, 0, 1, 2, MAX_VALUE }, |
| 13 | + { MAX_VALUE, 4, 0, 8, 2 }, |
| 14 | + { MAX_VALUE, MAX_VALUE, MAX_VALUE, 0, 7 }, |
| 15 | + { MAX_VALUE, MAX_VALUE, MAX_VALUE, 9, 0 } |
| 16 | + }; |
| 17 | + } |
| 18 | + |
| 19 | + public void dijkstra(){ |
| 20 | + this.dist = new int[5]; |
| 21 | + for(int i = 0; i < 5; i++){ |
| 22 | + this.dist[i] = MAX_VALUE; |
| 23 | + } |
| 24 | + this.dist[0] = 0; |
| 25 | + this.visited = new boolean[5]; |
| 26 | + int count = 0; |
| 27 | + PriorityQueue q = new PriorityQueue(); |
| 28 | + q.insert(new Node(0,0)); |
| 29 | +// for(int i = 0; i < dist.length - 1 ; i++){ |
| 30 | +// q.insert(new Node(i+1, MAX_VALUE)); |
| 31 | +// } |
| 32 | + while(q.getQueueSize() != 0){ |
| 33 | + Node minNode = q.extractMin(); |
| 34 | + int vertex = minNode.getVertex(); |
| 35 | + if(visited[vertex]) continue; // 방문한 정점이면 인접 정점의 최소값 다시 찾지 않음 |
| 36 | + visited[vertex] = true; |
| 37 | + |
| 38 | + System.out.println("==========================================================="); |
| 39 | + System.out.println("S["+ count +"] : d[" + (char)(vertex + 65) +"] = "+ dist[vertex]); |
| 40 | + System.out.println("-----------------------------------------------------------"); |
| 41 | + |
| 42 | + for(int i = 0; i < this.adjacencyMatrix[vertex].length ; i++){ |
| 43 | + if(visited[i]) continue; // 인접노드 방문 했다면 다시 방문 X |
| 44 | + int adjVertex = i; |
| 45 | + System.out.print("Q[" + i + "] : d[" + (char)(adjVertex + 65) + "] = " + dist[adjVertex]); |
| 46 | + if(this.adjacencyMatrix[vertex][adjVertex] != MAX_VALUE && dist[adjVertex] > dist[vertex] + this.adjacencyMatrix[vertex][adjVertex]){ |
| 47 | + dist[adjVertex] = dist[vertex] + this.adjacencyMatrix[vertex][adjVertex]; |
| 48 | + System.out.println(" => d[" + (char)(adjVertex + 65) + "] = " + dist[adjVertex]); |
| 49 | + q.insert(new Node(adjVertex, dist[adjVertex])); |
| 50 | +// q.update(adjVertex, dist[adjVertex]); |
| 51 | + continue; |
| 52 | + } |
| 53 | + System.out.println(); |
| 54 | + } |
| 55 | + System.out.println(); |
| 56 | +// System.out.println("==========================================================="); |
| 57 | + count++; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + private class PriorityQueue{ |
| 63 | + |
| 64 | + private Node[] heap; |
| 65 | + private int heapSize; |
| 66 | + |
| 67 | + public PriorityQueue(){ |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + private void buildMinHeap(Node[] arr){ |
| 72 | + for(int i = (this.heapSize) / 2 ; i > 0 ; i--){ |
| 73 | + minHeapify(arr, i); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private void minHeapify(Node[] arr, int i){ |
| 78 | + int smallest; |
| 79 | + int leftChildIdx = getLeftChildIdx(i); |
| 80 | + int rightChildIdx = getRightChildIdx(i); |
| 81 | + if(this.heapSize >= leftChildIdx && arr[leftChildIdx].dist < arr[i].dist){ |
| 82 | + smallest = leftChildIdx; |
| 83 | + }else{ |
| 84 | + smallest = i; |
| 85 | + } |
| 86 | + if(this.heapSize >= rightChildIdx && arr[rightChildIdx].dist < arr[smallest].dist){ |
| 87 | + smallest = rightChildIdx; |
| 88 | + } |
| 89 | + if(smallest != i){ |
| 90 | + swap(arr, smallest, i); |
| 91 | + minHeapify(arr, smallest); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + public Node getNode(int index){ |
| 96 | + return this.heap[index+1]; |
| 97 | + } |
| 98 | + |
| 99 | + public int getQueueSize(){ |
| 100 | + return this.heapSize; |
| 101 | + } |
| 102 | + |
| 103 | + public void update(int vertex, int newDist){ |
| 104 | + for(int i = 1 ; i <= this.heapSize; i++){ |
| 105 | + if(this.heap[i].vertex == vertex){ |
| 106 | + this.heap[i].dist = newDist; |
| 107 | + break; |
| 108 | + } |
| 109 | + } |
| 110 | + buildMinHeap(this.heap); |
| 111 | + } |
| 112 | + |
| 113 | + public Node extractMin(){ |
| 114 | + Node minNode = heap[1]; |
| 115 | + swap(heap, heapSize, 1); |
| 116 | + this.heapSize--; |
| 117 | + buildMinHeap(heap); |
| 118 | + return minNode; |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | + public void insert(Node x){ |
| 123 | + Node[] temp = new Node[this.heapSize + 2]; |
| 124 | + for(int i = 1 ; i <= this.heapSize; i++){ |
| 125 | + temp[i] = heap[i]; |
| 126 | + } |
| 127 | + temp[temp.length - 1] = x; |
| 128 | + this.heap = temp; |
| 129 | + this.heapSize = temp.length - 1; |
| 130 | + if(this.heapSize != 1 && this.heap[this.heapSize/2].dist < this.heap[this.heapSize].dist) return; |
| 131 | + buildMinHeap(this.heap); |
| 132 | + } |
| 133 | + |
| 134 | + private void swap(Node[] arr, int i, int j){ |
| 135 | + Node temp = arr[i]; |
| 136 | + arr[i] = arr[j]; |
| 137 | + arr[j] = temp; |
| 138 | + } |
| 139 | + |
| 140 | + private int getLeftChildIdx(int parentIdx){ |
| 141 | + return 2*parentIdx; |
| 142 | + } |
| 143 | + |
| 144 | + private int getRightChildIdx(int parentIdx){ |
| 145 | + return 2*parentIdx+1; |
| 146 | + } |
| 147 | + |
| 148 | + } |
| 149 | + private class Node{ |
| 150 | + private int dist; |
| 151 | + private int vertex; |
| 152 | + |
| 153 | + public Node(int vertex, int dist){ |
| 154 | + this.vertex = vertex; |
| 155 | + this.dist = dist; |
| 156 | + } |
| 157 | + |
| 158 | + public int getDist() { |
| 159 | + return dist; |
| 160 | + } |
| 161 | + |
| 162 | + public int getVertex() { |
| 163 | + return vertex; |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | +} |
0 commit comments