예제 #1
0
        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));
        }
예제 #2
0
        public bool FordFulkersonBFSTypeAlgorithm(Node Start, Node Target)
        {
            if (Start.Equals(Target))
            {
                CapacityOfFlow = 0.0;
                return(true);
            }
            Node CurrentNode = new Node();

            for (; BreadthFirstSearch.BreadthFirstSearch(Start, Target, Path);)
            {
                double CurrentStreamCapacity = double.PositiveInfinity;
                for (int Index = Target.Index; Index != Start.Index; Index = Path[Index].Index)
                {
                    CurrentNode = Path[Index];
                    if (CurrentStreamCapacity < InnerGraph.FindEdge(CurrentNode, InnerGraph.SetOfNodes[Index]).Weight)
                    {
                        CurrentStreamCapacity = InnerGraph.FindEdge(CurrentNode, InnerGraph.SetOfNodes[Index]).Weight;
                    }
                }
                for (int Index = Target.Index; Index != Start.Index; Index = Path[Index].Index)
                {
                    InnerGraph.FindEdge(CurrentNode, InnerGraph.SetOfNodes[Index]).Weight -= CurrentStreamCapacity;
                    InnerGraph.FindEdge(InnerGraph.SetOfNodes[Index], CurrentNode).Weight += CurrentStreamCapacity;
                }
                CapacityOfFlow    += CurrentStreamCapacity;
                BreadthFirstSearch = new BreadthFirstSearchAlgorithm(InnerGraph);
            }
            return(true);
        }
예제 #3
0
        public bool FindTheShortestPathes(Node Start)
        {
            ShortestPathes[Start.Index].Weight = 0;

            for (int FirstIndex = 0; FirstIndex < InnerGraph.NumberOfNodes - 1; FirstIndex++)
            {
                Node CurrentNode = new Node {
                    Weight = Infinity
                };

                for (int SecondIndex = 0; SecondIndex < InnerGraph.NumberOfNodes; SecondIndex++)
                {
                    if (!VisitedNodes.Contains(ShortestPathes[SecondIndex]) && ShortestPathes[SecondIndex].Weight <= CurrentNode.Weight)
                    {
                        CurrentNode = ShortestPathes[SecondIndex];

                        Index = ShortestPathes[SecondIndex].Index;
                    }
                }

                VisitedNodes.Add(ShortestPathes[Index]);

                for (int SecondIndex = 0; SecondIndex < InnerGraph.NumberOfNodes; SecondIndex++)
                {
                    if (!VisitedNodes.Contains(ShortestPathes[SecondIndex]) && InnerGraph.SetOfNodes[Index][SecondIndex] != null && !ShortestPathes[Index].Weight.Equals(Infinity) && ShortestPathes[SecondIndex].Weight > ShortestPathes[Index].Weight + InnerGraph.FindEdge(InnerGraph.SetOfNodes[Index], InnerGraph.SetOfNodes[SecondIndex]).Weight)
                    {
                        ShortestPathes[SecondIndex].Weight = ShortestPathes[Index].Weight + InnerGraph.FindEdge(InnerGraph.SetOfNodes[Index], InnerGraph.SetOfNodes[SecondIndex]).Weight;
                    }
                }
            }

            return(InnerGraph.NegativeCycleChecker(ShortestPathes));
        }
예제 #4
0
        public bool BreadthFirstSearch(Node Start)
        {
            Queue.Enqueue(Start);

            VisitedNodes.Add(Start);

            for (; Queue.Count > 0;)
            {
                Start = Queue.Dequeue();

                for (int Index = 0; Index < InnerGraph.NumberOfNodes; Index++)
                {
                    if (InnerGraph.SetOfNodes[Start.Index][Index] != null && !VisitedNodes.Contains(InnerGraph.SetOfNodes[Index]))
                    {
                        ResultOfSearching.Add(InnerGraph.FindEdge(Start, InnerGraph.SetOfNodes[Index]));

                        Queue.Enqueue(InnerGraph.SetOfNodes[Index]);

                        VisitedNodes.Add(InnerGraph.SetOfNodes[Index]);
                    }
                }
            }

            return(VisitedNodes.Count.Equals(InnerGraph.NumberOfNodes));
        }
예제 #5
0
        public bool A_StarAlgorithm(Node Start, Node Target)
        {
            Node CurrentNode = Start;

            ActualNodes.Add(CurrentNode);

            for (; ActualNodes.Count > 0;)
            {
                CurrentNode = ActualNodes[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);
                }

                ActualNodes.Remove(CurrentNode);

                ClosedNodes.Add(CurrentNode);

                foreach (Node Incomer in CurrentNode.Incomers.Where(Node => Node != null && Node.Index != Node.Incomers.Length - 1))
                {
                    if (!ClosedNodes.Contains(Incomer))
                    {
                        if (!ActualNodes.Contains(Incomer))
                        {
                            Incomer[Incomer.Index] = CurrentNode;

                            Incomer.HeuristicCost = HeruisticPath(Incomer, Target);

                            Incomer.PastWayCost = InnerGraph.FindEdge(CurrentNode, Incomer).Weight;

                            Incomer.TotalPathCost = Incomer.PastWayCost + Incomer.HeuristicCost;

                            ActualNodes.Add(Incomer);

                            ActualNodes = ActualNodes.OrderBy(Node => Node.TotalPathCost).ToList <Node>();
                        }
                    }
                }

                VisitedNodes.Add(CurrentNode);
            }

            return(true);
        }
예제 #6
0
        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);
        }
예제 #7
0
 /// <summary>
 ///  that type of DFS that start from one Node ( that give as param) and researche all graph </summary>
 /// <param name="Start">< Node that our research start>
 /// <returns> true if we find all Nodes</returns>
 public bool DepthFirstSearch(Node Start)
 {
     VisitedNodes.Add(Start);
     for (int Index = 0; Index < InnerGraph.QuantityOfNodes; Index++)
     {
         if (InnerGraph.SetOfNodes[Start.Index][Index] != null &&
             !VisitedNodes.Contains(InnerGraph.SetOfNodes[Index]))
         {
             ResultOfSearching.Add(InnerGraph.FindEdge(Start, InnerGraph.SetOfNodes[Index]));
             DepthFirstSearch(InnerGraph.SetOfNodes[Index]);
         }
     }
     return(VisitedNodes.Count.Equals(InnerGraph.QuantityOfNodes));
 }
예제 #8
0
        public bool FindTheShortestPathes(Node Start)
        {
            ShortestPathes[Start.Index].Weight = 0;

            for (int FirstIndex = 1; FirstIndex < InnerGraph.NumberOfNodes - 1; FirstIndex++)
            {
                for (int SecondIndex = 0; SecondIndex < InnerGraph.NumberOfEdges; SecondIndex++)
                {
                    ShortestPathes[InnerGraph.SetOfEdges[SecondIndex][1].Index].Weight = Min(ShortestPathes[InnerGraph.SetOfEdges[SecondIndex][1].Index].Weight, ShortestPathes[InnerGraph.SetOfEdges[SecondIndex][0].Index].Weight + InnerGraph.SetOfEdges[SecondIndex].Weight);
                }
            }

            return(InnerGraph.NegativeCycleChecker(ShortestPathes));
        }
예제 #9
0
        public Floyd_WarshallAlgorithm(Graph Graph)
        {
            InnerGraph = (Graph)Graph.Clone();

            MatrixOfTheShortesPathes = new double[Graph.QuantityOfNodes, Graph.QuantityOfNodes];
            // Initialise MatrixOfTheShortestPathes
            for (int FirstIndex = 0; FirstIndex < InnerGraph.QuantityOfNodes; FirstIndex++)
            {
                for (int SecondIndex = 0; SecondIndex < InnerGraph.QuantityOfNodes; SecondIndex++)
                {
                    if (InnerGraph.SetOfNodes[FirstIndex][SecondIndex] != null)
                    {
                        MatrixOfTheShortesPathes[FirstIndex, SecondIndex] = InnerGraph.FindEdge(InnerGraph.SetOfNodes[FirstIndex], InnerGraph.SetOfNodes[SecondIndex]).Weight;
                    }
                    else
                    {
                        MatrixOfTheShortesPathes[FirstIndex, SecondIndex] = double.PositiveInfinity;
                    }
                }
            }
        }
예제 #10
0
        public FloydWarshallAlgorithm(Graph Graph)
        {
            MatrixOfTheShortesPathes = new double[Graph.NumberOfNodes, Graph.NumberOfNodes];

            InnerGraph = (Graph)Graph.Clone();

            for (int FirstIndex = 0; FirstIndex < InnerGraph.NumberOfNodes; FirstIndex++)
            {
                for (int SecondIndex = 0; SecondIndex < InnerGraph.NumberOfNodes; SecondIndex++)
                {
                    if (InnerGraph.SetOfNodes[FirstIndex][SecondIndex] != null)
                    {
                        MatrixOfTheShortesPathes[FirstIndex, SecondIndex] = InnerGraph.FindEdge(InnerGraph.SetOfNodes[FirstIndex], InnerGraph.SetOfNodes[SecondIndex]).Weight;
                    }
                    else
                    {
                        MatrixOfTheShortesPathes[FirstIndex, SecondIndex] = Infinity;
                    }
                }
            }
        }
예제 #11
0
 /// <summary>
 /// that type of DFS that start from one Node ( that give as param) and researche targeted node. </summary>
 /// <param name="Start"> first Node in queue</param>
 /// <param name="Target"> Scope of our researche</param>
 /// <param name="FoundPath"></param>
 /// <returns> True if we find target (target in list of visited nodes)</returns>
 public bool DepthFirstSearch(Node Start, Node Target, Node[] FoundPath)
 {
     VisitedNodes.Add(Start);
     if (VisitedNodes.Contains(Target))
     {
         return(true);
     }
     for (int Index = 0; Index < InnerGraph.QuantityOfNodes; Index++)
     {
         if (InnerGraph.SetOfNodes[Start.Index][Index] != null &&
             !VisitedNodes.Contains(InnerGraph.SetOfNodes[Index]) &&
             InnerGraph.FindEdge(Start, InnerGraph.SetOfNodes[Index]).Weight > 0)
         {
             FoundPath[Index] = Start;
             if (DepthFirstSearch(InnerGraph.SetOfNodes[Index], Target, FoundPath))
             {
                 return(true);
             }
         }
     }
     return(VisitedNodes.Contains(Target));
 }
예제 #12
0
        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);
        }
예제 #13
0
        public bool DepthFirstSearch(Node Start, Node Target)
        {
            VisitedNodes.Add(Start);

            if (VisitedNodes.Contains(Target))
            {
                return(true);
            }

            for (int Index = 0; Index < InnerGraph.NumberOfNodes; Index++)
            {
                if (InnerGraph.SetOfNodes[Start.Index][Index] != null && !VisitedNodes.Contains(InnerGraph.SetOfNodes[Index]))
                {
                    ResultOfSearching.Add(InnerGraph.FindEdge(Start, InnerGraph.SetOfNodes[Index]));

                    if (DepthFirstSearch(InnerGraph.SetOfNodes[Index], Target))
                    {
                        return(true);
                    }
                }
            }

            return(VisitedNodes.Contains(Target));
        }
예제 #14
0
        ///
        /// bfs var 2
        /// searching path to target node from some start node
        ///
        /// <param name="Start"> - first node
        /// <param name="Target"> - our scope
        /// <param name="Path"> - for our scope
        /// <returns></returns>
        public bool BreadthFirstSearch(Node Start, Node Target, Node[] Path)
        {
            Queue.Enqueue(Start);

            VisitedNodes.Add(Start);

            Path[Start.Index] = Start;

            if (VisitedNodes.Contains(Target))
            {
                return(true);
            }

            for (; Queue.Count > 0;)
            {
                Start = Queue.Dequeue();

                for (int Index = 0; Index < InnerGraph.QuantityOfNodes; Index++)
                {
                    if (InnerGraph.SetOfNodes[Start.Index][Index] != null && !VisitedNodes.Contains(InnerGraph.SetOfNodes[Index]) && InnerGraph.FindEdge(Start, InnerGraph.SetOfNodes[Index]).Weight > 0)
                    {
                        Path[Index] = Start;

                        Queue.Enqueue(InnerGraph.SetOfNodes[Index]);

                        VisitedNodes.Add(InnerGraph.SetOfNodes[Index]);
                    }
                }
            }

            return(VisitedNodes.Contains(Target));
        }