private static void ShortestPath()
        {
            string  filename       = "mediumEWD.txt";
            Scanner scanner        = new Scanner(new StreamReader(File.OpenRead(filename)));
            IEdgeWeightedDIgraph G = new EdgeWeightedDigraph(scanner);

            StdOut.Print("输入一个边:");
            int           s  = StdIn.ReadInt();
            IShortestPath sp = new DijkstraSP(G, s);

            for (int t = 0; t < G.V; t++)
            {
                StdOut.Print(s + " to " + t);
                StdOut.Printf(" ({0}): ", sp.DistTo(t));
                if (sp.HasPathTo(t))
                {
                    foreach (var e in sp.PathTo(t))
                    {
                        StdOut.Print(e + "  ");
                    }
                }
                StdOut.Println();
            }
            DijkstraAllPairsSP allPairsSP = new DijkstraAllPairsSP(G);

            StdOut.Println();
            StdOut.Println(allPairsSP.Dist(1, 28));

            //string filename2 = "tinyEWDAG.txt";
            //scanner = new Scanner(new StreamReader(File.OpenRead(filename2)));
            //G = new EdgeWeightedDigraph(scanner);
            //IShortestPath sp2 = new AcyclicSP(G, s);
            //for (int t = 0; t < G.V; t++)
            //{
            //    StdOut.Print(s + " to " + t);
            //    StdOut.Printf(" ({0}): ", sp2.DistTo(t));
            //    if (sp2.HasPathTo(t))
            //        foreach (var e in sp.PathTo(t))
            //        {
            //            StdOut.Print(e + "  ");
            //        }
            //    StdOut.Println();
            //}

            //filename = "tinyEWDnc.txt";
            //scanner = new Scanner(new StreamReader(File.OpenRead(filename)));
            //G = new EdgeWeightedDigraph(scanner);
            //sp = new BellmanFordSP(G, s);
            //for (int t = 0; t < G.V; t++)
            //{
            //    StdOut.Print(s + " to " + t);
            //    StdOut.Printf(" ({0}): ", sp.DistTo(t));
            //    if (sp.HasPathTo(t))
            //        foreach (var e in sp.PathTo(t))
            //        {
            //            StdOut.Print(e + "  ");
            //        }
            //    StdOut.Println();
            //}
        }
Esempio n. 2
0
    void Start()
    {
        double a = double.PositiveInfinity;
        double b = (double.PositiveInfinity + 10.0);

        print(a > b);
        print(a < b);
        print(a == b);

        EdgeWeightedDigraph G = new EdgeWeightedDigraph(Graph);
        int s = 0;

        // compute shortest paths
        DijkstraSP sp = new DijkstraSP(G, s);


        // print shortest path
        for (int t = 0; t < G.V(); t++)
        {
            if (sp.hasPathTo(t))
            {
                string str = s + " to " + t + "  Distance=" + sp.DistTo(t);

                foreach (DirectedEdge e in sp.PathTo(t))
                {
                    str += (e + "\t");
                }
                print(str);
            }
            else
            {
                print(s + " to " + t + "         no path\n");
            }
        }
    }
Esempio n. 3
0
        public void Run()
        {
            Console.WriteLine("Choose file:");      // Prompt
            Console.WriteLine("1 - tinyEWD.txt");   // Prompt
            Console.WriteLine("2 - mediumEWD.txt"); // Prompt
            //Console.WriteLine("3 - mediumEWG.txt"); // Prompt

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

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

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

            case "2":
                fileName = "mediumEWD.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 <DirectedEdge>();

            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 DirectedEdge(ve, we, weight);
                    edges.Add(edge);
                }

                lineIterator++;
            }

            var edgeWeightedDigraph = new EdgeWeightedDigraph(v, e, edges);

            Console.WriteLine(edgeWeightedDigraph);

            const int s = 0;
            // compute shortest paths
            var sp = new DijkstraSP(edgeWeightedDigraph, s);


            // print shortest path
            for (var t = 0; t < edgeWeightedDigraph.V; t++)
            {
                if (sp.HasPathTo(t))
                {
                    Console.Write($"{s} to {t} {$"{sp.DistTo(t):0.00}"}  ");
                    foreach (var edge in sp.PathTo(t))
                    {
                        Console.Write($"{edge}   ");
                    }
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine($"{s} to {t}         no path{Environment.NewLine}");
                }
            }

            Console.ReadLine();
        }