Пример #1
0
    void Start()
    {
        int s = 5;
        EdgeWeightedDigraph G = new EdgeWeightedDigraph(txt);

        // find shortest path from s to each other vertex in DAG
        AcyclicSP sp = new AcyclicSP(G, s);

        for (int v = 0; v < G.V(); v++)
        {
            if (sp.hasPathTo(v))
            {
                string str = (s + " to " + v + "   ditance=" + sp.DistTo(v));
                foreach (DirectedEdge e in sp.pathTo(v))
                {
                    str += ("     " + e);
                }
                print(str);
            }
            else
            {
                print(s + " to " + v + "         no path\n");
            }
        }
    }
    /**/ public static void main(string[] strarr)
    {
        In  i  = new In(strarr[0]);
        int i2 = Integer.parseInt(strarr[1]);
        EdgeWeightedDigraph edgeWeightedDigraph = new EdgeWeightedDigraph(i);
        AcyclicSP           acyclicSP           = new AcyclicSP(edgeWeightedDigraph, i2);

        for (int j = 0; j < edgeWeightedDigraph.V(); j++)
        {
            if (acyclicSP.hasPathTo(j))
            {
                StdOut.printf("%d to %d (%.2f)  ", new object[]
                {
                    Integer.valueOf(i2),
                    Integer.valueOf(j),
                    java.lang.Double.valueOf(acyclicSP.distTo(j))
                });
                Iterator iterator = acyclicSP.pathTo(j).iterator();
                while (iterator.hasNext())
                {
                    DirectedEdge obj = (DirectedEdge)iterator.next();
                    StdOut.print(new StringBuilder().append(obj).append("   ").toString());
                }
                StdOut.println();
            }
            else
            {
                StdOut.printf("%d to %d         no path\n", new object[]
                {
                    Integer.valueOf(i2),
                    Integer.valueOf(j)
                });
            }
        }
    }
Пример #3
0
        public void Run()
        {
            Console.WriteLine("Choose file:");      // Prompt
            Console.WriteLine("1 - tinyEWDAG.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 = "tinyEWDAG.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 = 5;

            // find shortest path from s to each other vertex in DAG
            var sp = new AcyclicSP(edgeWeightedDigraph, s);

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