17 #ifndef GZ_MATH_GRAPH_GRAPHALGORITHMS_HH_
18 #define GZ_MATH_GRAPH_GRAPHALGORITHMS_HH_
24 #include <unordered_set>
28 #include <gz/math/config.hh>
35 inline namespace GZ_MATH_VERSION_NAMESPACE {
50 template<
typename V,
typename E,
typename EdgeType>
65 while (!pending.
empty())
74 if (seen.
insert(next).second)
88 template<
typename V,
typename E,
typename EdgeType>
102 while (!pending.
empty())
107 if (!seen.
insert(u).second)
114 if (!seen.
count(next))
184 template<
typename V,
typename E,
typename EdgeType>
189 auto allVertices = _graph.
Vertices();
192 if (allVertices.find(_from) == allVertices.end())
200 allVertices.find(_to) == allVertices.end())
213 for (
auto const &v : allVertices)
226 const double poppedCost = pq.top().first;
230 if (_to !=
kNullId && _to == u)
238 if (poppedCost > dist[u].first)
243 const auto &edge = edgePair.second.get();
244 const auto &v = edge.From(u);
245 double weight = edge.Weight();
248 if (dist[v].first > dist[u].first + weight)
268 template<
typename V,
typename E>
273 unsigned int componentCount = 0;
275 for (
auto const &v : _graph.
Vertices())
277 if (visited.
find(v.first) == visited.
end())
280 for (
auto const &vId : component)
281 visited[vId] = componentCount;
289 for (
auto const &vPair : _graph.
Vertices())
291 const auto &v = vPair.second.get();
292 const auto &componentId = visited[v.Id()];
293 res[componentId].AddVertex(v.Name(), v.Data(), v.Id());
297 for (
auto const &ePair : _graph.
Edges())
299 const auto &e = ePair.second.get();
300 const auto &vertices = e.Vertices();
301 const auto &componentId = visited[vertices.first];
302 res[componentId].AddEdge(vertices, e.Data(), e.Weight());
313 template<
typename V,
typename E>
320 for (
auto const &vPair : _graph.
Vertices())
327 for (
auto const &ePair : _graph.
Edges())
329 auto const &e = ePair.second.get();
330 edges.
push_back({e.Vertices(), e.Data(), e.Weight()});
358 template<
typename V,
typename E,
typename EdgeType>
364 return {chain,
false};
373 return {chain,
true};
374 const VertexId next = parents.begin()->first;
376 if (!seen.
insert(next).second)
377 return {chain,
false};
391 template<
typename V,
typename E,
typename EdgeType>
397 if (_ancestor == _descendant)
413 const VertexId next = parents.begin()->first;
414 if (next == _ancestor)
417 if (!seen.
insert(next).second)
437 template<
typename V,
typename E,
typename EdgeType>
452 for (
auto v :
Ancestors(_graph, _a).first)
455 if (ancestorsA.
count(_b))
457 for (
auto v :
Ancestors(_graph, _b).first)
459 if (ancestorsA.
count(v))
478 template<
typename V,
typename E,
typename EdgeType>
490 for (
auto id : descendants)
493 out.
AddVertex(v.Name(), v.Data(), v.Id());
496 for (
auto const &ePair : _graph.
Edges())
498 auto const &e = ePair.second.get();
499 auto vs = e.Vertices();
500 if (set.
count(vs.first) && set.
count(vs.second))
501 out.
AddEdge(vs, e.Data(), e.Weight());
513 template<
typename V,
typename E,
typename EdgeType>
522 pending.
push(_vertex);
524 while (!pending.
empty())
530 if (out.
insert(adj.first).second)
531 pending.
push(adj.first);