-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPrimsAlgorithm.java
More file actions
132 lines (106 loc) · 3.5 KB
/
PrimsAlgorithm.java
File metadata and controls
132 lines (106 loc) · 3.5 KB
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
/*
* Code by:@yinengy
* Time: 10/8/2018
*
* No pseudocode in the book,
* but I mainly follow the content on page 150
*/
import java.io.FileNotFoundException;
import java.util.LinkedList;
import java.util.PriorityQueue;
public class PrimsAlgorithm {
/**
* node with cost(to s) as key, which is comparable and changeable,
* which is the same as I implemented in Dijkstra's Algorithm
*/
private static class Node implements Comparable<Node> {
// it should be static in order to be referenced by static method
int id;
int key;
private static final int MAX_DISTANCE = 999; // a number to indicate infinity cost
/**
* initial cost is infinity(MAX_DISTANCE)
*
* @param id node id
*/
Node(int id) {
this.id = id;
this.key = MAX_DISTANCE;
}
public int getKey() {
return key;
}
public int getId() {
return id;
}
@Override
public int compareTo(Node n) {
return key - n.key;
}
public void changeKey(int newKey) {
this.key = newKey;
}
}
/**
* main algorithm
*
* input is similar to Dijkstra's Algorithm
*/
public static AdjacencyList MST(AdjacencyList G, int[][] l, int s) {
// initial a priority queue
PriorityQueue<Node> pq = new PriorityQueue<>();
Node[] d = new Node[G.getNumV() + 1];
for (int i = 1; i <= G.getNumV(); i++) {
d[i] = new Node(i); // initially infinity
if (i == s) {
d[s].changeKey(0);
}
pq.add(d[i]); // add to priority queue
}
// a set S for explored nodes
boolean[] S = new boolean[G.getNumV() + 1];
// a array to store the parent of each node in the tree;
int[] parent = new int[G.getNumV() + 1];
while (!pq.isEmpty()) {
int u = pq.poll().getId();
S[u] = true; // true for explored.
LinkedList<Integer> edges = G.getEdges(u);
for (int v : edges) {
if (!S[v] & l[u][v] < d[v].getKey()) { // this is different to Dijkstra
// update key in PQ
pq.remove(d[v]); // O(n)
d[v].changeKey(l[u][v]);
pq.add(d[v]);
// update parent
parent[v] = u;
}
}
}
// create tree from parent[], O(n)
AdjacencyList Tree = new AdjacencyList(G.getNumV());
for (int i = 1; i < parent.length; i++) {
if (i != s) {
Tree.addEdge(parent[i], i);
Tree.addEdge(i, parent[i]);
}
}
return Tree;
}
public static void main(String[] args) throws FileNotFoundException {
int[][] l = {{0, 1, 2, 3, 4, 5, 6, 7, 8},
{1, 0, 5, 0, 0, 9, 0, 0, 8},
{2, 5, 0, 12, 15, 0, 0, 0, 4},
{3, 0, 12, 0, 3, 0, 1, 11, 7},
{4, 0, 15, 3, 0, 0, 0, 9, 0},
{5, 9, 0, 0, 0, 0, 4, 20, 5},
{6, 0, 0, 1, 0, 4, 0, 13, 6},
{7, 0, 0, 11, 9, 20, 13, 0, 0},
{8, 8, 4, 7, 0, 5, 6, 0, 0}
};
AdjacencyList graph = new AdjacencyList(8);
graph.addFromCSV("test\\MST.csv");
System.out.println(graph);
AdjacencyList tree = MST(graph, l, 1);
System.out.println(tree);
}
}