Beispiel #1
0
 /// <summary>
 /// Computes a shortest paths tree from each vertex to to every other vertex in
 /// the edge-weighted digraph <tt>G</tt>.
 /// </summary>
 /// <param name="g">G the edge-weighted digraph</param>
 /// <exception cref="ArgumentException">if an edge weight is negative</exception>
 /// <exception cref="ArgumentException">unless 0 &lt;= <tt>s</tt> &lt;= <tt>V</tt> - 1</exception>
 public DijkstraAllPairsSP(EdgeWeightedDigraph g)
 {
     _all = new DijkstraSP[g.V];
     for (var v = 0; v < g.V; v++)
     {
         _all[v] = new DijkstraSP(g, v);
     }
 }
        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();
        }
 /// <summary>
 /// Computes a shortest paths tree from each vertex to to every other vertex in
 /// the edge-weighted digraph <tt>G</tt>.
 /// </summary>
 /// <param name="g">G the edge-weighted digraph</param>
 /// <exception cref="ArgumentException">if an edge weight is negative</exception>
 /// <exception cref="ArgumentException">unless 0 &lt;= <tt>s</tt> &lt;= <tt>V</tt> - 1</exception>
 public DijkstraAllPairsSP(EdgeWeightedDigraph g)
 {
     _all = new DijkstraSP[g.V];
     for (var v = 0; v < g.V; v++)
         _all[v] = new DijkstraSP(g, v);
 }