示例#1
0
        public static bool ExistsPathOPT2(Graph<int> graph, int start, int destination)
        {
            var nodes = graph.Nodes;
               foreach (var node in nodes)
               {
               node.NodeState = State.Unvisted;
               }

               var discovered = new Stack<Node<int>>();
               var currentNode = graph.FindNode(start);

               currentNode.NodeState = State.Visited;
               discovered.Push(currentNode);

               while (discovered.Count > 0)
               {
               currentNode = discovered.Pop();
               foreach (var node in currentNode.Neighbors)
               {
                   if (node.NodeState.Equals(State.Unvisted))
                   {
                       node.NodeState = State.Visited;
                       if (node.Value.Equals(destination)) return true;
                       discovered.Push(node);
                   }
               }
               }
               return false;
        }