Click here to Skip to main content
Click here to Skip to main content

Kruskal Algorithm

By , 5 Jul 2012
 

Introduction

According to Wikipedia:"Kruskal's algorithm is an algorithm in graph theory that finds a minimum spanning tree for a connectedweighted graph. This means it finds a subset of the edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is minimized. If the graph is not connected, then it finds a minimum spanning forest (a minimum spanning tree for each connected component). Kruskal's algorithm is an example of a greedy algorithm."

In short, Kruskal's algorithm is used to connect all nodes in a graph, using the least cost possible.

Example

A cable TV company is laying a cable in a new neighborhood. An internet cafe is connecting all PCs via network.

Using the Demo

Click anywhere to plot the vertices. Hold down ctrl and select two vertices to create an edge. A popup window appears to enter edge cost. Having finished plotting the graph, click Solve.

High Level Design

Low Level Design

Using the Code

IList<Edge> Solve(IList<Edge> graph, out int totalCost);  

How the Graph is formed with GDI is not covered as it is irrelevant.

Classes

Typically, our graph consists of two components, Vertices, and Edges connecting these vertices.

Each Edge is marked by a value or weight, which is the Cost of connecting the two vertices.

Vertex

Holds:

  • Vertex Name (which must be unique within Graph) and its Drawing Point
  • Rank and Root (we'll get to those later)
using System;
using System.Drawing;

namespace Kruskal
{
    public class Vertex
    {
        #region Members

        private int name;

        #endregion

        #region Public Properties

        public int Name
        {
            get
            {
                return name;
            }
        }

        public int Rank { get; set; }

        public Vertex Root { get; set; }

        public Point Position { get; set; }

        #endregion

        #region Constructor

        public Vertex(int name, Point position)
        {
            this.name = name;
            this.Rank = 0;
            this.Root = this;
            this.Position = position;
        } 

        #endregion

        #region Methods

        internal Vertex GetRoot()
        {
            if (this.Root != this)// am I my own parent ? (am i the root ?)
            {
                this.Root = this.Root.GetRoot();// No? then get my parent
            }

            return this.Root;
        }

        internal static void Join(Vertex root1, Vertex root2)
        {
            if (root2.Rank < root1.Rank)//is the rank of Root2 less than that of Root1 ?
            {
                root2.Root = root1;//yes! then Root1 is the parent of Root2 (since it has the higher rank)
            }
            else //rank of Root2 is greater than or equal to that of Root1
            {
                root1.Root = root2;//make Root2 the parent
                if (root1.Rank == root2.Rank)//both ranks are equal ?
                {
                    root2.Rank++;//increment Root2, we need to reach a single root for the whole tree
                }
            }
        }

        #endregion
    }
} 

Edge

Holds two Vertices, Cost of connection between them, and CostDrawing Point.

Note that it implements IComparable, we'll need it to sort Edges by Cost later on.

using System;
using System.Drawing;

namespace Kruskal
{
    public class Edge : IComparable
    {
        #region Members

        private Vertex v1, v2;
        private int cost;
        private Point stringPosition;

        #endregion

        #region Public Properties

        public Vertex V1
        {
            get
            {
                return v1;
            }
        }

        public Vertex V2
        {
            get
            {
                return v2;
            }
        }

        public int Cost
        {
            get
            {
                return cost;
            }
        }

        public Point StringPosition
        {
            get
            {
                return stringPosition;
            }
        }

        #endregion

        #region Constructor

        public Edge(Vertex v1, Vertex v2, int cost, Point stringPosition)
        {
            this.v1 = v1;
            this.v2 = v2;
            this.cost = cost;
            this.stringPosition = stringPosition;
        } 

        #endregion

        #region IComparable Members

        public int CompareTo(object obj)
        {
            Edge e = (Edge)obj;
            return this.cost.CompareTo(e.cost);
        }

        #endregion
    }
} 

Algorithm Implementation

Sorting Edges

Edges are sorted in ascending order according to Cost using Quick Sort.

Making Sets

Initially, every Vertex is its own Root and has rank zero.

        public Vertex(int name, Point position)
        {
            this.name = name;
            this.Rank = 0;
            this.Root = this;
            this.Position = position;
        }  

Why This Step?

We need it to ensure that, on adding our Vertices, we are not forming a loop.

Consider this example:

The Edge BD was not considered, because B,D are already connected through B,A,D.

Thus, for every Edge we examine, we must inspect that its two Vertices belong to different sets (trees).

How To Find Out If Two Vertices Are In Different Sets?

Using the recursive function GetRoot().

internal Vertex GetRoot()
        {
            if (this.Root != this)// am I my own parent ? (am i the root ?)
            {
                this.Root = this.Root.GetRoot();// No? then get my parent
            }

            return this.Root;
        } 

If roots are indeed different, (each Vertex is in a different set), Join() the two Vertices.

internal static void Join(Vertex root1, Vertex root2)
        {
            if (root2.Rank < root1.Rank)//is the rank of Root2 less than that of Root1 ?
            {
                root2.Root = root1;//yes! then Root1 is the parent of Root2 (since it has the higher rank)
            }
            else //rank of Root2 is greater than or equal to that of Root1
            {
                root1.Root = root2;//make Root2 the parent
                if (root1.Rank == root2.Rank)//both ranks are equal ?
                {
                    root2.Rank++;//increment Root2, we need to reach a single root for the whole tree
                }
            }
        } 

Conclusion

Hope I delivered a clear explanation.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Omar Gameel Salem
Software Developer
Egypt Egypt
Member
Enthusiastic programmer/researcher, passionate to learn new technologies, interested in problem solving,data structures, algorithms and automation.
 
If you have a question\suggestion about one of my articles, or you want an algorithm implemented in C#, feel free to contact me.
 
Résumé
 
vWorker Account

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionWhat compiler you use?? thanks :)memberMember 100400969 May '13 - 23:09 
What compiler you use?? thanks Smile | :)
AnswerRe: What compiler you use?? thanks :)memberOmar Gameel Salem10 May '13 - 2:53 
old fashioned Visual Studio 2010 Wink | ;)
GeneralMy vote of 5memberKenneth Haugland10 Aug '12 - 23:00 
Very interesting article and a nice program Smile | :)
GeneralMy vote of 5memberAbinash Bishoyi6 Apr '12 - 3:05 
Nice
GeneralMy vote of 5membermanoj kumar choubey27 Mar '12 - 23:44 
Nice
GeneralMy Vote of 5memberRaviRanjankr30 Dec '11 - 4:58 
Nice Article Thumbs Up | :thumbsup:
GeneralRe: My Vote of 5membermobilat30 Dec '11 - 6:39 
can u help me in understanding Quick sort function
and how can i draw !?
thanks in advance
QuestionQuick sortmembermobilat28 Dec '11 - 22:48 
i didn't get this section of code "Quick sort" part
plz would you explain it in details Smile | :)
GeneralMy vote of 5memberal13n9 Oct '11 - 21:42 
Thanks Omar, your article was concise and easy to read. I look forward to more!
QuestionSome responses to unjustified criticisms [modified]memberAndyUk0611 Sep '11 - 4:02 
I think some of the criticisms aimed at Omar's article are a little unfair.
 
Like Omar says, if you bother to read this, you will at least get the gist of what the article is about: finding Minimal Spanning Trees using Kruskal's Algorithm, by using a fairly intuitive user interface with which to plot the links, link connection strengths and nodes. He DOES describe what the algorithm does and how it works - basically to find the subset of the available edges such that their overall weight is minimised, while avoiding network cycles.
 
Snortle
"Looked interesting, turned out to be a waste of an article."
 
That is not a contribution Snortle. Its a chippy little comment, that offers nothing in the way of positive suggestions.
 
Adrian Pasik
"...i want to see some real life demo where i can use it."
 
What Omar has done is provide a plausible prototype in C#. Tell you what, why don't YOU take his working version and apply it to whatever real-world application YOU have in mind, be it telecoms networks, pipe networks or whatever. His proof of concept is sound - feed it links, nodes and link connection strengths, press "Solve" and out plops the minimal spanning tree. What more do you want Adrian?
 
JV9999
1. This article is about Kruskal, not sorting algorithms.
2. "And how do I give those edges values? Those are probably all part of the algorithm I assume."
You insert edge values exactly in the manner Omar described - hold down the control button, select two nodes and enter the value. Omar cannot do these kinds of things for you, my friend.
3. It's clear you are struggling with some of the concepts Omar describes. Don't criticize just because you don't understand them.
4. "I'm not gonna drag with cables just to use this!"
What the heck are you talking about?
 
Dave Kreskowiak
 
After removing code snippets and pictures I think you'll you'll find that he does. His brief Wiki entry I would say describes what Kruskal is all about better and more succinctly than most I've ever seen, and so I would say his short inclusion is justified. He at least makes a reference to it and doesn't try to pass it off as his own.
 
The use of pictures and visuals I also think can be a far more effective key to understanding than lots of dry theory. What appeals to me is his intuitive graphical user interface used to represent nodes and links. I would definitely find this useful.
 
"You went into no detail at all about how this maps into the real world example"
 
How about "laying a cable in a new neighborhood" or "An internet cafe is connecting all PCs via network."? You make the same misinterpretation as Adrian Pasik. It doesn't take a huge stretch of the imagination to visualize the link integer values he displays as representing quantities like pipe lengths (miles), network bandwidths (kbit/s), etc.

modified on Sunday, September 11, 2011 6:38 PM

AnswerRe: Some responses to unjustified criticismsmemberOmar Gamil11 Sep '11 - 4:09 
Thank Andy Smile | :)
GeneralRe: Some responses to unjustified criticismsmemberAndyUk0611 Sep '11 - 6:52 
You're welcome. Your articles in my opinion are at least as good if not better than those of your critics.
QuestionMy vote of 5memberAndyUk069 Sep '11 - 1:01 
Splendid!
 
I have a similar application over at my blog that uses the Boost libraries in an C++/MFC environment: http://technical-recipes.com/?p=16[^]
 
I am currently trying to get to grips with C# / .NET / WPF related stuff and I will definitely use this as a good learning example. Cheers.
GeneralMy vote of 5membernaser abu khas2 Apr '11 - 14:24 
very helpfull
GeneralMy vote of 5memberparyj9 Mar '11 - 18:36 
Excellent article - Oram
 
Thanks.
GeneralMy vote of 5memberEyalSch28 Mar '11 - 4:57 
Nice Article
GeneralMy vote of 1memberJV99992 Mar '11 - 4:16 
Dave pretty much summed it up. I do see some code examples, but these seem to be random snippets which I have no clue how they are related to the algorithm (I guess they are?) and what they are used for or what they are doing.
 
And how do I sort the edges by sort? In a big graph this will be still slow and hard. And how do I give those edges values? Those are probably all part of the algorithm I assume.
 
And your examples are nice, but we're in a software engineering world, so give us some example how we could use it in our world, because I'm not gonna drag with cables just to use this!
GeneralMy vote of 1memberPetr Pechovic2 Mar '11 - 0:57 
agree with Dave
GeneralRe: My vote of 1memberOmar Gamil2 Mar '11 - 1:54 
Response intended to Dave also:
 
"you provide no detailed discussion of the concepts behind the algorithm"
That's absolutely right, no excuses
 
"what it's used for"
have you read the article, you'd have noticed--> In short, Kruskal algorithm is used to connect all nodes in a graph, using the least cost possible
 
"examples of its use"
Again, if you bothered to look at the article-->
 
Example:
A cable TV company is laying cable in a new neighborhood.
An internet Cafe is connecting all PC's via network.
 
and finally "how the code works"
 
i explained all steps except sorting, which is known to any computer science first grader, and the article is aimed to intermediate level anyway.
 

to sum up, i dont think you or dave even read the article at all, and giving such low rank for someone who took the time and effort to try to help others is unprofessional
GeneralRe: My vote of 1memberPetr Pechovic2 Mar '11 - 7:58 
your update is much better. I re-voted to 4.
GeneralRe: My vote of 1memberOmar Gamil2 Mar '11 - 21:33 
Thanks
GeneralRe: My vote of 1mvpDave Kreskowiak2 Mar '11 - 9:05 
Omar Gamil wrote:
"what it's used for"
have you read the article, you'd have noticed--> In
short, Kruskal algorithm is used to connect all nodes in a graph, using the
least cost possible

"examples of its use"
Again, if you bothered to
look at the article-->

Example:
A cable TV company is laying cable
in a new neighborhood.
An internet Cafe is connecting all PC's via network.

 
I did read the article. You went into no detail at all about how this maps into the real world example, and you only provided the one use case.
 

Omar Gamil wrote:
to sum up, i dont think you or dave even read the article at all, and giving
such low rank for someone who took the time and effort to try to help others is
unprofessional

 
I've been around here for an awful long time and I've seen a thousand articles. How I evaluate one is to remove all of the code snippets and pictures, in this case, the part copied from Wikipedia, and read what's left. What's left should COMPLETELY discuss the concepts and implementation of the topic at hand. Code snippets and pictures are there to enhance the discussion, not provide it. (Technical Writing 101) Your work didn't pass that test.

GeneralRe: My vote of 1memberM i s t e r L i s t e r2 Mar '11 - 12:36 
Interesting...
 
I remove all of the text and see if the code and pictures make sense   Smile | :)
 
I also check the messages to see how many bugs were found in the code
If there is bug after bug after bug.. I consider it a bad example.
GeneralMy vote of 1membersnortle2 Mar '11 - 0:14 
Looked interesting, turned out to be a waste of an article.
GeneralMy vote of 2memberAdrian Pasik1 Mar '11 - 22:45 
Format Your code better please, because when people see it first impression is that You didnt spend much time formatting Your code. Then i want to see some real life demo where i can use it.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 5 Jul 2012
Article Copyright 2011 by Omar Gameel Salem
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid