Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
grid_example.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32#include <iostream>
33#include <numbers>
34#include <grid.H>
35#include <tpl_graph.H>
36
37using namespace std;
38using namespace Aleph;
39
40// A simple structure to hold node coordinates
41struct Coordinate {
42 size_t row, col;
43 Coordinate() : row(0), col(0) {}
44 Coordinate(size_t r, size_t c) : row(r), col(c) {}
45};
46
47// Functor to initialize node coordinates
48struct InitNode {
49 template <class Graph>
50 void operator()(Graph& /*g*/, typename Graph::Node* n, size_t r, size_t c) {
51 n->get_info() = Coordinate(r, c);
52 }
53};
54
55// Functor to initialize arc weights
56// For a grid, we might set orthogonal moves to cost 1.0 and diagonal to 1.414 (sqrt(2))
57struct InitArc {
58 template <class Graph>
59 void operator()(Graph& g, typename Graph::Arc* a, size_t /*r*/, size_t /*c*/) {
60 // Determine if the connection is diagonal or orthogonal based on coordinates
61 auto src_info = g.get_src_node(a)->get_info();
62 auto tgt_info = g.get_tgt_node(a)->get_info();
63
64 bool is_diagonal = (src_info.row != tgt_info.row) and (src_info.col != tgt_info.col);
65 a->get_info() = is_diagonal ? std::numbers::sqrt2 : 1.0;
66 }
67};
68
69int main() {
70 cout << "========================================" << endl;
71 cout << " Grid Graph Generation Example" << endl;
72 cout << "========================================" << endl;
73
74 // We define a graph with Coordinate as node info and double as arc info
76
77 // We use our custom functors to initialize the nodes and arcs during generation
78 Build_Grid<decltype(g), InitNode, InitArc> builder;
79
80 const size_t rows = 4;
81 const size_t cols = 4;
82
83 cout << "\nBuilding a " << rows << " x " << cols << " grid..." << endl;
84 // Note: parameters are (graph, width, height)
85 builder(g, cols, rows);
86
87 cout << "Grid successfully built!" << endl;
88 cout << "Number of nodes: " << g.get_num_nodes() << endl;
89 cout << "Number of arcs: " << g.get_num_arcs() << endl;
90
91 cout << "\nSample of connections from the bottom-right node (" << rows-1 << ", " << cols-1 << "):" << endl;
92
93 // Find the node at (rows-1, cols-1)
95 for (auto it = g.get_node_it(); it.has_curr(); it.next()) {
96 auto n = it.get_curr();
97 if (n->get_info().row == rows - 1 and n->get_info().col == cols - 1) {
98 last_node = n;
99 break;
100 }
101 }
102
103 if (last_node) {
104 // In Aleph-w, arcs are directed from the node that created them.
105 // Build_Grid connects node (i,j) to its previously created neighbors (left, up, diags).
106 // So the last node has outgoing arcs to its top, left, and upper-left neighbors.
107 for (auto it = g.get_out_it(last_node); it.has_curr(); it.next()) {
108 auto a = it.get_curr();
109 auto tgt = g.get_tgt_node(a);
110 cout << " -> Connected to node (" << tgt->get_info().row << ", " << tgt->get_info().col
111 << ") with weight " << a->get_info() << endl;
112 }
113 }
114
115 cout << "\n========================================" << endl;
116 return 0;
117}
WeightedDigraph::Node Node
size_t * rows
Definition ca-c-api.h:112
size_t cols
Definition ca-c-api.h:105
Functor to build a 2D grid graph with 8-connectivity.
Definition grid.H:178
Node Node
The graph type.
Definition tpl_graph.H:433
Arc Arc
The node class type.
Definition tpl_graph.H:434
bool has_curr() const noexcept
Return true is the iterator has a current arc.
Definition graph-dry.H:3107
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:779
Out_Iterator get_out_it(Node *p) const noexcept
Return an output iterator on the incoming nodes to p
Definition graph-dry.H:3205
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:737
constexpr size_t get_num_arcs() const noexcept
Definition graph-dry.H:826
auto get_node_it() const noexcept
Obtains an iterator to the nodes of graph.
Definition graph-dry.H:2886
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:785
2D grid graph generation with 8-connectivity.
int main()
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
and
Check uniqueness with explicit hash + equality functors.
STL namespace.
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:223
Coordinate(size_t r, size_t c)
void operator()(Graph &g, typename Graph::Arc *a, size_t, size_t)
void operator()(Graph &, typename Graph::Node *n, size_t r, size_t c)
gsl_rng * r
Generic graph and digraph implementations.
Treap< int >::Node * last_node
Definition writeTreap.C:60