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