public DijkstraSP(EdgeWeightedDigraph g, int s) { foreach (DirectedEdge e in g.Edges()) { if (e.Weight() < 0) { throw new ArgumentOutOfRangeException("graph edge must have nonegative weights"); } } distTo = new double[g.V]; edgeTo = new DirectedEdge[g.V]; for (int v = 0; v < g.V; v++) { distTo[v] = double.PositiveInfinity; } distTo[s] = 0; //relax vertices in order of distance from s pq = new IndexMinPQ <double>(g.V); pq.Insert(s, distTo[s]); while (!pq.IsEmpty()) { int v = pq.DelMin(); foreach (DirectedEdge e in g.Adj(v)) { relax(e); } } }
public LazyPrimMinSpanningTree(EdgeWeightedGraph g) { pq = new IndexMinPQ <Edge>(g.EdgeCount); marked = new bool[g.VertexCount]; mst = new Queue <Edge>(); Visit(g, 0); while (!pq.IsEmpty()) { Edge e = pq.DelMin(); int v1 = e.CurrentValue; int v2 = e.Other(v1); if (marked[v1] && marked[v2]) { continue; } mst.Enqueue(e); if (marked[v1]) { Visit(g, v1); } if (!marked[v2]) { Visit(g, v2); } } }
private readonly IndexMinPQ<Double> _pq; // priority queue of vertices #endregion Fields #region Constructors /// <summary> /// Computes a shortest-paths tree from the source vertex <tt>s</tt> to every /// other vertex in the edge-weighted graph <tt>G</tt>. /// </summary> /// <param name="g">g the edge-weighted graph</param> /// <param name="s">s the source vertex</param> /// <exception cref="ArgumentException">if an edge weight is negative</exception> /// <exception cref="ArgumentException">unless 0 <= <tt>s</tt> <= <tt>V</tt> - 1</exception> public DijkstraUndirectedSP(EdgeWeightedGraph g, int s) { foreach (var e in g.Edges()) { if (e.Weight < 0) throw new ArgumentException($"edge {e} has negative weight"); } _distTo = new double[g.V]; _edgeTo = new EdgeW[g.V]; for (var v = 0; v < g.V; v++) _distTo[v] = double.PositiveInfinity; _distTo[s] = 0.0; // relax vertices in order of distance from s _pq = new IndexMinPQ<Double>(g.V); _pq.Insert(s, _distTo[s]); while (!_pq.IsEmpty()) { var v = _pq.DelMin(); foreach (var e in g.Adj(v)) Relax(e, v); } // check optimality conditions //assert check(G, s); }
/// <summary> /// Dijkstra algorithm (shortest path) based on graph g for start vertice s. Positive cycles are allowed in shortest path algorithm /// </summary> /// <param name="g">Graph for search</param> /// <param name="s">Start vertice</param> public static PathStats ShortestPath(GraphBase g, int s) { var ps = new PathStats(g.V); for (var i = 0; i < ps.Dist.Length; i++) { ps.Dist[i] = int.MaxValue; ps.Prev[i] = -1; } ps.Dist[s] = 0;//start vertice var pq = new IndexMinPQ<Distance>(ps.Dist.Length); for (int i = 0; i < ps.Dist.Length; i++) { pq.Insert(i, new Distance { V = i, Dist = ps.Dist[i] }); } while (!pq.IsEmpty()) { var v = pq.DelRoot(); foreach (var e in g.Adjacent(v)) { if (ps.Dist[e.V2] > ps.Dist[v] + e.Weight) { ps.Dist[e.V2] = ps.Dist[v] + e.Weight; ps.Prev[e.V2] = v; pq.ChangeKey(e.V2, new Distance { V = e.V2, Dist = ps.Dist[e.V2] }); } } } return ps; }
private readonly IndexMinPQ <Double> _pq; // priority queue of vertices /// <summary> /// Computes a shortest-paths tree from the source vertex <tt>s</tt> to every other /// vertex in the edge-weighted digraph <tt>G</tt>. /// </summary> /// <param name="g">g the edge-weighted digraph</param> /// <param name="s">s the source vertex</param> /// <exception cref="ArgumentException">if an edge weight is negative</exception> /// <exception cref="ArgumentException">unless 0 <= <tt>s</tt> <= <tt>V</tt> - 1</exception> public DijkstraSP(EdgeWeightedDigraph g, int s) { foreach (var e in g.Edges()) { if (e.Weight < 0) { throw new ArgumentException($"edge {e} has negative weight"); } } _distTo = new double[g.V]; _edgeTo = new DirectedEdge[g.V]; for (var v = 0; v < g.V; v++) { _distTo[v] = double.PositiveInfinity; } _distTo[s] = 0.0; // relax vertices in order of distance from s _pq = new IndexMinPQ <Double>(g.V); _pq.Insert(s, _distTo[s]); while (!_pq.IsEmpty()) { var v = _pq.DelMin(); foreach (var e in g.Adj(v)) { Relax(e); } } // check optimality conditions //assert check(G, s); }
private void prim(EdgeWeightedGraph g, int s) { distTo[s] = 0.0; pq.Insert(s, distTo[s]); while (!pq.IsEmpty()) { int v = pq.DelMin(); scan(g, v); } }
/// <summary> /// run Prim's algorithm in graph G, starting from vertex s /// </summary> /// <param name="g"></param> /// <param name="s"></param> private void Prim(EdgeWeightedGraph g, int s) { _distTo[s] = 0.0; _pq.Insert(s, _distTo[s]); while (!_pq.IsEmpty()) { var v = _pq.DelMin(); Scan(g, v); } }
public DifkstraSP(EdgeWeightedDigraph g, int s) { _edgeTo = new DirectedEdge[g.V]; _distTo = Enumerable.Repeat(double.PositiveInfinity, g.V).ToArray(); _pq = new IndexMinPQ <double>(g.V); _distTo[s] = 0.0; _pq.Insert(s, 0.0); while (!_pq.IsEmpty()) { Relax(g, _pq.DelMin()); } }
public DijkstraAlgorithm(EdgeWeightedDigraph digraph, int source) : base(digraph, source) { //pq maintain a set of vertex need to deal with. IndexMinPQ <double> pq = new IndexMinPQ <double>(digraph.V); pq.Insert(source, distTo[source]); while (!pq.IsEmpty()) { //when v pops up, the distance and path to v have been confirmed int v = pq.DelMin(); foreach (DirectedEdge e in digraph.Adj(v)) { Relax(pq, e); } } }
public PrimMinSpanningTree(EdgeWeightedGraph g) { edgeTo = new Edge[g.VertexCount]; distanceTo = new double[g.VertexCount]; marked = new bool[g.VertexCount]; pq = new IndexMinPQ <double>(g.VertexCount); for (int i = 0; i < g.VertexCount; i++) { distanceTo[i] = double.MaxValue; } distanceTo[0] = 0; pq.Insert(0, 0); while (!pq.IsEmpty()) { Visit(g, pq.DeleteMin()); } }
private readonly decimal[] _distTo; // distTo[v] = distance of shortest s->v path #endregion Fields #region Constructors /** * Computes a shortest paths tree from <tt>s</tt> to every other vertex in * the edge-weighted digraph <tt>G</tt>. * @param G the edge-weighted digraph * @param s the source vertex * @throws IllegalArgumentException if an edge weight is negative * @throws IllegalArgumentException unless 0 ≤ <tt>s</tt> ≤ <tt>V</tt> - 1 */ public ShortestNeuronPath(NeuronGraph G, int s) { _distTo = new decimal[G.V()]; edgeTo = new DirectedEdge[G.V()]; for (int v = 0; v < G.V(); v++) _distTo[v] = decimal.MaxValue; _distTo[s] = 0.0M; // relax vertices in order of distance from s pq = new IndexMinPQ<decimal>(G.V()); pq.Insert(s, _distTo[s]); while (!pq.IsEmpty()) { int v = pq.DelMin(); foreach (DirectedEdge e in G.adj(v)) relax(e); } }
void IndexMinPQTest() { var strArr = FileHandler.ReadFileAsStrArr("words3.txt"); strArr.Show(); var pq = new IndexMinPQ <string>(strArr.Length); for (int i = 0; i < strArr.Length; i++) { pq.Insert(i, strArr[i]); } while (!pq.IsEmpty()) { Console.WriteLine(pq.DelMin()); } Console.WriteLine(); Console.ReadKey(); }
public DijkstraShortestPath(EdgeWeightedDirectedGraph graph, int start) { EdgeTo = new DirectedEdge[graph.VertexCount]; DistTo = new double[graph.VertexCount]; pq = new IndexMinPQ <double>(graph.VertexCount); for (int i = 0; i < graph.VertexCount; i++) { DistTo[i] = double.PositiveInfinity; DistTo[start] = 0; pq.Insert(start, 0); while (!pq.IsEmpty()) { Relax(graph, pq.DeleteMin()); } } }
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()); } }
public KruskalMinSpanningTree(EdgeWeightedGraph g) { mst = new Queue <Edge>(); IndexMinPQ <Edge> pq = new IndexMinPQ <Edge>(g.EdgeCount); UnionFind_1 uf = new UnionFind_1(g.VertexCount); while (!pq.IsEmpty() && mst.Count < g.VertexCount - 1) { Edge e = pq.DelMin(); int v1 = e.V1; int v2 = e.Other(v1); if (uf.Connected(v1, v2)) { continue; } uf.Union(v1, v2); mst.Enqueue(e); } }
public void DifkstraSPMy(EdgeWeightedDigraph g, int s) { _edgeTo = new DirectedEdge[g.V]; _distTo = Enumerable.Repeat(double.PositiveInfinity, g.V).ToArray(); _distTo[s] = 0.0; _pq = new IndexMinPQ <double>(g.V); foreach (var e in g.Adj(s)) { _edgeTo[e.To] = e; _distTo[e.To] = e.Weight; _pq.Insert(e.To, e.Weight); } while (!_pq.IsEmpty()) { var i = _pq.DelMin(); foreach (var e in g.Adj(i)) { RelaxMy(i, e); } } }
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); } }
public PrimMst(EdgeWeightedGraph graph) { edgeTo = new Edge[graph.Vertices]; distTo = new double[graph.Vertices]; marked = new bool[graph.Vertices]; for (int vertex = 0; vertex < graph.Vertices; vertex++) { distTo[vertex] = double.MaxValue; } prioryQueue = new IndexMinPQ <double>(graph.Vertices); distTo[0] = 0.0f; prioryQueue.Insert(0, 0.0f); while (!prioryQueue.IsEmpty()) { Visit(graph, prioryQueue.DelMin()); } }
//ctor inits DSs, builds shortest path tree and computes distances //takes a built DiGraph and a source vertex public DijkstrasShortestPath(EdgeWeightedDiGraph g, int s) { _pq = new IndexMinPQ <double>(g.V); _edgeTo = new DirectedEdge[g.V]; _distTo = new double[g.V]; //load initial values for (int i = 1; i < g.V; i++) { _distTo[i] = Double.PositiveInfinity; } _distTo[s] = 0.0; _edgeTo[s] = null; //loads a starting value onto PQ to start process _pq.Insert(s, _distTo[s]); //start business logic //start cycle of deleting min edges and checking adj edges for 'eligability' while (!_pq.IsEmpty()) { var minV = _pq.DelMin(); Relax(g, minV); } }