Example #1
0
        public ExchangeRateResponse ExchangeRate(ExchangeRateRequest request)
        {
            lock (lockTheEntireGraph)
            {
                var response = new ExchangeRateResponse();

                var spt = new BellmanFord <ExchangeCurrency>(graph, request.Source);
                if (spt.HasNegativeCycle())
                {
                    response.IsCycle = true;

                    var cycle = spt.NegativeCycle();
                    response.Cycle = EdgesToVertexes(cycle);
                    return(response); // TODO: cycle ==> arbitrage opportunity
                }
                if (spt.HasPathTo(request.Destination))
                {
                    response.IsHasPath = true;

                    var    path           = spt.PathTo(request.Destination);
                    var    vertexesOnPath = EdgesToVertexes(path);
                    double rate           = 1;
                    foreach (var edge in path)
                    {
                        rate *= Math.Exp(-edge.Weight);
                    }

                    response.Source      = request.Source;
                    response.Destination = request.Destination;
                    response.Rate        = rate;
                    response.Path        = vertexesOnPath;
                    return(response);
                }
                if (!spt.HasPathTo(request.Destination))
                {
                    response.IsHasPath = false;
                    return(response);
                }

                throw new NotImplementedException();
            }
        }