Ejemplo n.º 1
0
    // Use this for initialization
    void Start()
    {
        //利用LazyPrim算法计算稀疏有权图的最短路径
        print("求稀疏有权图的最小生成树");
        string name = "";
        string url  = "";

        name = "testWeightG1.txt";
        url  = FileHelper.FileNameHelper(name);
        WeightSpareGraph <float> wsGraph = null;

        ReadWeightGraph.ReadGraphFromFile(url, out wsGraph, false);
        wsGraph.print();
        LazyPrimMST lazyPrim = new LazyPrimMST(wsGraph);

        lazyPrim.print();
        print("最小生成树的长度为:" + lazyPrim.length());
        print("使用深度优先搜索的方法得到一棵生成树");
        lazyPrim = new LazyPrimMST(wsGraph, true);
        lazyPrim.printOneTree();
        print("*********************");
        //利用Prim算法计算稀疏有权图的最短路径

        PrimMST prim = new PrimMST(wsGraph);

        prim.print();
    }
Ejemplo n.º 2
0
        public void TestLazyPrimMST()
        {
            LazyPrimMST mst = graph.LazyPrimMST();

            Edge[] edges = mst.Edges();
            foreach (Edge edge in edges)
            {
                // TestContext.WriteLine(edge.ToString());
            }
            TestContext.WriteLine(mst.Weight().ToString());
        }
Ejemplo n.º 3
0
    void Start()
    {
        EdgeWeightedGraph G   = new EdgeWeightedGraph(txt, ' ');
        LazyPrimMST       mst = new LazyPrimMST(G);

        foreach (Edge e in mst.edges())
        {
            print(e.ToString());
        }
        print(mst.Weight());
    }
    /**/ public static void main(string[] strarr)
    {
        In i = new In(strarr[0]);
        EdgeWeightedGraph ewg         = new EdgeWeightedGraph(i);
        LazyPrimMST       lazyPrimMST = new LazyPrimMST(ewg);
        Iterator          iterator    = lazyPrimMST.edges().iterator();

        while (iterator.hasNext())
        {
            Edge obj = (Edge)iterator.next();
            StdOut.println(obj);
        }
        StdOut.printf("%.5f\n", new object[]
        {
            java.lang.Double.valueOf(lazyPrimMST.weight())
        });
    }
Ejemplo n.º 5
0
        void MinimumSpanningTreesTest()
        {
            var strArr = FileHandler.ReadFileAsStrArr("tinyEWG.txt");
            var ewg    = new EdgeWeightedGraph(strArr);

            foreach (var e in ewg.Adj(0))
            {
                Console.WriteLine(e);
            }
            Console.WriteLine();
            var lpm = new LazyPrimMST(ewg);

            foreach (var e in lpm.Edges())
            {
                Console.WriteLine(e);
            }
            Console.ReadKey();
        }
Ejemplo n.º 6
0
    void MinTreeTest()
    {
        EdgeWeightedGraph g = new EdgeWeightedGraph(5);

        g.AddEdge(new Edge(0, 1, 1));
        g.AddEdge(new Edge(1, 2, 2));
        g.AddEdge(new Edge(1, 3, 6));
        g.AddEdge(new Edge(2, 4, 4));
        g.AddEdge(new Edge(3, 4, 5));

        LazyPrimMST mst = new LazyPrimMST(g);
        //Debug.Log("延迟:" + mst.Weight());

        PrimMST primMst = new PrimMST(g);
        //Debug.Log("即时:" + primMst.Weight());

        KruskalMST kruskalMst = new KruskalMST(g);

        Debug.Log(kruskalMst.Weight());
    }
Ejemplo n.º 7
0
        public void TestLazyPrimMST()
        {
            string fileName = @"Graph\testG1.txt";
            var    g1       = new SparseGraph <double>(8, false);
            var    gr1      = new ReadGraph <double>(g1, fileName);

            g1.Show();

            // test lazy prim
            Console.WriteLine("Test Lazy Prim MST: ");
            var pq          = new MinHeap <Edge <double> >();
            var lazyPrimMST = new LazyPrimMST <double>(g1, pq);
            var mst         = lazyPrimMST.MSTEdges;

            foreach (var edge in mst)
            {
                Console.WriteLine(edge);
            }
            Console.WriteLine("The MST weight is: " + lazyPrimMST.MSTWeight);
        }
Ejemplo n.º 8
0
        private static void MST()
        {
            int i = 0;

            StdOut.Println("输入选择:0,1");
            i = StdIn.ReadInt();
            string filename = i switch
            {
                0 => "tinyEWG.txt",
                1 => "mediumEWG.txt",
                _ => throw new Exception("no file name is assigned"),
            };
            IEdgeWeightGraph G    = new EdgeWeightGraph(new Scanner(new StreamReader(File.OpenRead(filename))));
            IMST             mst  = new LazyPrimMST(G);
            IMST             mst2 = new PrimeMST(G);
            IMST             mst3 = new KruskalMST(G);

            ShowMSTResult(G, mst);
            ShowMSTResult(G, mst2);
            ShowMSTResult(G, mst3);
        }
Ejemplo n.º 9
0
        public void LazyPrimMSTTest()
        {
            //            LazyPrimMST primMST = new LazyPrimMST()
            EdgeWeightedGraph ewg;

            //using (StreamReader sr = new StreamReader(@"E:\Study\ALG2017\ALGRKC\dataSelf\tinyEWG.txt"))
            using (StreamReader sr = new StreamReader(@"C:\WBox\Study\Code\ALG2017\ALGRKC\dataSelf\tinyEWG.txt"))
            {
                ewg = new EdgeWeightedGraph(sr);
            }

            LazyPrimMST lazyPrimMST = new LazyPrimMST(ewg);

            IEnumerable <Edge> mstEdges = lazyPrimMST.MSTEdges();

            foreach (Edge e in mstEdges)
            {
                Console.WriteLine(e.ToString());
            }

            Console.WriteLine(lazyPrimMST.MSTValue());
        }
Ejemplo n.º 10
0
        static void Main(string[] args)
        {
            //var graph = GraphBuilder.GenerateGraph("../../../graph1.txt", allowSelfLoop: false, allowParallelEdges: false);
            //AnalyseGraph(graph);

            //var symbolGraph = GraphBuilder.GenerateSymbolGraph("../../../routes.txt", allowSelfLoop: false, allowParallelEdges: false);
            //AnalyseSymbolGraph(symbolGraph);


            //var graph = GraphBuilder.GenerateSymbolGraph("../../../routes_nl.txt", allowSelfLoop: false, allowParallelEdges: false);
            //Console.WriteLine(graph.ToString());

            //var cc = new ConnectedComponents(graph.Graph);
            //Console.WriteLine("\r\n" + cc.ToString());

            //var bfs = new BreadthFirstPaths(graph.Graph, graph.Index("DRA"));
            //var sb = new StringBuilder();
            //foreach (var v in bfs.PathTo(graph.Index("UTR")))
            //    sb.Append($"{graph.Name(v)}->");
            //Console.WriteLine(sb.ToString().Substring(0, sb.ToString().Length - 2));

            //var prop = new GraphProperties(graph.Graph);
            //Console.WriteLine(prop.ToString());

            //var digraph = GraphBuilder.GenerateDigraph("../../../Graphs/digraph1.txt");
            //Console.WriteLine(digraph.ToString());
            //Console.WriteLine(digraph.Reverse().ToString());

            //var ddfs = new DirectedDFS(digraph, 0);
            //Console.WriteLine(ddfs.ToString());

            //var cc = new ConnectedComponents(digraph);
            //Console.WriteLine(cc.ToString());

            //var directedCycle = new DirectedCycle(digraph);
            //Console.WriteLine($"Directed graph contains {(directedCycle.HasCycle() ? "a" : "no")} cycle.");
            //if (directedCycle.HasCycle())
            //{
            //    Console.Write("There is a cycle in: ");
            //    var cycle = new List<int>();
            //    foreach (var v in directedCycle.Cycle())
            //        cycle.Add(v);
            //    Console.WriteLine(String.Join("-", cycle));
            //}

            //var digraph = GraphBuilder.GenerateDigraph("../../../Graphs/digraph2.txt", allowParallelEdges: false, allowSelfLoop: false);
            //var topological = new Topological(digraph);

            //Console.WriteLine($"Directed acyclic graph: {(topological.IsDAG() ? "yes" : "no")}");
            //Console.Write("PIN code: ");
            //foreach (var o in topological.Order())
            //{
            //    if (o == 4 || o == 5) continue;
            //    Console.Write(o.ToString());
            //}
            //Console.WriteLine();

            //var scc = new KosarajuSharirSCC(digraph);
            //Console.WriteLine(scc.ToString());

            //var digraph = GraphBuilder.GenerateSymbolDigraph("../../../Graphs/prerequisite_scheduling.txt", allowParallelEdges: false, allowSelfLoop: false);
            //var topological = new Topological(digraph.Digraph);

            //Console.WriteLine($"Directed acyclic graph: {(topological.IsDAG() ? "yes" : "no")}");
            //Console.WriteLine("Precedence-constrained schedule (DAG in topological order):");

            //foreach(int v in topological.Order())
            //{
            //    Console.WriteLine($"{v}: {digraph.Name(v)}");
            //}

            //Console.ReadKey();

            //var rng = new Random();

            //var edges = new List<Edge>();
            //var pq = new MaxPQ<Edge>(16);
            //for (int i = 0; i < 16; i++)
            //{
            //    var edge = new Edge(0, 0, rng.Next(0, 100));
            //    pq.Insert(edge);
            //    Console.WriteLine($"{i}: {edge.Weight}");
            //}

            //Console.WriteLine("PriorityQueue output:");
            //while(!pq.IsEmpty)
            //{
            //    var e = (Edge)pq.DeleteMax();
            //    Console.WriteLine($"{e.Weight}");
            //}

            var ewg     = GraphBuilder.GenerateEdgeWeightedGraph("../../../Graphs/tiny_ewg.txt");
            var mst     = new LazyPrimMST(ewg);
            var kruskal = new KruskalMST(ewg);

            Console.WriteLine(ewg.ToString());
            Console.WriteLine(mst.ToString());
            Console.WriteLine(kruskal.ToString());

            Console.ReadKey();
        }
Ejemplo n.º 11
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 LazyPrimMST(edgeWeightedGraph);

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

            Console.ReadLine();
        }