Demo about descriptor map.
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/demo/descriptor_map_demo.cc Wed Jul 13 14:05:49 2005 +0000
1.3 @@ -0,0 +1,95 @@
1.4 +#include <lemon/list_graph.h>
1.5 +#include <lemon/graph_utils.h>
1.6 +#include <lemon/xy.h>
1.7 +
1.8 +#include <lemon/graph_to_eps.h>
1.9 +
1.10 +#include <cstdlib>
1.11 +#include <cmath>
1.12 +#include <ctime>
1.13 +
1.14 +using namespace lemon;
1.15 +
1.16 +// Special map type
1.17 +// It gives back a position for each node. The position of the nodes
1.18 +// are on the circle with the given center and radius.
1.19 +//
1.20 +// Because we use the descriptor map it will hold the proprty described above
1.21 +// even if a node added or deleted.
1.22 +template <typename Graph>
1.23 +class CircleMap {
1.24 +public:
1.25 +
1.26 + typedef xy<double> Value;
1.27 + typedef typename Graph::Node Key;
1.28 +
1.29 + CircleMap(const Graph& _graph,
1.30 + const Value& _center = Value(0.0, 0.0),
1.31 + double _radius = 1.0)
1.32 + : descriptor(_graph), center(_center), radius(_radius) {}
1.33 +
1.34 + Value operator[](const Key& key) const {
1.35 + double angle = descriptor[key] * 2 * M_PI
1.36 + / (double)descriptor.inverse().size();
1.37 + double x = std::cos(angle) * radius + center.x;
1.38 + double y = std::sin(angle) * radius + center.y;
1.39 + return Value(x, y);
1.40 + }
1.41 +
1.42 +private:
1.43 +
1.44 + DescriptorMap<Graph, typename Graph::Node> descriptor;
1.45 + Value center;
1.46 + double radius;
1.47 +};
1.48 +
1.49 +int main() {
1.50 + std::srand(std::time(0));
1.51 + typedef ListGraph Graph;
1.52 + typedef Graph::Node Node;
1.53 + typedef Graph::Edge Edge;
1.54 +
1.55 + // Generating a graph
1.56 +
1.57 + Graph graph;
1.58 +
1.59 + const int NODE = 16;
1.60 + for (int i = 0; i < NODE; ++i) {
1.61 + graph.addNode();
1.62 + }
1.63 +
1.64 + // Creating descriptor map and inverse
1.65 + DescriptorMap<Graph, Node> nodeDesc(graph);
1.66 + DescriptorMap<Graph, Node>::InverseMap nodeInv(nodeDesc);
1.67 +
1.68 + // Adding edges
1.69 + // The descriptor map always maps an integer value for each node.
1.70 + // The range of the values is always [0..n - 1] where n is the
1.71 + // number of the nodes of the graph. The inverse map gives back the
1.72 + // the node by its descriptor value.
1.73 + //
1.74 + // The inversemap cannot works without its DescriptorMap because
1.75 + // it holds reference to it.
1.76 + const int EDGE = (int)(NODE * std::log(NODE));
1.77 + for (int i = 0; i < EDGE; ++i) {
1.78 + int si = (int)(std::rand() / (RAND_MAX + 1.0) * NODE);
1.79 + int ti = (int)(std::rand() / (RAND_MAX + 1.0) * NODE);
1.80 +
1.81 + graph.addEdge(nodeInv[si], nodeInv[ti]);
1.82 + }
1.83 +
1.84 + // Make postscript from the graph.
1.85 +
1.86 + CircleMap<Graph> coords(graph, xy<double>(0.0, 0.0), 10.0);
1.87 +
1.88 + graphToEps(graph,"descriptor_map_demo.eps").scaleToA4().
1.89 + title("Generated graph").
1.90 + copyright("(C) 2005 LEMON Project").
1.91 + coords(coords).
1.92 + nodeScale(1.0).
1.93 + enableParallel().parEdgeDist(1).
1.94 + drawArrows().arrowWidth(1).arrowLength(1).
1.95 + run();
1.96 +
1.97 + return 0;
1.98 +}