Shields.Graphs/Graph.cs
/// <summary>
/// Gets the connected components of a graph. For digraphs use <see cref="StronglyConnectedComponents"/>.
/// </summary>
/// <typeparam name="TNode">The type of a node.</typeparam>
/// <typeparam name="TKey">The type of a node key.</typeparam>
/// <param name="nodes">The set of nodes.</param>
/// <param name="key">The function which maps a node to its key.</param>
/// <param name="next">The function which maps a node to its adjacent nodes.</param>
/// <returns>The connected components of the graph.</returns>
Gets the connected components of a graph. For digraphs use .
Gets the connected components of a graph . For digraphs use .
public static IEnumerable<IEnumerable<TNode>> ConnectedComponents<TNode, TKey>(
            this IEnumerable<TNode> nodes,
            Func<TNode, TKey> key,
            Func<TNode, IEnumerable<TNode>> next)
        {
            var visited = new HashSet<TKey>();
            foreach (var node in nodes)
            {
                if (visited.Contains(key(node)))
                {
                    continue;
                }
                yield return BreadthFirstTraversal(Return(node), key, next, visited).ToList();
            }
        }
public static IEnumerable < IEnumerable < TNode > > ConnectedComponents < TNode , TKey > ( this IEnumerable < TNode > nodes , Func < TNode , TKey > key , Func < TNode , IEnumerable < TNode > > next ) { var visited = new HashSet < TKey > ( ) ; foreach ( var node in nodes ) { if ( visited . Contains ( key ( node ) ) ) { continue ; } yield return BreadthFirstTraversal ( Return ( node ) , key , next , visited ) . ToList ( ) ; } }
Gets the connected components of a graph.
Gets the connected components of a graph .
The connected components of the graph.
The connected components of the graph .
IEnumerable<TNode>
this
key
Func<TNode, TKey>
The function which maps a node to its key.
The function which maps a node to its key .
next
Func<TNode, IEnumerable<TNode>>
The function which maps a node to its adjacent nodes.
The function which maps a node to its adjacent nodes .
nodes
The set of nodes.
The set of nodes .
typeparam
The type of a node.
The type of a node .
typeparam
The type of a node key.
The type of a node key .