Пример #1
0
        public void Compute <TVertex, TEdge>([PexAssumeNotNull] IUndirectedGraph <TVertex, TEdge> g)
            where TEdge : IEdge <TVertex>
        {
            var dfs = new ConnectedComponentsAlgorithm <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.AdjacentEdges(vertex))
                {
                    Assert.AreEqual(dfs.Components[edge.Source], dfs.Components[edge.Target]);
                }
            }
        }
Пример #2
0
        private void Compute <TVertex, TEdge>(IUndirectedGraph <TVertex, TEdge> g)
            where TEdge : IEdge <TVertex>
        {
            var dfs = new ConnectedComponentsAlgorithm <TVertex, TEdge>(g);

            dfs.Compute();
            if (g.VertexCount == 0)
            {
                Assert.Equal(0, dfs.ComponentCount);
                return;
            }

            Assert.True(0 < dfs.ComponentCount);
            Assert.True(dfs.ComponentCount <= g.VertexCount);
            foreach (var kv in dfs.Components)
            {
                Assert.True(0 <= kv.Value);
                Assert.True(kv.Value < dfs.ComponentCount);
            }

            foreach (var vertex in g.Vertices)
            {
                foreach (var edge in g.AdjacentEdges(vertex))
                {
                    Assert.Equal(dfs.Components[edge.Source], dfs.Components[edge.Target]);
                }
            }
        }