private void Dfs(EdgeWeightedDigraph graph, int vertex) { onStack[vertex] = true; marked[vertex] = true; foreach (DirectedEdge edge in graph.Adj(vertex)) { int w = edge.DestinationVertex; if (Cycle != null) { return; } else if (!marked[w]) { edgeTo[w] = edge; Dfs(graph, w); } else if (onStack[w]) { Cycle = new Stack <DirectedEdge>(); DirectedEdge f = edge; while (f.StartVertex != w) { Cycle.Push(f); f = edgeTo[f.StartVertex]; } Cycle.Push(f); return; } } onStack[vertex] = false; }
private void Relax(EdgeWeightedDigraph graph, int vertex) { foreach (var edge in graph.Adj(vertex)) { int w = edge.DestinationVertex; if (distTo[w] > distTo[vertex] + edge.Weight) { distTo[w] = distTo[vertex] + edge.Weight; edgeTo[w] = edge; } } }
public EdgeWeightedDirectedCycle(EdgeWeightedDigraph graph) { marked = new bool[graph.Vertices]; onStack = new bool[graph.Vertices]; edgeTo = new DirectedEdge[graph.Vertices]; for (int v = 0; v < graph.Vertices; v++) { if (!marked[v]) { Dfs(graph, v); } } }
public EdgeWeightedCycleFinder(EdgeWeightedDigraph graph) { this.graph = graph; onStack = new bool[graph.Vertices]; edgeTo = new DirectedEdge[graph.Vertices]; marked = new bool[graph.Vertices]; for (int i = 0; i < graph.Vertices; ++i) { if (!marked[i] && !HasCycle()) { Dfs(i); } } }
private void Relax(EdgeWeightedDigraph graph, int vertex) { foreach (var edge in graph.Adj(vertex)) { int w = edge.DestinationVertex; if (!(DistTo[w] > DistTo[vertex] + edge.Weight)) { continue; } DistTo[w] = DistTo[vertex] + edge.Weight; EdgeTo[w] = edge; } }
private void FindNegativeCycle() { int vertex = EdgeTo.Length; var spt = new EdgeWeightedDigraph(vertex); for (int v = 0; v < vertex; v++) { if (EdgeTo[v] != null) { spt.AddEdge(EdgeTo[v]); } } var cf = new EdgeWeightedCycleFinder(spt); cycle = cf.Cycle(); }
public AcyclicSP(EdgeWeightedDigraph graph, int startVertex) { edgeTo = new DirectedEdge[graph.Vertices]; distTo = new double[graph.Vertices]; for (int v = 0; v < graph.Vertices; v++) { distTo[v] = double.MaxValue; } distTo[startVertex] = 0.0f; var topological = new Topological(graph); foreach (var v in topological.Order()) { Relax(graph, v); } }
public DijkstraSP(EdgeWeightedDigraph graph, int startVertex) { edgeTo = new DirectedEdge[graph.Vertices]; distTo = new double[graph.Vertices]; priorityQueue = new IndexMinPQ <double>(graph.Vertices); for (int v = 0; v < graph.Vertices; v++) { distTo[v] = double.MaxValue; } distTo[startVertex] = 0.0f; priorityQueue.Insert(startVertex, 0.0f); while (!priorityQueue.IsEmpty()) { Relax(graph, priorityQueue.DelMin()); } }
private void Relax(EdgeWeightedDigraph graph, int vertex) { foreach (var edge in graph.Adj(vertex)) { int w = edge.DestinationVertex; if (distTo[w] > distTo[vertex] + edge.Weight) { distTo[w] = distTo[vertex] + edge.Weight; edgeTo[w] = edge; if (priorityQueue.Contains(w)) { priorityQueue.Change(w, distTo[w]); } else { priorityQueue.Insert(w, distTo[w]); } } } }
private void Relax(EdgeWeightedDigraph graph, int vertex) { foreach (var edge in graph.Adj(vertex)) { int w = edge.DestinationVertex; if (DistTo[w] > DistTo[vertex] + edge.Weight) { DistTo[w] = DistTo[vertex] + edge.Weight; EdgeTo[w] = edge; if (!onQueue[w]) { queue.Enqueue(w); onQueue[w] = true; } } if (cost++ % graph.Vertices == 0) { FindNegativeCycle(); } } }
public BellmanFordSp(EdgeWeightedDigraph graph, int startVertex) { DistTo = new double[graph.Vertices]; EdgeTo = new DirectedEdge[graph.Vertices]; onQueue = new bool[graph.Vertices]; queue = new Queue <int>(); for (int v = 0; v < graph.Vertices; v++) { DistTo[v] = double.MaxValue; } DistTo[startVertex] = 0.0f; queue.Enqueue(startVertex); onQueue[startVertex] = true; while (!queue.Any() && HasNegativeCycle()) { int v = queue.Dequeue(); onQueue[v] = false; Relax(graph, v); } }
private void Relax(EdgeWeightedDigraph graph, int vertex) { foreach (var edge in graph.Adj(vertex)) { int w = edge.DestinationVertex; if (!(DistTo[w] > DistTo[vertex] + edge.Weight)) { continue; } DistTo[w] = DistTo[vertex] + edge.Weight; EdgeTo[w] = edge; if (priorityQueue.Contains(w)) { priorityQueue.ChangeKey(w, DistTo[w]); } else { priorityQueue.Insert(w, DistTo[w]); } } }