您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

146 行
4.0KB

  1. /* Template class for Dijkstra's algorithm on directed graphs.
  2. Copyright (C) 2019-2020 Free Software Foundation, Inc.
  3. Contributed by David Malcolm <dmalcolm@redhat.com>.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GCC is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #ifndef GCC_SHORTEST_PATHS_H
  17. #define GCC_SHORTEST_PATHS_H
  18. #include "timevar.h"
  19. /* A record of the shortest path to each node in an graph
  20. from the origin node.
  21. The constructor runs Dijkstra's algorithm, and the results are
  22. stored in this class. */
  23. template <typename GraphTraits, typename Path_t>
  24. class shortest_paths
  25. {
  26. public:
  27. typedef typename GraphTraits::graph_t graph_t;
  28. typedef typename GraphTraits::node_t node_t;
  29. typedef typename GraphTraits::edge_t edge_t;
  30. typedef Path_t path_t;
  31. shortest_paths (const graph_t &graph, const node_t *origin);
  32. path_t get_shortest_path (const node_t *to) const;
  33. private:
  34. const graph_t &m_graph;
  35. /* For each node (by index), the minimal distance to that node from the
  36. origin. */
  37. auto_vec<int> m_dist;
  38. /* For each exploded_node (by index), the previous edge in the shortest
  39. path from the origin. */
  40. auto_vec<const edge_t *> m_prev;
  41. };
  42. /* shortest_paths's constructor.
  43. Use Dijkstra's algorithm relative to ORIGIN to populate m_dist and
  44. m_prev with enough information to be able to generate Path_t instances
  45. to give the shortest path to any node in GRAPH from ORIGIN. */
  46. template <typename GraphTraits, typename Path_t>
  47. inline
  48. shortest_paths<GraphTraits, Path_t>::shortest_paths (const graph_t &graph,
  49. const node_t *origin)
  50. : m_graph (graph),
  51. m_dist (graph.m_nodes.length ()),
  52. m_prev (graph.m_nodes.length ())
  53. {
  54. auto_timevar tv (TV_ANALYZER_SHORTEST_PATHS);
  55. auto_vec<int> queue (graph.m_nodes.length ());
  56. for (unsigned i = 0; i < graph.m_nodes.length (); i++)
  57. {
  58. m_dist.quick_push (INT_MAX);
  59. m_prev.quick_push (NULL);
  60. queue.quick_push (i);
  61. }
  62. m_dist[origin->m_index] = 0;
  63. while (queue.length () > 0)
  64. {
  65. /* Get minimal distance in queue.
  66. FIXME: this is O(N^2); replace with a priority queue. */
  67. int idx_with_min_dist = -1;
  68. int idx_in_queue_with_min_dist = -1;
  69. int min_dist = INT_MAX;
  70. for (unsigned i = 0; i < queue.length (); i++)
  71. {
  72. int idx = queue[i];
  73. if (m_dist[queue[i]] < min_dist)
  74. {
  75. min_dist = m_dist[idx];
  76. idx_with_min_dist = idx;
  77. idx_in_queue_with_min_dist = i;
  78. }
  79. }
  80. gcc_assert (idx_with_min_dist != -1);
  81. gcc_assert (idx_in_queue_with_min_dist != -1);
  82. // FIXME: this is confusing: there are two indices here
  83. queue.unordered_remove (idx_in_queue_with_min_dist);
  84. node_t *n
  85. = static_cast <node_t *> (m_graph.m_nodes[idx_with_min_dist]);
  86. int i;
  87. edge_t *succ;
  88. FOR_EACH_VEC_ELT (n->m_succs, i, succ)
  89. {
  90. // TODO: only for dest still in queue
  91. node_t *dest = succ->m_dest;
  92. int alt = m_dist[n->m_index] + 1;
  93. if (alt < m_dist[dest->m_index])
  94. {
  95. m_dist[dest->m_index] = alt;
  96. m_prev[dest->m_index] = succ;
  97. }
  98. }
  99. }
  100. }
  101. /* Generate an Path_t instance giving the shortest path to the node
  102. TO from the origin node. */
  103. template <typename GraphTraits, typename Path_t>
  104. inline Path_t
  105. shortest_paths<GraphTraits, Path_t>::get_shortest_path (const node_t *to) const
  106. {
  107. Path_t result;
  108. while (m_prev[to->m_index])
  109. {
  110. result.m_edges.safe_push (m_prev[to->m_index]);
  111. to = m_prev[to->m_index]->m_src;
  112. }
  113. result.m_edges.reverse ();
  114. return result;
  115. }
  116. #endif /* GCC_SHORTEST_PATHS_H */