Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
mat_path.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
82#ifndef MAT_PATH_H
83#define MAT_PATH_H
84
85#include <concepts>
86#include <functional>
87#include <utility>
88#include <tpl_matgraph.H>
89
90namespace Aleph
91{
92
93namespace mat_path_detail
94{
97 template <class GT, class Compare>
98 concept Arc_With_Distance = requires (typename GT::Arc * a, Compare cmp)
99 {
100 { cmp(a->get_info().get_distance(), a->get_info().get_distance()) }
101 -> std::convertible_to<bool>;
102 };
103
104 // Appends node to path through the lightest, by Compare, of the arcs
105 // from the last node of path to node, if the arcs give their weights;
106 // otherwise through the first one, as Path::append(Node *) does
107 template <class GT, class Compare>
108 void append_node(GT & g, Path<GT> & path, typename GT::Node * node)
109 {
110 if constexpr (Arc_With_Distance<GT, Compare>)
111 {
112 typename GT::Node * src = path.get_last_node();
113 typename GT::Node * tgt = node;
114
115 // In a graph that is not directed, the arcs of the node with fewer
116 // arcs are searched, as search_arc() does
117 if (not g.is_digraph() and tgt->num_arcs < src->num_arcs)
118 std::swap(src, tgt);
119
120 typename GT::Arc * lightest = nullptr;
121 for (typename GT::Node_Arc_Iterator it(src); it.has_curr(); it.next_ne())
122 if (it.get_tgt_node_ne() == tgt and
123 (lightest == nullptr or
124 Compare () (it.get_current_arc_ne()->get_info().get_distance(),
125 lightest->get_info().get_distance())))
126 lightest = it.get_current_arc_ne();
127
128 ah_invalid_argument_if(lightest == nullptr)
129 << "find_min_path(): there is no arc between two nodes of the path";
130
131 path.append(lightest);
132 }
133 else
134 path.append(node);
135 }
136} // namespace mat_path_detail
137
190template <class Mat, class Compare = std::less<>>
191void find_min_path(Mat & p, long src_idx, long tgt_idx,
193{
194 using GT = typename Mat::Graph_Type;
195 GT & g = p.get_list_graph();
196
197 // Handle trivial case: source equals target
198 if (src_idx == tgt_idx)
199 {
200 path.set_graph(g, p(src_idx));
201 return;
202 }
203
204 // Initialize path with source node
205 typename GT::Node * src = p(src_idx);
206 path.set_graph(g, src);
207
208 // Follow path matrix to reconstruct the path
209 for (long i = src_idx, j = p(i, tgt_idx); ; i = j, j = p(i, tgt_idx))
210 {
211 mat_path_detail::append_node<GT, Compare>(g, path, p(j));
212 if (j == tgt_idx)
213 break;
214 }
215}
216
227template <class Mat, class Compare = std::less<>>
228void find_min_path(Mat & p,
229 typename Mat::Node * src_node,
230 typename Mat::Node * tgt_node,
232{
233 find_min_path<Mat, Compare>(p, p(src_node), p(tgt_node), path);
234}
235
254template <class Mat, class Compare = std::less<>>
256{
257public:
261 void operator()(Mat & p,
262 typename Mat::Node * src_node,
263 typename Mat::Node * tgt_node,
265 {
266 find_min_path<Mat, Compare>(p, src_node, tgt_node, path);
267 }
268
272 void operator()(Mat & p,
273 long src_index, long tgt_index,
275 {
276 find_min_path<Mat, Compare>(p, src_index, tgt_index, path);
277 }
278};
279
280} // namespace Aleph
281
282// Global namespace compatibility
285
286#endif // MAT_PATH_H
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Definition ah-errors.H:787
Functor wrapper for find_min_path.
Definition mat_path.H:256
void operator()(Mat &p, typename Mat::Node *src_node, typename Mat::Node *tgt_node, Path< typename Mat::Graph_Type > &path) const
Reconstruct path using node pointers.
Definition mat_path.H:261
void operator()(Mat &p, long src_index, long tgt_index, Path< typename Mat::Graph_Type > &path) const
Reconstruct path using matrix indices.
Definition mat_path.H:272
Path on a graph.
Definition tpl_graph.H:3832
void set_graph(const GT &__g, Node *start_node=nullptr)
Set the graph of the path.
Definition tpl_graph.H:3977
void append(Arc *arc)
Append an arc to the path.
Definition tpl_graph.H:4052
Node * get_last_node() const
Return the last node of path; throws overflow_error if path is empty.
Definition tpl_graph.H:4323
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Definition graph-dry.H:854
size_t num_arcs
data associated to the node. Access it with get_info()
Definition graph-dry.H:713
bool is_digraph() const noexcept
Return true if the graph this is directed.
Definition graph-dry.H:937
Whether the arcs of GT give their weights by get_info().get_distance(), and Compare compares them.
Definition mat_path.H:98
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4196
void find_min_path(Mat &p, long src_idx, long tgt_idx, Path< typename Mat::Graph_Type > &path)
Build a minimum path from Floyd-Warshall path matrix using indices.
Definition mat_path.H:191
void append_node(GT &g, Path< GT > &path, typename GT::Node *node)
Definition mat_path.H:108
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
Adjacency matrix representations for graphs.