-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #258 from MrJayendra/master
Kruskal's Algorithm in C#
- Loading branch information
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; | ||
} |