Example #1
0
        public void BellmanFindMinDistancePath(Node start, Node end)
        {
            _cloud.Clear();
            _reachableNodes.Clear();
            Node currentNode = start;

            currentNode.Visited = true;
            start.TotalCost     = 0;
            _cloud.Add(currentNode);
            ReachableNode currentReachableNode;

            while (currentNode != end)
            {
                BellmanAddReachableNodes(currentNode);     // Komşu düğümleri kenarlarla bağlar
                BellmanUpdateReachableNodesTotalCost(currentNode);

                //if we cannot reach any other node, the graph is not connected
                if (_reachableNodes.ReachableNodes.Count == 0)
                {
                    _isGraphConnected = false;
                    break;
                }
                int kenarSayisi = _edges.Count;

                //get the closest reachable node
                currentReachableNode = _reachableNodes.ReachableNodes[0];
                //remove if from the reachable nodes list
                _reachableNodes.RemoveReachableNode(currentReachableNode);
                //mark the current node as visited
                currentNode.Visited = true;
                //set the current node to the closest one from the cloud
                currentNode = currentReachableNode.Node;
                //set a pointer to the edge from where we came from
                currentNode.EdgeCameFrom = currentReachableNode.Edge;
                //mark the edge as visited
                currentReachableNode.Edge.Visited = true;


                _cloud.Add(currentNode);
            }
        }
Example #2
0
        /// <summary>
        /// The implementation of the Djikstra algorithm
        /// </summary>
        /// <param name="start">Starting Node</param>
        /// <param name="end">Ending Node</param>
        private void FindMinDistancePath(Node start, Node end)
        {
            cloud.Clear();
            reachableNodes.Clear();
            Node currentNode = start;

            currentNode.Visited = true;
            start.TotalCost     = 0;
            cloud.Add(currentNode);
            ReachableNode currentReachableNode;

            while (currentNode != end)
            {
                AddReachableNodes(currentNode);
                UpdateReachableNodesTotalCost(currentNode);

                //if we cannot reach any other node, the graph is not connected
                if (reachableNodes.ReachableNodes.Count == 0)
                {
                    isGraphConnected = false;
                    break;
                }

                //get the closest reachable node
                currentReachableNode = reachableNodes.ReachableNodes[0];
                //remove if from the reachable nodes list
                reachableNodes.RemoveReachableNode(currentReachableNode);
                //mark the current node as visited
                currentNode.Visited = true;
                //set the current node to the closest one from the cloud
                currentNode = currentReachableNode.Node;
                //set a pointer to the edge from where we came from
                currentNode.EdgeCameFrom = currentReachableNode.Edge;
                //mark the edge as visited
                currentReachableNode.Edge.Visited = true;

                cloud.Add(currentNode);
            }
        }