/// <summary>
        /// Returns the sequence of predecessor edges to the specified node.
        /// </summary>
        /// <typeparam name="TNodeValue">The type of the labels associated with graph nodes.</typeparam>
        /// <typeparam name="TEdgeLabel">The type of the labels associated with graph edges.</typeparam>
        /// <param name="source">The source directed graph to search for predecessors.</param>
        /// <param name="node">The node for which to obtain the sequence of predecessors.</param>
        /// <returns>
        /// A sequence of triples containing the predecessor node, the edge linking the predecessor
        /// to the specified node and the edge index.
        /// </returns>
        public static IEnumerable <Tuple <Node <TNodeValue, TEdgeLabel>, Edge <TNodeValue, TEdgeLabel>, int> > PredecessorEdges <TNodeValue, TEdgeLabel>(
            this DirectedGraph <TNodeValue, TEdgeLabel> source,
            Node <TNodeValue, TEdgeLabel> node)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            if (!source.Contains(node))
            {
                throw new ArgumentException("The specified node does not belong to the graph.", "node");
            }

            foreach (var predecessor in source)
            {
                int edgeIndex = 0;
                foreach (var successor in predecessor.Successors)
                {
                    if (successor.Target == node)
                    {
                        yield return(Tuple.Create(predecessor, successor, edgeIndex));

                        break;
                    }

                    edgeIndex++;
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Returns the sequence of predecessors to the specified node.
        /// </summary>
        /// <typeparam name="TNodeValue">The type of the labels associated with graph nodes.</typeparam>
        /// <typeparam name="TEdgeLabel">The type of the labels associated with graph edges.</typeparam>
        /// <param name="source">The source directed graph to search for predecessors.</param>
        /// <param name="node">The node for which to obtain the sequence of predecessors.</param>
        /// <returns>
        /// A sequence of <see cref="T:Bonsai.Dag.Node`2{T,U}"/> that contains all the predecessors
        /// to the specified node.
        /// </returns>
        public static IEnumerable <Node <TNodeValue, TEdgeLabel> > Predecessors <TNodeValue, TEdgeLabel>(this DirectedGraph <TNodeValue, TEdgeLabel> source, Node <TNodeValue, TEdgeLabel> node)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (!source.Contains(node))
            {
                throw new ArgumentException("The specified node does not belong to the graph.", nameof(node));
            }

            foreach (var predecessor in source)
            {
                foreach (var successor in predecessor.Successors)
                {
                    if (successor.Target == node)
                    {
                        yield return(predecessor);

                        break;
                    }
                }
            }
        }
        /// <summary>
        /// Returns the sequence of successors to the specified node.
        /// </summary>
        /// <typeparam name="TNodeValue">The type of the labels associated with graph nodes.</typeparam>
        /// <typeparam name="TEdgeLabel">The type of the labels associated with graph edges.</typeparam>
        /// <param name="source">The source directed graph to search for successors.</param>
        /// <param name="node">The node for which to obtain the sequence of successors.</param>
        /// <returns>
        /// A sequence of <see cref="T:Bonsai.Dag.Node`2{T,U}"/> that contains all the successors
        /// to the specified node.
        /// </returns>
        public static IEnumerable <Node <TNodeValue, TEdgeLabel> > Successors <TNodeValue, TEdgeLabel>(
            this DirectedGraph <TNodeValue, TEdgeLabel> source,
            Node <TNodeValue, TEdgeLabel> node)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            if (!source.Contains(node))
            {
                throw new ArgumentException("The specified node does not belong to the graph.", "node");
            }

            foreach (var successor in node.Successors)
            {
                yield return(successor.Target);
            }
        }