public bool FindTheShortesPath(Node Start, Node Target) { Node CurrentNode = Start; OpenList.Add(CurrentNode); for (; OpenList.Count > 0;) { CurrentNode = OpenList[0]; if (CurrentNode.Equals(Target)) { VisitedNodes.Add(Target); for (int Index = 0; Index < VisitedNodes.Count - 1; Index++) { ShortesPath.Add(InnerGraph.FindEdge(VisitedNodes[Index], VisitedNodes[Index + 1])); } return(true); } OpenList.Remove(CurrentNode); ClosedList.Add(CurrentNode); foreach (Node Inheritor in CurrentNode.Inheritors.Where(Node => Node != null && Node.Index != Node.Inheritors.Length - 1)) { if (!ClosedList.Contains(Inheritor)) { if (!OpenList.Contains(Inheritor)) { Inheritor[Inheritor.Index] = CurrentNode; Inheritor.HeuristicPathWeight = CalculateHeuristic(Inheritor, Target); Inheritor.GainedPathWeight = InnerGraph.FindEdge(CurrentNode, Inheritor).Weight; Inheritor.TotalPathWeight = Inheritor.GainedPathWeight + Inheritor.HeuristicPathWeight; OpenList.Add(Inheritor); OpenList = OpenList.OrderBy(Node => Node.TotalPathWeight).ToList <Node>(); } } } VisitedNodes.Add(CurrentNode); } return(true); }
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); }
private Node FindSubTree(Node Root) { Node OldNode = new Node(); Node RootOfTree = Root; for (; !RootOfTree.Equals(InnerGraph.SetOfNodes[RootOfTree.Index]);) { RootOfTree = InnerGraph.SetOfNodes[RootOfTree.Index]; } for (; !Root.Equals(RootOfTree);) { OldNode = InnerGraph.SetOfNodes[Root.Index]; InnerGraph.SetOfNodes[Root.Index] = RootOfTree; Root = OldNode; } return(RootOfTree); }
public bool FindMaximumFlowWithDFSRealization(Node Start, Node Target) { if (Start.Equals(Target)) { CapacityOfFlow = 0.0; return(true); } Node CurrentNode = new Node(); for (; DepthFirstSearch.DepthFirstSearch(Start, Target, FoundPath);) { double CurrentStreamCapacity = Infinity; for (int Index = Target.Index; Index != Start.Index; Index = FoundPath[Index].Index) { CurrentNode = FoundPath[Index]; CurrentStreamCapacity = Min(CurrentStreamCapacity, InnerGraph.FindEdge(CurrentNode, InnerGraph.SetOfNodes[Index]).Weight); } for (int Index = Target.Index; Index != Start.Index; Index = FoundPath[Index].Index) { InnerGraph.FindEdge(CurrentNode, InnerGraph.SetOfNodes[Index]).Weight -= CurrentStreamCapacity; InnerGraph.FindEdge(InnerGraph.SetOfNodes[Index], CurrentNode).Weight += CurrentStreamCapacity; } CapacityOfFlow += CurrentStreamCapacity; DepthFirstSearch = new DepthFirstSearchAlgorithm(InnerGraph); } return(true); }
public bool Equals(Node <T> x, Node <T> y) { return(x.Equals(y)); }