Click here to Skip to main content
15,896,557 members
Articles / Desktop Programming / WPF

Kruskal's Minimum Spanning Tree

Rate me:
Please Sign up or sign in to vote.
4.62/5 (10 votes)
2 Nov 2011GPL36 min read 44.9K   3.2K   9  
An example of a .NET implementation of Kruskal’s algorithm for finding the minimum spanning tree of a connected, undirected graph.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace MinSpanTreeWpf
{
    /// <summary>
    /// Interaction logic for DistanceDialog.xaml
    /// </summary>
    public partial class DistanceDialog : Window
    {
        private double _distance;
        public DistanceDialog()
        {
            InitializeComponent();
            _distance = 0.0;
        }

        private void okBtn_Click(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(distanceTextBox.Text) && double.TryParse(distanceTextBox.Text, out _distance) && _distance > 0.0)
                this.DialogResult = true;
            else
                MessageBox.Show("Please give a valid number greater than 0", "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
        }

        public double Distance
        {
            get { return _distance; }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (_distance == 0.0)
                this.DialogResult = false;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            distanceTextBox.Focus();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer
Bulgaria Bulgaria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions