public void Constructor()
        {
            var graph      = new AdjacencyGraph <int, Edge <int> >();
            var components = new Dictionary <int, int>();
            var algorithm  = new WeaklyConnectedComponentsAlgorithm <int, Edge <int> >(graph);

            AssertAlgorithmProperties(algorithm, graph);

            algorithm = new WeaklyConnectedComponentsAlgorithm <int, Edge <int> >(graph, components);
            AssertAlgorithmProperties(algorithm, graph);

            algorithm = new WeaklyConnectedComponentsAlgorithm <int, Edge <int> >(null, graph, components);
            AssertAlgorithmProperties(algorithm, graph);

            #region Local function

            void AssertAlgorithmProperties <TVertex, TEdge>(
                WeaklyConnectedComponentsAlgorithm <TVertex, TEdge> algo,
                IVertexListGraph <TVertex, TEdge> g)
                where TEdge : IEdge <TVertex>
            {
                AssertAlgorithmState(algo, g);
                Assert.AreEqual(0, algo.ComponentCount);
                CollectionAssert.IsEmpty(algo.Components);
                CollectionAssert.IsEmpty(algo.Graphs);
            }

            #endregion
        }
        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 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]);
                }
            }
        }
Beispiel #4
0
    public void testing4()
    {
        BidirectionalGraph <string, Edge <string> > graph = new BidirectionalGraph <string, Edge <string> >(true);

        graph.AddVertex("A");
        graph.AddVertex("B");
        graph.AddVertex("C");
        graph.AddVertex("D");
        graph.AddVertex("E");
        graph.AddVertex("F");
        graph.AddVertex("G");
        graph.AddVertex("H");
        graph.AddVertex("I");
        graph.AddVertex("J");

        graph.AddEdge(new Edge <string>("A", "B"));
        graph.AddEdge(new Edge <string>("B", "C"));
        graph.AddEdge(new Edge <string>("C", "D"));
        graph.AddEdge(new Edge <string>("C", "E"));
        graph.AddEdge(new Edge <string>("E", "F"));
        graph.AddEdge(new Edge <string>("G", "H"));
        graph.AddEdge(new Edge <string>("H", "I"));
        graph.AddEdge(new Edge <string>("H", "J"));

        WeaklyConnectedComponentsAlgorithm <string, Edge <string> > ConnectedComponent = new WeaklyConnectedComponentsAlgorithm <string, Edge <string> >(graph);

        ConnectedComponent.Compute();
        // Debug.Log(ConnectedComponent.ComponentCount);
        //
        // Debug.Log(ConnectedComponent.Components);
        // foreach (string key in ConnectedComponent.Components.Keys) {
        //  Debug.Log(key);
        //  Debug.Log(ConnectedComponent.Components[key]);
        // }

        List <string> vertexfound      = new List <string>();
        List <string> startvertexfound = new List <string>();

        // ReversedBidirectionalGraph<string, Edge<string>> Rgraph = new ReversedBidirectionalGraph<string, Edge<string>>(graph);

        BidirectionalDepthFirstSearchAlgorithm <string, Edge <string> > DFS = new  BidirectionalDepthFirstSearchAlgorithm <string, Edge <string> >(graph);

        DFS.SetRootVertex("C");
        // DFS.TreeEdge += vertexfound.Add;
        DFS.TreeEdge += e => isRoot(graph, e);
        // DFS.FinishVertex += startvertexfound.Add;
        DFS.Compute();
        //
        Debug.Log("Found:");
        foreach (string V in Roots)
        {
            Debug.Log(V);
        }
        //
        // Debug.Log("START VERTEX Found:");
        // foreach(string V in vertexfound) {
        //  Debug.Log(V);
        // }
        // Debug.Log(DFS.rootVertex);
    }
        public void OneComponent()
        {
            var graph = new AdjacencyGraph <int, Edge <int> >();

            graph.AddVerticesAndEdgeRange(new[]
            {
                new Edge <int>(1, 2),
                new Edge <int>(1, 3),
                new Edge <int>(2, 3),
                new Edge <int>(4, 2),
                new Edge <int>(4, 3)
            });

            var algorithm = new WeaklyConnectedComponentsAlgorithm <int, Edge <int> >(graph);

            algorithm.Compute();

            Assert.AreEqual(1, algorithm.ComponentCount);
            CollectionAssert.AreEquivalent(
                new Dictionary <int, int>
            {
                [1] = 0,
                [2] = 0,
                [3] = 0,
                [4] = 0
            },
                algorithm.Components);
            Assert.AreEqual(1, algorithm.Graphs.Length);
            CollectionAssert.AreEquivalent(
                new[] { 1, 2, 3, 4 },
                algorithm.Graphs[0].Vertices);
        }
        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]);
                }
        }
Beispiel #7
0
    public IEnumerator CoComputeComponents()
    {
        WeaklyConnectedComponentsAlgorithm <string, Edge <string> > ConnectedComponent = new WeaklyConnectedComponentsAlgorithm <string, Edge <string> >(graph);

        ConnectedComponent.Compute();
        Node2Components = ConnectedComponent.Components;
        ComponentsCount = ConnectedComponent.ComponentCount;
        Components2Node = new List <List <string> >();
        for (int i = 0; i < ComponentsCount; i++)
        {
            Components2Node.Add(new List <string>());
            yield return(null);
        }
        foreach (string key in Node2Components.Keys)
        {
            if (Node2Components[key] >= 0 && Node2Components[key] < Components2Node.Count)
            {
                Components2Node[Node2Components[key]].Add(key);
            }
            else if (Node2Components[key] >= Components2Node.Count)
            {
                Debug.Log("index is more than the Count !");
            }
            else
            {
                Debug.Log("index is less than 0 !");
            }
            yield return(null);
        }
    }
        public static IDictionary <int, int> Get(AdjacencyGraph <int, Edge <int> > g)
        {
            var algorithm = new WeaklyConnectedComponentsAlgorithm <int, Edge <int> >(g);

            algorithm.Compute();
            return(algorithm.Components);
        }
        public static BidirectionalGraph <string, TaggedEdge <string, string> >[] GetGraphs(AdjacencyGraph <string, TaggedEdge <string, string> > g)
        {
            var algorithm = new WeaklyConnectedComponentsAlgorithm <string, TaggedEdge <string, string> >(g);

            algorithm.Compute();
            return(algorithm.Graphs);
        }
        public static IDictionary <string, int> Get(AdjacencyGraph <string, TaggedEdge <string, string> > g)
        {
            var algorithm = new WeaklyConnectedComponentsAlgorithm <string, TaggedEdge <string, string> >(g);

            algorithm.Compute();
            return(algorithm.Components);
        }
Beispiel #11
0
        public static double MeasureConnectedComponentsQuickGraphSpeed(string path)
        {
            AdjacencyGraph <int, Edge <int> > graph = Util.ReadMatrixMarketFileToQuickGraph(path);
            var algo = new WeaklyConnectedComponentsAlgorithm <int, Edge <int> >(graph, new Dictionary <int, int>());

            return(MeasureQuickGraphAlgorithmSpeed(algo));
        }
        public void MultipleComponents()
        {
            var graph = new AdjacencyGraph <int, Edge <int> >();

            graph.AddVerticesAndEdgeRange(new[]
            {
                new Edge <int>(1, 2),
                new Edge <int>(1, 3),
                new Edge <int>(2, 3),
                new Edge <int>(4, 2),
                new Edge <int>(4, 3),

                new Edge <int>(5, 6),
                new Edge <int>(5, 7),
                new Edge <int>(7, 6),

                new Edge <int>(8, 9)
            });
            graph.AddVertex(10);

            var algorithm = new WeaklyConnectedComponentsAlgorithm <int, Edge <int> >(graph);

            algorithm.Compute();

            Assert.AreEqual(4, algorithm.ComponentCount);
            CollectionAssert.AreEquivalent(
                new Dictionary <int, int>
            {
                [1]  = 0,
                [2]  = 0,
                [3]  = 0,
                [4]  = 0,
                [5]  = 1,
                [6]  = 1,
                [7]  = 1,
                [8]  = 2,
                [9]  = 2,
                [10] = 3
            },
                algorithm.Components);
            Assert.AreEqual(4, algorithm.Graphs.Length);
            CollectionAssert.AreEquivalent(
                new[] { 1, 2, 3, 4 },
                algorithm.Graphs[0].Vertices);
            CollectionAssert.AreEquivalent(
                new[] { 5, 6, 7 },
                algorithm.Graphs[1].Vertices);
            CollectionAssert.AreEquivalent(
                new[] { 8, 9 },
                algorithm.Graphs[2].Vertices);
            CollectionAssert.AreEquivalent(
                new[] { 10 },
                algorithm.Graphs[3].Vertices);
        }
        private int ComputeComponentCount(Dictionary <TVertex, int> components)
        {
            IConnectedComponentAlgorithm <TVertex, TEdge, IVertexListGraph <TVertex, TEdge> > componentAlgorithm;

            if (this.StronglyConnected)
            {
                componentAlgorithm = new StronglyConnectedComponentsAlgorithm <TVertex, TEdge>(
                    this,
                    this.VisitedGraph,
                    components);
            }
            else
            {
                componentAlgorithm = new WeaklyConnectedComponentsAlgorithm <TVertex, TEdge>(
                    this,
                    this.VisitedGraph,
                    components);
            }
            componentAlgorithm.Compute();
            return(componentAlgorithm.ComponentCount);
        }