public void RunWeaklyConnectedComponentsAndCheck <TVertex, TEdge>([NotNull] IVertexListGraph <TVertex, TEdge> graph) where TEdge : IEdge <TVertex> { var algorithm = new WeaklyConnectedComponentsAlgorithm <TVertex, TEdge>(graph); algorithm.Compute(); Assert.AreEqual(graph.VertexCount, algorithm.Components.Count); if (graph.VertexCount == 0) { Assert.IsTrue(algorithm.ComponentCount == 0); return; } Assert.Positive(algorithm.ComponentCount); Assert.LessOrEqual(algorithm.ComponentCount, graph.VertexCount); foreach (KeyValuePair <TVertex, int> pair in algorithm.Components) { Assert.GreaterOrEqual(pair.Value, 0); Assert.IsTrue(pair.Value < algorithm.ComponentCount, $"{pair.Value} < {algorithm.ComponentCount}"); } foreach (TVertex vertex in graph.Vertices) { foreach (TEdge edge in graph.OutEdges(vertex)) { Assert.AreEqual(algorithm.Components[edge.Source], algorithm.Components[edge.Target]); } } }
public void Compute <TVertex, TEdge>([NotNull] IVertexListGraph <TVertex, TEdge> graph) where TEdge : IEdge <TVertex> { var dfs = new WeaklyConnectedComponentsAlgorithm <TVertex, TEdge>(graph); dfs.Compute(); if (graph.VertexCount == 0) { Assert.IsTrue(dfs.ComponentCount == 0); return; } Assert.IsTrue(0 < dfs.ComponentCount); Assert.IsTrue(dfs.ComponentCount <= graph.VertexCount); foreach (KeyValuePair <TVertex, int> pair in dfs.Components) { Assert.IsTrue(0 <= pair.Value); Assert.IsTrue(pair.Value < dfs.ComponentCount, $"{pair.Value} < {dfs.ComponentCount}"); } foreach (TVertex vertex in graph.Vertices) { foreach (TEdge edge in graph.OutEdges(vertex)) { Assert.AreEqual(dfs.Components[edge.Source], dfs.Components[edge.Target]); } } }
public void Compute <TVertex, TEdge>([PexAssumeNotNull] IVertexListGraph <TVertex, TEdge> g) where TEdge : IEdge <TVertex> { var dfs = new WeaklyConnectedComponentsAlgorithm <TVertex, TEdge>(g); dfs.Compute(); if (g.VertexCount == 0) { Assert.IsTrue(dfs.ComponentCount == 0); return; } Assert.IsTrue(0 < dfs.ComponentCount); Assert.IsTrue(dfs.ComponentCount <= g.VertexCount); foreach (var kv in dfs.Components) { Assert.IsTrue(0 <= kv.Value); Assert.IsTrue(kv.Value < dfs.ComponentCount, "{0} < {1}", kv.Value, dfs.ComponentCount); } foreach (var vertex in g.Vertices) { foreach (var edge in g.OutEdges(vertex)) { Assert.AreEqual(dfs.Components[edge.Source], dfs.Components[edge.Target]); } } }
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]); } } }
public static void DisplayGraph <TVertex, TEdge>([NotNull] IVertexListGraph <TVertex, TEdge> graph) where TEdge : IEdge <TVertex> { Console.WriteLine($"{graph.VertexCount} vertices"); foreach (TVertex vertex in graph.Vertices) { foreach (TEdge edge in graph.OutEdges(vertex)) { Console.WriteLine($"\t{edge}"); } } }
public static void DisplayGraph <TVertex, TEdge>(IVertexListGraph <TVertex, TEdge> g) where TEdge : IEdge <TVertex> { TestConsole.WriteLine("{0} vertices", g.VertexCount); foreach (var v in g.Vertices) { foreach (var edge in g.OutEdges(v)) { TestConsole.WriteLine("\t{0}", edge); } } }
public static void DisplayGraph <Vertex, Edge>(IVertexListGraph <Vertex, Edge> g) where Edge : IEdge <Vertex> { Console.WriteLine("{0} vertices", g.VertexCount); foreach (Vertex v in g.Vertices) { foreach (Edge edge in g.OutEdges(v)) { Console.WriteLine("\t{0}", edge); } } }
public static Dictionary <TEdge, double> UnaryWeightsFromVertexList( IVertexListGraph <TVertex, TEdge> graph) { if (graph == null) { throw new ArgumentNullException("graph"); } var weights = new Dictionary <TEdge, double>(graph.VertexCount * 2); foreach (var v in graph.Vertices) { foreach (var e in graph.OutEdges(v)) { weights.Add(e, 1); } } return(weights); }
/// <summary> /// Create a edge unary weight dictionary. /// </summary> /// <param name="graph">graph to map</param> /// <returns>Dictionary where each edge wheight is 1</returns> public static EdgeDoubleDictionary UnaryWeightsFromVertexList(IVertexListGraph graph) { if (graph == null) { throw new ArgumentNullException("graph"); } EdgeDoubleDictionary weights = new EdgeDoubleDictionary(); foreach (IVertex v in graph.Vertices) { foreach (IEdge e in graph.OutEdges(v)) { weights[e] = 1; } } return(weights); }
/// <summary> /// Create a edge unary weight dictionary. /// </summary> /// <param name="graph">graph to map</param> /// <returns>Dictionary where each edge wheight is 1</returns> public static EdgeDoubleDictionary UnaryWeightsFromVertexList(IVertexListGraph graph) { if (graph==null) throw new ArgumentNullException("graph"); EdgeDoubleDictionary weights=new EdgeDoubleDictionary(); foreach(IVertex v in graph.Vertices) { foreach(IEdge e in graph.OutEdges(v)) { weights[e]=1; } } return weights; }
public static EdgeDoubleDictionary UnaryWeightsFromVertexList(IVertexListGraph graph) { if (graph == null) { throw new ArgumentNullException("graph"); } EdgeDoubleDictionary dictionary = new EdgeDoubleDictionary(); IVertexEnumerator enumerator = graph.get_Vertices().GetEnumerator(); while (enumerator.MoveNext()) { IVertex vertex = enumerator.get_Current(); IEdgeEnumerator enumerator2 = graph.OutEdges(vertex).GetEnumerator(); while (enumerator2.MoveNext()) { IEdge edge = enumerator2.get_Current(); dictionary.set_Item(edge, 1.0); } } return dictionary; }