2 * demo/descriptor_map_demo.cc - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
17 #include <lemon/list_graph.h>
18 #include <lemon/graph_utils.h>
19 #include <lemon/graph_writer.h>
21 #include <lemon/graph_to_eps.h>
29 using namespace lemon;
32 // It gives back a position for each node. The position of the nodes
33 // are on the circle with the given center and radius.
35 // Because we use the descriptor map it will hold the proprty described above
36 // even if a node added or deleted.
37 template <typename Graph>
41 typedef xy<double> Value;
42 typedef typename Graph::Node Key;
44 CircleMap(const Graph& _graph,
45 const Value& _center = Value(0.0, 0.0),
47 : descriptor(_graph), center(_center), radius(_radius) {}
49 Value operator[](const Key& key) const {
50 double angle = descriptor[key] * 2 * M_PI
51 / (double)descriptor.inverse().size();
52 double x = std::cos(angle) * radius + center.x;
53 double y = std::sin(angle) * radius + center.y;
59 DescriptorMap<Graph, typename Graph::Node> descriptor;
65 std::srand(std::time(0));
66 typedef ListGraph Graph;
67 typedef Graph::Node Node;
68 typedef Graph::Edge Edge;
72 std::cout << "Generating graph: " << std::endl;
77 for (int i = 0; i < NODE; ++i) {
81 // Creating descriptor map and inverse
82 DescriptorMap<Graph, Node> nodeDesc(graph);
83 DescriptorMap<Graph, Node>::InverseMap nodeInv(nodeDesc);
86 // The descriptor map always maps an integer value for each node.
87 // The range of the values is always [0..n - 1] where n is the
88 // number of the nodes of the graph. The inverse map gives back the
89 // the node by its descriptor value.
91 // The inversemap cannot works without its DescriptorMap because
92 // it holds reference to it.
93 const int EDGE = (int)(NODE * std::log(double(NODE)));
94 for (int i = 0; i < EDGE; ++i) {
95 int si = (int)(std::rand() / (RAND_MAX + 1.0) * NODE);
96 int ti = (int)(std::rand() / (RAND_MAX + 1.0) * NODE);
98 graph.addEdge(nodeInv[si], nodeInv[ti]);
101 GraphWriter<Graph>(std::cout, graph).run();
103 std::cout << std::endl;
104 std::cout << "Postscript file: descriptor_map_demo.eps" << std::endl;
106 // Make postscript from the graph.
108 CircleMap<Graph> coords(graph, xy<double>(0.0, 0.0), 10.0);
110 graphToEps(graph,"descriptor_map_demo.eps").scaleToA4().
111 title("Generated graph").
112 copyright("(C) 2005 LEMON Project").
115 enableParallel().parEdgeDist(1).
116 drawArrows().arrowWidth(1).arrowLength(1).