Ejemplo n.º 1
0
        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();
            //}
        }
Ejemplo n.º 2
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 DijkstraAllPairsSP(edgeWeightedDigraph);

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

            Console.ReadLine();
        }