Exemplo n.º 1
0
        public static void Main()
        {
            int numberOfVertices = 9;
            var graphEdges       = new List <Edge>
            {
                new Edge(0, 3, 9),
                new Edge(0, 5, 4),
                new Edge(0, 8, 5),
                new Edge(1, 4, 8),
                new Edge(1, 7, 7),
                new Edge(2, 6, 12),
                new Edge(3, 5, 2),
                new Edge(3, 6, 8),
                new Edge(3, 8, 20),
                new Edge(4, 7, 10),
                new Edge(6, 8, 7)
            };

            var minimumSpanningForest = KruskalAlgorithm.Kruskal(numberOfVertices, graphEdges);

            Console.WriteLine("Minimum spanning forest weight: " +
                              minimumSpanningForest.Sum(e => e.Weight));

            foreach (var edge in minimumSpanningForest)
            {
                Console.WriteLine(edge);
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            List <Vertice> listaVertices = new List <Vertice>();

            listaVertices.Add(new Vertice("A"));
            listaVertices.Add(new Vertice("B"));
            listaVertices.Add(new Vertice("C"));
            listaVertices.Add(new Vertice("D"));
            listaVertices.Add(new Vertice("E"));
            listaVertices.Add(new Vertice("F"));
            listaVertices.Add(new Vertice("G"));
            listaVertices.Add(new Vertice("H"));

            List <Arista> listaAristas = new List <Arista>();

            listaAristas.Add(new Arista(1, listaVertices[0], listaVertices[1]));
            listaAristas.Add(new Arista(2, listaVertices[0], listaVertices[2]));
            listaAristas.Add(new Arista(5, listaVertices[0], listaVertices[3]));
            listaAristas.Add(new Arista(13, listaVertices[1], listaVertices[5]));
            listaAristas.Add(new Arista(2, listaVertices[1], listaVertices[3]));
            listaAristas.Add(new Arista(5, listaVertices[2], listaVertices[4]));
            listaAristas.Add(new Arista(2, listaVertices[2], listaVertices[3]));
            listaAristas.Add(new Arista(4, listaVertices[3], listaVertices[4]));
            listaAristas.Add(new Arista(6, listaVertices[3], listaVertices[5]));
            listaAristas.Add(new Arista(3, listaVertices[3], listaVertices[6]));
            listaAristas.Add(new Arista(6, listaVertices[4], listaVertices[6]));
            listaAristas.Add(new Arista(2, listaVertices[5], listaVertices[6]));
            listaAristas.Add(new Arista(3, listaVertices[5], listaVertices[7]));
            listaAristas.Add(new Arista(6, listaVertices[6], listaVertices[7]));

            KruskalAlgorithm kruskalAlgorithm = new KruskalAlgorithm();

            kruskalAlgorithm.SpanningTree(listaVertices, listaAristas);

            Console.ReadKey();
        }