Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2012
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. #ifndef GRAPHTOGRAPHVIZ_H_
  2. #define GRAPHTOGRAPHVIZ_H_
  3.  
  4. #include <lemon/list_graph.h>
  5. #include <unordered_map>
  6.  
  7. using namespace lemon;
  8. using namespace std;
  9.  
  10. /* USAGE:
  11.  * ListDigraph::NodeMap<unordered_map<string, string>> nodeAttribs(g);
  12.  * ListDigraph::ArcMap<unordered_map<string, string>> arcAttribs(g);
  13.  * nodeAttribs[node]["label"] = "node_label";
  14.  * string dot = graphToGraphviz(g, &nodeAttribs, &arcAttribs, "hello");
  15.  */
  16.  
  17. template<class Map>
  18. string getAttribs(Map &map){
  19.     string attribs = "";
  20.     for (const auto &el : map){
  21.         if (el.second != "")
  22.             attribs += "\"" + el.first + "\"=\"" + el.second + "\",";
  23.     }
  24.     if (attribs != "")
  25.         attribs = " [" + attribs + "]";
  26.     return attribs;
  27. }
  28.  
  29.  
  30. template<class Graph,
  31.          class NodeAttribs = ListDigraph::NodeMap<std::unordered_map<string, string>>,
  32.          class ArcAttribs = ListDigraph::ArcMap<std::unordered_map<string, string>>
  33.          >
  34. string graphToGraphviz(Graph       &graph,
  35.                        NodeAttribs *nattribs = NULL,
  36.                        ArcAttribs  *aattribs = NULL,
  37.                        string      name      = "")
  38. {
  39.  
  40.     typedef typename Graph::template NodeMap<string> NodeMap;
  41.     typedef typename Graph::NodeIt NodeIterator;
  42.     typedef typename Graph::ArcIt  ArcIterator;
  43.  
  44.     NodeMap labels(graph);
  45.     ostringstream layout;
  46.     layout << "strict digraph \""+name+"\" {\n";
  47.  
  48.     // prepare labels
  49.     for (NodeIterator node(graph); node != INVALID; ++node){
  50.         string label = "";
  51.         if (nattribs != NULL)
  52.             label = (*nattribs)[node]["label"];
  53.         if (label == "") label = static_cast<ostringstream*>( &(ostringstream() << graph.id(node)) )->str();
  54.         label = "\"" + label + "\"";
  55.         labels[node] = label;
  56.     }
  57.  
  58.     // initialize nodes
  59.     for (NodeIterator node(graph); node != INVALID; ++node){
  60.         layout << labels[node];
  61.         if (nattribs != NULL)
  62.             layout << getAttribs((*nattribs)[node]);
  63.         layout << ";" << std::endl;
  64.     }
  65.  
  66.     // initialize arcs
  67.     for (ArcIterator arc(graph); arc != INVALID; ++arc){
  68.         layout << labels[graph.source(arc)] << "->" << labels[graph.target(arc)];
  69.         if (aattribs != NULL)
  70.             layout << getAttribs((*aattribs)[arc]);
  71.         layout << ";" << std::endl;
  72.     }
  73.     layout << "}";
  74.     return layout.str();
  75. }
  76.  
  77.  
  78. #endif /* GRAPHTOGRAPHVIZ_H_ */
  79.  
  80. int main ()
  81. {
  82.     lemon::ListDigraph g;
  83.     graphToGraphviz(g);
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement