Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
dial_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# include <iostream>
32# include <iomanip>
33# include <aleph.H>
34# include <tpl_graph.H>
35# include <Dial.H>
36
37using namespace Aleph;
38using namespace std;
39
41using Arc_Info = int;
43
44int main()
45{
46 cout << "=== Dial's Algorithm Example ===" << endl << endl;
47
48 GT g;
49
50 auto a = g.insert_node('A');
51 auto b = g.insert_node('B');
52 auto c = g.insert_node('C');
53 auto d = g.insert_node('D');
54 auto e = g.insert_node('E');
55 auto f = g.insert_node('F');
56
57 g.insert_arc(a, b, 2);
58 g.insert_arc(a, c, 3);
59 g.insert_arc(b, d, 1);
60 g.insert_arc(b, e, 3);
61 g.insert_arc(c, e, 2);
62 g.insert_arc(d, f, 2);
63 g.insert_arc(e, f, 2);
64
65 cout << "Delivery network with small integer weights:" << endl
66 << " A ---(2)---> B ---(1)---> D ---(2)---> F" << endl
67 << " | | ^" << endl
68 << " (3) (3) (2)" << endl
69 << " | | |" << endl
70 << " v v |" << endl
71 << " C ---(2)---> E -------------------------+" << endl << endl;
72
74
76
77 cout << "Shortest distances from A:" << endl;
78 cout << left << setw(10) << "Node" << setw(8) << "Hours" << endl;
79 cout << setfill('-') << setw(18) << "-" << setfill(' ') << endl;
80
81 for (auto it = g.get_node_it(); it.has_curr(); it.next())
82 {
83 auto node = it.get_curr();
84 auto dist = dial.get_distance(node);
85 cout << left << setw(10) << (char)node->get_info()
86 << setw(8) << dist << endl;
87 }
88
89 cout << endl << "=== Performance ===" << endl
90 << "Time: O(V + E + C)" << endl
91 << "Space: O(V + C)" << endl;
92
93 return 0;
94}
Dial's algorithm for shortest paths with small integer weights.
Core header for the Aleph-w library.
string Node_Info
Dial's algorithm for shortest paths with integer weights.
Definition Dial.H:126
void paint_min_paths_tree(const GT &g, Node *start)
Paints the shortest paths tree on the graph from start.
Definition Dial.H:444
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
auto get_node_it() const noexcept
Obtains an iterator to the nodes of graph.
Definition graph-dry.H:2786
int main()
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.
STL namespace.
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:222
Generic graph and digraph implementations.