Exemplo n.º 1
0
        public void Case1()
        {
            var vertices = new ExtendedVertex[]
                {
                     new ExtendedVertex(1, new AdjacentEdge[]
                        {
                            new AdjacentEdge(2,1),
                            new AdjacentEdge(3,3),
                            new AdjacentEdge(4,4),
                        }),
                    new ExtendedVertex(2, new AdjacentEdge[]
                    {
                        new AdjacentEdge(1,1),
                        new AdjacentEdge(3,2),
                    }),
                    new ExtendedVertex(3, new AdjacentEdge[]
                        {
                            new AdjacentEdge(4,5),
                            new AdjacentEdge(2,2),
                        }),
                    new ExtendedVertex(4, new AdjacentEdge[]
                    {
                        new AdjacentEdge(1,4),
                        new AdjacentEdge(3,5),
                    }),
                };

            var primAlg = new PrimMST();
            Assert.AreEqual(7, primAlg.CalcMSTLength(vertices));
        }
Exemplo n.º 2
0
        public void Case2()
        {
            var vertices = new ExtendedVertex[]
                {
                    new ExtendedVertex(1, new AdjacentEdge[]
                        {
                            new AdjacentEdge(2,6),
                            new AdjacentEdge(3,3),
                            new AdjacentEdge(4,-14),
                        }),
                    new ExtendedVertex(2, new AdjacentEdge[]
                    {
                        new AdjacentEdge(1,6),
                        new AdjacentEdge(4,8),
                    }),
                    new ExtendedVertex(3, new AdjacentEdge[]
                        {
                            new AdjacentEdge(4,12),
                            new AdjacentEdge(1,3),
                        }),
                    new ExtendedVertex(4, new AdjacentEdge[]
                    {
                        new AdjacentEdge(1,-14),
                        new AdjacentEdge(3,12),
                        new AdjacentEdge(5,-5),
                    }),
                    new ExtendedVertex(5, new AdjacentEdge[]
                    {
                        new AdjacentEdge(4,-5),
                    }),
                };

            var primAlg = new PrimMST();
            Assert.AreEqual(-10, primAlg.CalcMSTLength(vertices));
        }
Exemplo n.º 3
0
        private static void PrimMST(string filePath)
        {
            var lines = File.ReadAllLines(filePath);
            int verticesCount = int.Parse(lines[0].Split(' ').First());
            var vertices = Enumerable.Range(1, verticesCount).Select(x => new ExtendedVertex(x)).ToArray();

            foreach (var line in lines.Skip(1))
            {
                var node = line.Split(new[] {' ', '\t', ','}, StringSplitOptions.RemoveEmptyEntries);
                var left = int.Parse(node[0]);
                var right = int.Parse(node[1]);
                var weight = int.Parse(node[2]);
                vertices[left - 1].AddAdjacenEdge(new AdjacentEdge(right, weight));
                vertices[right - 1].AddAdjacenEdge(new AdjacentEdge(left, weight));
            }

            var primAlg = new PrimMST();

            Console.WriteLine("Prim MST lenght: {0}", primAlg.CalcMSTLength(vertices));
        }