Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
k_shortest_paths_example.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
31
43# include <format>
44# include <iostream>
45# include <string>
46
47# include <K_Shortest_Paths.H>
48# include <tpl_array.H>
49# include <tpl_graph.H>
50
51using namespace std;
52using namespace Aleph;
53
54namespace
55{
58
59 struct Edge
60 {
61 size_t u = 0;
62 size_t v = 0;
63 long long w = 0;
64 };
65
66 struct Scenario
67 {
68 size_t num_nodes = 0;
69 Array<Edge> edges;
70 size_t source = 0;
71 size_t target = 0;
72 size_t k = 5;
73 };
74
75
85 Graph build_graph(const Scenario & s, Array<Graph::Node *> & nodes)
86 {
87 Graph g;
88 nodes.reserve(s.num_nodes);
89 for (size_t i = 0; i < s.num_nodes; ++i)
90 nodes.append(g.insert_node(static_cast<int>(i)));
91
92 for (typename Array<Edge>::Iterator it(s.edges); it.has_curr(); it.next_ne())
93 {
94 const Edge & e = it.get_curr();
95 g.insert_arc(nodes[e.u], nodes[e.v], e.w);
96 }
97
98 return g;
99 }
100
101
110 string path_to_string(const Path<Graph> & path)
111 {
112 string out;
113 bool first = true;
114 for (Path<Graph>::Iterator it(path); it.has_current_node(); it.next_ne())
115 {
116 if (not first)
117 out += " -> ";
118 first = false;
119 out += to_string(it.get_current_node_ne()->get_info());
120 }
121 return out;
122 }
123
124
136 void print_results(const string & title, const DynList<Result_Item> & results)
137 {
138 cout << title << '\n';
139 size_t rank = 1;
140 for (DynList<Result_Item>::Iterator it(results); it.has_curr(); it.next_ne(), ++rank)
141 {
142 const auto & item = it.get_curr();
143 cout << std::format(" #{} cost={} path={}\n",
144 rank, item.total_cost, path_to_string(item.path));
145 }
146 if (results.is_empty())
147 cout << " (no path found)\n";
148 cout << '\n';
149 }
150}
151
162int main()
163{
164 Scenario scenario;
165 scenario.num_nodes = 8;
166 scenario.source = 0;
167 scenario.target = 7;
168 scenario.k = 6;
169 scenario.edges.reserve(8);
170 scenario.edges.append(Edge{0, 1, 1});
171 scenario.edges.append(Edge{1, 2, 1});
172 scenario.edges.append(Edge{2, 7, 1});
173 scenario.edges.append(Edge{0, 3, 2});
174 scenario.edges.append(Edge{3, 4, 2});
175 scenario.edges.append(Edge{4, 7, 2});
176 scenario.edges.append(Edge{2, 5, 1});
177 scenario.edges.append(Edge{5, 2, 1});
178
180 Graph g = build_graph(scenario, nodes);
181
182 auto * source = nodes[scenario.source];
183 auto * target = nodes[scenario.target];
184
185 cout << std::format("K shortest paths in Aleph graphs\n");
186 cout << std::format("source={}, target={}, k={}\n\n",
187 scenario.source, scenario.target, scenario.k);
188
189 const auto yen = yen_k_shortest_paths<Graph>(g, source, target, scenario.k);
190 const auto epp = eppstein_k_shortest_paths<Graph>(g, source, target, scenario.k);
191
192 print_results("Yen (loopless/simple paths)", yen);
193 print_results("Eppstein-style API (general, may include cycles)", epp);
194
195 return 0;
196}
K-shortest path algorithms (Yen and Eppstein-style API).
long double w
Definition btreepic.C:153
int num_nodes
Definition btreepic.C:410
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:139
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:3854
Iterator on the items of list.
Definition htlist.H:1420
Dynamic singly linked list with functional programming support.
Definition htlist.H:1155
bool has_curr() const noexcept
Definition htlist.H:930
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:524
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:604
Iterator on nodes and arcs of a path.
Definition tpl_graph.H:3208
bool has_current_node() const noexcept
Return true if the iterator has a current node.
Definition tpl_graph.H:3321
Path on a graph.
Definition tpl_graph.H:2669
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
int main()
Runs a sample demonstration computing K shortest paths using two APIs and prints their results.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
Definition ah-date.H:140
STL namespace.
Iterator on the items of an array.
Definition tpl_array.H:470
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:222
A shortest-path item returned by k-shortest algorithms.
static int * k
Dynamic array container with automatic resizing.
Generic graph and digraph implementations.