demo/dim_to_dot.cc
author alpar
Wed, 17 Aug 2005 22:07:35 +0000
changeset 1641 77f6ab7ad66f
parent 1577 15098fb5275c
child 1875 98698b69a902
permissions -rw-r--r--
Demos' documentations include the source.
     1 /* -*- C++ -*-
     2  * demo/dim_to_dot.cc - Part of LEMON, a generic C++ optimization library
     3  *
     4  * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     5  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     6  *
     7  * Permission to use, modify and distribute this software is granted
     8  * provided that this copyright notice appears in all copies. For
     9  * precise terms see the accompanying LICENSE file.
    10  *
    11  * This software is provided "AS IS" with no warranty of any kind,
    12  * express or implied, and with no claim as to its suitability for any
    13  * purpose.
    14  *
    15  */
    16 
    17 // Use a DIMACS max flow file as stdin.
    18 // dim_to_dot < dimacs_max_flow_file > dot_output_file
    19 // This program makes a dot file from a dimacs max flow file. 
    20 // This program can be an aid in making up to date visualized documantation 
    21 // of demo programs.
    22 
    23 // For later documentation (if marci does not do it)
    24 // Az a graphviz csomag egy egyszeru formatuma, ami egy graphrajzolo csomag.
    25 // Az EdgeSubGraphAdaptor doksijaban szerepel egy kirajzolt graf. Azt nem
    26 // kezzel csinaltam, hanem a megfelelo dim file-bol ezzel a progival. A
    27 // doxygen ugyanis ilyet eszik, igy a juzer vizualisan is latja a grafot a
    28 // doksiban, es sajat maga is le tudja futtatni az algoritmust, mert ott van
    29 // a kezeben a dim file is. Es mivel ez egy generalt file, ezert ha vmit
    30 // valtoztatunk a dim-en, ezt is konnyu bemasolni. Uff.
    31 
    32 
    33 #include <iostream>
    34 #include <fstream>
    35 
    36 #include <lemon/smart_graph.h>
    37 #include <lemon/dimacs.h>
    38 
    39 using namespace lemon;
    40 
    41 using std::cout;
    42 using std::endl;
    43 
    44 int main(int argc, char *argv[]) 
    45 {
    46   if(argc<2)
    47   {
    48       std::cerr << "USAGE: sub_graph_adaptor_demo input_file.dim" << std::endl;
    49       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;
    50       return 0;
    51   }
    52 
    53 
    54   //input stream to read the graph from
    55   std::ifstream is(argv[1]);
    56 
    57   typedef SmartGraph Graph;
    58 
    59   typedef Graph::Edge Edge;
    60   typedef Graph::Node Node;
    61   typedef Graph::EdgeIt EdgeIt;
    62   typedef Graph::NodeIt NodeIt;
    63   typedef Graph::EdgeMap<int> LengthMap;
    64 
    65   Graph g;
    66   Node s, t;
    67   LengthMap length(g);
    68 
    69   readDimacs(is, g, length, s, t);
    70 
    71   cout << "digraph lemon_dot_example {" << endl;
    72   cout << "  node [ shape=ellipse, fontname=Helvetica, fontsize=10 ];" << endl;
    73   for(NodeIt n(g); n!=INVALID; ++n) {
    74     if (n==s) {
    75       cout << "  n" << g.id(n) 
    76 	   << " [ label=\"" << g.id(n) << " (s)\" ]; " << endl;
    77     } else {
    78       if (n==t) {
    79 	cout << "  n" << g.id(n) 
    80 	     << " [ label=\"" << g.id(n) << " (t)\" ]; " << endl; 
    81       } else {
    82 	cout << "  n" << g.id(n) 
    83 	     << " [ label=\"" << g.id(n) << "\" ]; " << endl; 
    84       }
    85     }
    86   }
    87   cout << "  edge [ shape=ellipse, fontname=Helvetica, fontsize=10 ];" << endl;
    88   for(EdgeIt e(g); e!=INVALID; ++e) {
    89     cout << "  n" << g.id(g.source(e)) << " -> " << " n" << g.id(g.target(e))
    90 	 << " [ label=\"" << g.id(e) 
    91 	 << ", length:" << length[e] << "\" ]; " << endl;
    92   } 
    93   cout << "}" << endl;
    94 }