demo/coloring.cc
author alpar
Wed, 17 Aug 2005 22:07:35 +0000
changeset 1641 77f6ab7ad66f
parent 1577 15098fb5275c
child 1683 13648409b596
permissions -rw-r--r--
Demos' documentations include the source.
ladanyi@1636
     1
/* -*- C++ -*-
ladanyi@1636
     2
 * demo/coloring.cc - Part of LEMON, a generic C++ optimization library
ladanyi@1636
     3
 *
ladanyi@1636
     4
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
ladanyi@1636
     5
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
ladanyi@1636
     6
 *
ladanyi@1636
     7
 * Permission to use, modify and distribute this software is granted
ladanyi@1636
     8
 * provided that this copyright notice appears in all copies. For
ladanyi@1636
     9
 * precise terms see the accompanying LICENSE file.
ladanyi@1636
    10
 *
ladanyi@1636
    11
 * This software is provided "AS IS" with no warranty of any kind,
ladanyi@1636
    12
 * express or implied, and with no claim as to its suitability for any
ladanyi@1636
    13
 * purpose.
ladanyi@1636
    14
 *
ladanyi@1636
    15
 */
ladanyi@1636
    16
deba@1422
    17
#include <vector>
deba@1422
    18
deba@1422
    19
#include <lemon/smart_graph.h>
deba@1422
    20
#include <lemon/bin_heap.h>
deba@1422
    21
#include <lemon/graph_reader.h>
deba@1422
    22
#include <lemon/graph_to_eps.h>
deba@1422
    23
athos@1560
    24
#include <fstream>
athos@1560
    25
#include <iostream>
athos@1560
    26
athos@1560
    27
deba@1422
    28
using namespace std;
deba@1422
    29
using namespace lemon;
deba@1422
    30
athos@1560
    31
int main(int argc, char *argv[]) 
athos@1560
    32
{
athos@1560
    33
  if(argc<2)
athos@1560
    34
  {
athos@1577
    35
      std::cerr << "USAGE: coloring input_file.lgf" << std::endl;
athos@1560
    36
      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
    37
      return 0;
athos@1560
    38
  }
athos@1560
    39
athos@1560
    40
athos@1560
    41
  //input stream to read the graph from
athos@1560
    42
  std::ifstream is(argv[1]);
athos@1560
    43
deba@1422
    44
  typedef UndirSmartGraph Graph;
deba@1422
    45
  typedef Graph::Node Node;
deba@1422
    46
  typedef Graph::NodeIt NodeIt;
deba@1422
    47
  typedef Graph::UndirEdge UndirEdge;
deba@1422
    48
  typedef Graph::IncEdgeIt IncEdgeIt;
deba@1422
    49
deba@1422
    50
  Graph graph;
deba@1422
    51
athos@1560
    52
  UndirGraphReader<Graph> reader(is, graph);
deba@1422
    53
  Graph::NodeMap<xy<double> > coords(graph);
deba@1422
    54
  reader.readNodeMap("coords", coords);
deba@1422
    55
  
deba@1422
    56
  reader.run();
deba@1422
    57
deba@1422
    58
  Graph::NodeMap<int> color(graph, -2);
deba@1422
    59
  
deba@1422
    60
  Graph::NodeMap<int> heapMap(graph, -1);
deba@1422
    61
  BinHeap<Node, int, Graph::NodeMap<int> > heap(heapMap);
deba@1422
    62
  
deba@1422
    63
  for (NodeIt it(graph); it != INVALID; ++it) {
deba@1422
    64
    heap.push(it, countOutEdges(graph, it));
deba@1422
    65
  }
deba@1422
    66
deba@1422
    67
  vector<Node> order;
deba@1422
    68
deba@1422
    69
  while (!heap.empty()) {
deba@1422
    70
    Node node = heap.top();
deba@1422
    71
    heap.pop();
deba@1422
    72
    color[node] = -1;
deba@1422
    73
    order.push_back(node);
deba@1422
    74
    for (IncEdgeIt it(graph, node); it != INVALID; ++it) {
deba@1422
    75
      Node target = graph.runningNode(it); 
deba@1422
    76
      if (color[target] == -2) {
deba@1422
    77
	heap.decrease(target, heap[target] - 1);
deba@1422
    78
      }
deba@1422
    79
    }
deba@1422
    80
  }
deba@1422
    81
deba@1422
    82
  for (int i = order.size() - 1; i >= 0; --i) {
deba@1422
    83
    set<int> forbidden;
deba@1422
    84
    for (IncEdgeIt it(graph, order[i]); it != INVALID; ++it) {
deba@1422
    85
      Node target = graph.runningNode(it); 
deba@1422
    86
      if (color[target] != -1) {
deba@1422
    87
	forbidden.insert(color[target]);
deba@1422
    88
      }
deba@1422
    89
    }
deba@1422
    90
    int current = 0;
deba@1422
    91
    while (forbidden.find(current) != forbidden.end()) ++current;
deba@1422
    92
    color[order[i]] = current;
deba@1422
    93
  }
deba@1422
    94
deba@1422
    95
  ColorSet colorSet;
deba@1422
    96
deba@1422
    97
  graphToEps(graph, "six_coloring.eps").
deba@1422
    98
    title("Six Colored Graph").copyright("(C) 2005 LEMON Project").
deba@1422
    99
    coords(coords).nodeColors(composeMap(colorSet, color)).
deba@1422
   100
    scaleToA4().run();
deba@1422
   101
deba@1422
   102
  return 0;
deba@1422
   103
}