public DijkstraSP(EdgeWeightedDigraph G, int s) { foreach (DirectedEdge e in G.GetEdges()) { if (e.GetWeight() < 0) { throw new ArgumentException("Negative weight"); } } distTo = new double[G.GetVertices()]; edgeTo = new DirectedEdge[G.GetVertices()]; ValidateVertex(s); for (int v = 0; v < G.GetVertices(); v++) { distTo[v] = double.PositiveInfinity; } distTo[s] = 0.0; // relax vertices in order of distance from s Comparer <double> comparer = Comparer <double> .Default; pq = new IndexMinPQ <Double>(G.GetVertices(), comparer); pq.Insert(s, distTo[s]); while (!pq.IsEmpty()) { pq.DelMin(out int index, out double key); int v = index; foreach (DirectedEdge e in G.GetAdj(v)) { Relax(e); } } }
public DijkstraAllPairsSP(EdgeWeightedDigraph G) { all = new DijkstraSP[G.GetVertices()]; for (int v = 0; v < G.GetVertices(); v++) { all[v] = new DijkstraSP(G, v); } }
private Stack <DirectedEdge> cycle; // directed cycle (or null if no such cycle) public EdgeWeightedDirectedCycle(EdgeWeightedDigraph G) { marked = new bool[G.GetVertices()]; onStack = new bool[G.GetVertices()]; edgeTo = new DirectedEdge[G.GetVertices()]; for (int v = 0; v < G.GetVertices(); v++) { if (!marked[v]) { Dfs(G, v); } } }
public CycleFinder(EdgeWeightedDigraph G) { marked = new bool[G.GetVertices()]; onStack = new bool[G.GetVertices()]; edgeTo = new int[G.GetVertices()]; for (int v = 0; v < G.GetVertices(); v++) { if (!marked[v] && cycle == null) { Dfs(G, v); } } }
public Topological(EdgeWeightedDigraph G) { Pre = new Queue <int>(); Post = new Queue <int>(); ReversePost = new Stack <int>(); marked = new bool[G.GetVertices()]; for (int v = 0; v < G.GetVertices(); v++) { if (!marked[v]) { Dfs(G, v); } } }
// relax vertex v and put other endpoints on queue if changed private void Relax(EdgeWeightedDigraph G, int v) { //for (DirectedEdge e : G.adj(v)) foreach (DirectedEdge e in G.GetAdj(v)) { int w = e.To(); if (distTo[w] > distTo[v] + e.GetWeight()) { distTo[w] = distTo[v] + e.GetWeight(); edgeTo[w] = e; if (!onQueue[w]) { queue.Enqueue(w); onQueue[w] = true; } } if (cost++ % G.GetVertices() == 0) { FindNegativeCycle(); if (IsNegativeCycle()) { return; // found a negative cycle } } } }
private IEnumerable <DirectedEdge> cycle; // negative cycle (or null if no such cycle) // by finding a cycle in predecessor graph public BellmanFordSP(EdgeWeightedDigraph G, int s) { distTo = new double[G.GetVertices()]; edgeTo = new DirectedEdge[G.GetVertices()]; onQueue = new bool[G.GetVertices()]; for (int v = 0; v < G.GetVertices(); v++) { distTo[v] = Double.PositiveInfinity; } distTo[s] = 0.0; // Bellman-Ford algorithm queue = new Queue <int>(); queue.Enqueue(s); onQueue[s] = true; while (queue.Count != 0 && !IsNegativeCycle()) { int v = queue.Dequeue(); onQueue[v] = false; Relax(G, v); } }
public AcyclicSP(EdgeWeightedDigraph G, int s) { distTo = new double[G.GetVertices()]; edgeTo = new DirectedEdge[G.GetVertices()]; for (int v = 0; v < G.GetVertices(); v++) { distTo[v] = Double.PositiveInfinity; } distTo[s] = 0.0; ValidateVertex(s); Topological topological = new Topological(G); if (!topological.IsOrder()) { throw new ArgumentException("Digraph is not acyclic."); } foreach (int v in topological.ReversePost) { foreach (DirectedEdge e in G.GetAdj(v)) { Relax(e); } } }