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))); } } } } }
public BellmanFord(WeightedDiGraph <T> G, T s) { this.s = s; vertexes = new List <T>(G.Vertexes()); foreach (var v in vertexes) { distTo.Add(v, double.MaxValue); } foreach (var v in vertexes) { edgeTo.Add(v, null); } foreach (var v in vertexes) { onQueue.Add(v, false); } distTo[s] = 0.0; queue.Enqueue(s); onQueue[s] = true; while (!queue.IsEmpty && !HasNegativeCycle()) { T v = queue.Dequeue(); onQueue[v] = false; Relax(G, v); } }
public EdgeWeightedDirectedCycle(WeightedDiGraph <T> G) { var vertexes = new List <T>(G.Vertexes()); foreach (var v in vertexes) { marked.Add(v, false); } foreach (var v in vertexes) { edgeTo.Add(v, null); } foreach (var v in vertexes) { onStack.Add(v, false); } foreach (var v in vertexes) { if (!marked[v]) { Dfs(G, v); } } }