-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrimsMST.cs
55 lines (46 loc) · 1.18 KB
/
PrimsMST.cs
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
private static int MinKey(int[] key, bool[] set, int verticesCount)
{
int min = int.MaxValue, minIndex = 0;
for (int v = 0; v < verticesCount; ++v)
{
if (set[v] == false && key[v] < min)
{
min = key[v];
minIndex = v;
}
}
return minIndex;
}
private static void Print(int[] parent, int[,] graph, int verticesCount)
{
Console.WriteLine("Edge Weight");
for (int i = 1; i < verticesCount; ++i)
Console.WriteLine("{0} - {1} {2}", parent[i], i, graph[i, parent[i]]);
}
public static void Prim(int[,] graph, int verticesCount)
{
int[] parent = new int[verticesCount];
int[] key = new int[verticesCount];
bool[] mstSet = new bool[verticesCount];
for (int i = 0; i < verticesCount; ++i)
{
key[i] = int.MaxValue;
mstSet[i] = false;
}
key[0] = 0;
parent[0] = -1;
for (int count = 0; count < verticesCount - 1; ++count)
{
int u = MinKey(key, mstSet, verticesCount);
mstSet[u] = true;
for (int v = 0; v < verticesCount; ++v)
{
if (Convert.ToBoolean(graph[u, v]) && mstSet[v] == false && graph[u, v] < key[v])
{
parent[v] = u;
key[v] = graph[u, v];
}
}
}
Print(parent, graph, verticesCount);
}