public bool PrimaSMSTAlgorithm() { VisitedNodes.Add(InnerGraph.SetOfNodes[0]); for (; VisitedNodes.Count < InnerGraph.QuantityOfNodes;) { Node Start = new Node(); Node Finish = new Node(); // maybe throw infinity to group of special onjects and instruments fo algorithm Edge CurrentEdge = new Edge { Weight = double.PositiveInfinity }; for (int Index = 0; Index < InnerGraph.QuantityOfNodes; Index++) { if (VisitedNodes.Contains(InnerGraph.SetOfNodes[Index])) { foreach (Node Incomer in InnerGraph.SetOfNodes[Index].Incomers.Where(Node => Node != null && !VisitedNodes.Contains(Node))) { if (CurrentEdge.Weight > InnerGraph.FindEdge(InnerGraph.SetOfNodes[Index], Incomer).Weight) { CurrentEdge = InnerGraph.FindEdge(InnerGraph.SetOfNodes[Index], Incomer); Start = CurrentEdge[0]; Finish = CurrentEdge[1]; } } } } VisitedNodes.Add(Finish); MinimumSpanningTree.Add(InnerGraph.FindEdge(Start, Finish)); } return(VisitedNodes.Count.Equals(InnerGraph.QuantityOfNodes)); }
/// <summary> /// Using Findsub tree develop from starting tree (root) to minimum spanning tree /// Don't if it should be bool type</summary> /// <returns></returns> public bool KruskalSMSTAlgorithm() { InnerGraph.SetOfEdges = InnerGraph.SetOfEdges.OrderBy(Edge => Edge.Weight).ToList <Edge>(); for (int Index = 0; Index < InnerGraph.QuantityOfEdges; 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); }