Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
backtracking_cyclic_graph_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
61#include <cstddef>
62#include <iostream>
63
64#include <State_Search.H>
65
66using namespace Aleph;
67
68// ---------------------------------------------------------------------------
69// Domain definition
70// ---------------------------------------------------------------------------
71
73{
74 size_t node = 0;
75};
76
78{
79 struct Move
80 {
81 size_t from = 0;
82 size_t to = 0;
83 };
84
86 using State_Key = size_t;
87
88 [[nodiscard]] State_Key state_key(const State &s) const noexcept
89 {
90 return s.node;
91 }
92
93 [[nodiscard]] bool is_goal(const State &s) const noexcept
94 {
95 return s.node == 3;
96 }
97
98 void apply(State &s, const Move &m) const noexcept
99 {
100 s.node = m.to;
101 }
102
103 void undo(State &s, const Move &m) const noexcept
104 {
105 s.node = m.from;
106 }
107
108 template <typename Visitor>
109 bool for_each_successor(const State &s, Visitor visit) const
110 {
111 switch (s.node)
112 {
113 case 0:
114 return visit(Move{0, 1});
115 case 1:
116 if (not visit(Move{1, 0})) // back-edge (cycle)
117 return false;
118 return visit(Move{1, 2});
119 case 2:
120 return visit(Move{2, 3});
121 default:
122 return true;
123 }
124 }
125};
126
127// ---------------------------------------------------------------------------
128// Main
129// ---------------------------------------------------------------------------
130
131int main()
132{
133 CyclicGraphDomain domain;
135
136 // Depth-aware visited map: key=node id, value=minimum depth seen (updated if a shorter path is found).
138
139 auto result = engine.search(GraphState{}, visited);
140
141 if (result.found_solution())
142 {
143 std::cout << "Goal reached at node "
144 << result.best_solution.get().state.node << "\n";
145 std::cout << "Visited states : " << result.stats.visited_states << "\n";
146 std::cout << "Pruned by visited: " << result.stats.pruned_by_visited << "\n";
147 }
148 else
149 {
150 std::cout << "No solution found.\n";
151 }
152
153 return 0;
154}
Umbrella header for the implicit state-space search framework.
Recursive depth-first backtracking over an implicit state space.
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.
void apply(State &s, const Move &m) const noexcept
State_Key state_key(const State &s) const noexcept
bool for_each_successor(const State &s, Visitor visit) const
void undo(State &s, const Move &m) const noexcept
bool is_goal(const State &s) const noexcept
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
static mt19937 engine