void Relax(EdgeWeightedDiagraph g, int v) { foreach (DirectedEdge e in g.AdjList(v)) { int w = e.To(); if (distTo[w] < distTo[v] + e.Weight()) { distTo[w] = distTo[v] + e.Weight(); edgeTo[w] = e; } } }
//Bellman Ford has the complexity of O(VE) public BellmanFordSP(EdgeWeightedDiagraph ewg, int source) { vertexNum = ewg.V(); distTo = new double[vertexNum]; edgeTo = new DirectedEdge[vertexNum]; for (int i = 0; i < vertexNum; i++) { distTo[i] = double.PositiveInfinity; } distTo[source] = 0.0; }
public AcyclicSP(EdgeWeightedDiagraph ewg, int source) { edgeTo = new DirectedEdge[ewg.V()]; distTo = new double[ewg.V()]; for (int i = 0; i < ewg.V(); i++) { distTo[i] = double.PositiveInfinity; } distTo[source] = 0; TopologicalOrder tlOrder = new TopologicalOrder(ewg); foreach (int v in tlOrder.Order()) { Relax(ewg, v); } }
public DijkstraSP(EdgeWeightedDiagraph g, int source) { vertexNumber = g.V(); distTo = new double[vertexNumber]; for (int i = 0; i < vertexNumber; i++) { distTo[i] = double.PositiveInfinity; } distTo[source] = 0.0; edgeTo = new DirectedEdge[vertexNumber]; pq = new IndexMinPQ <double>(vertexNumber); pq.Insert(source, 0.0); while (!pq.IsEmpty()) { int current = pq.DelMin(); Relax(g, current); } }
void Relax(EdgeWeightedDiagraph g, int v) { foreach (DirectedEdge e in g.AdjList(v)) { int w = e.To(); double weight = e.Weight(); if (distTo[w] > distTo[v] + weight) { //relax edgeTo[w] = e; distTo[w] = distTo[v] + weight; if (pq.Contains(w)) { pq.ChangeKey(w, distTo[w]); } else { pq.Insert(w, distTo[w]); } } } }
void Relax(EdgeWeightedDiagraph ewg, int v) { }