demo/coloring.cc
author alpar
Wed, 20 Jul 2005 08:01:16 +0000
changeset 1570 da93692e6537
parent 1435 8e85e6bbefdf
child 1577 15098fb5275c
permissions -rw-r--r--
docfix
deba@1422
     1
#include <vector>
deba@1422
     2
deba@1422
     3
#include <lemon/smart_graph.h>
deba@1422
     4
#include <lemon/bin_heap.h>
deba@1422
     5
#include <lemon/graph_reader.h>
deba@1422
     6
#include <lemon/graph_to_eps.h>
deba@1422
     7
athos@1560
     8
#include <fstream>
athos@1560
     9
#include <iostream>
athos@1560
    10
athos@1560
    11
deba@1422
    12
using namespace std;
deba@1422
    13
using namespace lemon;
deba@1422
    14
athos@1560
    15
int main(int argc, char *argv[]) 
athos@1560
    16
{
athos@1560
    17
  if(argc<2)
athos@1560
    18
  {
athos@1560
    19
      std::cerr << "USAGE: coloring <input_file.lgf>" << std::endl;
athos@1560
    20
      std::cerr << "The file 'input_file.lgf' has to contain a graph in LEMON format together with a nodemap called 'coords' to draw the graph (e.g. sample.lgf is not such a file)." << std::endl;
athos@1560
    21
      return 0;
athos@1560
    22
  }
athos@1560
    23
athos@1560
    24
athos@1560
    25
  //input stream to read the graph from
athos@1560
    26
  std::ifstream is(argv[1]);
athos@1560
    27
deba@1422
    28
  typedef UndirSmartGraph Graph;
deba@1422
    29
  typedef Graph::Node Node;
deba@1422
    30
  typedef Graph::NodeIt NodeIt;
deba@1422
    31
  typedef Graph::UndirEdge UndirEdge;
deba@1422
    32
  typedef Graph::IncEdgeIt IncEdgeIt;
deba@1422
    33
deba@1422
    34
  Graph graph;
deba@1422
    35
athos@1560
    36
  UndirGraphReader<Graph> reader(is, graph);
deba@1422
    37
  Graph::NodeMap<xy<double> > coords(graph);
deba@1422
    38
  reader.readNodeMap("coords", coords);
deba@1422
    39
  
deba@1422
    40
  reader.run();
deba@1422
    41
deba@1422
    42
  Graph::NodeMap<int> color(graph, -2);
deba@1422
    43
  
deba@1422
    44
  Graph::NodeMap<int> heapMap(graph, -1);
deba@1422
    45
  BinHeap<Node, int, Graph::NodeMap<int> > heap(heapMap);
deba@1422
    46
  
deba@1422
    47
  for (NodeIt it(graph); it != INVALID; ++it) {
deba@1422
    48
    heap.push(it, countOutEdges(graph, it));
deba@1422
    49
  }
deba@1422
    50
deba@1422
    51
  vector<Node> order;
deba@1422
    52
deba@1422
    53
  while (!heap.empty()) {
deba@1422
    54
    Node node = heap.top();
deba@1422
    55
    heap.pop();
deba@1422
    56
    color[node] = -1;
deba@1422
    57
    order.push_back(node);
deba@1422
    58
    for (IncEdgeIt it(graph, node); it != INVALID; ++it) {
deba@1422
    59
      Node target = graph.runningNode(it); 
deba@1422
    60
      if (color[target] == -2) {
deba@1422
    61
	heap.decrease(target, heap[target] - 1);
deba@1422
    62
      }
deba@1422
    63
    }
deba@1422
    64
  }
deba@1422
    65
deba@1422
    66
  for (int i = order.size() - 1; i >= 0; --i) {
deba@1422
    67
    set<int> forbidden;
deba@1422
    68
    for (IncEdgeIt it(graph, order[i]); it != INVALID; ++it) {
deba@1422
    69
      Node target = graph.runningNode(it); 
deba@1422
    70
      if (color[target] != -1) {
deba@1422
    71
	forbidden.insert(color[target]);
deba@1422
    72
      }
deba@1422
    73
    }
deba@1422
    74
    int current = 0;
deba@1422
    75
    while (forbidden.find(current) != forbidden.end()) ++current;
deba@1422
    76
    color[order[i]] = current;
deba@1422
    77
  }
deba@1422
    78
deba@1422
    79
  ColorSet colorSet;
deba@1422
    80
deba@1422
    81
  graphToEps(graph, "six_coloring.eps").
deba@1422
    82
    title("Six Colored Graph").copyright("(C) 2005 LEMON Project").
deba@1422
    83
    coords(coords).nodeColors(composeMap(colorSet, color)).
deba@1422
    84
    scaleToA4().run();
deba@1422
    85
deba@1422
    86
  return 0;
deba@1422
    87
}