public static SP GetSp(EdgeWeightedDigraph G, int s) { SP sp; sp = new DijkstraSP(G, s); return(sp); }
private void Relax(EdgeWeightedDigraph G, int v) { foreach (var e in G.Adj(v)) { var w = e.To(); if (_distTo[w] > _distTo[v] + e.Weight()) { _distTo[w] = _distTo[v] + e.Weight(); _edgeTo[w] = e; } } }
public DijkstraSP(EdgeWeightedDigraph G, int s) : base(G, s) { _pq = new IndexMinPQ <double>(G.E()); for (int i = 0; i < G.V(); i++) { _distTo[i] = double.PositiveInfinity; } _distTo[s] = 0d; _pq.Insert(0, 0d); while (!_pq.IsEmpty()) { var w = _pq.RemoveMin(); Relax(G, w); } }
private void Relax(EdgeWeightedDigraph G, int v) { foreach (var e in G.Adj(v)) { var w = e.To(); if (_distTo[w] > _distTo[v] + e.Weight()) { _distTo[w] = _distTo[v] + e.Weight(); _edgeTo[w] = e; if (_pq.Contains(w)) { _pq.Change(w, _distTo[w]); } else { _pq.Insert(w, _distTo[w]); } } } }
public void RunSp() { EdgeWeightedDigraph G = new EdgeWeightedDigraph("tinyEWD.txt"); int s = 0; SP sp = Factory.GetSp(G, s); for (int t = 0; t < G.V(); t++) { Console.Write(s + " to " + t); Console.Write(" ({0:F2}): ", sp.DistTo(t)); if (sp.HasPathTo(t)) { foreach (DirectedEdge e in sp.PathTo(t)) { Console.Write(e + " "); } } Console.WriteLine(); } }