98# ifndef GENERATE_GRAPH_H
99# define GENERATE_GRAPH_H
109using namespace Aleph;
119 template <AlephGraph GT, ArcFilter<GT> SA>
inline static
123 const SA & sa)
noexcept(nothrow_arc_filter_v<GT, SA>)
125 if (not g->is_digraph())
128 return search_arc<GT,SA>(*g, src, tgt, sa) !=
nullptr and
129 search_arc<GT,SA>(*g, tgt, src, sa) !=
nullptr;
133 template <AlephGraph GT>
165 template <AlephGraph
GT,
166 class Write_Node,
class Write_Arc,
167 class Shade_Node,
class Shade_Arc, ArcFilter<GT> SA>
169 const double & xdist,
170 [[maybe_unused]]
const double & ydist,
175 typename GT::Node_Iterator it(g);
176 for (
int i = 0; it.has_curr(); it.next_ne(), ++i)
178 auto p = it.get_current_node_ne();
182 if (Shade_Node() (p).
size() != 0)
183 output << Shade_Node() (p) <<
" " << i << std::endl;
185 const std::string text_node = Write_Node () (p);
187 if (text_node.size() == 0)
190 output <<
"NODE-TEXT " << i <<
" \"" << text_node <<
"\" 0 0" << std::endl;
195 auto a = it.get_current_arc_ne();
199 const auto src_idx = search_node <GT> (
nodes, src);
200 const auto tgt_idx = search_node <GT> (
nodes, tgt);
202 if (is_there_a_double_arc <GT, SA> (&g, src, tgt, sa))
203 output <<
"CURVE-ARC " << src_idx <<
" " << tgt_idx <<
" "
204 << xdist/5 <<
" L" << std::endl;
206 output <<
"ARC " << src_idx <<
" " << tgt_idx << std::endl;
208 if ( Shade_Arc()(a).
size() != 0)
209 output << Shade_Arc()(a) <<
" "
210 << src_idx <<
" " << tgt_idx <<
" " << std::endl;
212 const std::string text_arc = Write_Arc() (a);
214 if (text_arc.size() == 0)
217 output <<
"ARC-TEXT " << src_idx <<
" " << tgt_idx <<
" \""
218 << text_arc <<
"\" 0 0 " << std::endl;
249 template <AlephGraph
GT,
259 const std::string & rankdir =
"TB",
264 output <<
"// Generated by generate_graphviz() from Aleph-w library. See at:" << std::endl
265 <<
"// http://webdelprofesor.ula.ve/ingenieria/lrleon/aleph/html/index.html" << std::endl
266 <<
"// for documentation" << std::endl
267 <<
"// Copyleft Leandro Rabindranath Leon lrleon@ula.ve" << std::endl
268 <<
"// for using of graphviz system. See at http://graphviz.org/"
269 << std::endl << std::endl;
274 output <<
"digraph {" << std::endl;
279 output <<
"graph {" << std::endl;
282 <<
"rankdir = " << rankdir << std::endl
283 <<
"style = none" << std::endl
284 <<
"truecolor=false" << std::endl
285 <<
"ranksep = " << ranksep << std::endl
286 <<
"nodesep = " << nodesep << std::endl << std::endl;
291 for (
int i = 0; it.has_curr(); it.
next_ne(), ++i)
295 auto p = it.get_current_node_ne();
298 if (Shade_Node () (p))
299 output <<
"style = bold ";
301 const std::string text_node = Write_Node () (p);
303 if (text_node.size() != 0)
304 output <<
"label = \"" << text_node <<
"\"";
305 output <<
"]" << std::endl;
312 auto a = it.get_current_arc_ne();
316 auto src_idx = search_node <GT> (
nodes, src);
317 auto tgt_idx = search_node <GT> (
nodes, tgt);
319 output << src_idx << arc_str << tgt_idx <<
" [";
321 if (Shade_Arc () (a))
322 output <<
"style = bold ";
324 const std::string text_arc = Write_Arc() (a);
326 if (text_arc.size() != 0)
327 output <<
"label = \"" << text_arc <<
"\"";
328 output <<
"]" << std::endl;
331 output <<
"}" << std::endl;
371 template <AlephGraph
GT,
377 Node_Attr node_attr = Node_Attr(),
378 Arc_Attr arc_attr = Arc_Attr(),
379 const std::string & rankdir =
"TB",
382 out <<
"// Generated by generate_graphviz() from Aleph-w library" << std::endl
384 <<
"// http://webdelprofesor.ula.ve/ingenieria/lrleon/aleph/html/index.html" << std::endl
385 <<
"// for documentation of Aleph-w library" << std::endl
386 <<
"// Copyleft Leandro Rabindranath Leon lrleon@ula.ve" << std::endl
387 <<
"// for using of graphviz system. See at http://graphviz.org/"
388 << std::endl << std::endl
389 << (g.
is_digraph() ?
"digraph {" :
"graph {") << std::endl
391 <<
"rankdir = " << rankdir << std::endl
393 <<
"// Node list" << std::endl
399 for (
int i = 0; it.has_curr(); it.
next_ne(), ++i)
401 auto p = it.get_current_node_ne();
403 nodes_table.insert(p, i);
407 node_attr (g, p,
out);
409 out <<
"]" << std::endl;
414 <<
"// Arc list" << std::endl
417 const std::string arrow = g.
is_digraph() ?
"->" :
"--";
421 auto a = it.get_current_arc_ne();
425 const auto src_idx = nodes_table.find(src);
426 const auto tgt_idx = nodes_table.find(tgt);
428 out << src_idx << arrow << tgt_idx <<
" [";
429 arc_attr (g, a,
out) ;
430 out <<
"]" << std::endl;
433 out <<
"}" << std::endl;
458 template <AlephGraph
GT,
464 Node_Attr node_attr = Node_Attr(),
465 Arc_Attr arc_attr = Arc_Attr(),
466 const std::string & rankdir =
"LR",
469 out <<
"// Generated by generate_graphviz() from Aleph-w library" << std::endl
471 <<
"// http://webdelprofesor.ula.ve/ingenieria/lrleon/aleph/html/index.html" << std::endl
472 <<
"// for documentation of Aleph-w library" << std::endl
473 <<
"// Copyleft Leandro Rabindranath Leon lrleon@ula.ve" << std::endl
474 <<
"// for using of graphviz system. See at http://graphviz.org/"
475 << std::endl << std::endl
476 <<
"digraph {" << std::endl
478 <<
"rankdir = " << rankdir << std::endl
480 <<
"// Node list" << std::endl
486 for (
int i = 0; it.has_curr(); it.
next_ne(), ++i)
488 auto p = it.get_current_node_ne();
493 node_attr (g, p,
out);
495 out <<
"]" << std::endl;
500 <<
"// Arc list" << std::endl
503 const std::string arrow =
"->";
507 auto a = it.get_current_arc_ne();
511 const auto src_idx = nodes_table.
find(src);
512 const auto tgt_idx = nodes_table.
find(tgt);
514 out << src_idx << arrow << tgt_idx <<
" [";
515 arc_attr (g, a,
out) ;
516 out <<
"]" << std::endl;
519 out <<
"}" << std::endl;
549 template <AlephGraph
GT,
555 Node_Attr node_attr = Node_Attr(),
556 Arc_Attr arc_attr = Arc_Attr(),
557 const std::string & rankdir =
"LR",
560 out <<
"// Generated by generate_graphviz() from Aleph-w library" << std::endl
562 <<
"// http://webdelprofesor.ula.ve/ingenieria/lrleon/aleph/html/index.html" << std::endl
563 <<
"// for documentation of Aleph-w library" << std::endl
564 <<
"// Copyleft Leandro Rabindranath Leon lrleon@ula.ve" << std::endl
565 <<
"// for using of graphviz system. See at http://graphviz.org/"
566 << std::endl << std::endl
567 <<
"digraph {" << std::endl
569 <<
"rankdir = " << rankdir << std::endl
570 <<
"rank = same" << std::endl
572 <<
"// Node list" << std::endl
578 size_t rank = 0, i = 0;
579 for (
auto rank_it =
ranks.
get_it(); rank_it.has_curr();
580 rank_it.next_ne(), ++rank)
582 out <<
"subgraph rank_" << rank << std::endl
584 <<
"label = \"rank " << rank <<
"\"" << std::endl;
585 for (
auto it = rank_it.get_curr().get_it(); it.has_curr();
588 auto p = it.get_curr();
591 node_attr(g, p,
out);
592 out <<
"]" << std::endl;
594 out <<
"}" << std::endl;
599 <<
"// Arc list" << std::endl
602 const std::string arrow =
"->";
605 auto a = it.get_current_arc_ne();
609 const auto src_idx = nodes_table.
find(src);
610 const auto tgt_idx = nodes_table.
find(tgt);
612 out << src_idx << arrow << tgt_idx <<
" [";
613 arc_attr (g, a,
out) ;
614 out <<
"]" << std::endl;
616 out <<
"}" << std::endl;
687 template <AlephGraph
GT,
708 const Node_Attr & node_attr = Node_Attr(),
709 const Arc_Attr & arc_attr = Arc_Attr(),
710 const std::string & rankdir =
"LR")
712 generate_graphviz <GT, Node_Attr, Arc_Attr, SN, SA>
713 (g,
out, node_attr, arc_attr, rankdir,
sa);
717 const Node_Attr & node_attr = Node_Attr(),
718 const Arc_Attr & arc_attr = Arc_Attr(),
719 const std::string & rankdir =
"LR")
721 digraph_graphviz <GT, Node_Attr, Arc_Attr, SN, SA>
722 (g,
out, node_attr, arc_attr, rankdir,
sa);
726 const Node_Attr & node_attr = Node_Attr(),
727 const Arc_Attr & arc_attr = Arc_Attr(),
728 const std::string & rankdir =
"LR")
730 rank_graphviz <GT, Node_Attr, Arc_Attr, SN, SA>
731 (g,
out, node_attr, arc_attr, rankdir,
sa);
737 template <AlephGraph GT>
764 template <AlephGraph
GT,
789 const std::string & rankdir =
"TB",
790 float ranksep = 0.4,
float nodesep = 0.4)
793 Shade_Arc, Dashed_Node, Dashed_Arc, SA, SN>
794 (g,
out, rankdir, ranksep, nodesep,
sa);
820 template <AlephGraph
GT,
821 class Write_Node,
class Write_Arc,
822 class Shade_Node,
class Shade_Arc, ArcFilter<GT> SA>
824 const size_t & nodes_by_level,
825 const double & xdist,
826 const double & ydist,
831 out <<
"cross-net-digraph ";
833 out <<
"cross-net-graph ";
836 << xdist <<
" " << ydist << std::endl
839 generate_graphpic<GT, Write_Node, Write_Arc, Shade_Node, Shade_Arc, SA>
840 (g, xdist, ydist,
out, sa);
843 template <AlephGraph
GT,
844 class Write_Node,
class Write_Arc,
845 class Shade_Node,
class Shade_Arc>
847 const size_t & nodes_by_level,
848 const double & xdist,
849 const double & ydist,
853 generate_cross_graph<GT, Write_Node, Write_Arc, Shade_Node, Shade_Arc, DSA>
854 (g, nodes_by_level, xdist, ydist,
out);
878 template <AlephGraph
GT,
879 class Write_Node,
class Write_Arc,
880 class Shade_Node,
class Shade_Arc, ArcFilter<GT> SA>
882 const size_t & nodes_by_level,
883 const double & xdist,
884 const double & ydist,
889 out <<
"net-digraph ";
894 << xdist <<
" " << ydist << std::endl
897 generate_graphpic<GT, Write_Node, Write_Arc, Shade_Node, Shade_Arc, SA>
898 (g, xdist, ydist,
out, sa);
901 template <AlephGraph
GT,
902 class Write_Node,
class Write_Arc,
903 class Shade_Node,
class Shade_Arc>
905 const size_t & nodes_by_level,
906 const double & xdist,
907 const double & ydist,
911 generate_net_graph<GT, Write_Node, Write_Arc, Shade_Node, Shade_Arc, DSA>
912 (g, nodes_by_level, xdist, ydist,
out);
934 template <AlephGraph GT,
class Write_Node,
class Write_Arc, ArcFilter<GT> SA>
936 const size_t & nodes_by_level,
937 const double & xdist,
938 const double & ydist,
944 (g, nodes_by_level, xdist, ydist,
out, sa);
947 template <AlephGraph GT,
class Write_Node,
class Write_Arc, ArcFilter<GT> SA>
949 const size_t & nodes_by_level,
950 const double & xdist,
951 const double & ydist,
957 (g, nodes_by_level, xdist, ydist,
out, sa);
961 template <AlephGraph GT,
class Write_Node,
class Write_Arc>
963 const size_t & nodes_by_level,
964 const double & xdist,
965 const double & ydist,
971 (g, nodes_by_level, xdist, ydist,
out);
974 template <AlephGraph GT,
class Write_Node,
class Write_Arc>
976 const size_t & nodes_by_level,
977 const double & xdist,
978 const double & ydist,
984 (g, nodes_by_level, xdist, ydist,
out);
C++20 concepts for the protocol shared by graph algorithms.
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
size_t size_t int32_t * out
Doubly-linked list (defined in tpl_dynList.H).
Dynamic map implemented with a treap.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
Data & find(const Key &key)
Find the value associated with key.
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
Filtered iterator on the nodes of a graph.
Computes topological ordering using breadth-first search (Kahn's algorithm).
RankList< List< typename GT::Node * > > ranks(const GT &g)
Compute rank-based topological ordering.
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
bool is_digraph() const noexcept
Return true if the graph this is directed.
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
DynArray< Graph::Node * > nodes
void generate_graphviz(const GT &g, std::ostream &output, const std::string &rankdir="TB", float ranksep=0.2, float nodesep=0.2, SA sa=SA())
Generate a Graphviz DOT specification for graph visualization.
void generate_graphpic(const GT &g, const double &xdist, const double &ydist, std::ostream &output, SA sa=SA())
Generate a graphpic specification for graph visualization.
void generate_net_graph(GT &g, const size_t &nodes_by_level, const double &xdist, const double &ydist, std::ostream &out, SA sa=SA())
Generate a net-graph layout specification for graphpic.
void digraph_graphviz(const GT &g, std::ostream &out, Node_Attr node_attr=Node_Attr(), Arc_Attr arc_attr=Arc_Attr(), const std::string &rankdir="LR", SA sa=SA())
Generate Graphviz DOT output specifically for digraphs.
size_t rank_graphviz(const GT &g, std::ostream &out, Node_Attr node_attr=Node_Attr(), Arc_Attr arc_attr=Arc_Attr(), const std::string &rankdir="LR", SA sa=SA())
Generate Graphviz DOT output with topological ranking.
void generate_cross_graph(GT &g, const size_t &nodes_by_level, const double &xdist, const double &ydist, std::ostream &out, SA sa=SA())
Generate a cross-graph layout specification for graphpic.
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
static int search_node(DynArray< typename GT::Node * > &nodes, typename GT::Node *p) noexcept
and
Check uniqueness with explicit hash + equality functors.
static bool is_there_a_double_arc(const GT *g, typename GT::Node *src, typename GT::Node *tgt, const SA &sa) noexcept(nothrow_arc_filter_v< GT, SA >)
Whether the digraph g has, among the arcs that sa shows, an arc from src to tgt and another from tgt ...
Array< size_t > ranks(const Array< T > &array)
Computes the rank of each element in an Array.
long sequential_search(T *a, const T &x, const long l, const long r, Equal eq=Equal())
Linear search for an element in an array.
Filtered iterator on all the arcs of a graph.
void operator()(const GT &, typename GT::Arc *a, std::ostream &out)
void operator()(const GT &, typename GT::Node *p, std::ostream &out)
Default filter for filtered iterators on arcs.
Default filter for the graph nodes.
bool operator()(typename GT::Node *) const
Functor for generating Graphviz specifications.
SA sa
the filter of the arcs, which each generation copies
Generate_Graphviz(SA __sa=SA())
Keeps sa, the filter of the arcs.
void operator()(GT &g, std::ostream &out, const std::string &rankdir="TB", float ranksep=0.4, float nodesep=0.4)
Generate DOT specification for the graph.
Functor class for generating Graphviz DOT specifications.
void operator()(const GT &g, std::ostream &out, const Node_Attr &node_attr=Node_Attr(), const Arc_Attr &arc_attr=Arc_Attr(), const std::string &rankdir="LR")
Generate DOT specification for a graph.
void ranks(const GT &g, std::ostream &out, const Node_Attr &node_attr=Node_Attr(), const Arc_Attr &arc_attr=Arc_Attr(), const std::string &rankdir="LR")
void digraph(const GT &g, std::ostream &out, const Node_Attr &node_attr=Node_Attr(), const Arc_Attr &arc_attr=Arc_Attr(), const std::string &rankdir="LR")
SA sa
the filter of the arcs, which each generation copies
To_Graphviz(SA __sa=SA())
Keeps sa, the filter of the arcs.
std::string operator()(typename GT::Arc *) const
std::string operator()(typename GT::Node *) const
Topological sorting algorithms for directed acyclic graphs (DAGs).
Lazy and scalable dynamic array implementation.
Generic graph and digraph implementations.
Comprehensive sorting algorithms and search utilities for Aleph-w.