Exemple #1
0
	void Start () {
      
        int s = 0;
        EdgeWeightedDigraph G = new EdgeWeightedDigraph(txt);

        BellmanFordSP sp = new BellmanFordSP(G, s);

        // print negative cycle
        if (sp.hasNegativeCycle())
        {
            foreach (DirectedEdge e in sp.negativeCycle())
                print(e);
        }

        // print shortest paths
        else
        {
            for (int v = 0; v < G.V(); v++)
            {
                if (sp.hasPathTo(v))
                {
                    string str=(s+" to "+ v+"  Distance="+ sp.DistTo(v)+"     ");
                    foreach (DirectedEdge e in sp.PathTo(v))
                    {
                       str+=(e + "   ");
                    }
                   print(str);
                }
                else
                {
                   print(s+" to "+ v+"           no path\n");
                }
            }
        }
    }
        static void Main(string[] args)
        {
            EdgeWeightedDigraph g = new EdgeWeightedDigraph(8);

            foreach (var temp in TestEdgeData)
            {
                g.AddEdge(temp);
            }

            SP sp;

            //sp = new ShortestPath(g, 0);
            sp = new BellmanFordSP(g, 0);

            for (int i = 0; i < g.Vertice; i++)
            {
                Console.Write(0 + "to" + i + "( {0} )", sp.DisTo(i));

                if (sp.HasPath(i))
                {
                    foreach (DirectedEdge temp in sp.PathTo(i))
                    {
                        Console.Write(" " + temp.ToString() + "   ");
                    }

                    Console.WriteLine();
                }
            }
        }
Exemple #3
0
    /**/ public static void main(string[] strarr)
    {
        In  i  = new In(strarr[0]);
        int i2 = Integer.parseInt(strarr[1]);
        EdgeWeightedDigraph edgeWeightedDigraph = new EdgeWeightedDigraph(i);
        BellmanFordSP       bellmanFordSP       = new BellmanFordSP(edgeWeightedDigraph, i2);

        if (bellmanFordSP.hasNegativeCycle())
        {
            Iterator iterator = bellmanFordSP.negativeCycle().iterator();
            while (iterator.hasNext())
            {
                DirectedEdge obj = (DirectedEdge)iterator.next();
                StdOut.println(obj);
            }
        }
        else
        {
            for (int j = 0; j < edgeWeightedDigraph.V(); j++)
            {
                if (bellmanFordSP.hasPathTo(j))
                {
                    StdOut.printf("%d to %d (%5.2f)  ", new object[]
                    {
                        Integer.valueOf(i2),
                        Integer.valueOf(j),
                        java.lang.Double.valueOf(bellmanFordSP.distTo(j))
                    });
                    Iterator iterator2 = bellmanFordSP.pathTo(j).iterator();
                    while (iterator2.hasNext())
                    {
                        DirectedEdge obj2 = (DirectedEdge)iterator2.next();
                        StdOut.print(new StringBuilder().append(obj2).append("   ").toString());
                    }
                    StdOut.println();
                }
                else
                {
                    StdOut.printf("%d to %d           no path\n", new object[]
                    {
                        Integer.valueOf(i2),
                        Integer.valueOf(j)
                    });
                }
            }
        }
    }
Exemple #4
0
    void Start()
    {
        string[] lines = txt.text.Split(new char[] { '\n' });

        // V currencies
        int V = int.Parse(lines[0]);

        string[] name = new string[V];

        // create complete network
        EdgeWeightedDigraph G = new EdgeWeightedDigraph(V);

        for (int v = 0; v < V; v++)
        {
            string[] lineStrs = lines[v + 1].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            name[v] = lineStrs[0];
            for (int w = 0; w < V; w++)
            {
                double       rate = double.Parse(lineStrs[w + 1]);
                DirectedEdge e    = new DirectedEdge(v, w, -Math.Log(rate));
                G.addEdge(e);
            }
        }

        // find negative cycle
        BellmanFordSP spt = new BellmanFordSP(G, 0);

        if (spt.hasNegativeCycle())
        {
            double stake = 1000.0;
            foreach (DirectedEdge e in spt.negativeCycle())
            {
                string str = (stake + " " + name[e.from()] + " ");
                stake *= Math.Exp(-e.Weight());
                str   += ("= " + stake + " " + name[e.to()] + "\n");
                print(str);
            }

            print("以1000元为本金,该负权重环一圈套利" + (stake - 1000));
        }
        else
        {
            print("No arbitrage opportunity");
        }
    }
Exemple #5
0
        public void Run()
        {
            Console.WriteLine("Choose file:");      // Prompt
            Console.WriteLine("1 - tinyEWDn.txt");  // Prompt
            Console.WriteLine("2 - tinyEWDnc.txt"); // Prompt
            //Console.WriteLine("3 - mediumEWG.txt"); // Prompt

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

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

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

            case "2":
                fileName = "tinyEWDnc.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 BellmanFordSP(edgeWeightedDigraph, s);

            // print negative cycle
            if (sp.HasNegativeCycle())
            {
                foreach (var edge in sp.NegativeCycle())
                {
                    Console.WriteLine(edge);
                }
            }
            else
            {
                // 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();
        }