Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
generate_graph.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
98# ifndef GENERATE_GRAPH_H
99# define GENERATE_GRAPH_H
100
101# include <ah-graph-concepts.H>
102
103# include <fstream>
104# include <tpl_dynArray.H>
105# include <tpl_sort_utils.H>
106# include <tpl_graph.H>
107# include <topological_sort.H>
108
109using namespace Aleph;
110
111namespace Aleph {
112
113
114 template <AlephGraph GT, ArcFilter<GT> SA> inline static
115 bool is_there_a_double_arc(const GT * g,
116 typename GT::Node * src,
117 typename GT::Node * tgt) noexcept
118 {
119 if (not g->is_digraph())
120 return false;
121
122 return search_arc<GT,SA>(*g, src, tgt) != nullptr and
123 search_arc<GT,SA>(*g, tgt, src) != nullptr;
124 }
125
126
127 template <AlephGraph GT>
129 typename GT::Node * p) noexcept
130 {
131 return sequential_search(nodes, p, 0, nodes.size() - 1);
132 }
133
134
158 template <AlephGraph GT,
159 class Write_Node, class Write_Arc,
160 class Shade_Node, class Shade_Arc, ArcFilter<GT> SA>
161 void generate_graphpic(const GT & g,
162 const double & xdist,
163 const double & ydist,
164 std::ostream & output)
165 {
167 typename GT::Node_Iterator it(g);
168 for (int i = 0; it.has_curr(); it.next_ne(), ++i)
169 {
170 auto p = it.get_current_node_ne();
171
172 nodes[i] = p;
173
174 if (Shade_Node() (p).size() != 0)
175 output << Shade_Node() (p) << " " << i << std::endl;
176
177 const std::string text_node = Write_Node () (p);
178
179 if (text_node.size() == 0)
180 continue;
181
182 output << "NODE-TEXT " << i << " \"" << text_node << "\" 0 0" << std::endl;
183 }
184
185 for (Arc_Iterator<GT, SA> it(g); it.has_curr(); it.next_ne())
186 {
187 auto a = it.get_current_arc_ne();
188 auto src = g.get_src_node(a);
189 auto tgt = g.get_tgt_node(a);
190
191 const auto src_idx = search_node <GT> (nodes, src);
192 const auto tgt_idx = search_node <GT> (nodes, tgt);
193
194 if (is_there_a_double_arc <GT, SA> (&g, src, tgt))
195 output << "CURVE-ARC " << src_idx << " " << tgt_idx << " "
196 << xdist/5 << " L" << std::endl;
197 else
198 output << "ARC " << src_idx << " " << tgt_idx << std::endl;
199
200 if ( Shade_Arc()(a).size() != 0)
201 output << Shade_Arc()(a) << " "
202 << src_idx << " " << tgt_idx << " " << std::endl;
203
204 const std::string text_arc = Write_Arc() (a);
205
206 if (text_arc.size() == 0)
207 continue;
208
209 output << "ARC-TEXT " << src_idx << " " << tgt_idx << " \""
210 << text_arc << "\" 0 0 " << std::endl;
211 }
212 }
213
240 template <AlephGraph GT,
241 class Write_Node,
242 class Write_Arc,
243 class Shade_Node,
244 class Shade_Arc,
245 class Dashed_Node,
246 class Dashed_Arc,
247 ArcFilter<GT> SA,
249 void generate_graphviz(const GT & g, std::ostream & output,
250 const std::string & rankdir = "TB",
251 float ranksep = 0.2,
252 float nodesep = 0.2)
253 {
254 output << "// Generated by generate_graphviz() from Aleph-w library. See at:" << std::endl
255 << "// http://webdelprofesor.ula.ve/ingenieria/lrleon/aleph/html/index.html" << std::endl
256 << "// for documentation" << std::endl
257 << "// Copyleft Leandro Rabindranath Leon lrleon@ula.ve" << std::endl
258 << "// for using of graphviz system. See at http://graphviz.org/"
259 << std::endl << std::endl;
260 std::string arc_str;
261 if (g.is_digraph())
262 {
263 arc_str = " -> ";
264 output << "digraph {" << std::endl;
265 }
266 else
267 {
268 arc_str = " -- ";
269 output << "graph {" << std::endl;
270 }
271 output << std::endl
272 << "rankdir = " << rankdir << std::endl
273 << "style = none" << std::endl
274 << "truecolor=false" << std::endl
275 << "ranksep = " << ranksep << std::endl
276 << "nodesep = " << nodesep << std::endl << std::endl;
277
279
281 for (int i = 0; it.has_curr(); it.next_ne(), ++i)
282 {
283 output << i << " [ ";
284
285 auto p = it.get_current_node_ne();
286 nodes[i] = p;
287
288 if (Shade_Node () (p))
289 output << "style = bold ";
290
291 const std::string text_node = Write_Node () (p);
292
293 if (text_node.size() != 0)
294 output << "label = \"" << text_node << "\"";
295 output << "]" << std::endl;
296 }
297
298 output << std::endl;
299
300 for (Arc_Iterator<GT, SA> it(g); it.has_curr(); it.next_ne())
301 {
302 auto a = it.get_current_arc_ne();
303 auto src = g.get_src_node(a);
304 auto tgt = g.get_tgt_node(a);
305
306 auto src_idx = search_node <GT> (nodes, src);
307 auto tgt_idx = search_node <GT> (nodes, tgt);
308
309 output << src_idx << arc_str << tgt_idx << " [";
310
311 if (Shade_Arc () (a))
312 output << "style = bold ";
313
314 const std::string text_arc = Write_Arc() (a);
315
316 if (text_arc.size() != 0)
317 output << "label = \"" << text_arc << "\"";
318 output <<"]" << std::endl;
319 }
320
321 output << "}" << std::endl;
322 }
323
324
360 template <AlephGraph GT,
361 class Node_Attr,
362 class Arc_Attr,
364 ArcFilter<GT> SA>
365 void generate_graphviz(const GT & g, std::ostream & out,
368 const std::string & rankdir = "TB")
369 {
370 out << "// Generated by generate_graphviz() from Aleph-w library" << std::endl
371 << "// See at:"
372 << "// http://webdelprofesor.ula.ve/ingenieria/lrleon/aleph/html/index.html" << std::endl
373 << "// for documentation of Aleph-w library" << std::endl
374 << "// Copyleft Leandro Rabindranath Leon lrleon@ula.ve" << std::endl
375 << "// for using of graphviz system. See at http://graphviz.org/"
376 << std::endl << std::endl
377 << (g.is_digraph() ? "digraph {" : "graph {") << std::endl
378 << std::endl
379 << "rankdir = " << rankdir << std::endl
380 << std::endl
381 << "// Node list" << std::endl
382 << std::endl;
383
385
387 for (int i = 0; it.has_curr(); it.next_ne(), ++i)
388 {
389 auto p = it.get_current_node_ne();
390
391 nodes_table.insert(p, i);
392
393 out << i << " [ ";
394
395 node_attr (g, p, out);
396
397 out << "]" << std::endl;
398 }
399
400 out << std::endl
401 << std::endl
402 << "// Arc list" << std::endl
403 << std::endl;
404
405 const std::string arrow = g.is_digraph() ? "->" : "--";
406
407 for (Arc_Iterator<GT, SA> it(g); it.has_curr(); it.next_ne())
408 {
409 auto a = it.get_current_arc_ne();
410 auto src = g.get_src_node(a);
411 auto tgt = g.get_tgt_node(a);
412
413 const auto src_idx = nodes_table.find(src);
414 const auto tgt_idx = nodes_table.find(tgt);
415
416 out << src_idx << arrow << tgt_idx << " [";
417 arc_attr (g, a, out) ;
418 out << "]" << std::endl;
419 }
420
421 out << "}" << std::endl;
422 }
423
445 template <AlephGraph GT,
446 class Node_Attr,
447 class Arc_Attr,
449 ArcFilter<GT> SA>
450 void digraph_graphviz(const GT & g, std::ostream & out,
453 const std::string & rankdir = "LR")
454 {
455 out << "// Generated by generate_graphviz() from Aleph-w library" << std::endl
456 << "// See at:"
457 << "// http://webdelprofesor.ula.ve/ingenieria/lrleon/aleph/html/index.html" << std::endl
458 << "// for documentation of Aleph-w library" << std::endl
459 << "// Copyleft Leandro Rabindranath Leon lrleon@ula.ve" << std::endl
460 << "// for using of graphviz system. See at http://graphviz.org/"
461 << std::endl << std::endl
462 << "digraph {" << std::endl
463 << std::endl
464 << "rankdir = " << rankdir << std::endl
465 << std::endl
466 << "// Node list" << std::endl
467 << std::endl;
468
470
472 for (int i = 0; it.has_curr(); it.next_ne(), ++i)
473 {
474 auto p = it.get_current_node_ne();
475 nodes_table.insert(p, i);
476
477 out << i << " [ ";
478
479 node_attr (g, p, out);
480
481 out << "]" << std::endl;
482 }
483
484 out << std::endl
485 << std::endl
486 << "// Arc list" << std::endl
487 << std::endl;
488
489 const std::string arrow = "->";
490
491 for (Arc_Iterator<GT, SA> it(g); it.has_curr(); it.next_ne())
492 {
493 auto a = it.get_current_arc_ne();
494 auto src = g.get_src_node(a);
495 auto tgt = g.get_tgt_node(a);
496
497 const auto src_idx = nodes_table.find(src);
498 const auto tgt_idx = nodes_table.find(tgt);
499
500 out << src_idx << arrow << tgt_idx << " [";
501 arc_attr (g, a, out) ;
502 out << "]" << std::endl;
503 }
504
505 out << "}" << std::endl;
506 }
507
533 template <AlephGraph GT,
534 class Node_Attr,
535 class Arc_Attr,
537 ArcFilter<GT> SA>
538 size_t rank_graphviz(const GT & g, std::ostream & out,
541 const std::string & rankdir = "LR")
542 {
543 out << "// Generated by generate_graphviz() from Aleph-w library" << std::endl
544 << "// See at:"
545 << "// http://webdelprofesor.ula.ve/ingenieria/lrleon/aleph/html/index.html" << std::endl
546 << "// for documentation of Aleph-w library" << std::endl
547 << "// Copyleft Leandro Rabindranath Leon lrleon@ula.ve" << std::endl
548 << "// for using of graphviz system. See at http://graphviz.org/"
549 << std::endl << std::endl
550 << "digraph {" << std::endl
551 << std::endl
552 << "rankdir = " << rankdir << std::endl
553 << "rank = same" << std::endl
554 << std::endl
555 << "// Node list" << std::endl
556 << std::endl;
557
560 size_t rank = 0, i = 0;
561 for (auto rank_it = ranks.get_it(); rank_it.has_curr();
562 rank_it.next_ne(), ++rank)
563 {
564 out << "subgraph rank_" << rank << std::endl
565 << "{" << std::endl
566 << "label = \"rank " << rank << "\"" << std::endl;
567 for (auto it = rank_it.get_curr().get_it(); it.has_curr();
568 it.next_ne(), ++i)
569 {
570 auto p = it.get_curr();
571 nodes_table.insert(p, i);
572 out << i << " [ ";
573 node_attr(g, p, out);
574 out << "]" << std::endl;
575 }
576 out << "}" << std::endl;
577 }
578
579 out << std::endl
580 << std::endl
581 << "// Arc list" << std::endl
582 << std::endl;
583
584 const std::string arrow = "->";
585 for (Arc_Iterator<GT, SA> it(g); it.has_curr(); it.next_ne())
586 {
587 auto a = it.get_current_arc_ne();
588 auto src = g.get_src_node(a);
589 auto tgt = g.get_tgt_node(a);
590
591 const auto src_idx = nodes_table.find(src);
592 const auto tgt_idx = nodes_table.find(tgt);
593
594 out << src_idx << arrow << tgt_idx << " [";
595 arc_attr (g, a, out) ;
596 out << "]" << std::endl;
597 }
598 out << "}" << std::endl;
599
600 return rank;
601 }
602
603 template <class GT>
605 {
606 void operator () (const GT&, typename GT::Node * p, std::ostream & out)
607 {
608 out << "label = \"" << p->get_info() << "\"";
609 }
610 };
611
612 template <class GT>
614 {
615 void operator () (const GT&, typename GT::Arc * a, std::ostream & out)
616 {
617 out << "label = \"" << a->get_info() << "\"";
618 }
619 };
620
669 template <AlephGraph GT,
675 {
684 void operator () (const GT & g, std::ostream & out,
685 const Node_Attr & node_attr = Node_Attr(),
686 const Arc_Attr & arc_attr = Arc_Attr(),
687 const std::string & rankdir = "LR")
688 {
691 }
692
693 void digraph(const GT & g, std::ostream & out,
694 const Node_Attr & node_attr = Node_Attr(),
695 const Arc_Attr & arc_attr = Arc_Attr(),
696 const std::string & rankdir = "LR")
697 {
700 }
701
702 void ranks(const GT & g, std::ostream & out,
703 const Node_Attr & node_attr = Node_Attr(),
704 const Arc_Attr & arc_attr = Arc_Attr(),
705 const std::string & rankdir = "LR")
706 {
709 }
710 };
711
712
713
714 template <AlephGraph GT>
716 {
717 bool operator () (typename GT::Node *) const { return false; }
718
719 bool operator () (typename GT::Arc *) const { return false; }
720 };
721
722
741 template <AlephGraph GT,
742 class Write_Node,
743 class Write_Arc,
744 class Shade_Node = Dummy_Attr<GT>,
745 class Shade_Arc = Dummy_Attr<GT>,
747 class Dashed_Arc = Dummy_Attr<GT>,
751 {
760 void operator () (GT & g, std::ostream & out,
761 const std::string & rankdir = "TB",
762 float ranksep = 0.4, float nodesep = 0.4)
763 {
766 (g, out, rankdir, ranksep, nodesep);
767 }
768 };
769
791 template <AlephGraph GT,
792 class Write_Node, class Write_Arc,
793 class Shade_Node, class Shade_Arc, ArcFilter<GT> SA>
795 const size_t & nodes_by_level,
796 const double & xdist,
797 const double & ydist,
798 std::ostream & out)
799 {
800 if (g.is_digraph())
801 out << "cross-net-digraph ";
802 else
803 out << "cross-net-graph ";
804
805 out << g.get_num_nodes() << " " << nodes_by_level << " "
806 << xdist << " " << ydist << std::endl
807 << std::endl;
808
810 (g, xdist, ydist, out);
811 }
812
813 template <AlephGraph GT,
814 class Write_Node, class Write_Arc,
815 class Shade_Node, class Shade_Arc>
817 const size_t & nodes_by_level,
818 const double & xdist,
819 const double & ydist,
820 std::ostream & out)
821 {
822 typedef Dft_Show_Arc<GT> DSA;
825 }
826
847 template <AlephGraph GT,
848 class Write_Node, class Write_Arc,
849 class Shade_Node, class Shade_Arc, ArcFilter<GT> SA>
851 const size_t & nodes_by_level,
852 const double & xdist,
853 const double & ydist,
854 std::ostream & out)
855 {
856 if (g.is_digraph())
857 out << "net-digraph ";
858 else
859 out << "net-graph ";
860
861 out << g.get_num_nodes() << " " << nodes_by_level << " "
862 << xdist << " " << ydist << std::endl
863 << std::endl;
864
866 (g, xdist, ydist, out);
867 }
868
869 template <AlephGraph GT,
870 class Write_Node, class Write_Arc,
871 class Shade_Node, class Shade_Arc>
873 const size_t & nodes_by_level,
874 const double & xdist,
875 const double & ydist,
876 std::ostream & out)
877 {
878 typedef Dft_Show_Arc<GT> DSA;
881
882 }
883
884 template <AlephGraph GT> struct __Shade_Node
885 {
886 std::string operator () (typename GT::Node *) const
887 {
888 return "";
889 }
890 };
891
892
893 template <AlephGraph GT> struct __Shade_Arc
894 {
895 std::string operator () (typename GT::Arc *) const
896 {
897 return "";
898 }
899 };
900
901
902 template <AlephGraph GT, class Write_Node, class Write_Arc, ArcFilter<GT> SA>
904 const size_t & nodes_by_level,
905 const double & xdist,
906 const double & ydist,
907 std::ostream & out)
908 {
912 }
913
914 template <AlephGraph GT, class Write_Node, class Write_Arc, ArcFilter<GT> SA>
916 const size_t & nodes_by_level,
917 const double & xdist,
918 const double & ydist,
919 std::ostream & out)
920 {
924 }
925
926
927 template <AlephGraph GT, class Write_Node, class Write_Arc>
929 const size_t & nodes_by_level,
930 const double & xdist,
931 const double & ydist,
932 std::ostream & out)
933 {
934 typedef Dft_Show_Arc<GT> DSA;
938 }
939
940 template <AlephGraph GT, class Write_Node, class Write_Arc>
942 const size_t & nodes_by_level,
943 const double & xdist,
944 const double & ydist,
945 std::ostream & out)
946 {
947 typedef Dft_Show_Arc<GT> DSA;
951 }
952
953
954} // end namespace Aleph
955
956
957# endif // GENERATE_GRAPH_H
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
Definition ca-c-api.h:120
T & insert(const T &data)
insert a copy of data at the beginning of the array.
Definition tpl_array.H:286
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1155
Dynamic map implemented with a treap.
void next_ne() noexcept
Advances the iterator to the next filtered element (noexcept version).
Filtered iterator on the nodes of a graph.
Definition tpl_graph.H:1207
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Definition graph-dry.H:637
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition graph-dry.H:536
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:779
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:737
bool is_digraph() const noexcept
Return true if the graph this is directed.
Definition graph-dry.H:699
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:785
auto get_it() const
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:228
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
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")
Generate Graphviz DOT output with topological ranking.
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")
Generate Graphviz DOT output specifically for digraphs.
void generate_graphpic(const GT &g, const double &xdist, const double &ydist, std::ostream &output)
Generate a graphpic specification for graph visualization.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
void generate_cross_graph(GT &g, const size_t &nodes_by_level, const double &xdist, const double &ydist, std::ostream &out)
Generate a cross-graph layout specification for graphpic.
void generate_graphviz(const GT &g, std::ostream &output, const std::string &rankdir="TB", float ranksep=0.2, float nodesep=0.2)
Generate a Graphviz DOT 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)
Generate a net-graph layout specification for graphpic.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
size_t size(Node *root) noexcept
static int search_node(DynArray< typename GT::Node * > &nodes, typename GT::Node *p) noexcept
and
Check uniqueness with explicit hash + equality functors.
Array< size_t > ranks(const Array< T > &array)
Computes the rank of each element in an Array.
Definition ahSort.H:698
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.
static bool is_there_a_double_arc(const GT *g, typename GT::Node *src, typename GT::Node *tgt) noexcept
Filtered iterator on all the arcs of a graph.
Definition tpl_graph.H:1165
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.
Definition tpl_graph.H:1001
Default filter for the graph nodes.
Definition tpl_graph.H:1193
bool operator()(typename GT::Node *) const
Functor for generating Graphviz specifications.
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")
std::string operator()(typename GT::Arc *) const
std::string operator()(typename GT::Node *) const
Writer that outputs only the node key.
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.
ofstream output
Definition writeHeap.C:215