Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
bidirectional_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
37# include <iostream>
38# include <iomanip>
39# include <string>
40# include <utility>
41# include <aleph.H>
42# include <tpl_graph.H>
43# include <Bidirectional_BFS.H>
44
45using namespace Aleph;
46using namespace std;
47
48using Node_Info = string;
49using Arc_Info = int;
51
52int main() {
53 cout << "=== Bidirectional BFS Example ===" << endl << endl;
54
55 GT g;
56
57 auto alice = g.insert_node("Alice");
58 auto bob = g.insert_node("Bob");
59 auto charlie = g.insert_node("Charlie");
60 auto david = g.insert_node("David");
61 auto eve = g.insert_node("Eve");
62 auto frank = g.insert_node("Frank");
63 auto grace = g.insert_node("Grace");
64
65 // Undirected: insert both directions
80
81 cout << "Social network (degrees of separation):" << endl
82 << endl
83 << " Alice --- Bob --- Charlie --- David" << endl
84 << " | |" << endl
85 << " Eve --- Frank - Grace" << endl << endl;
86
88
90 {alice, david}, {alice, charlie}, {alice, grace}, {bob, eve}};
91
92 cout << "Finding shortest paths:" << endl;
93
94 for (auto [src, tgt] : queries) {
95 Path<GT> path(g);
96 bool found = bibfs(g, src, tgt, path);
97
98 cout << " " << left << setw(12) << src->get_info()
99 << " to " << left << setw(12) << tgt->get_info()
100 << ": ";
101
102 if (found) {
103 path.for_each_node([](auto * n) { cout << n->get_info() << " "; });
104 } else {
105 cout << "No path";
106 }
107
108 cout << endl;
109 }
110
111 cout << endl << "=== Performance ===" << endl
112 << "Time: O(b^(d/2))" << endl
113 << "Space: O(b^(d/2))" << endl
114 << "vs Standard BFS: O(b^d) both time and space" << endl;
115
116 return 0;
117}
Bidirectional BFS for shortest unweighted path.
Core header for the Aleph-w library.
string Node_Info
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:139
Bidirectional BFS for finding shortest unweighted paths.
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
Path on a graph.
Definition tpl_graph.H:2669
void for_each_node(Operation op=Operation()) const
Execute an operation on each node of path.
Definition tpl_graph.H:3338
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.