-
Notifications
You must be signed in to change notification settings - Fork 98
/
kruskalsAlg.cs
65 lines (60 loc) · 1.58 KB
/
kruskalsAlg.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
static void
Main (string[]args)
{
int k = 1;
int vert = 4;
int e = 0;
KGraph objGraph = new KGraph (vert); //Step 1
KVertex[]vertcoll = objGraph.vertcoll;
KEdge[]result = new KEdge[vert];
List < KEdge > edgecoll = new List < KEdge > (); //Step 2
KEdge objEdge = new KEdge ();
for (int i = 0; i < vert; i++) //Step 3
{
for (int j = i; j < vert; j++)
{
if (i != j)
{
Console.WriteLine ("KEdge weight from src{0} to destn{1}", i,
j);
int wt = int.Parse (Console.ReadLine ());
if (wt == 0)
continue;
objEdge = new KEdge (vertcoll[i], vertcoll[j], wt);
edgecoll.Add (objEdge);
k++;
}
}
}
objGraph.Edgecoll = edgecoll.ToList ().OrderBy (p = >p.weight).ToList ();
KSubsets[]sub = new KSubsets[vert]; //Step 4
KSubsets subobj;
for (int i = 0; i < vert; i++)
{
subobj = new KSubsets ();
subobj.parent = vertcoll[i];
subobj.rank = 0;
sub[i] = subobj;
}
k = 0;
while (e < vert - 1)
{
objEdge = objGraph.Edgecoll.ElementAt (k);
KVertex x = find (sub, objEdge.V1, Array.IndexOf (objGraph.vertcoll, objEdge.V1), objGraph.vertcoll); //Step 5
KVertex y = find (sub, objEdge.V2, Array.IndexOf (objGraph.vertcoll, objEdge.V2), objGraph.vertcoll); //Step 6
if (x != y)
{
result[e] = objEdge;
Union (sub, x, y, objGraph.vertcoll);
e++;
}
k++;
}
for (int i = 0; i < e; i++) //Step 7
{
Console.WriteLine ("edge from src:{0} to dest:{1} with weight:{2}",
result[i].V1.Label, result[i].V2.Label,
result[i].weight);
}
return;
}