demo/coloring.cc
author alpar
Wed, 13 Jul 2005 20:02:29 +0000
changeset 1556 caf0f91e16a7
parent 1422 469b3f628dd1
child 1560 01707a8a4ca6
permissions -rw-r--r--
Avoid ambiguity.
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
deba@1422
     8
using namespace std;
deba@1422
     9
using namespace lemon;
deba@1422
    10
deba@1422
    11
int main() {
deba@1422
    12
  typedef UndirSmartGraph Graph;
deba@1422
    13
  typedef Graph::Node Node;
deba@1422
    14
  typedef Graph::NodeIt NodeIt;
deba@1422
    15
  typedef Graph::UndirEdge UndirEdge;
deba@1422
    16
  typedef Graph::IncEdgeIt IncEdgeIt;
deba@1422
    17
deba@1422
    18
  Graph graph;
deba@1422
    19
deba@1422
    20
  UndirGraphReader<Graph> reader(std::cin, graph);
deba@1422
    21
  Graph::NodeMap<xy<double> > coords(graph);
deba@1422
    22
  reader.readNodeMap("coords", coords);
deba@1422
    23
  
deba@1422
    24
  reader.run();
deba@1422
    25
deba@1422
    26
  Graph::NodeMap<int> color(graph, -2);
deba@1422
    27
  
deba@1422
    28
  Graph::NodeMap<int> heapMap(graph, -1);
deba@1422
    29
  BinHeap<Node, int, Graph::NodeMap<int> > heap(heapMap);
deba@1422
    30
  
deba@1422
    31
  for (NodeIt it(graph); it != INVALID; ++it) {
deba@1422
    32
    heap.push(it, countOutEdges(graph, it));
deba@1422
    33
  }
deba@1422
    34
deba@1422
    35
  vector<Node> order;
deba@1422
    36
deba@1422
    37
  while (!heap.empty()) {
deba@1422
    38
    Node node = heap.top();
deba@1422
    39
    heap.pop();
deba@1422
    40
    color[node] = -1;
deba@1422
    41
    order.push_back(node);
deba@1422
    42
    for (IncEdgeIt it(graph, node); it != INVALID; ++it) {
deba@1422
    43
      Node target = graph.runningNode(it); 
deba@1422
    44
      if (color[target] == -2) {
deba@1422
    45
	heap.decrease(target, heap[target] - 1);
deba@1422
    46
      }
deba@1422
    47
    }
deba@1422
    48
  }
deba@1422
    49
deba@1422
    50
  for (int i = order.size() - 1; i >= 0; --i) {
deba@1422
    51
    set<int> forbidden;
deba@1422
    52
    for (IncEdgeIt it(graph, order[i]); it != INVALID; ++it) {
deba@1422
    53
      Node target = graph.runningNode(it); 
deba@1422
    54
      if (color[target] != -1) {
deba@1422
    55
	forbidden.insert(color[target]);
deba@1422
    56
      }
deba@1422
    57
    }
deba@1422
    58
    int current = 0;
deba@1422
    59
    while (forbidden.find(current) != forbidden.end()) ++current;
deba@1422
    60
    color[order[i]] = current;
deba@1422
    61
  }
deba@1422
    62
deba@1422
    63
  ColorSet colorSet;
deba@1422
    64
deba@1422
    65
  graphToEps(graph, "six_coloring.eps").
deba@1422
    66
    title("Six Colored Graph").copyright("(C) 2005 LEMON Project").
deba@1422
    67
    coords(coords).nodeColors(composeMap(colorSet, color)).
deba@1422
    68
    scaleToA4().run();
deba@1422
    69
deba@1422
    70
  return 0;
deba@1422
    71
}