Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
zero_one_bfs_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 <Zero_One_BFS.H>
36
37using namespace Aleph;
38using namespace std;
39
41using Arc_Info = int;
43
44int main()
45{
46 cout << "=== Zero_One_BFS 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
56 g.insert_arc(a, b, 0); // A to B: free
57 g.insert_arc(a, c, 0); // A to C: free
58 g.insert_arc(c, e, 0); // C to E: free
59 g.insert_arc(b, d, 1); // B to D: costs 1
60 g.insert_arc(b, e, 1); // B to E: costs 1
61
62 cout << "Graph with 0/1 weights:" << endl
63 << " A ---(0)---> B" << endl
64 << " | |" << endl
65 << " (0) (1)" << endl
66 << " | |" << endl
67 << " V V" << endl
68 << " C ---(0)---> E" << endl << endl;
69
71
73
74 cout << "Shortest distances from A:" << endl;
75 cout << setw(8) << "Node" << setw(10) << "Distance" << endl;
76 cout << setfill('-') << setw(18) << "-" << setfill(' ') << endl;
77
78 for (auto it = g.get_node_it(); it.has_curr(); it.next())
79 {
80 auto node = it.get_curr();
81 auto dist = bfs01.get_distance(node);
82 cout << setw(8) << node->get_info()
83 << setw(10) << dist << endl;
84 }
85
86 cout << endl << "=== Performance ===" << endl
87 << "Time complexity: O(V + E)" << endl
88 << "Space complexity: O(V)" << endl;
89
90 return 0;
91}
0-1 BFS shortest path algorithm.
Core header for the Aleph-w library.
string Node_Info
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
0-1 BFS algorithm for shortest paths in graphs with 0/1 weights.
void paint_min_paths_tree(const GT &g, Node *start)
Paints the shortest paths tree on the graph from start.
auto get_node_it() const noexcept
Obtains an iterator to the nodes of graph.
Definition graph-dry.H:2786
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.
int main()