コード例 #1
0
        public void TestKnapsackProblem()
        {
            //int[] value = { 10, 50, 70};
            //int[] weight = { 10, 20, 30};
            //int capacity = 40;
            //int itemCount = 3;

            var items = new[]
            {
                new KnapsackItem {
                    Value = 10, Weight = 10
                },
                new KnapsackItem {
                    Value = 50, Weight = 20
                },
                new KnapsackItem {
                    Value = 70, Weight = 30
                },
            };

            int result = DPAlgos <int> .KnapSack(items, 40, 3);

            //int result = DPAlgos.KnapsackProblem(capacity, weight, value, itemCount);

            Assert.IsTrue(result == 80);
        }
コード例 #2
0
        public void TestGraph()
        {
            int           verticesCount = 5;
            int           edgesCount    = 8;
            Graph <char>  graph         = new Graph <char>(verticesCount, edgesCount);
            Vertex <char> vertex1       = new Vertex <char>('A');
            Vertex <char> vertex2       = new Vertex <char>('B');
            Vertex <char> vertex3       = new Vertex <char>('C');
            Vertex <char> vertex4       = new Vertex <char>('D');
            Vertex <char> vertex5       = new Vertex <char>('E');

            graph.Edges[0]             = new Edge <char>();
            graph.Edges[0].Source      = vertex1;
            graph.Edges[0].Destination = vertex2;
            graph.Edges[0].Weight      = -1;

            // Edge 0-2
            graph.Edges[1]             = new Edge <char>();
            graph.Edges[1].Source      = vertex1;
            graph.Edges[1].Destination = vertex3;
            graph.Edges[1].Weight      = 4;

            // Edge 1-2
            graph.Edges[2]             = new Edge <char>();
            graph.Edges[2].Source      = vertex2;
            graph.Edges[2].Destination = vertex3;
            graph.Edges[2].Weight      = 3;

            // Edge 1-3
            graph.Edges[3]             = new Edge <char>();
            graph.Edges[3].Source      = vertex2;
            graph.Edges[3].Destination = vertex4;
            graph.Edges[3].Weight      = 2;

            // Edge 1-4
            graph.Edges[4]             = new Edge <char>();
            graph.Edges[4].Source      = vertex2;
            graph.Edges[4].Destination = vertex5;
            graph.Edges[4].Weight      = 2;

            // Edge 3-2
            graph.Edges[5]             = new Edge <char>();
            graph.Edges[5].Source      = vertex4;
            graph.Edges[5].Destination = vertex3;
            graph.Edges[5].Weight      = 5;

            // Edge 3-1
            graph.Edges[6]             = new Edge <char>();
            graph.Edges[6].Source      = vertex4;
            graph.Edges[6].Destination = vertex2;
            graph.Edges[6].Weight      = 1;

            // Edge 4-3
            graph.Edges[7]             = new Edge <char>();
            graph.Edges[7].Source      = vertex5;
            graph.Edges[7].Destination = vertex4;
            graph.Edges[7].Weight      = -3;

            IList <Vertex <char> > vertices = DPAlgos <char> .BellmanFordAlgorithm(graph, vertex1);

            Assert.IsTrue(vertices.Count > 0);
        }