Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ run filtered_graph_edge_range.cpp ;
run filtered_vec_as_graph.cpp ;
run filtered-copy-example.cpp ;
exe fr_layout : fr_layout.cpp /boost/timer//boost_timer : [ requires cxx11_noexcept cxx11_rvalue_references sfinae_expr cxx11_auto_declarations cxx11_lambdas cxx11_unified_initialization_syntax cxx11_hdr_tuple cxx11_hdr_initializer_list cxx11_hdr_chrono cxx11_thread_local cxx11_constexpr cxx11_nullptr cxx11_numeric_limits cxx11_decltype cxx11_hdr_array cxx11_hdr_atomic cxx11_hdr_type_traits cxx11_allocator cxx11_explicit_conversion_operators ] ;
exe geometric_graph_generator_example : geometric_graph_generator_example.cpp ;
run gerdemann.cpp ;
run graph.cpp ;
run graph_as_tree.cpp ;
Expand Down
59 changes: 59 additions & 0 deletions example/geometric_graph_generator_example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//=======================================================================
// Copyright 2026 Matyas W Egyhazy
// Copyright (C) 2026 Arnaud Becheler
// Author: Matyas W Egyhazy
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//=======================================================================

#include <boost/graph/adjacency_matrix.hpp>
#include <boost/graph/geometric_graph_generator.hpp>
#include <boost/graph/kruskal_min_spanning_tree.hpp>
#include <boost/graph/properties.hpp>
#include <boost/graph/simple_point.hpp>

#include <cstddef>
#include <iostream>
#include <random>
#include <vector>

namespace
{

using Graph = ::boost::adjacency_matrix< ::boost::undirectedS, ::boost::no_property, ::boost::property< ::boost::edge_weight_t, double > >;
using Point = ::boost::simple_point< double >;
using Edge = ::boost::graph_traits< Graph >::edge_descriptor;


} // end anonymous namespace

int main()
{
std::size_t constexpr num_points = 20;

Graph g(num_points);
std::mt19937 gen(42);
std::uniform_real_distribution< double > coordinates(0.0, 500.0);
auto const weight_map = ::boost::get(::boost::edge_weight, g);

::boost::graph::generate_random_complete_geometric_graph< Point >(g, num_points,
coordinates, coordinates, weight_map,
::boost::get(::boost::vertex_index, g), gen,
::boost::graph::default_euclidean_distance{});

std::cout << "Complete geometric graph: " << ::boost::num_vertices(g) << " vertices, " << ::boost::num_edges(g) << " edges\n";

std::vector< Edge > mst;
::boost::kruskal_minimum_spanning_tree(g, std::back_inserter(mst));

double total = 0.0;

for (Edge const& e : mst)
total += ::boost::get(weight_map, e);

std::cout << "Minimum spanning tree: " << mst.size() << " edges, total weight " << total << "\n";

return 0;
}
163 changes: 163 additions & 0 deletions include/boost/graph/geometric_graph_generator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
//=======================================================================
// Copyright 2026 Matyas W Egyhazy
// Copyright (C) 2026 Arnaud Becheler
// Author: Matyas W Egyhazy
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//=======================================================================

#ifndef BOOST_GRAPH_GEOMETRIC_GRAPH_GENERATOR_HPP
#define BOOST_GRAPH_GEOMETRIC_GRAPH_GENERATOR_HPP

#include <boost/assert.hpp>
#include <boost/concept/assert.hpp>
#include <boost/concept_check.hpp>
#include <boost/container_hash/hash.hpp>
#include <boost/graph/graph_concepts.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/properties.hpp>
#include <boost/graph/simple_point.hpp>
#include <boost/static_assert.hpp>
#include <boost/unordered/unordered_flat_set.hpp>

#include <algorithm>
#include <cstddef>
#include <iterator>
#include <stdexcept>
#include <type_traits>
#include <utility>
#include <vector>

namespace boost
Comment thread
Becheler marked this conversation as resolved.
Resolved
{
namespace graph
{
/**
* Calculates the 2D Euclidean distance between two points using
* std::hypot.
*
* Works with any point type possessing public '.x' and '.y' members.
* Use your own distance functor (e.g. boost::geometry distance()
* if your point type does not have those members or don't want euclidean distances.
*/
struct default_euclidean_distance
{
template < typename Point1, typename Point2 >
auto constexpr operator()(Point1 const& p1, Point2 const& p2) const
{
return std::hypot(p1.x - p2.x, p1.y - p2.y);
}
};

// Connects every pair of vertices with an edge weighted by the distance between
// the corresponding points. A common preprocessing step for TSP algorithms.
//
// The distance functor must be supplied by the caller.
//
// Preconditions: num_vertices(g) == points.size() and g has no edges.
// Complexity: O(V^2).
template < typename VertexListGraph, typename PointContainer, typename WeightMap, typename VertexIndexMap, typename BinaryFunction >
void connect_all_geometric(VertexListGraph& g, PointContainer const& points,
WeightMap wmap, VertexIndexMap vmap, BinaryFunction distance)
{
using Traits = ::boost::graph_traits< VertexListGraph >;

BOOST_CONCEPT_ASSERT((::boost::VertexListGraphConcept< VertexListGraph >));
BOOST_CONCEPT_ASSERT((::boost::MutableGraphConcept< VertexListGraph >));
BOOST_CONCEPT_ASSERT((::boost::RandomAccessContainerConcept< PointContainer >));
BOOST_CONCEPT_ASSERT((::boost::ReadablePropertyMapConcept< VertexIndexMap, typename Traits::vertex_descriptor >));
BOOST_CONCEPT_ASSERT((::boost::WritablePropertyMapConcept< WeightMap, typename Traits::edge_descriptor >));

BOOST_STATIC_ASSERT_MSG((!std::is_convertible< typename Traits::directed_category, ::boost::directed_tag >::value), "connect_all_geometric requires an undirected graph type.");

using WeightType = typename ::boost::property_traits< WeightMap >::value_type;

BOOST_STATIC_ASSERT_MSG(!std::is_integral< WeightType >::value, "connect_all_geometric requires a non-integral weight type. Integer weights truncate distances.");

BOOST_ASSERT_MSG(num_vertices(g) == points.size(), "connect_all_geometric requires num_vertices(g) == points.size()");

auto verts(vertices(g));

for (auto src(verts.first); src != verts.second; ++src)
{
auto const src_index = get(vmap, *src);
auto dest(src);
++dest;

for (; dest != verts.second; ++dest)
{
auto const dest_index = get(vmap, *dest);
auto const weight = static_cast< WeightType >(
distance(points[src_index], points[dest_index]));
put(wmap, add_edge(*src, *dest, g).first, weight);
}
}
}

// Writes num_points distinct points to out, in generation order.
//
// Uniqueness is decided on the drawn coordinate pair rather than on PointType, so
// PointType needs no hash and no equality of its own. That is equivalent as long
// as PointType{x, y} is injective. A point type that normalises or snaps on
// construction needs its own filter.
//
// Returns the number of points actually written, which is less than num_points
// when the distributions cannot supply enough distinct values within
// max_attempts draws. A max_attempts of zero selects a default budget.
template < typename PointType, typename OutputIterator, typename XDistribution, typename YDistribution, typename URBG >
std::size_t generate_unique_random_points(std::size_t num_points, XDistribution x_dist, YDistribution y_dist, OutputIterator out, URBG&& gen, std::size_t max_attempts = 0)
{
using CoordType = typename XDistribution::result_type;

BOOST_STATIC_ASSERT_MSG((std::is_same< CoordType, typename YDistribution::result_type >::value), "X and Y distributions must have the same result type");

if (max_attempts == 0)
max_attempts = std::max< std::size_t >(10 * num_points, 100);

::boost::unordered_flat_set< std::pair< CoordType, CoordType > > seen;
seen.reserve(num_points);

std::size_t attempts = 0;

while (seen.size() < num_points && attempts < max_attempts)
{
auto const x = x_dist(gen);
auto const y = y_dist(gen);

if (seen.insert(std::make_pair(x, y)).second)
*out++ = PointType { x, y };

++attempts;
}

return seen.size();
}

// Populates g with num_points random points and complete geometric weights with parameterized X and Y distributions.
// Useful for testing algorithms on random complete geometric graphs (e.g. TSP algorithms).
// Note: Could be generalized to work on 3D+ spaces using variadic templates for the point type and
// distributions
//
// Throws std::runtime_error when the distributions cannot supply num_points
// distinct points, because a shorter point set would leave connect_all_geometric
// indexing past the end.
template < typename PointType, typename VertexListGraph, typename WeightMap, typename VertexIndexMap, typename XDistribution, typename YDistribution, typename URBG, typename BinaryFunction >
void generate_random_complete_geometric_graph(VertexListGraph& g, std::size_t num_points, XDistribution x_dist, YDistribution y_dist, WeightMap weight_map, VertexIndexMap vertex_index_map, URBG&& gen, BinaryFunction distance)
{
std::vector< PointType > points;
points.reserve(num_points);

std::size_t const generated = generate_unique_random_points< PointType >(num_points, x_dist, y_dist, std::back_inserter(points), gen);

if (generated != num_points)
throw std::runtime_error("generate_random_complete_geometric_graph: the given distributions cannot supply num_points distinct points");

connect_all_geometric(g, points, weight_map, vertex_index_map, distance);
}

} // end namespace graph
} // end namespace boost

#endif // BOOST_GRAPH_GEOMETRIC_GRAPH_GENERATOR_HPP
1 change: 1 addition & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ alias graph_test_regular :
[ compile filtered_graph_cc.cpp ]
[ run filter_graph_vp_test.cpp ]
[ run generator_test.cpp ]
[ run geometric_graph_generator_test.cpp ]
[ run graph.cpp : : : <define>TEST=1 : graph_1 ]
[ run graph.cpp : : : <define>TEST=2 : graph_2 ]
[ run graph.cpp : : : <define>TEST=3 : graph_3 ]
Expand Down
Loading