-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMinCostToConnectAllThePoints.java
67 lines (59 loc) · 2.17 KB
/
MinCostToConnectAllThePoints.java
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
/*https://leetcode.com/problems/min-cost-to-connect-all-points/*/
//prim's algorithm
class Solution {
static int getMinKeyVertex(int[] key, boolean[] mstSet, int V)
{
int minKey = Integer.MAX_VALUE, index = -1;
for (int i = 0; i < V; i++)
{
if (!mstSet[i] && key[i] < minKey)
{
minKey = key[i];
index = i;
}
}
return index;
}
public int minCostConnectPoints(int[][] points) {
int n = points.length, i, j;
//convert the points and their distances to graph
int[][] graph = new int[n][n];
for (i = 0; i < n; ++i)
{
for (j = 0; j < n; ++j)
{
graph[i][j] = Math.abs(points[i][0]-points[j][0])+Math.abs(points[i][1]-points[j][1]);
}
}
int result = 0;
int[] parent = new int[n]; //stores mst
int[] key = new int[n]; //stores key values for each vertex
boolean[] mstSet = new boolean[n]; //keeps track of the vertices added to the mst
//initializing all keys to inifinite except 0
for (i = 1; i < n; ++i)
key[i] = Integer.MAX_VALUE;
parent[0] = -1; //0th vertex included to the mst
int minKeyVertex, count;
//for all vertices
for (count = 1; count < n; ++count)
{
minKeyVertex = getMinKeyVertex(key,mstSet,n);//find the minimum key which is not added to the mst yet
mstSet[minKeyVertex] = true; //add it to the mst
//for all the edges from the adjacent vertex
for (i = 0; i < n; ++i)
{
//if it is not added to the mst and edge weight is smaller than the current key
if (i != minKeyVertex && !mstSet[i] && graph[i][minKeyVertex] < key[i])
{
//update the value and the parent
parent[i] = minKeyVertex;
key[i] = graph[i][minKeyVertex];
}
}
}
//add all the values and return
for (i = 0; i < key.length; ++i)
result += key[i];
return result;
}
}