demo/coloring.cc
author deba
Wed, 01 Mar 2006 10:25:30 +0000
changeset 1991 d7442141d9ef
parent 1956 a055123339d5
child 2038 33db14058543
permissions -rw-r--r--
The graph adadptors can be alteration observed.
In most cases it uses the adapted graph alteration notifiers.
Only special case is now the UndirGraphAdaptor, where
we have to proxy the signals from the graph.

The SubBidirGraphAdaptor is removed, because it doest not
gives more feature than the EdgeSubGraphAdaptor<UndirGraphAdaptor<Graph>>.

The ResGraphAdaptor is based on this composition.
ladanyi@1636
     1
/* -*- C++ -*-
ladanyi@1636
     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
ladanyi@1636
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
ladanyi@1636
     8
 *
ladanyi@1636
     9
 * Permission to use, modify and distribute this software is granted
ladanyi@1636
    10
 * provided that this copyright notice appears in all copies. For
ladanyi@1636
    11
 * precise terms see the accompanying LICENSE file.
ladanyi@1636
    12
 *
ladanyi@1636
    13
 * This software is provided "AS IS" with no warranty of any kind,
ladanyi@1636
    14
 * express or implied, and with no claim as to its suitability for any
ladanyi@1636
    15
 * purpose.
ladanyi@1636
    16
 *
ladanyi@1636
    17
 */
ladanyi@1636
    18
deba@1727
    19
///\ingroup demos
deba@1727
    20
///\file
deba@1727
    21
///\brief Coloring of a graph.
deba@1727
    22
///
alpar@1959
    23
/// This example shows how can we color the nodes of a planar graph efficiently
deba@1727
    24
/// with six colors.
deba@1727
    25
///
deba@1727
    26
/// \include coloring.cc
deba@1727
    27
deba@1422
    28
#include <vector>
deba@1422
    29
deba@1727
    30
#include <iostream>
deba@1727
    31
deba@1422
    32
#include <lemon/smart_graph.h>
deba@1727
    33
#include <lemon/linear_heap.h>
deba@1422
    34
#include <lemon/graph_reader.h>
deba@1422
    35
#include <lemon/graph_to_eps.h>
deba@1422
    36
athos@1560
    37
#include <fstream>
athos@1560
    38
athos@1560
    39
deba@1422
    40
using namespace std;
deba@1422
    41
using namespace lemon;
deba@1422
    42
deba@1683
    43
int main() {
athos@1560
    44
klao@1909
    45
  typedef SmartUGraph Graph;
deba@1422
    46
  typedef Graph::Node Node;
deba@1422
    47
  typedef Graph::NodeIt NodeIt;
klao@1909
    48
  typedef Graph::UEdge UEdge;
deba@1422
    49
  typedef Graph::IncEdgeIt IncEdgeIt;
deba@1422
    50
alpar@1959
    51
  std::cout << "Six coloring of a planar graph" << std::endl;
deba@1683
    52
  std::cout << "Input file: coloring.lgf" << std::endl;
deba@1683
    53
  std::cout << "Output file: coloring.eps" << std::endl;
deba@1683
    54
deba@1422
    55
  Graph graph;
deba@1422
    56
klao@1909
    57
  UGraphReader<Graph> reader("coloring.lgf", graph);
deba@1422
    58
  Graph::NodeMap<xy<double> > coords(graph);
deba@1422
    59
  reader.readNodeMap("coords", coords);
deba@1422
    60
  
deba@1422
    61
  reader.run();
deba@1422
    62
deba@1422
    63
  Graph::NodeMap<int> color(graph, -2);
deba@1422
    64
  
deba@1422
    65
  Graph::NodeMap<int> heapMap(graph, -1);
deba@1727
    66
  LinearHeap<Node, Graph::NodeMap<int> > heap(heapMap);
deba@1422
    67
  
deba@1422
    68
  for (NodeIt it(graph); it != INVALID; ++it) {
deba@1422
    69
    heap.push(it, countOutEdges(graph, it));
deba@1422
    70
  }
deba@1422
    71
deba@1422
    72
  vector<Node> order;
deba@1422
    73
deba@1422
    74
  while (!heap.empty()) {
deba@1422
    75
    Node node = heap.top();
deba@1422
    76
    heap.pop();
deba@1422
    77
    color[node] = -1;
deba@1422
    78
    order.push_back(node);
deba@1422
    79
    for (IncEdgeIt it(graph, node); it != INVALID; ++it) {
deba@1422
    80
      Node target = graph.runningNode(it); 
deba@1422
    81
      if (color[target] == -2) {
deba@1422
    82
	heap.decrease(target, heap[target] - 1);
deba@1422
    83
      }
deba@1422
    84
    }
deba@1422
    85
  }
deba@1422
    86
deba@1422
    87
  for (int i = order.size() - 1; i >= 0; --i) {
deba@1422
    88
    set<int> forbidden;
deba@1422
    89
    for (IncEdgeIt it(graph, order[i]); it != INVALID; ++it) {
deba@1422
    90
      Node target = graph.runningNode(it); 
deba@1422
    91
      if (color[target] != -1) {
deba@1422
    92
	forbidden.insert(color[target]);
deba@1422
    93
      }
deba@1422
    94
    }
deba@1422
    95
    int current = 0;
deba@1422
    96
    while (forbidden.find(current) != forbidden.end()) ++current;
deba@1422
    97
    color[order[i]] = current;
deba@1422
    98
  }
deba@1422
    99
deba@1422
   100
  ColorSet colorSet;
deba@1422
   101
deba@1683
   102
  graphToEps(graph, "coloring.eps").
alpar@1875
   103
    title("Six Colored Plan Graph").copyright("(C) 2006 LEMON Project").
deba@1422
   104
    coords(coords).nodeColors(composeMap(colorSet, color)).
deba@1683
   105
    nodeScale(5.0).scaleToA4().run();
deba@1422
   106
deba@1422
   107
  return 0;
deba@1422
   108
}