27 if ( startPointIdx < 0 || startPointIdx >= source->
vertexCount() )
33 QVector< double > *result =
nullptr;
40 result =
new QVector<double>();
44 result->insert( result->begin(), source->
vertexCount(), std::numeric_limits<double>::infinity() );
45 ( *result )[ startPointIdx ] = 0.0;
50 resultTree->insert( resultTree->begin(), source->
vertexCount(), -1 );
55 QMultiMap< double, int > not_begin;
56 QMultiMap< double, int >::iterator it;
58 not_begin.insert( 0.0, startPointIdx );
60 while ( !not_begin.empty() )
62 it = not_begin.begin();
63 double curCost = it.key();
64 int curVertex = it.value();
65 not_begin.erase( it );
69 for (
int edgeId : outgoingEdges )
72 double cost = arc.
cost( criterionNum ).toDouble() + curCost;
74 if ( cost < ( *result )[ arc.
toVertex()] )
79 ( *resultTree )[ arc.
toVertex()] = edgeId;
81 not_begin.insert( cost, arc.
toVertex() );
99 QVector<int> source2result( tree.size(), -1 );
102 source2result[ startVertexIdx ] = treeResult->
addVertex( source->
vertex( startVertexIdx ).
point() );
106 if ( tree[ i ] != -1 )
115 if ( tree[ i ] != -1 )
static void dijkstra(const QgsGraph *source, int startVertexIdx, int criterionNum, QVector< int > *resultTree=nullptr, QVector< double > *resultCost=nullptr)
Solve shortest path problem using Dijkstra algorithm.
static QgsGraph * shortestTree(const QgsGraph *source, int startVertexIdx, int criterionNum)
Returns shortest path tree with root-node in startVertexIdx.
This class implements a graph edge.
int fromVertex() const
Returns the index of the vertex at the start of this edge.
QVector< QVariant > strategies() const
Returns array of available strategies.
int toVertex() const
Returns the index of the vertex at the end of this edge.
QVariant cost(int strategyIndex) const
Returns edge cost calculated using specified strategy.
QgsGraphEdgeIds outgoingEdges() const
Returns outgoing edge ids, i.e.
QgsPointXY point() const
Returns point associated with graph vertex.
Mathematical graph representation.
const QgsGraphVertex & vertex(int idx) const
Returns vertex at given index.
int addEdge(int fromVertexIdx, int toVertexIdx, const QVector< QVariant > &strategies)
Add an edge to the graph, going from the fromVertexIdx to toVertexIdx.
const QgsGraphEdge & edge(int idx) const
Returns edge at given index.
int addVertex(const QgsPointXY &pt)
Add a vertex to the graph.
int vertexCount() const
Returns number of graph vertices.
QList< int > QgsGraphEdgeIds