Skip to content

Commit 9e2ab91

Browse files
committed
add Dijkstra & Prim / modify README.md
1 parent 65475f4 commit 9e2ab91

File tree

4 files changed

+498
-0
lines changed

4 files changed

+498
-0
lines changed

06/Dijkstra.java

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

06/Dijkstra2.java

+167
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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

Comments
 (0)