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