Exemplo n.º 1
0
        public static string Traverse <T>(DirectedGraphNode <T> root)
        {
            string output = "";

            var visited = new List <DirectedGraphNode <T> >();
            var queue   = new Queue <DirectedGraphNode <T> >();

            queue.Enqueue(root);

            while (queue.Count != 0)
            {
                var curr = queue.Dequeue();

                foreach (var neighbor in curr.GetNeighbors())
                {
                    if (!(visited.Any(node => node.Equals(neighbor)) || queue.Any(node => node.Equals(neighbor))))
                    {
                        queue.Enqueue(neighbor);
                    }
                }

                visited.Add(curr);

                output += curr.Data.ToString() + " ";
            }

            return(output.Trim());
        }
Exemplo n.º 2
0
        public static string Traverse <T>(DirectedGraphNode <T> root)
        {
            string output = "";

            var visited = new List <DirectedGraphNode <T> >();
            var stack   = new Stack <DirectedGraphNode <T> >();

            stack.Push(root);

            while (stack.Count != 0)
            {
                var curr = stack.Pop();

                foreach (var neighbor in curr.GetNeighbors())
                {
                    if (!(visited.Any(node => node.Equals(neighbor)) || stack.Any(node => node.Equals(neighbor))))
                    {
                        stack.Push(neighbor);
                    }
                }

                visited.Add(curr);

                output += curr.Data.ToString() + " ";
            }

            return(output.Trim());
        }
Exemplo n.º 3
0
 public void AddNeighbor(DirectedGraphNode <T> neighbor)
 {
     neighbors.Add(neighbor);
 }