示例#1
0
 private void Relax(WeightedDiGraph <T> G, T v)
 {
     foreach (var e in G.Adj(v))
     {
         T w = e.To();
         if (distTo[w] > distTo[v] + e.Weight)
         {
             distTo[w] = distTo[v] + e.Weight;
             edgeTo[w] = e;
             if (!onQueue[w])
             {
                 queue.Enqueue(w);
                 onQueue[w] = true;
             }
         }
         if (cost++ % G.V() == 0)
         {
             FindNegativeCycle();
             if (HasNegativeCycle())
             {
                 return;                     // found a negative cycle
             }
         }
     }
 }
示例#2
0
        public void PriceUpdate(PriceUpdate update)
        {
            lock (lockTheEntireGraph)
            {
                var    from   = new ExchangeCurrency(update.Exchange, update.SourceCurrency);
                var    to     = new ExchangeCurrency(update.Exchange, update.DestinationCurrency);
                double weight = -Math.Log(update.Factor);

                var oldEdge = graph.Adj(from).Where(e => e.To().CompareTo(to) == 0).SingleOrDefault();
                var newEdge = new Edge <ExchangeCurrency>(from, to, weight, update.Timestamp);
                if (oldEdge != null && oldEdge.TimeStamp < update.Timestamp)
                {
                    graph.RemoveEdge(oldEdge);
                    graph.AddEdge(newEdge);
                }
                else if (oldEdge == null) // add 1<-->1 edges by currency
                {
                    graph.AddEdge(newEdge);
                    foreach (var v in graph.Vertexes()
                             .Where(v => v.Currency == update.SourceCurrency)
                             .Where(v => v.CompareTo(from) != 0))
                    {
                        if (!graph.HasEdgeBetween(v, from))
                        {
                            graph.AddEdge(new Edge <ExchangeCurrency>(v, from, -Math.Log(1)));
                        }
                        if (!graph.HasEdgeBetween(from, v))
                        {
                            graph.AddEdge(new Edge <ExchangeCurrency>(from, v, -Math.Log(1)));
                        }
                    }

                    foreach (var v in graph.Vertexes()
                             .Where(v => v.Currency == update.DestinationCurrency)
                             .Where(v => v.CompareTo(to) != 0))
                    {
                        if (!graph.HasEdgeBetween(v, to))
                        {
                            graph.AddEdge(new Edge <ExchangeCurrency>(v, to, -Math.Log(1)));
                        }
                        if (!graph.HasEdgeBetween(to, v))
                        {
                            graph.AddEdge(new Edge <ExchangeCurrency>(to, v, -Math.Log(1)));
                        }
                    }
                }
            }
        }
示例#3
0
        private void Dfs(WeightedDiGraph <T> G, T v)
        {
            onStack[v] = true;
            marked[v]  = true;
            foreach (var e in G.Adj(v))
            {
                T w = e.To();
                if (!marked.ContainsKey(w))
                {
                    marked.Add(w, false);                         // fix to dictionary exception
                }
                if (cycle != null)
                {
                    return;                // if directed cycle found
                }
                else if (!marked[w])
                {
                    edgeTo[w] = e;
                    Dfs(G, w);
                }
                else if (onStack[w])
                {
                    cycle = new StackLinkedList <Edge <T> >();

                    Edge <T> f = e;
                    while (f.From().CompareTo(w) != 0)
                    {
                        cycle.Push(f);
                        f = edgeTo[f.From()];
                    }

                    cycle.Push(f);
                    return;
                }
            }
            onStack[v] = false;
        }