-
Notifications
You must be signed in to change notification settings - Fork 229
Euclidean generator and tsp enhancements #463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
megyhazy
wants to merge
5
commits into
boostorg:develop
Choose a base branch
from
megyhazy:euclidiean_generator_and_tsp_enhancements
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+775
−12
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6466542
euclidean random point and graph generators
megyhazy a07b13a
Examples for euclidean_graph_generator.hpp
megyhazy 79724c7
accidentally pushed .gitignore
megyhazy a491af7
euclidean_graph_generator_test.cpp - has tests for euclidean random g…
megyhazy ab893fb
minor enhancements to examples and precondition for connect_all_eucli…
megyhazy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| //======================================================================= | ||
| // Copyright 2026 | ||
| // 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/adjacency_list.hpp> | ||
| #include <boost/graph/euclidean_graph_generator.hpp> | ||
| #include <boost/graph/simple_point.hpp> | ||
| #include <boost/graph/kruskal_min_spanning_tree.hpp> | ||
| #include <boost/graph/graphml.hpp> | ||
| #include <boost/property_map/property_map.hpp> | ||
| #include <boost/property_map/dynamic_property_map.hpp> | ||
| #include <iostream> | ||
| #include <iomanip> | ||
| #include <vector> | ||
| #include <random> | ||
| #include <fstream> | ||
| #include <map> | ||
|
|
||
| // Utility function to write graph to GraphML file with point positions | ||
| template < typename Graph > | ||
| void write_graph_to_graphml(Graph& g, const std::string& filename, | ||
| const std::vector< boost::simple_point< double > >& points) | ||
| { | ||
| std::ofstream file(filename); | ||
| if (!file) | ||
| { | ||
| std::cerr << "Error opening file: " << filename << std::endl; | ||
| return; | ||
| } | ||
|
|
||
| // Create property maps for x and y coordinates | ||
| std::map< typename boost::graph_traits< Graph >::vertex_descriptor, double > | ||
| x_map, y_map; | ||
|
|
||
| typename boost::graph_traits< Graph >::vertex_iterator vi, vi_end; | ||
| for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) | ||
| { | ||
| std::size_t idx = boost::get(boost::vertex_index, g, *vi); | ||
| x_map[*vi] = points[idx].x; | ||
| y_map[*vi] = points[idx].y; | ||
| } | ||
|
|
||
| boost::associative_property_map< std::map< | ||
| typename boost::graph_traits< Graph >::vertex_descriptor, double > > | ||
| x_pmap(x_map), y_pmap(y_map); | ||
|
|
||
| // Create dynamic properties for GraphML output | ||
| boost::dynamic_properties dp; | ||
| dp.property("x", x_pmap); | ||
| dp.property("y", y_pmap); | ||
| dp.property("weight", boost::get(boost::edge_weight, g)); | ||
|
|
||
| // Write GraphML | ||
| boost::write_graphml(file, g, dp, true); | ||
| } | ||
|
|
||
| void example_basic_random_graph() | ||
| { | ||
| using Graph = boost::adjacency_matrix< boost::undirectedS, | ||
| boost::no_property, boost::property< boost::edge_weight_t, double > >; | ||
| using Point = boost::simple_point< double >; | ||
|
|
||
| const std::size_t num_vertices = 25; | ||
| Graph g(num_vertices); | ||
|
|
||
| // Generate a container of random points w/ default rng and distribution (uniform) | ||
| std::vector< Point > points; | ||
| points.reserve(num_vertices); | ||
| auto back_itr = std::back_inserter(points); | ||
| boost::generate_random_points(num_vertices, 500, back_itr); | ||
|
|
||
| // Get the edge weight map (matches edges to weight) and vertex index map | ||
| auto weight_map = boost::get(boost::edge_weight, g); | ||
| auto vertex_index_map = boost::get(boost::vertex_index, g); | ||
|
|
||
| // Create complete graph (generate edges) | ||
| // using the stored points and populate weight map | ||
| boost::connect_all_euclidean(g, points, weight_map, | ||
| vertex_index_map); | ||
| } | ||
|
|
||
| void example_custom_distribution() | ||
| { | ||
| using Graph = boost::adjacency_matrix< boost::undirectedS, | ||
| boost::no_property, boost::property< boost::edge_weight_t, double > >; | ||
| using Point = boost::simple_point< double >; | ||
|
|
||
| const std::size_t num_vertices = 15; | ||
| Graph g(num_vertices); | ||
|
|
||
| std::normal_distribution< double > normal_dist(50.0, 10.0); | ||
| std::vector< Point > points; | ||
| points.reserve(num_vertices); | ||
| boost::generate_random_points( | ||
| num_vertices, normal_dist, normal_dist, std::back_inserter(points)); | ||
|
|
||
| // Create graph | ||
| boost::connect_all_euclidean(g, points, boost::get(boost::edge_weight, g), | ||
| boost::get(boost::vertex_index, g)); | ||
| } | ||
|
|
||
| void example_mst_on_euclidean_graph() | ||
| { | ||
| // Use adjacency_list to make graphML printing possible | ||
| using Graph = boost::adjacency_list< boost::vecS, boost::vecS, | ||
| 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; | ||
|
|
||
| const std::size_t num_vertices = 20; | ||
| Graph g(num_vertices); | ||
|
|
||
| // Generate and store points | ||
| std::vector< Point > points; | ||
| points.reserve(num_vertices); | ||
| boost::generate_random_points( | ||
| num_vertices, 500, std::back_inserter(points)); | ||
|
|
||
| // Create complete graph using adjacency_list | ||
| boost::connect_all_euclidean(g, points, boost::get(boost::edge_weight, g), | ||
| boost::get(boost::vertex_index, g)); | ||
|
|
||
| // Write full graph with GraphML format | ||
| write_graph_to_graphml(g, "full_graph.graphml", points); | ||
|
|
||
| // Compute MST | ||
| std::vector< Edge > mst_edges; | ||
| boost::kruskal_minimum_spanning_tree(g, std::back_inserter(mst_edges)); | ||
|
|
||
| // Create MST subgraph using adjacency_list | ||
| Graph mst_graph(num_vertices); | ||
| for (const auto& e : mst_edges) | ||
| { | ||
| auto src = boost::source(e, g); | ||
| auto tgt = boost::target(e, g); | ||
| auto weight = boost::get(boost::edge_weight, g, e); | ||
| boost::add_edge(src, tgt, | ||
| boost::property< boost::edge_weight_t, double >(weight), mst_graph); | ||
| } | ||
|
|
||
| // Write MST with GraphML format | ||
| write_graph_to_graphml(mst_graph, "mst_graph.graphml", points); | ||
| } | ||
|
|
||
| void example_make_convenient_euclidean_graph() | ||
| { | ||
| using Graph = boost::adjacency_matrix< boost::undirectedS, | ||
| boost::no_property, boost::property< boost::edge_weight_t, double > >; | ||
|
|
||
| const std::size_t num_vertices = 10; | ||
| const std::size_t coord_max = 100; | ||
| Graph g(num_vertices); | ||
|
|
||
| // Use simple convenience function to create complete graph | ||
| boost::make_random_euclidean_graph(g, num_vertices, coord_max, | ||
| boost::get(boost::edge_weight, g), boost::get(boost::vertex_index, g)); | ||
| } | ||
|
|
||
| int main() | ||
| { | ||
| // Generates a basic random Euclidean graph from convenience function | ||
| example_make_convenient_euclidean_graph(); | ||
|
|
||
| // Generates a basic random Euclidean graph from building blocks | ||
| example_basic_random_graph(); | ||
|
|
||
| // Shows how to use custom distributions (e.g. normal) with separate | ||
| // point generation and graph construction steps | ||
| example_custom_distribution(); | ||
|
|
||
| // Computes MST on a | ||
| // generated Euclidean graph and output | ||
| // GraphML files which can be viewed with tools like | ||
| // Gephi or Cytoscape https://lite.gephi.org/ | ||
| example_mst_on_euclidean_graph(); | ||
|
|
||
| return EXIT_SUCCESS; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess you deleted the .gitignore file by mistake and committed your changes. This should probably be restored :)