Esempio n. 1
0
        public KruskalNode Root()
        {
            if (this.parent == null)
            {
                return(this);
            }
            KruskalNode newNode = this.parent.Root();

            this.parent = newNode;
            return(newNode);
        }
Esempio n. 2
0
        private void Visualize_Click(object sender, EventArgs e)
        {
            kruskalTextLabel.Text = "";
            ParseResults parsedResult = KruskalTextParser.ParseText(kruskalTextBox.Text);

            points = new Point[parsedResult.numberOfVertices];
            if (parsedResult.numberOfVertices > 0)
            {
                double angle = 360 / parsedResult.numberOfVertices;
                points[0] = new Point(60, anchor.Y);

                for (int i = 1; i < parsedResult.numberOfVertices; i++)
                {
                    points[i] = RotatePoint(points[0], anchor, angle * i);
                }
            }
            allEdges = parsedResult.allEdges;

            try
            {
                Graph graph = new Graph(parsedResult.numberOfVertices);
                foreach (Edge edge in allEdges)
                {
                    graph.AddEdge(edge.from, edge.to, edge.weight);
                }
                kruskalEdges = KruskalNode.Kruskal(graph);
                foreach (Edge edge in kruskalEdges)
                {
                    kruskalTextLabel.Text += "" + edge.from + " -> " + edge.to + " = " + edge.weight + "\n";
                }
            }
            catch
            {
                kruskalTextLabel.Text = "Couldn't execute Kruskal algorithm.";
            }

            panel1.Invalidate();
        }
Esempio n. 3
0
        bool Merge(KruskalNode other)
        {
            KruskalNode rootThis  = this.Root();
            KruskalNode rootOther = other.Root();

            if (rootThis == rootOther)
            {
                return(false);
            }
            if (rootThis.height < rootOther.height)
            {
                rootThis.parent = rootOther;
            }
            else
            {
                rootOther.parent = rootThis;
                if (rootThis.height == rootOther.height)
                {
                    rootThis.height += 1;
                }
            }
            return(true);  // merge successful
        }
Esempio n. 4
0
        public static List <Edge> Kruskal(Graph graph)
        {
            int numberofVertices = graph.GetNumberOfVertices();

            List <Edge> results           = new List <Edge>();
            int         resultEdge        = 0;
            int         currentSortedEdge = 0;


            List <Edge> sortedEdges = graph.GetSortedEdges();

            KruskalNode[] subSets = new KruskalNode[numberofVertices];
            for (int i = 0; i < numberofVertices; i++)
            {
                subSets[i] = new KruskalNode();
                //subSets[i].parent = null;
                subSets[i].height = 0;
            }

            while (resultEdge < numberofVertices - 1)
            {
                Edge nextEdge = sortedEdges[currentSortedEdge++];

                KruskalNode firstNode  = subSets[nextEdge.from].Root();
                KruskalNode secondNode = subSets[nextEdge.to].Root();

                if (firstNode.Merge(secondNode))
                {
                    results.Add(nextEdge);
                    resultEdge++;
                }
            }

            //Print(results);

            return(results);
        }
Esempio n. 5
0
 bool InSameSet(KruskalNode other)
 {
     return(this.Root() == other.Root());
 }
Esempio n. 6
0
 public KruskalNode()
 {
     this.parent = null;
     this.height = 0;
 }