예제 #1
0
        public void GraphKnowsTheIncomingAndOutgoingEdges()
        {
            var graph = new DiGraph <int, int>(1);

            graph.AddEdge(1, 1);
            graph.AddEdge(2, 2);
            graph.AddEdge(3, 3);
            graph.AddEdge(1, 2);
            graph.AddEdge(2, 1);
            graph.AddEdge(2, 3);
            graph.AddEdge(3, 2);
            graph.AddEdge(3, 1);
            graph.AddEdge(1, 3);

            CollectionAssert.AreEquivalent(new List <IEdge <int, int> >
            {
                new Edge <int, int>(1, 1, 1),
                new Edge <int, int>(2, 1, 1),
                new Edge <int, int>(3, 1, 1),
            }, graph.GetIncomingEdges(1));

            CollectionAssert.AreEquivalent(new List <IEdge <int, int> >
            {
                new Edge <int, int>(1, 1, 1),
                new Edge <int, int>(1, 2, 1),
                new Edge <int, int>(1, 3, 1),
            }, graph.GetOutgoingEdges(1));

            CollectionAssert.AreEquivalent(new List <IEdge <int, int> >
            {
                new Edge <int, int>(1, 2, 1),
                new Edge <int, int>(2, 2, 1),
                new Edge <int, int>(3, 2, 1),
            }, graph.GetIncomingEdges(2));

            CollectionAssert.AreEquivalent(new List <IEdge <int, int> >
            {
                new Edge <int, int>(2, 1, 1),
                new Edge <int, int>(2, 2, 1),
                new Edge <int, int>(2, 3, 1),
            }, graph.GetOutgoingEdges(2));

            CollectionAssert.AreEquivalent(new List <IEdge <int, int> >
            {
                new Edge <int, int>(1, 3, 1),
                new Edge <int, int>(2, 3, 1),
                new Edge <int, int>(3, 3, 1),
            }, graph.GetIncomingEdges(3));

            CollectionAssert.AreEquivalent(new List <IEdge <int, int> >
            {
                new Edge <int, int>(3, 1, 1),
                new Edge <int, int>(3, 2, 1),
                new Edge <int, int>(3, 3, 1),
            }, graph.GetOutgoingEdges(3));

            Assert.IsTrue(graph.HasEdge(3, 1));
            Assert.IsFalse(graph.HasEdge(4, 2));
        }
예제 #2
0
        public void DiGraph_Smoke_Test()
        {
            var graph = new DiGraph <int>();

            graph.AddVertex(1);
            graph.AddVertex(2);
            graph.AddVertex(3);
            graph.AddVertex(4);
            graph.AddVertex(5);

            graph.AddEdge(1, 2);
            Assert.IsTrue(graph.HasEdge(1, 2));
            Assert.IsFalse(graph.HasEdge(2, 1));

            graph.AddEdge(3, 2);
            Assert.AreEqual(2, graph.InEdges(2).Count());
            graph.RemoveEdge(3, 2);

            graph.AddEdge(2, 3);
            graph.AddEdge(3, 4);
            graph.AddEdge(4, 5);
            graph.AddEdge(4, 1);
            graph.AddEdge(3, 5);

            Assert.AreEqual(2, graph.OutEdges(4).Count());

            Assert.AreEqual(5, graph.VerticesCount);

            Assert.IsTrue(graph.HasEdge(1, 2));

            graph.RemoveEdge(1, 2);

            Assert.IsFalse(graph.HasEdge(1, 2));

            graph.RemoveEdge(2, 3);
            graph.RemoveEdge(3, 4);
            graph.RemoveEdge(4, 5);
            graph.RemoveEdge(4, 1);

            Assert.IsTrue(graph.HasEdge(3, 5));
            graph.RemoveEdge(3, 5);
            Assert.IsFalse(graph.HasEdge(3, 5));

            graph.RemoveVertex(1);
            graph.RemoveVertex(2);
            graph.RemoveVertex(3);

            graph.AddEdge(4, 5);
            graph.RemoveVertex(4);

            graph.AddEdge(5, 5);
            graph.RemoveEdge(5, 5);
            graph.RemoveVertex(5);


            Assert.AreEqual(0, graph.VerticesCount);
        }
        /// <summary>
        /// Computes cost of the most expensive path in the <see cref="graph"/>.
        /// </summary>
        /// <param name="graph"></param>
        /// <param name="topologicalOrder"></param>
        /// <returns></returns>
        public double GetMaximumCost(DiGraph <Operation> graph, IReadOnlyList <Operation> topologicalOrder)
        {
            double[] highestOperationCosts = new double[topologicalOrder.Count];

            // find longest distance to i-th operation for every i
            for (int i = 1; i < topologicalOrder.Count; i++)
            {
                // check all edges to the i-th vertex
                var operation = topologicalOrder[i];
                for (int j = 0; j < i; j++)
                {
                    var otherOperation = topologicalOrder[j];

                    if (graph.HasEdge(otherOperation, operation))
                    {
                        highestOperationCosts[i] = Math.Max(highestOperationCosts[i],
                                                            highestOperationCosts[j] + otherOperation.Cost);
                    }
                }
            }

            return(highestOperationCosts.Last());
        }