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 |
|
deba@1683
|
31 |
int main() {
|
athos@1560
|
32 |
|
deba@1422
|
33 |
typedef UndirSmartGraph Graph;
|
deba@1422
|
34 |
typedef Graph::Node Node;
|
deba@1422
|
35 |
typedef Graph::NodeIt NodeIt;
|
deba@1422
|
36 |
typedef Graph::UndirEdge UndirEdge;
|
deba@1422
|
37 |
typedef Graph::IncEdgeIt IncEdgeIt;
|
deba@1422
|
38 |
|
deba@1683
|
39 |
std::cout << "Six coloring of a plan graph" << std::endl;
|
deba@1683
|
40 |
std::cout << "Input file: coloring.lgf" << std::endl;
|
deba@1683
|
41 |
std::cout << "Output file: coloring.eps" << std::endl;
|
deba@1683
|
42 |
|
deba@1422
|
43 |
Graph graph;
|
deba@1422
|
44 |
|
deba@1683
|
45 |
UndirGraphReader<Graph> reader("coloring.lgf", graph);
|
deba@1422
|
46 |
Graph::NodeMap<xy<double> > coords(graph);
|
deba@1422
|
47 |
reader.readNodeMap("coords", coords);
|
deba@1422
|
48 |
|
deba@1422
|
49 |
reader.run();
|
deba@1422
|
50 |
|
deba@1422
|
51 |
Graph::NodeMap<int> color(graph, -2);
|
deba@1422
|
52 |
|
deba@1422
|
53 |
Graph::NodeMap<int> heapMap(graph, -1);
|
deba@1422
|
54 |
BinHeap<Node, int, Graph::NodeMap<int> > heap(heapMap);
|
deba@1422
|
55 |
|
deba@1422
|
56 |
for (NodeIt it(graph); it != INVALID; ++it) {
|
deba@1422
|
57 |
heap.push(it, countOutEdges(graph, it));
|
deba@1422
|
58 |
}
|
deba@1422
|
59 |
|
deba@1422
|
60 |
vector<Node> order;
|
deba@1422
|
61 |
|
deba@1422
|
62 |
while (!heap.empty()) {
|
deba@1422
|
63 |
Node node = heap.top();
|
deba@1422
|
64 |
heap.pop();
|
deba@1422
|
65 |
color[node] = -1;
|
deba@1422
|
66 |
order.push_back(node);
|
deba@1422
|
67 |
for (IncEdgeIt it(graph, node); it != INVALID; ++it) {
|
deba@1422
|
68 |
Node target = graph.runningNode(it);
|
deba@1422
|
69 |
if (color[target] == -2) {
|
deba@1422
|
70 |
heap.decrease(target, heap[target] - 1);
|
deba@1422
|
71 |
}
|
deba@1422
|
72 |
}
|
deba@1422
|
73 |
}
|
deba@1422
|
74 |
|
deba@1422
|
75 |
for (int i = order.size() - 1; i >= 0; --i) {
|
deba@1422
|
76 |
set<int> forbidden;
|
deba@1422
|
77 |
for (IncEdgeIt it(graph, order[i]); it != INVALID; ++it) {
|
deba@1422
|
78 |
Node target = graph.runningNode(it);
|
deba@1422
|
79 |
if (color[target] != -1) {
|
deba@1422
|
80 |
forbidden.insert(color[target]);
|
deba@1422
|
81 |
}
|
deba@1422
|
82 |
}
|
deba@1422
|
83 |
int current = 0;
|
deba@1422
|
84 |
while (forbidden.find(current) != forbidden.end()) ++current;
|
deba@1422
|
85 |
color[order[i]] = current;
|
deba@1422
|
86 |
}
|
deba@1422
|
87 |
|
deba@1422
|
88 |
ColorSet colorSet;
|
deba@1422
|
89 |
|
deba@1683
|
90 |
graphToEps(graph, "coloring.eps").
|
deba@1683
|
91 |
title("Six Colored Plan Graph").copyright("(C) 2005 LEMON Project").
|
deba@1422
|
92 |
coords(coords).nodeColors(composeMap(colorSet, color)).
|
deba@1683
|
93 |
nodeScale(5.0).scaleToA4().run();
|
deba@1422
|
94 |
|
deba@1422
|
95 |
return 0;
|
deba@1422
|
96 |
}
|