Ejemplo n.º 1
0
    void Start()
    {
        EdgeWeightedGraph G   = new EdgeWeightedGraph(txt, ' ');
        BoruvkaMST        mst = new BoruvkaMST(G);

        foreach (Edge e in mst.edges())
        {
            print(e);
        }
        print(mst.Weight());
    }
Ejemplo n.º 2
0
    public BoruvkaMST(EdgeWeightedGraph ewg)
    {
        this.mst = new Bag();
        UF  uF  = new UF(ewg.V());
        int num = 1;

        while (num < ewg.V() && this.mst.size() < ewg.V() - 1)
        {
            Edge[]   array    = new Edge[ewg.V()];
            Iterator iterator = ewg.edges().iterator();
            while (iterator.hasNext())
            {
                Edge edge = (Edge)iterator.next();
                int  num2 = edge.either();
                int  num3 = edge.other(num2);
                int  num4 = uF.find(num2);
                int  num5 = uF.find(num3);
                if (num4 != num5)
                {
                    if (array[num4] == null || BoruvkaMST.less(edge, array[num4]))
                    {
                        array[num4] = edge;
                    }
                    if (array[num5] == null || BoruvkaMST.less(edge, array[num5]))
                    {
                        array[num5] = edge;
                    }
                }
            }
            for (int i = 0; i < ewg.V(); i++)
            {
                Edge edge = array[i];
                if (edge != null)
                {
                    int num2 = edge.either();
                    int num3 = edge.other(num2);
                    if (!uF.connected(num2, num3))
                    {
                        this.mst.add(edge);
                        this.weight += edge.weight();
                        uF.union(num2, num3);
                    }
                }
            }
            num += num;
        }
        if (!BoruvkaMST.s_assertionsDisabled && !this.check(ewg))
        {
            throw new AssertionError();
        }
    }
Ejemplo n.º 3
0
    /**/ public static void main(string[] strarr)
    {
        In i = new In(strarr[0]);
        EdgeWeightedGraph ewg        = new EdgeWeightedGraph(i);
        BoruvkaMST        boruvkaMST = new BoruvkaMST(ewg);
        Iterator          iterator   = boruvkaMST.edges().iterator();

        while (iterator.hasNext())
        {
            Edge obj = (Edge)iterator.next();
            StdOut.println(obj);
        }
        StdOut.printf("%.5f\n", new object[]
        {
            java.lang.Double.valueOf(boruvkaMST.weight())
        });
    }
Ejemplo n.º 4
0
        public void Run()
        {
            Console.WriteLine("Choose file:");      // Prompt
            Console.WriteLine("1 - tinyEWG.txt");   // Prompt
            Console.WriteLine("2 - mediumEWG.txt"); // Prompt
            //Console.WriteLine("3 - mediumEWG.txt"); // Prompt

            Console.WriteLine("or quit"); // Prompt

            var    fileNumber = Console.ReadLine();
            string fileName;

            switch (fileNumber)
            {
            case "1":
                fileName = "tinyEWG.txt";
                break;

            case "2":
                fileName = "mediumEWG.txt";
                break;

            //case "3":
            //    fileName = "largeEWG.zip";
            //    break;
            case "quit":
                return;

            default:
                return;
            }


            var @in   = new In($"Files\\Graphs\\{fileName}");
            var lines = @in.ReadAllLines();

            var lineIterator = 0;
            var v            = 0;
            var e            = 0;
            var edges        = new List <EdgeW>();

            foreach (var line in lines)
            {
                if (lineIterator == 0)
                {
                    v = Convert.ToInt32(line);
                }
                if (lineIterator == 1)
                {
                    e = Convert.ToInt32(line);
                }
                if (lineIterator > 1)
                {
                    var lineSplitted = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    var ve           = Convert.ToInt32(lineSplitted[0]);
                    var we           = Convert.ToInt32(lineSplitted[1]);
                    var weight       = Convert.ToDouble(lineSplitted[2], CultureInfo.InvariantCulture);
                    var edge         = new EdgeW(ve, we, weight);
                    edges.Add(edge);
                }

                lineIterator++;
            }

            var edgeWeightedGraph = new EdgeWeightedGraph(v, e, edges);

            Console.WriteLine(edgeWeightedGraph);

            var mst = new BoruvkaMST(edgeWeightedGraph);

            foreach (var edge in mst.Edges())
            {
                Console.WriteLine(edge);
            }
            Console.Write($"{$"{mst.Weight():0.00000}"}{Environment.NewLine}");

            Console.ReadLine();
        }