demo/dim_to_dot.cc
author ladanyi
Wed, 02 Jul 2008 12:37:47 +0000
changeset 2615 2bf1f6e3d5ae
parent 2413 21eb3ccdc3df
permissions -rw-r--r--
Fix bug caused by m4 consuming pairs of square brackets (#108).
     1 /* -*- C++ -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     4  *
     5  * Copyright (C) 2003-2008
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 ///\file
    20 ///\brief Dim (DIMACS) to Dot (Graphviz) converter
    21 ///
    22 /// This program can convert the DIMACS format to Graphviz format.
    23 ///
    24 /// <tt>dim_to_dot dimacs_max_flow_file > dot_output_file</tt>
    25 ///
    26 /// This program makes a dot file from a DIMACS max flow file. 
    27 /// This program can be an aid in making up to date visualized 
    28 /// documantation of demo programs.
    29 ///
    30 /// \include dim_to_dot.cc
    31 
    32 #include <iostream>
    33 #include <fstream>
    34 
    35 #include <lemon/smart_graph.h>
    36 #include <lemon/dimacs.h>
    37 
    38 using namespace lemon;
    39 
    40 using std::cout;
    41 using std::endl;
    42 
    43 int main(int argc, char *argv[]) 
    44 {
    45   if(argc<2)
    46   {
    47       std::cerr << "USAGE: dim_to_dot input_file.dim" << std::endl;
    48       std::cerr << "The file 'input_file.dim' has to contain a max flow instance in DIMACS format (e.g. sub_graph_adaptor_demo.dim is such a file)." << std::endl;
    49       return 0;
    50   }
    51 
    52   //input stream to read the graph from
    53   std::ifstream is(argv[1]);
    54 
    55   typedef SmartGraph Graph;
    56 
    57   typedef Graph::Edge Edge;
    58   typedef Graph::Node Node;
    59   typedef Graph::EdgeIt EdgeIt;
    60   typedef Graph::NodeIt NodeIt;
    61   typedef Graph::EdgeMap<int> LengthMap;
    62 
    63   Graph g;
    64   Node s, t;
    65   LengthMap length(g);
    66 
    67   readDimacs(is, g, length, s, t);
    68 
    69   cout << "digraph lemon_dot_example {" << endl;
    70   cout << "  node [ shape=ellipse, fontname=Helvetica, fontsize=10 ];" << endl;
    71   for(NodeIt n(g); n!=INVALID; ++n) {
    72     if (n==s) {
    73       cout << "  n" << g.id(n) 
    74 	   << " [ label=\"" << g.id(n) << " (s)\" ]; " << endl;
    75     } else {
    76       if (n==t) {
    77 	cout << "  n" << g.id(n) 
    78 	     << " [ label=\"" << g.id(n) << " (t)\" ]; " << endl; 
    79       } else {
    80 	cout << "  n" << g.id(n) 
    81 	     << " [ label=\"" << g.id(n) << "\" ]; " << endl; 
    82       }
    83     }
    84   }
    85   cout << "  edge [ shape=ellipse, fontname=Helvetica, fontsize=10 ];" << endl;
    86   for(EdgeIt e(g); e!=INVALID; ++e) {
    87     cout << "  n" << g.id(g.source(e)) << " -> " << " n" << g.id(g.target(e))
    88 	 << " [ label=\"" << g.id(e) 
    89 	 << ", length:" << length[e] << "\" ]; " << endl;
    90   } 
    91   cout << "}" << endl;
    92 }