File: DijkstraShortestPathBase.h

package info (click to toggle)
polymake 4.14-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 35,888 kB
  • sloc: cpp: 168,933; perl: 43,407; javascript: 31,575; ansic: 3,007; java: 2,654; python: 632; sh: 268; xml: 117; makefile: 61
file content (270 lines) | stat: -rw-r--r-- 8,573 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/* Copyright (c) 1997-2024
   Ewgenij Gawrilow, Michael Joswig, and the polymake team
   Technische Universität Berlin, Germany
   https://polymake.org

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version: http://www.gnu.org/licenses/gpl.txt.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
--------------------------------------------------------------------------------
*/

#pragma once

#include "polymake/Graph.h"
#include "polymake/Heap.h"
#include "polymake/internal/chunk_allocator.h"
#include <cassert>

namespace polymake {
namespace graph {

//! Result of comparison of two labels
enum class DijkstraLabelCmp {
   //! new label is better in all criteria, old label and all its siblings must be discarded
   discard_old_and_siblings,
   //! old label is better in all criteria, new label must be discarded
   discard_new,
   //! labels are not comparable, both must be propagated further
   propagate_both,
   //! new label is better in the sense of priority queue order,
   //! old label becomes its sibling
   replace_old,
   //! new label is better in the sense of priority queue order,
   //! old label must be discarded but its siblings survive
   discard_old_keep_siblings,
   //! old label is better in the sense of priority queue order,
   //! new label becomes its sibling
   keep_new_as_sibling,
   //! old label is better in the sense of priority queue order but some secondary criteria were improved,
   //! new label becomes its sibling
   keep_new_update_old
};

class DijkstraShortestPathBase {
public:
   template <typename FullLabel>
   struct Label {
      explicit Label(Int node_arg)
         : node(node_arg) {}

      void set_predecessor(FullLabel* pred)
      {
         assert(pred && !predecessor);
         predecessor = pred;
         ++pred->refc;
      }

      //! predecessor on the shortest path from the source, nullptr for start node
      FullLabel* predecessor = nullptr;
      //! Graph node index
      Int node;
      //! number of successor labels plus 1 for the node or sibling list
      Int refc = 0;
      //! position in the priority queue or -1 for already processed labels
      Int heap_pos = -1;
   };

   struct LabelCostComparator : public operations::cmp {
      template <typename FullLabel>
      pm::cmp_value operator() (const FullLabel& l1, const FullLabel& l2) const
      {
         return operations::cmp::operator()(l1.get_min_cost(), l2.get_min_cost());
      }
   };

   template <typename Top>
   class Data {
   public:
      using top_t = Top;
      using graph_t = typename Top::graph_t;
      using label_t = typename Top::label_t;
      using labels_on_node_dict = NodeMap<typename graph_t::dir, label_t*>;
      using label_comparator_t = typename Top::label_comparator_t;

      class HeapPolicy {
      public:
         using value_type = label_t*;

         static Int position(const label_t* label)
         {
            return label->heap_pos;
         }
         static void update_position(label_t* label, Int old_pos, Int new_pos)
         {
            label->heap_pos=new_pos;
         }
         const label_t& key(const label_t* label) const
         {
            return *label;
         }
         label_comparator_t key_comparator() const
         {
            return label_comparator_t{};
         }
      };

      using prio_queue_t = Heap<HeapPolicy>;

      explicit Data(const graph_t& G_arg)
         : G(G_arg)
         , labels_on_node(G, nullptr)
         , label_alloc(sizeof(label_t)) {}

      //! remove all labels
      void clear()
      {
         fill_range(entire_range(labels_on_node), nullptr);
         heap.clear();
         label_alloc.clear();
      }

      template <typename... Args, typename = std::enable_if_t<std::is_constructible<label_t, Int, Args...>::value>>
      label_t* construct_label(Int node, Args&&... args)
      {
         return new(label_alloc.allocate()) label_t(node, std::forward<Args>(args)...);
      }

      void reclaim_label(label_t* label)
      {
         label->~label_t();
         label_alloc.reclaim(label);
      }

      const graph_t& G;
      labels_on_node_dict labels_on_node;
      prio_queue_t heap;
      pm::chunk_allocator label_alloc;
   };

   template <typename Top>
   class Algo {
   public:
      using top_t = Top;
      using graph_t = typename Top::graph_t;
      using label_t = typename Top::label_t;
      using data_t = typename Top::template Data<Top>;
      using algo_top = typename Top::template Algo<Top>;

      explicit Algo(data_t& data_arg)
         : data(data_arg) {}

      const algo_top& top() const { return static_cast<const algo_top&>(*this); }

      template <typename Predicate>
      const label_t* solve(Int source, const Predicate& target, bool backward = false) const
      {
         if (backward && graph_t::dir::value)
            throw std::runtime_error("backward search is only defined for directed graphs");
         start_search(source);
         return do_search(target, backward);
      }

      bool process_popped(const label_t* label, bool backward) const
      {
         return true;
      }

      template <typename... Args, typename = std::enable_if_t<std::is_constructible<label_t, Int, Args...>::value>>
      label_t* construct_label(Args&&... args) const
      {
         return data.construct_label(std::forward<Args>(args)...);
      }

   protected:
      void start_search(Int node) const
      {
         label_t* const label = top().construct_label(node);
         data.labels_on_node[node] = label;
         label->refc = 1;
         data.heap.push(label);
      }

      template <typename Predicate>
      const label_t* do_search(const Predicate& target, bool backward) const
      {
         while (!data.heap.empty()) {
            label_t* const pred_label = data.heap.pop();
            if (top().process_popped(pred_label, backward)) {
               if (target(*pred_label))
                  return pred_label;
            }

            if (backward) {
               for (auto edge_it = entire(data.G.in_edges(pred_label->node)); !edge_it.at_end(); ++edge_it)
                  top().propagate(pred_label, edge_it.from_node(), *edge_it);
            } else {
               for (auto edge_it = entire(data.G.out_edges(pred_label->node)); !edge_it.at_end(); ++edge_it)
                  top().propagate(pred_label, edge_it.to_node(), *edge_it);
            }
         }
         return nullptr;
      }

      void propagate(label_t* const pred_label, const Int cur_node, const Int cur_edge_id) const
      {
         label_t* cur_label = data.labels_on_node[cur_node];
         if (cur_label && cur_label->heap_pos < 0) {
            // node already settled
            return;
         }
         label_t* new_label = top().construct_label(pred_label, cur_node, cur_edge_id);

         if (cur_label) {
            switch (top().compare_labels(new_label, cur_label)) {
            case DijkstraLabelCmp::discard_new:
               data.reclaim_label(new_label);
               return;
            case DijkstraLabelCmp::discard_old_and_siblings:
               top().drop_label(cur_label);
               top().erase_label(cur_label);
               break;
            default:
               throw std::runtime_error("DijkstraShortestPathBase: unexpected label comparison value");
            }
         }
         top().push_new_label(new_label, pred_label);
      }

      void push_new_label(label_t* new_label, label_t* pred_label) const
      {
         new_label->set_predecessor(pred_label);
         new_label->refc = 1;
         data.labels_on_node[new_label->node] = new_label;
         data.heap.push(new_label);
      }

      void drop_label(label_t* label) const
      {
         if (label->heap_pos >= 0)
            data.heap.erase_at(label->heap_pos);
      }

      void erase_label(label_t* label) const
      {
         if (--label->refc==0) {
            if (label->predecessor) {
               --label->predecessor->refc;
            }
            data.reclaim_label(label);
         }
      }

      data_t& data;
   };
};

} }


// Local Variables:
// mode:C++
// c-basic-offset:3
// indent-tabs-mode:nil
// End: