private void checkStrong <TVertex, TEdge>(StronglyConnectedComponentsAlgorithm <TVertex, TEdge> strong)
            where TEdge : IEdge <TVertex>
        {
            Assert.Equal(strong.VisitedGraph.VertexCount, strong.Components.Count);
            Assert.Equal(strong.VisitedGraph.VertexCount, strong.DiscoverTimes.Count);
            Assert.Equal(strong.VisitedGraph.VertexCount, strong.Roots.Count);

            foreach (var v in strong.VisitedGraph.Vertices)
            {
                Assert.True(strong.Components.ContainsKey(v));
                Assert.True(strong.DiscoverTimes.ContainsKey(v));
            }

            foreach (var de in strong.Components)
            {
                //Assert.NotNull(de.Key);
                Assert.True(de.Value <= strong.ComponentCount);
            }

            /*
             * foreach (var de in strong.DiscoverTimes)
             * {
             *  Assert.NotNull(de.Key);
             * }
             */
        }
        public void Compute <TVertex, TEdge>([PexAssumeNotNull] AdjacencyGraph <TVertex, TEdge> g)
            where TEdge : IEdge <TVertex>
        {
            var strong = new StronglyConnectedComponentsAlgorithm <TVertex, TEdge>(g);

            strong.Compute();
            checkStrong(strong);
        }
        private void Compute <TVertex, TEdge>(AdjacencyGraph <TVertex, TEdge> g)
            where TEdge : IEdge <TVertex>
        {
            var strong = new StronglyConnectedComponentsAlgorithm <TVertex, TEdge>(g);

            strong.Compute();
            checkStrong(strong);
        }
Exemple #4
0
        public void Simple()
        {
            IVertexListGraph <string, Edge <string> > g = new AdjacencyGraphFactory().Simple();
            StronglyConnectedComponentsAlgorithm <string, Edge <String> > strong = new StronglyConnectedComponentsAlgorithm <string, Edge <String> >(g);

            strong.Compute();
            checkStrong(strong);
        }
        public void EmptyGraph()
        {
            var g      = new AdjacencyGraph <string, Edge <string> >(true);
            var strong = new StronglyConnectedComponentsAlgorithm <string, Edge <String> >(g);

            strong.Compute();
            Assert.AreEqual(0, strong.ComponentCount);
            checkStrong(strong);
        }
Exemple #6
0
        public void Loop()
        {
            IVertexListGraph <string, Edge <string> > g = new AdjacencyGraphFactory().Loop();
            StronglyConnectedComponentsAlgorithm <string, Edge <String> > strong = new StronglyConnectedComponentsAlgorithm <string, Edge <String> >(g);

            strong.Compute();
            Assert.AreEqual(1, strong.ComponentCount);

            checkStrong(strong);
        }
        public void OneVertex()
        {
            AdjacencyGraph <string, Edge <string> > g = new AdjacencyGraph <string, Edge <string> >(true);

            g.AddVertex("test");
            var strong = new StronglyConnectedComponentsAlgorithm <string, Edge <String> >(g);

            strong.Compute();
            Assert.AreEqual(1, strong.ComponentCount);

            checkStrong(strong);
        }
        public static int StronglyConnectedComponents <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 StronglyConnectedComponentsAlgorithm <TVertex, TEdge>(g, components);

            conn.Compute();
            return(conn.ComponentCount);
        }
        /// <summary>
        /// Computes the strongly 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 StronglyConnectedComponents <TVertex, TEdge>
            (this IVertexListGraph <TVertex, TEdge> g, out IDictionary <TVertex, int> components)
            where TEdge : IEdge <TVertex>
        {
            Contract.Requires(g != null);
            Contract.Ensures(Contract.ValueAtReturn(out components) != null);

            components = new Dictionary <TVertex, int>();
            var conn = new StronglyConnectedComponentsAlgorithm <TVertex, TEdge>(g, components);

            conn.Compute();
            return(conn.ComponentCount);
        }
        public void TwoVertexOnEdge()
        {
            AdjacencyGraph <string, Edge <string> > g = new AdjacencyGraph <string, Edge <string> >(true);

            g.AddVertex("v1");
            g.AddVertex("v2");
            g.AddEdge(new Edge <string>("v1", "v2"));
            var strong = new StronglyConnectedComponentsAlgorithm <string, Edge <String> >(g);

            strong.Compute();
            Assert.AreEqual(2, strong.ComponentCount);

            checkStrong(strong);
        }
Exemple #11
0
        private void checkStrong(StronglyConnectedComponentsAlgorithm <string, Edge <String> > strong)
        {
            Assert.AreEqual(strong.VisitedGraph.VertexCount, strong.Components.Count);
            Assert.AreEqual(strong.VisitedGraph.VertexCount, strong.DiscoverTimes.Count);
            Assert.AreEqual(strong.VisitedGraph.VertexCount, strong.Roots.Count);

            foreach (string v in strong.VisitedGraph.Vertices)
            {
                Assert.IsTrue(strong.Components.ContainsKey(v));
                Assert.IsTrue(strong.DiscoverTimes.ContainsKey(v));
            }

            foreach (KeyValuePair <string, int> de in strong.Components)
            {
                Assert.IsNotNull(de.Key);
                Assert.LowerEqualThan(de.Value, strong.ComponentCount);
            }

            foreach (KeyValuePair <string, int> de in strong.DiscoverTimes)
            {
                Assert.IsNotNull(de.Key);
            }
        }