Ejemplo n.º 1
0
        public static IEnumerable <(GraphNode node, int distance)> FindPaths(GraphNode startGraphNode)
        {
            var queue   = new System.Collections.Generic.Queue <GraphNode>();
            var visited = new HashSet <GraphNode>();
            var result  = new List <(GraphNode, int)>();

            queue.Enqueue(startGraphNode);
            var distance = 1;

            while (queue.Count != 0)
            {
                var nodes = queue.ToList();
                queue.Clear();
                foreach (var node in nodes)
                {
                    visited.Add(node);
                    foreach (var incidentNode in node.Edges.Where(x => !visited.Contains(x)))
                    {
                        queue.Enqueue(incidentNode);
                        result.Add((incidentNode, distance));
                    }
                }

                distance++;
            }

            return(result);
        }