Ejemplo n.º 1
0
        public static List <UnweightedNode <T> > UnweightedDepthFirstSearch <T>(UnweightedNode <T> start)
        {
            List <UnweightedNode <T> >    nodes   = new List <UnweightedNode <T> >();
            HashSet <UnweightedNode <T> > visited = new HashSet <UnweightedNode <T> >();

            DepthFirstSearch.TraverseUnweightedDepthFirstSearch(start, nodes, visited);

            return(nodes);
        }
Ejemplo n.º 2
0
        private static void TraverseUnweightedDepthFirstSearch <T>(UnweightedNode <T> start, List <UnweightedNode <T> > nodes, HashSet <UnweightedNode <T> > visited)
        {
            nodes.Add(start);
            visited.Add(start);

            foreach (UnweightedNode <T> neighbor in start.GetNeighborsInOrder())
            {
                if (!visited.Contains(neighbor))
                {
                    DepthFirstSearch.TraverseUnweightedDepthFirstSearch(neighbor, nodes, visited);
                }
            }
        }
Ejemplo n.º 3
0
        public static void TestUnweighted()
        {
            string[] order = new string[] { "root", "apple", "banana", "red", "yellow" };
            Dictionary <string, UnweightedNode <string> > nodes = new Dictionary <string, UnweightedNode <string> >();

            foreach (string key in order)
            {
                UnweightedNode <string> node = new UnweightedNode <string>(key);
                nodes[key] = node;
            }

            Dictionary <string, string[]> adjacencies = new Dictionary <string, string[]>
            {
                { "root", new string[] { "apple", "banana" } },
                { "apple", new string[] { "red", "yellow" } },
                { "banana", new string[] { "yellow" } },
                { "red", new string[0] },
                { "yellow", new string[0] }
            };

            foreach (string key in order)
            {
                UnweightedNode <string> node = nodes[key];
                string[] neighborKeys        = adjacencies[key];

                foreach (string neighborKey in neighborKeys)
                {
                    node.AddNeighbor(nodes[neighborKey]);
                    Console.WriteLine(string.Format("{0} borders {1}", key, neighborKey));
                }
            }

            foreach (UnweightedNode <string> node in DepthFirstSearch.UnweightedDepthFirstSearch(nodes["root"]))
            {
                Console.WriteLine(node.Data);
            }
        }