demo/graph_orientation.cc
author deba
Wed, 01 Mar 2006 12:46:52 +0000
changeset 1992 6e1b62d42d94
parent 1913 49fe71fce7fb
child 2158 0b620ff10e7c
permissions -rw-r--r--
UNDIRGRAPH_TYPEDEFS => UGRAPH_TYPEDEFS
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@1956
     5
 * Copyright (C) 2003-2006
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@1678
    22
#include <lemon/xy.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
 
alpar@1678
    28
alpar@1678
    29
typedef ListGraph::Node Node;
alpar@1678
    30
typedef ListGraph::NodeIt NodeIt;
alpar@1678
    31
typedef ListGraph::Edge Edge;
alpar@1678
    32
typedef ListGraph::EdgeIt EdgeIt;
alpar@1678
    33
typedef ListGraph::OutEdgeIt OutEdgeIt;
alpar@1678
    34
typedef ListGraph::InEdgeIt InEdgeIt;
alpar@1678
    35
alpar@1678
    36
int main(int argc, char** argv) 
alpar@1678
    37
{
alpar@1678
    38
  if(argc!=2) {
alpar@1678
    39
    std::cerr << "\n  USAGE: " << argv[0]
alpar@1678
    40
	      << " input_file.lgf" << std::endl << std::endl;
alpar@1678
    41
    std::cerr << "  The file 'input_file.lgf' has to contain "
alpar@1678
    42
	      << "at least three node maps called \n  'f', 'coordinates_x' "
alpar@1678
    43
	      << "and 'coordinates_y'.\n"
alpar@1678
    44
	      << "  The in-degree requirement is given by 'f', while the two "
alpar@1678
    45
	      << "others is used\n  to create to output drawing."
alpar@1678
    46
	      << std::endl;
alpar@1678
    47
    return 1;
alpar@1678
    48
  }
alpar@1678
    49
  
alpar@1678
    50
  ListGraph g;
alpar@1678
    51
alpar@1678
    52
  ListGraph::NodeMap<int> f(g); //in-deg requirement;
deba@1901
    53
  ListGraph::NodeMap<int> label(g);
alpar@1678
    54
  ListGraph::NodeMap<xy<double> > coords(g);
alpar@1678
    55
  
alpar@1678
    56
  try {
alpar@1678
    57
    GraphReader<ListGraph> reader(argv[1],g);
alpar@1678
    58
    reader.readNodeMap("f",f);
deba@1901
    59
    reader.readNodeMap("label",label);
alpar@1678
    60
    reader.readNodeMap("coordinates_x",xMap(coords));
alpar@1678
    61
    reader.readNodeMap("coordinates_y",yMap(coords));
alpar@1678
    62
    reader.run();
alpar@1678
    63
  } catch (DataFormatError& error) {
alpar@1678
    64
    std::cerr << error.what() << std::endl;
alpar@1678
    65
    return 1;
alpar@1678
    66
  }
alpar@1678
    67
alpar@1678
    68
  
alpar@1678
    69
  ListGraph::NodeMap<int> level(g,0);
alpar@1678
    70
  
alpar@1678
    71
  ListGraph::NodeMap<int> def(g); //deficiency of the nodes
alpar@1678
    72
  def = subMap(f,InDegMap<ListGraph>(g));
alpar@1678
    73
  
deba@1913
    74
  IterableBoolMap<ListGraph, Node> active(g,false);
alpar@1678
    75
  for(NodeIt n(g);n!=INVALID;++n) active[n]=(def[n]>0);
alpar@1678
    76
  
alpar@1678
    77
  ListGraph::EdgeMap<bool> rev(g,false); // rev[e]==true <=> e is be 
alpar@1678
    78
                                         //                  reversed
alpar@1678
    79
alpar@1678
    80
  int nodeNum=countNodes(g);
alpar@1678
    81
  
alpar@1678
    82
  Node act;
deba@1913
    83
  while((act=IterableBoolMap<ListGraph, Node>::TrueIt(active))!=INVALID) {
deba@1901
    84
    std::cout << "Process node " << label[act]
alpar@1678
    85
	      << " (def=" << def[act]
alpar@1678
    86
	      << " lev=" << level[act] << "): ";
alpar@1678
    87
    OutEdgeIt e(g,act);
alpar@1678
    88
    while(e!=INVALID && level[g.target(e)]>=level[act]) ++e;
alpar@1678
    89
    if(e!=INVALID) {
alpar@1678
    90
      std::cout << " REVERT EDGE " << g.id(e)
deba@1901
    91
		<< " (" << label[g.source(e)] << "---"
deba@1901
    92
		<< label[g.target(e)] << ")"
alpar@1678
    93
		<< std::endl;
alpar@1678
    94
      if(--def[act]==0) active[act]=false;
alpar@1678
    95
      if(++def[g.target(e)]>0) active[g.target(e)]=true;
alpar@1678
    96
      g.reverseEdge(e);
alpar@1687
    97
      rev[e]=!rev[e];
alpar@1678
    98
    }
alpar@1678
    99
    else {
alpar@1678
   100
      std::cout << " LIFT" << std::endl;
alpar@1678
   101
      if(++level[act]>nodeNum) {
alpar@1678
   102
	std::cout << "NINCS ILYEN\n";
alpar@1678
   103
	return 1;
alpar@1678
   104
      }
alpar@1678
   105
    }
alpar@1678
   106
  }
alpar@1678
   107
  
alpar@1678
   108
  //Show the graph
alpar@1678
   109
alpar@1687
   110
  graphToEps(g,"graph_orientation.eps").scaleToA4().
alpar@1678
   111
    title("Sample .eps figure (fits to A4)").
alpar@1875
   112
    copyright("(C) 2006 LEMON Project").
alpar@1678
   113
    nodeScale(15).
alpar@1678
   114
    coords(coords).
alpar@1678
   115
    negateY().
alpar@1678
   116
    edgeColors(composeMap(ColorSet(),rev)).
alpar@1678
   117
    edgeWidthScale(1).
alpar@1678
   118
    nodeTexts(f).nodeTextSize(20).
alpar@1678
   119
    drawArrows().arrowWidth(10).arrowLength(10).
alpar@1678
   120
    run();
alpar@1678
   121
alpar@1678
   122
  
alpar@1678
   123
} //end of main()
alpar@1678
   124
alpar@1678
   125