Skip to content
Draft
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 ] ;
run geometric_graph_generator_example.cpp ;
run gerdemann.cpp ;
run graph.cpp ;
run graph_as_tree.cpp ;
Expand Down
63 changes: 63 additions & 0 deletions example/geometric_graph_generator_example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//=======================================================================
// 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;

struct euclidean
{
template < typename P > auto operator()(P const& a, P const& b) const -> decltype(distance(a, b))
{
return distance(a, b);
}
};

} // end anonymous namespace

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

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

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

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 (const Edge& e : mst)
total += ::boost::get(weight_map, e);

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

return 0;
}
146 changes: 146 additions & 0 deletions include/boost/graph/geometric_graph_generator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
//=======================================================================
// 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
{
namespace graph
{

// 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. Writing an ADL wrapper
// inside namespace boost is not possible because unqualified lookup there also
// finds boost::distance from Boost.Range.
//
// 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, const PointContainer& 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;
using IndexType = typename ::boost::property_traits< VertexIndexMap >::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()");

using VertexIterator = typename Traits::vertex_iterator;
std::pair< VertexIterator, VertexIterator > verts(vertices(g));

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

for (; dest != verts.second; ++dest)
{
const IndexType dest_index = get(vmap, *dest);
const WeightType 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)
{
const CoordType x = x_dist(gen);
const CoordType 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.
//
// 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 make_random_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);

const std::size_t 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("make_random_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
12 changes: 12 additions & 0 deletions include/boost/graph/simple_point.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//=======================================================================
// Copyright 2005 Trustees of Indiana University
// Copyright (C) 2026 Arnaud Becheler
// Authors: Andrew Lumsdaine, Douglas Gregor
//
// Distributed under the Boost Software License, Version 1.0. (See
Expand All @@ -9,13 +10,24 @@
#ifndef BOOST_GRAPH_SIMPLE_POINT_HPP
#define BOOST_GRAPH_SIMPLE_POINT_HPP

#include <cmath>
#include <type_traits>

namespace boost
{

template < typename T > struct simple_point
{
T x;
T y;

// Integral coordinates widen to double so that distances are not truncated.
using distance_type = typename std::conditional< std::is_same< T, long double >::value, long double, typename std::conditional< std::is_same< T, float >::value, float, double >::type >::type;

friend distance_type distance(const simple_point& a, const simple_point& b)
{
return std::hypot(static_cast< distance_type >(a.x) - static_cast< distance_type >(b.x), static_cast< distance_type >(a.y) - static_cast< distance_type >(b.y));
}
};

} // end namespace boost
Expand Down
1 change: 1 addition & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,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
Loading