Skip to content

Commit

Permalink
Add fns to write spanning trees as graphviz data
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyelewis committed Nov 18, 2017
1 parent f23fe1b commit d4b81fa
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
40 changes: 40 additions & 0 deletions source/src_common/common/boost_addenda/graph/spanning_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@

#include "spanning_tree.hpp"

#include <boost/algorithm/string/join.hpp>
#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/adaptor/transformed.hpp>
#include <boost/range/irange.hpp>

#include "common/boost_addenda/range/indices.hpp"
#include "common/file/open_fstream.hpp"
#include "common/size_t_literal.hpp"

#include <algorithm>
#include <fstream>
#include <set>
#include <tuple>
#include <vector>
Expand All @@ -37,11 +40,15 @@ using namespace cath::common;

using boost::adaptors::filtered;
using boost::adaptors::transformed;
using boost::algorithm::join;
using boost::filesystem::path;
using boost::irange;
using std::make_pair;
using std::make_tuple;
using std::max;
using std::min;
using std::ofstream;
using std::string;
using std::vector;

/// \brief Get a simple ( 0 <-> 1 <-> 2 <-> ... <-> (arg_num_items -1) spanning tree)
Expand Down Expand Up @@ -174,3 +181,36 @@ size_size_doub_tpl_vec cath::common::order_spanning_tree_from_start(const size_s
);
}

/// \brief Create a graphviz string representing the specified spanning tree
string cath::common::make_graphviz_string_of_spanning_tree(const size_size_doub_tpl_vec &arg_spanning_tree ///< The tree to represent in graphviz format
) {
using std::to_string;
return "digraph example {\n"
+ join(
indices( arg_spanning_tree.size() )
| transformed( [&] (const size_t &x) {
return
to_string( get<0>( arg_spanning_tree[ x ] ) )
+ " -> "
+ to_string( get<1>( arg_spanning_tree[ x ] ) )
+ " [ "
+ to_string( get<2>( arg_spanning_tree[ x ] ) )
+ ", "
+ to_string( x )
+ "]"
+ "\n";
} ),
""
)
+ "}\n";
}

/// \brief Write the specified spanning tree as a graphviz string to the specified file
void cath::common::write_graphviz_string_of_spanning_tree(const size_size_doub_tpl_vec &arg_spanning_tree, ///< The spanning tree to represent in the specified file as a graphviz string
const path &arg_file ///< The file to write to
) {
ofstream output_stream;
open_ofstream( output_stream, arg_file );
output_stream << make_graphviz_string_of_spanning_tree( arg_spanning_tree );
output_stream.close();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#ifndef _CATH_TOOLS_SOURCE_SUPERPOSITION_SPANNING_TREE_H
#define _CATH_TOOLS_SOURCE_SUPERPOSITION_SPANNING_TREE_H

#include <boost/filesystem/path.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/kruskal_min_spanning_tree.hpp>
#include <boost/range/adaptor/transformed.hpp>
Expand Down Expand Up @@ -175,6 +176,9 @@ namespace cath {
size_size_pair_vec get_edges_of_spanning_tree(const size_size_doub_tpl_vec &);
size_size_doub_tpl_vec order_spanning_tree_from_start(const size_size_doub_tpl_vec &,
const size_t &);
std::string make_graphviz_string_of_spanning_tree(const size_size_doub_tpl_vec &);
void write_graphviz_string_of_spanning_tree(const size_size_doub_tpl_vec &,
const boost::filesystem::path &);

} // namespace common
} // namespace cath
Expand Down

0 comments on commit d4b81fa

Please sign in to comment.