#include #include #include #include #include #include #include using namespace std; using namespace lemon; int main(int argc, char *argv[]) { if(argc<2) { std::cerr << "USAGE: coloring " << std::endl; 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; return 0; } //input stream to read the graph from std::ifstream is(argv[1]); typedef UndirSmartGraph Graph; typedef Graph::Node Node; typedef Graph::NodeIt NodeIt; typedef Graph::UndirEdge UndirEdge; typedef Graph::IncEdgeIt IncEdgeIt; Graph graph; UndirGraphReader reader(is, graph); Graph::NodeMap > coords(graph); reader.readNodeMap("coords", coords); reader.run(); Graph::NodeMap color(graph, -2); Graph::NodeMap heapMap(graph, -1); BinHeap > heap(heapMap); for (NodeIt it(graph); it != INVALID; ++it) { heap.push(it, countOutEdges(graph, it)); } vector order; while (!heap.empty()) { Node node = heap.top(); heap.pop(); color[node] = -1; order.push_back(node); for (IncEdgeIt it(graph, node); it != INVALID; ++it) { Node target = graph.runningNode(it); if (color[target] == -2) { heap.decrease(target, heap[target] - 1); } } } for (int i = order.size() - 1; i >= 0; --i) { set forbidden; for (IncEdgeIt it(graph, order[i]); it != INVALID; ++it) { Node target = graph.runningNode(it); if (color[target] != -1) { forbidden.insert(color[target]); } } int current = 0; while (forbidden.find(current) != forbidden.end()) ++current; color[order[i]] = current; } ColorSet colorSet; graphToEps(graph, "six_coloring.eps"). title("Six Colored Graph").copyright("(C) 2005 LEMON Project"). coords(coords).nodeColors(composeMap(colorSet, color)). scaleToA4().run(); return 0; }