-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathkruskal_mst.cs
98 lines (75 loc) · 1.98 KB
/
kruskal_mst.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
96
97
98
using System;
using System.Collections.Generic;
public class KruskalMST {
public struct Edge {
public int u, v, w;
public Edge(int u, int v, int w) {
this.u = u;
this.v = v;
this.w = w;
}
};
public int nodes, edges;
public int[] parent;
public List <Edge> graph;
public void initKruskalMST(int nodes, int edges) {
this.nodes = nodes;
this.edges = edges;
parent = new int[nodes];
graph = new List<Edge>();
}
public int find(int r) {
return (parent[r] == r) ? r : parent[r] = find(parent[r]);
}
public void kruskal_mst() {
graph.Sort(delegate(Edge e1, Edge e2) {
return e1.w.CompareTo(e2.w);
});
for (int i = 0; i < nodes; i++) {
parent[i] = i;
}
int cnt = 0, total_cost = 0;
for (int i = 0; i < edges; i++) {
int u = find(graph[i].u);
int v = find(graph[i].v);
if (u != v) {
Console.WriteLine("( {0} - {1} ) = {2}", graph[i].u, graph[i].v, graph[i].w);
parent[v] = u;
total_cost += graph[i].w;
cnt++;
if (cnt == nodes - 1) break;
}
}
Console.WriteLine("minimum cost = {0}", total_cost);
}
public void manageKruskalMST(string[] lines) {
string[] line1 = lines[0].Split(' ');
int nodes = int.Parse(line1[0]);
int edges = int.Parse(line1[1]);
initKruskalMST(nodes, edges);
for (int i = 1; i <= edges; i++) {
string[] all_edge = lines[i].Split(' ');
int u = int.Parse(all_edge[0]);
int v = int.Parse(all_edge[1]);
int w = int.Parse(all_edge[2]);
graph.Add(new Edge(u, v, w));
}
kruskal_mst();
}
}
/*
stdin
-
4 5
0 1 2
0 2 3
0 3 1
2 1 2
3 2 2
stdout
-
( 0 : 3 ) = 1
( 0 : 1 ) = 2
( 2 : 1 ) = 2
minimum cost = 5
*/