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

Tagged as

Go to top

Kruskal Algorithm

, 5 Jul 2012
Rate this:
Please Sign up or sign in to vote.
Implementation of Kruskal Algorithm in C#

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)

Share

About the Author

Omar Gameel Salem
Software Developer
Australia Australia
Enthusiastic programmer/researcher, passionate to learn new technologies, interested in problem solving, data structures, algorithms and automation.
 
Amateur guitarist/ keyboardist, squash player.
 
If you have a question\suggestion about one of my articles, or you want an algorithm implemented in C#, feel free to contact me.
Follow on   LinkedIn

Comments and Discussions

 
QuestionEdges PinmemberMember 106370692-Mar-14 6:14 
AnswerRe: Edges PinmemberMember 1080906410-May-14 8:15 
QuestionKRUSKAL ALGORITM EASY IMPLENTATION For A BEginer Learner PinmemberMember 104879102-Jan-14 10:08 
GeneralGreat article, Go 5! PinmemberThe Manoj Kumar21-Nov-13 8:13 
GeneralRe: Great article, Go 5! PinmemberOmar Gameel Salem21-Nov-13 21:44 
GeneralThis is a masterpiece PinmemberThirstyMind13-Oct-13 6:21 
GeneralRe: This is a masterpiece PinmemberOmar Gameel Salem13-Oct-13 6:34 
Questionaaa PinmemberMember 102258431-Oct-13 20:35 
QuestionWhat compiler you use?? thanks :) PinmemberMember 100400969-May-13 23:09 
AnswerRe: What compiler you use?? thanks :) PinmemberOmar Gameel Salem10-May-13 2:53 
GeneralMy vote of 5 PinmemberKenneth Haugland10-Aug-12 23:00 
GeneralMy vote of 5 PinmemberAbinash Bishoyi6-Apr-12 3:05 
GeneralMy vote of 5 Pinmembermanoj kumar choubey27-Mar-12 23:44 
GeneralMy Vote of 5 PinmemberRaviRanjankr30-Dec-11 4:58 
GeneralRe: My Vote of 5 Pinmembermobilat30-Dec-11 6:39 
QuestionQuick sort Pinmembermobilat28-Dec-11 22:48 
GeneralMy vote of 5 Pinmemberal13n9-Oct-11 21:42 
QuestionSome responses to unjustified criticisms [modified] PinmemberAndyUk0611-Sep-11 4:02 
AnswerRe: Some responses to unjustified criticisms PinmemberOmar Gamil11-Sep-11 4:09 
GeneralRe: Some responses to unjustified criticisms PinmemberAndyUk0611-Sep-11 6:52 
QuestionMy vote of 5 PinmemberAndyUk069-Sep-11 1:01 
GeneralMy vote of 5 Pinmembernaser abu khas2-Apr-11 14:24 
GeneralMy vote of 5 Pinmemberparyj9-Mar-11 18:36 
GeneralMy vote of 5 PinmemberEyalSch28-Mar-11 4:57 
GeneralMy vote of 1 PinmemberJV99992-Mar-11 4:16 
GeneralMy vote of 1 PinmemberPetr Pechovic2-Mar-11 0:57 
GeneralRe: My vote of 1 PinmemberOmar Gamil2-Mar-11 1:54 
GeneralRe: My vote of 1 PinmemberPetr Pechovic2-Mar-11 7:58 
GeneralRe: My vote of 1 PinmemberOmar Gamil2-Mar-11 21:33 
GeneralRe: My vote of 1 PinmvpDave Kreskowiak2-Mar-11 9:05 
GeneralRe: My vote of 1 PinmemberM i s t e r L i s t e r2-Mar-11 12:36 
GeneralMy vote of 1 Pinmembersnortle2-Mar-11 0:14 
GeneralMy vote of 2 PinmemberAdrian Pasik1-Mar-11 22:45 
GeneralRe: My vote of 2 PinmemberOmar Gamil1-Mar-11 23:28 
GeneralMy vote of 5 PinmemberTV Mogul1-Mar-11 10:17 
GeneralRe: My vote of 5 PinmemberOmar Gamil1-Mar-11 21:58 
General[My vote of 1] Not good a article... PinmvpDave Kreskowiak1-Mar-11 8:39 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 5 Jul 2012
Article Copyright 2011 by Omar Gameel Salem
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid