Пример #1
0
        public bool FindMinimumSpanningTree()
        {
            InnerGraph.SetOfEdges = InnerGraph.SetOfEdges.OrderBy(Edge => Edge.Weight).ToList <Edge>();

            for (int Index = 0; Index < InnerGraph.NumberOfEdges; Index++)
            {
                Node StartNode = FindSubTree(InnerGraph.SetOfEdges[Index][0]);

                Node EndNode = FindSubTree(InnerGraph.SetOfEdges[Index][1]);

                if (!StartNode.Equals(EndNode))
                {
                    MinimumSpanningTree.Add(InnerGraph.SetOfEdges[Index]);

                    InnerGraph.SetOfNodes[EndNode.Index] = StartNode;
                }
            }

            return(true);
        }
Пример #2
0
        public bool FindMinimumSpanningTree()
        {
            VisitedNodes.Add(InnerGraph.SetOfNodes[0]);

            for (; VisitedNodes.Count < InnerGraph.NumberOfNodes;)
            {
                Node Start = new Node();

                Node End = new Node();

                Edge CurrentEdge = new Edge {
                    Weight = Infinity
                };

                for (int Index = 0; Index < InnerGraph.NumberOfNodes; Index++)
                {
                    if (VisitedNodes.Contains(InnerGraph.SetOfNodes[Index]))
                    {
                        foreach (Node Inheritor in InnerGraph.SetOfNodes[Index].Inheritors.Where(Node => Node != null && !VisitedNodes.Contains(Node)))
                        {
                            if (CurrentEdge.Weight > InnerGraph.FindEdge(InnerGraph.SetOfNodes[Index], Inheritor).Weight)
                            {
                                CurrentEdge = InnerGraph.FindEdge(InnerGraph.SetOfNodes[Index], Inheritor);

                                Start = CurrentEdge[0];

                                End = CurrentEdge[1];
                            }
                        }
                    }
                }

                VisitedNodes.Add(End);

                MinimumSpanningTree.Add(InnerGraph.FindEdge(Start, End));
            }

            return(VisitedNodes.Count.Equals(InnerGraph.NumberOfNodes));
        }