public void Compute([PexAssumeNotNull] IVertexListGraph <string, Edge <string> > g) { GraphConsoleSerializer.DisplayGraph(g); WeaklyConnectedComponentsAlgorithm <string, Edge <string> > dfs = new WeaklyConnectedComponentsAlgorithm <string, Edge <string> >(g); dfs.Compute(); Console.WriteLine("Weak components: {0}", dfs.ComponentCount); Assert.AreEqual(g.VertexCount, dfs.Components.Count); foreach (KeyValuePair <string, int> kv in dfs.Components) { Console.WriteLine("\t{0}: {1}", kv.Key, kv.Value); } foreach (KeyValuePair <string, int> kv in dfs.Components) { Assert.IsLowerEqual(0, kv.Value); Assert.IsLower(kv.Value, dfs.ComponentCount); } foreach (string vertex in g.Vertices) { foreach (Edge <string> edge in g.OutEdges(vertex)) { Assert.AreEqual(dfs.Components[edge.Source], dfs.Components[edge.Target]); } } }
/// <summary> /// Computes the weakly connected components of a graph /// </summary> /// <typeparam name="TVertex">type of the vertices</typeparam> /// <typeparam name="TEdge">type of the edges</typeparam> /// <param name="g"></param> /// <param name="components"></param> /// <returns>number of components</returns> public static int WeaklyConnectedComponents <TVertex, TEdge> (this IVertexListGraph <TVertex, TEdge> g, IDictionary <TVertex, int> components) where TEdge : IEdge <TVertex> { Contract.Requires(g != null); Contract.Requires(components != null); var conn = new WeaklyConnectedComponentsAlgorithm <TVertex, TEdge>(g, components); conn.Compute(); return(conn.ComponentCount); }
public static int WeaklyConnectedComponents <TVertex, TEdge>( IVertexListGraph <TVertex, TEdge> g, IDictionary <TVertex, int> components) where TEdge : IEdge <TVertex> { GraphContracts.AssumeNotNull(g, "g"); GraphContracts.AssumeNotNull(components, "components"); var conn = new WeaklyConnectedComponentsAlgorithm <TVertex, TEdge>(g, components); conn.Compute(); return(conn.ComponentCount); }