Ejemplo n.º 1
0
        public static void MainTest(string[] args)
        {
            TextInput StdIn = new TextInput();
            // V currencies
            int V = StdIn.ReadInt();

            string[] name = new string[V];

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

            for (int v = 0; v < V; v++)
            {
                name[v] = StdIn.ReadString();
                for (int w = 0; w < V; w++)
                {
                    double       rate = StdIn.ReadDouble();
                    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.GetNegativeCycle())
                {
                    Console.Write("{0,10:F5} {1} ", stake, name[e.From]);
                    stake *= Math.Exp(-e.Weight);
                    Console.Write("= {0,10:F5} {1}\n", stake, name[e.To]);
                }
            }
            else
            {
                Console.WriteLine("No arbitrage opportunity");
            }
        }
Ejemplo n.º 2
0
        public static void MainTest(string[] args)
        {
            TextInput           input = new TextInput(args[0]);
            EdgeWeightedDigraph G     = new EdgeWeightedDigraph(input);
            int s = int.Parse(args[1]);

            BellmanFordSP sp = new BellmanFordSP(G, s);

            // print negative cycle
            if (sp.HasNegativeCycle)
            {
                foreach (DirectedEdge e in sp.GetNegativeCycle())
                {
                    Console.WriteLine(e);
                }
            }
            else // print shortest paths
            {
                for (int v = 0; v < G.V; v++)
                {
                    if (sp.HasPathTo(v))
                    {
                        Console.Write("{0} to {1} ({2:F2})  ", s, v, sp.DistTo(v));
                        foreach (DirectedEdge e in sp.PathTo(v))
                        {
                            Console.Write(e + "   ");
                        }
                        Console.WriteLine();
                    }
                    else
                    {
                        Console.Write("{0} to {1}: no path\n", s, v);
                    }
                }
            }
        }