-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKruskals.cs
95 lines (79 loc) · 1.89 KB
/
Kruskals.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
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
public struct Edge
{
public int Source;
public int Destination;
public int Weight;
}
public struct Graph
{
public int VerticesCount;
public int EdgesCount;
public Edge[] edge;
}
public struct Subset
{
public int Parent;
public int Rank;
}
public static Graph CreateGraph(int verticesCount, int edgesCoun)
{
Graph graph = new Graph();
graph.VerticesCount = verticesCount;
graph.EdgesCount = edgesCoun;
graph.edge = new Edge[graph.EdgesCount];
return graph;
}
private static int Find(Subset[] subsets, int i)
{
if (subsets[i].Parent != i)
subsets[i].Parent = Find(subsets, subsets[i].Parent);
return subsets[i].Parent;
}
private static void Union(Subset[] subsets, int x, int y)
{
int xroot = Find(subsets, x);
int yroot = Find(subsets, y);
if (subsets[xroot].Rank < subsets[yroot].Rank)
subsets[xroot].Parent = yroot;
else if (subsets[xroot].Rank > subsets[yroot].Rank)
subsets[yroot].Parent = xroot;
else
{
subsets[yroot].Parent = xroot;
++subsets[xroot].Rank;
}
}
private static void Print(Edge[] result, int e)
{
for (int i = 0; i < e; ++i)
Console.WriteLine("{0} -- {1} == {2}", result[i].Source, result[i].Destination, result[i].Weight);
}
public static void Kruskal(Graph graph)
{
int verticesCount = graph.VerticesCount;
Edge[] result = new Edge[verticesCount];
int i = 0;
int e = 0;
Array.Sort(graph.edge, delegate (Edge a, Edge b)
{
return a.Weight.CompareTo(b.Weight);
});
Subset[] subsets = new Subset[verticesCount];
for (int v = 0; v < verticesCount; ++v)
{
subsets[v].Parent = v;
subsets[v].Rank = 0;
}
while (e < verticesCount - 1)
{
Edge nextEdge = graph.edge[i++];
int x = Find(subsets, nextEdge.Source);
int y = Find(subsets, nextEdge.Destination);
if (x != y)
{
result[e++] = nextEdge;
Union(subsets, x, y);
}
}
Print(result, e);
}