示例#1
0
        public List <string> BFS(string node)
        {
            DSQueue <string> dSQueue = new DSQueue <string>();

            Dictionary <string, bool> visited = new Dictionary <string, bool>();

            dSQueue.Enqueue(node);

            while (!dSQueue.IsEmpty())
            {
                string current = dSQueue.Dequeue().Data;

                if (!visited.ContainsKey(current))
                {
                    visited.Add(current, true);
                }

                foreach (var item in _node[current])
                {
                    if (!visited.ContainsKey(item))
                    {
                        dSQueue.Enqueue(item);
                    }
                }
            }

            return(visited.Select(s => s.Key).ToList());
        }
        public static bool FindRoute(this DSGraph graph, string nodeA, string nodeB)
        {
            DSQueue <string>          queue   = new DSQueue <string>();
            Dictionary <string, bool> visible = new Dictionary <string, bool>();

            queue.Enqueue(nodeA);

            while (!queue.IsEmpty())
            {
                var current = queue.Dequeue().Data;
                if (!visible.ContainsKey(current))
                {
                    visible.Add(current, true);

                    if (current == nodeB)
                    {
                        return(true);
                    }
                }

                foreach (var item in graph._node[current])
                {
                    if (!visible.ContainsKey(item))
                    {
                        queue.Enqueue(item);
                    }
                }
            }

            return(visible.ContainsKey(nodeB));
        }