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 )
QVector< QVariant > strategies() const
Returns array of available strategies.
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.
const QgsGraphEdge & edge(int idx) const
Returns edge at given index.
int toVertex() const
Returns the index of the vertex at the end of this edge.
static QgsGraph * shortestTree(const QgsGraph *source, int startVertexIdx, int criterionNum)
Returns shortest path tree with root-node in startVertexIdx.
Mathematical graph representation.
int fromVertex() const
Returns the index of the vertex at the start of this edge.
int addEdge(int fromVertexIdx, int toVertexIdx, const QVector< QVariant > &strategies)
Add an edge to the graph, going from the fromVertexIdx to toVertexIdx.
int addVertex(const QgsPointXY &pt)
Add a vertex to the graph.
QList< int > QgsGraphEdgeIds
const QgsGraphVertex & vertex(int idx) const
Returns vertex at given index.
int vertexCount() const
Returns number of graph vertices.
This class implements a graph edge.
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.