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