It took me to find on the undirected unweighted graph the shortest paths from all to all. A little googling stumbled upon Dijkstra's algorithm. At the output of this algorithm, I get something like QMap<int, int> . The first int is the parent, the second is the heir in the shortest path.
For example, if you look for paths to the vertices starting from zero in this graph:
I will get this set:
0 - 0
ten
2 - 3
3 - 1
4 - 1
Now the question is how to effectively transform it in a way?
Now I do it like this:
//Поиск родителя QMap<int, int>::ConstIterator findParent(const QMap<int, int> &map, int child){ QMap<int, int>::ConstIterator parent = map.find(child); if(child == *parent){ return map.end(); } return parent; } //----------------------------------------------------------------------- //Построение маршрута QVector<int> route(const QMap<int, int> &map, int start){ QVector<int> route; route.append(start); forever{ QMap<int, int>::ConstIterator parent = findParent(map, route.last()); if(parent == map.end()){ break; } route.append(*parent); }; return route; } //----------------------------------------------------------------------- //Построение маршрутов QVector<QVector<int> > routes(const QMap<int, int> &map){ QVector<QVector<int> > routes; QMap<int, int>::ConstIterator begin = map.begin(); QMap<int, int>::ConstIterator end = map.end(); for(; begin != end; ++begin){ routes.append(route(map, begin.key())); } return routes; } It works and even correctly. But I am tormented by doubts, this is the most effective way. Many times I look for the same values in the map. Perhaps you can somehow build routes in a single pass through QMap<int, int> , or build routes right away when the algorithm runs?
Minimal example if suddenly someone needs
