Ejemplo n.º 1
0
        private bool BFS(CurrencyNode[] currencyNode)
        {
            LinkedList <int> queue = new LinkedList <int>();

            for (int i = 0; i < v; i++)
            {
                currencyNode[i] = new CurrencyNode();
            }

            currencyNode[initial_ccy].Visited  = true;
            currencyNode[initial_ccy].Distance = 0;
            queue.AddLast(initial_ccy);

            while (queue.Count != 0)
            {
                int u = queue.First();
                queue.RemoveFirst();
                for (int i = 0; i < vertex[u].Count; i++)
                {
                    int current_ccy = vertex[u][i].CurrencyID;

                    if (currencyNode[current_ccy].Visited == false)
                    {
                        currencyNode[current_ccy].SetVisited(currencyNode[u].Distance, u, vertex[u][i].Exchange_rate);
                        queue.AddLast(current_ccy);

                        if (current_ccy == desired_ccy)
                        {
                            return(true); // We stop BFS when we reach the desired currency.
                        }
                    }
                }
            }
            return(false);
        }
Ejemplo n.º 2
0
        /*
        ** solve functions
        */

        public void solve_graph()
        {
            CurrencyNode[] currencyNode = new CurrencyNode[v];

            if (BFS(currencyNode) == false)
            {
                Console.Error.WriteLine("Impossible to convert as missing exchange rate pair(s)");
                Environment.Exit(1);
            }

            List <CurrencyNode> path = new List <CurrencyNode>(get_shortest_path(currencyNode));

            for (int i = path.Count - 1; i >= 0; i--)
            {
                notional *= decimal.Round(path[i].Exchange_rate, 4);
            }

            Console.WriteLine(decimal.Round(notional));
        }