public void ClearRootVertex()
        {
            var graph     = new BidirectionalGraph <int, Edge <int> >();
            var algorithm = new BidirectionalDepthFirstSearchAlgorithm <int, Edge <int> >(graph);

            ClearRootVertex_Test(algorithm);
        }
        public void EmptyGraph(IBidirectionalGraph<string, Edge<string>> g)
        {
            this.dfs = new BidirectionalDepthFirstSearchAlgorithm<string, Edge<string>>(g);
            this.dfs.Compute();

            VerifyDfs();
        }
        public void SetRootVertex_Throws()
        {
            var graph     = new BidirectionalGraph <TestVertex, Edge <TestVertex> >();
            var algorithm = new BidirectionalDepthFirstSearchAlgorithm <TestVertex, Edge <TestVertex> >(graph);

            SetRootVertex_Throws_Test(algorithm);
        }
Esempio n. 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 ComputeWithRoot()
        {
            var graph = new BidirectionalGraph <int, Edge <int> >();

            graph.AddVertex(0);
            var algorithm = new BidirectionalDepthFirstSearchAlgorithm <int, Edge <int> >(graph);

            ComputeWithRoot_Test(algorithm);
        }
Esempio n. 6
0
        private static void Compute <TVertex, TEdge>([NotNull] IBidirectionalGraph <TVertex, TEdge> graph)
            where TEdge : IEdge <TVertex>
        {
            var dfs = new BidirectionalDepthFirstSearchAlgorithm <TVertex, TEdge>(graph);

            dfs.Compute();

            foreach (TVertex vertex in graph.Vertices)
            {
                Assert.IsTrue(dfs.VerticesColors.ContainsKey(vertex));
                Assert.AreEqual(dfs.VerticesColors[vertex], GraphColor.Black);
            }
        }
Esempio n. 7
0
    public List <string> FindRoots(string root)
    {
        List <string> Roots = new List <string>();

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


        DFS.SetRootVertex(root);
        DFS.DiscoverVertex += e => isRoot(e);
        // DFS.TreeEdge += e => isRoot(e);
        DFS.Compute();

        return(Roots);
    }
        public static BidirectionalDepthFirstSearchAlgorithm <int, Edge <int> > CreateAlgorithmAndMaybeDoComputation(
            [NotNull] ContractScenario scenario)
        {
            var graph = new BidirectionalGraph <int, Edge <int> >();

            graph.AddVerticesAndEdgeRange(scenario.EdgesInGraph.Select(e => new Edge <int>(e.Source, e.Target)));
            graph.AddVertexRange(scenario.SingleVerticesInGraph);

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

            if (scenario.DoComputation)
            {
                algorithm.Compute(scenario.Root);
            }
            return(algorithm);
        }
        public void GetVertexColor()
        {
            var graph = new BidirectionalGraph <int, Edge <int> >();

            graph.AddVerticesAndEdge(new Edge <int>(1, 2));

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

            // Algorithm not run
            // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
            Assert.Throws <VertexNotFoundException>(() => algorithm.GetVertexColor(1));

            algorithm.Compute();

            Assert.AreEqual(GraphColor.Black, algorithm.GetVertexColor(1));
            Assert.AreEqual(GraphColor.Black, algorithm.GetVertexColor(2));
        }
        public void Constructor()
        {
            var graph     = new BidirectionalGraph <int, Edge <int> >();
            var algorithm = new BidirectionalDepthFirstSearchAlgorithm <int, Edge <int> >(graph);

            AssertAlgorithmProperties(algorithm, graph);

            var verticesColors = new Dictionary <int, GraphColor>();

            algorithm = new BidirectionalDepthFirstSearchAlgorithm <int, Edge <int> >(graph, verticesColors);
            AssertAlgorithmProperties(algorithm, graph, verticesColors);

            algorithm = new BidirectionalDepthFirstSearchAlgorithm <int, Edge <int> >(null, graph, verticesColors);
            AssertAlgorithmProperties(algorithm, graph, verticesColors);

            algorithm.MaxDepth = 12;
            AssertAlgorithmProperties(algorithm, graph, verticesColors, 12);

            algorithm.ProcessAllComponents = true;
            AssertAlgorithmProperties(algorithm, graph, verticesColors, 12, true);

            #region Local function

            void AssertAlgorithmProperties <TVertex, TEdge>(
                BidirectionalDepthFirstSearchAlgorithm <TVertex, TEdge> algo,
                IBidirectionalGraph <TVertex, TEdge> g,
                IDictionary <TVertex, GraphColor> vColors = null,
                int maxDepth = int.MaxValue,
                bool processAllComponents = false)
                where TEdge : IEdge <TVertex>
            {
                AssertAlgorithmState(algo, g);
                if (vColors is null)
                {
                    CollectionAssert.IsEmpty(algo.VerticesColors);
                }
                else
                {
                    Assert.AreSame(vColors, algo.VerticesColors);
                }
                Assert.AreEqual(maxDepth, algo.MaxDepth);
                Assert.AreEqual(processAllComponents, algo.ProcessAllComponents);
            }

            #endregion
        }
        public void ProcessAllComponents(bool processAll)
        {
            var graph = new BidirectionalGraph <int, Edge <int> >();

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

                new Edge <int>(6, 7),
                new Edge <int>(6, 8),
                new Edge <int>(8, 6)
            });

            var algorithm = new BidirectionalDepthFirstSearchAlgorithm <int, Edge <int> >(graph)
            {
                ProcessAllComponents = processAll
            };

            algorithm.Compute(1);

            if (processAll)
            {
                QuikGraphAssert.TrueForAll(algorithm.VerticesColors, pair => pair.Value == GraphColor.Black);
            }
            else
            {
                QuikGraphAssert.TrueForAll(
                    new[] { 1, 2, 3, 4, 5 },
                    vertex => algorithm.VerticesColors[vertex] == GraphColor.Black);
                QuikGraphAssert.TrueForAll(
                    new[] { 6, 7, 8 },
                    vertex => algorithm.VerticesColors[vertex] == GraphColor.White);
            }
        }
        private static void RunDFSAndCheck <TVertex, TEdge>(
            [NotNull] IBidirectionalGraph <TVertex, TEdge> graph,
            int maxDepth = int.MaxValue)
            where TEdge : IEdge <TVertex>
        {
            var discoverTimes = new Dictionary <TVertex, int>();
            var finishTimes   = new Dictionary <TVertex, int>();
            int time          = 0;
            var dfs           = new BidirectionalDepthFirstSearchAlgorithm <TVertex, TEdge>(graph)
            {
                MaxDepth = maxDepth
            };

            dfs.InitializeVertex += vertex =>
            {
                Assert.AreEqual(GraphColor.White, dfs.VerticesColors[vertex]);
            };

            dfs.StartVertex += vertex =>
            {
                Assert.AreEqual(GraphColor.White, dfs.VerticesColors[vertex]);
            };

            dfs.DiscoverVertex += vertex =>
            {
                Assert.AreEqual(GraphColor.Gray, dfs.VerticesColors[vertex]);
                discoverTimes[vertex] = time++;
            };

            dfs.ExamineEdge += edge =>
            {
                // Depending if the edge was taken from in or out edges
                // Here we cannot determine in which case we are
                Assert.IsTrue(
                    dfs.VerticesColors[edge.Source] == GraphColor.Gray
                    ||
                    dfs.VerticesColors[edge.Target] == GraphColor.Gray);
            };

            dfs.TreeEdge += edge =>
            {
                // Depending if the edge was taken from in or out edges
                // Here we cannot determine in which case we are
                Assert.IsTrue(
                    dfs.VerticesColors[edge.Source] == GraphColor.White
                    ||
                    dfs.VerticesColors[edge.Target] == GraphColor.White);
            };

            dfs.BackEdge += edge =>
            {
                // Depending if the edge was taken from in or out edges
                // Here we cannot determine in which case we are
                Assert.IsTrue(
                    dfs.VerticesColors[edge.Source] == GraphColor.Gray
                    ||
                    dfs.VerticesColors[edge.Target] == GraphColor.Gray);
            };

            dfs.ForwardOrCrossEdge += edge =>
            {
                // Depending if the edge was taken from in or out edges
                // Here we cannot determine in which case we are
                Assert.IsTrue(
                    dfs.VerticesColors[edge.Source] == GraphColor.Black
                    ||
                    dfs.VerticesColors[edge.Target] == GraphColor.Black);
            };

            dfs.FinishVertex += vertex =>
            {
                Assert.AreEqual(GraphColor.Black, dfs.VerticesColors[vertex]);
                finishTimes[vertex] = time++;
            };

            dfs.Compute();

            // Check
            // All vertices should be black
            foreach (TVertex vertex in graph.Vertices)
            {
                Assert.IsTrue(dfs.VerticesColors.ContainsKey(vertex));
                Assert.AreEqual(dfs.VerticesColors[vertex], GraphColor.Black);
            }

            foreach (TVertex u in graph.Vertices)
            {
                foreach (TVertex v in graph.Vertices)
                {
                    if (!u.Equals(v))
                    {
                        Assert.IsTrue(
                            finishTimes[u] < discoverTimes[v] ||
                            finishTimes[v] < discoverTimes[u] ||
                            (discoverTimes[v] < discoverTimes[u] && finishTimes[u] < finishTimes[v]) ||
                            (discoverTimes[u] < discoverTimes[v] && finishTimes[v] < finishTimes[u]));
                    }
                }
            }
        }