demo/graph_orientation.cc
author kpeter
Thu, 13 Nov 2008 15:29:04 +0000
changeset 2629 84354c78b068
parent 2510 bb523a4758f7
permissions -rw-r--r--
Improved constructors for min cost flow classes
Removing the non-zero lower bounds is faster
alpar@1678
     1
/* -*- C++ -*-
alpar@1678
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@2553
     5
 * Copyright (C) 2003-2008
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1678
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@1678
     8
 *
alpar@1678
     9
 * Permission to use, modify and distribute this software is granted
alpar@1678
    10
 * provided that this copyright notice appears in all copies. For
alpar@1678
    11
 * precise terms see the accompanying LICENSE file.
alpar@1678
    12
 *
alpar@1678
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@1678
    14
 * express or implied, and with no claim as to its suitability for any
alpar@1678
    15
 * purpose.
alpar@1678
    16
 *
alpar@1678
    17
 */
alpar@1678
    18
alpar@1678
    19
#include <lemon/list_graph.h>
alpar@1678
    20
#include <lemon/graph_reader.h>
alpar@1678
    21
#include <lemon/iterable_maps.h>
alpar@2207
    22
#include <lemon/dim2.h>
alpar@1678
    23
#include <lemon/graph_to_eps.h>
alpar@1678
    24
alpar@1678
    25
alpar@1678
    26
using namespace lemon;
alpar@1678
    27
 
deba@2510
    28
GRAPH_TYPEDEFS(ListGraph);
alpar@1678
    29
alpar@1678
    30
int main(int argc, char** argv) 
alpar@1678
    31
{
alpar@1678
    32
  if(argc!=2) {
alpar@1678
    33
    std::cerr << "\n  USAGE: " << argv[0]
alpar@1678
    34
	      << " input_file.lgf" << std::endl << std::endl;
alpar@1678
    35
    std::cerr << "  The file 'input_file.lgf' has to contain "
alpar@1678
    36
	      << "at least three node maps called \n  'f', 'coordinates_x' "
alpar@1678
    37
	      << "and 'coordinates_y'.\n"
alpar@1678
    38
	      << "  The in-degree requirement is given by 'f', while the two "
alpar@1678
    39
	      << "others is used\n  to create to output drawing."
alpar@1678
    40
	      << std::endl;
alpar@1678
    41
    return 1;
alpar@1678
    42
  }
alpar@1678
    43
  
alpar@1678
    44
  ListGraph g;
alpar@1678
    45
alpar@1678
    46
  ListGraph::NodeMap<int> f(g); //in-deg requirement;
deba@1901
    47
  ListGraph::NodeMap<int> label(g);
alpar@2207
    48
  ListGraph::NodeMap<dim2::Point<double> > coords(g);
alpar@1678
    49
  
alpar@1678
    50
  try {
alpar@1678
    51
    GraphReader<ListGraph> reader(argv[1],g);
alpar@1678
    52
    reader.readNodeMap("f",f);
deba@1901
    53
    reader.readNodeMap("label",label);
alpar@1678
    54
    reader.readNodeMap("coordinates_x",xMap(coords));
alpar@1678
    55
    reader.readNodeMap("coordinates_y",yMap(coords));
alpar@1678
    56
    reader.run();
alpar@1678
    57
  } catch (DataFormatError& error) {
alpar@1678
    58
    std::cerr << error.what() << std::endl;
alpar@1678
    59
    return 1;
alpar@1678
    60
  }
alpar@1678
    61
alpar@1678
    62
  
alpar@1678
    63
  ListGraph::NodeMap<int> level(g,0);
alpar@1678
    64
  
alpar@1678
    65
  ListGraph::NodeMap<int> def(g); //deficiency of the nodes
alpar@1678
    66
  def = subMap(f,InDegMap<ListGraph>(g));
alpar@1678
    67
  
deba@1913
    68
  IterableBoolMap<ListGraph, Node> active(g,false);
alpar@1678
    69
  for(NodeIt n(g);n!=INVALID;++n) active[n]=(def[n]>0);
alpar@1678
    70
  
alpar@2158
    71
  ListGraph::EdgeMap<bool> rev(g,false); // rev[e]==true <=> e is to be 
alpar@1678
    72
                                         //                  reversed
alpar@1678
    73
alpar@1678
    74
  int nodeNum=countNodes(g);
alpar@1678
    75
  
alpar@1678
    76
  Node act;
deba@1913
    77
  while((act=IterableBoolMap<ListGraph, Node>::TrueIt(active))!=INVALID) {
deba@1901
    78
    std::cout << "Process node " << label[act]
alpar@1678
    79
	      << " (def=" << def[act]
alpar@1678
    80
	      << " lev=" << level[act] << "): ";
alpar@1678
    81
    OutEdgeIt e(g,act);
alpar@1678
    82
    while(e!=INVALID && level[g.target(e)]>=level[act]) ++e;
alpar@1678
    83
    if(e!=INVALID) {
alpar@1678
    84
      std::cout << " REVERT EDGE " << g.id(e)
deba@1901
    85
		<< " (" << label[g.source(e)] << "---"
deba@1901
    86
		<< label[g.target(e)] << ")"
alpar@1678
    87
		<< std::endl;
alpar@1678
    88
      if(--def[act]==0) active[act]=false;
alpar@1678
    89
      if(++def[g.target(e)]>0) active[g.target(e)]=true;
alpar@1678
    90
      g.reverseEdge(e);
alpar@1687
    91
      rev[e]=!rev[e];
alpar@1678
    92
    }
alpar@1678
    93
    else {
alpar@1678
    94
      std::cout << " LIFT" << std::endl;
alpar@1678
    95
      if(++level[act]>nodeNum) {
alpar@1678
    96
	std::cout << "NINCS ILYEN\n";
alpar@1678
    97
	return 1;
alpar@1678
    98
      }
alpar@1678
    99
    }
alpar@1678
   100
  }
alpar@1678
   101
  
alpar@1678
   102
  //Show the graph
alpar@1678
   103
alpar@1687
   104
  graphToEps(g,"graph_orientation.eps").scaleToA4().
alpar@1678
   105
    title("Sample .eps figure (fits to A4)").
alpar@2391
   106
    copyright("(C) 2003-2007 LEMON Project").
alpar@2310
   107
    absoluteNodeSizes().absoluteEdgeWidths().
alpar@1678
   108
    nodeScale(15).
alpar@1678
   109
    coords(coords).
alpar@1678
   110
    negateY().
alpar@2172
   111
    edgeColors(composeMap(Palette(),rev)).
alpar@1678
   112
    edgeWidthScale(1).
alpar@1678
   113
    nodeTexts(f).nodeTextSize(20).
alpar@1678
   114
    drawArrows().arrowWidth(10).arrowLength(10).
alpar@1678
   115
    run();
alpar@1678
   116
alpar@1678
   117
  
alpar@1678
   118
} //end of main()
alpar@1678
   119
alpar@1678
   120