deba@1554
|
1 |
/* -*- C++ -*-
|
deba@1554
|
2 |
*
|
alpar@1956
|
3 |
* This file is a part of LEMON, a generic C++ optimization library
|
alpar@1956
|
4 |
*
|
alpar@1956
|
5 |
* Copyright (C) 2003-2006
|
alpar@1956
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
deba@1554
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
deba@1554
|
8 |
*
|
deba@1554
|
9 |
* Permission to use, modify and distribute this software is granted
|
deba@1554
|
10 |
* provided that this copyright notice appears in all copies. For
|
deba@1554
|
11 |
* precise terms see the accompanying LICENSE file.
|
deba@1554
|
12 |
*
|
deba@1554
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
deba@1554
|
14 |
* express or implied, and with no claim as to its suitability for any
|
deba@1554
|
15 |
* purpose.
|
deba@1554
|
16 |
*
|
deba@1554
|
17 |
*/
|
deba@1554
|
18 |
|
deba@2081
|
19 |
/// \ingroup demos
|
deba@2081
|
20 |
/// \file
|
deba@2081
|
21 |
/// \brief Using descriptor map and own special map types.
|
deba@2081
|
22 |
///
|
deba@2081
|
23 |
/// This demo shows how can be used the DescriptorMap class
|
deba@2081
|
24 |
/// which helps to use unique label for each node or edge.
|
deba@2081
|
25 |
/// And it gives an example how easy is creating own map types.
|
deba@2081
|
26 |
///
|
deba@2081
|
27 |
/// \include descriptor_map_demo.cc
|
deba@2081
|
28 |
|
deba@1553
|
29 |
#include <lemon/list_graph.h>
|
deba@1553
|
30 |
#include <lemon/graph_utils.h>
|
deba@1554
|
31 |
#include <lemon/graph_writer.h>
|
alpar@2207
|
32 |
#include <lemon/dim2.h>
|
deba@1554
|
33 |
#include <lemon/graph_to_eps.h>
|
deba@1553
|
34 |
|
deba@2242
|
35 |
#include <lemon/random.h>
|
deba@2242
|
36 |
|
deba@1554
|
37 |
#include <iostream>
|
deba@1553
|
38 |
|
deba@1553
|
39 |
#include <cmath>
|
deba@2242
|
40 |
|
deba@1553
|
41 |
|
deba@1553
|
42 |
using namespace lemon;
|
deba@1553
|
43 |
|
alpar@2207
|
44 |
// Special dim2::Point<double> map type
|
deba@2081
|
45 |
//
|
deba@2081
|
46 |
// It gives back a position for each node. The position of the nodes
|
deba@1553
|
47 |
// are on the circle with the given center and radius.
|
deba@1553
|
48 |
//
|
deba@2081
|
49 |
// Because we use the descriptor map it will hold the proprty
|
deba@2081
|
50 |
// described above even if a node added or deleted.
|
deba@1553
|
51 |
template <typename Graph>
|
deba@1553
|
52 |
class CircleMap {
|
deba@1553
|
53 |
public:
|
deba@1553
|
54 |
|
alpar@2207
|
55 |
typedef dim2::Point<double> Value;
|
deba@1553
|
56 |
typedef typename Graph::Node Key;
|
deba@1553
|
57 |
|
deba@1553
|
58 |
CircleMap(const Graph& _graph,
|
deba@1553
|
59 |
const Value& _center = Value(0.0, 0.0),
|
deba@1553
|
60 |
double _radius = 1.0)
|
deba@1553
|
61 |
: descriptor(_graph), center(_center), radius(_radius) {}
|
deba@1553
|
62 |
|
deba@1553
|
63 |
Value operator[](const Key& key) const {
|
deba@1553
|
64 |
double angle = descriptor[key] * 2 * M_PI
|
deba@1553
|
65 |
/ (double)descriptor.inverse().size();
|
deba@1553
|
66 |
double x = std::cos(angle) * radius + center.x;
|
deba@1553
|
67 |
double y = std::sin(angle) * radius + center.y;
|
deba@1553
|
68 |
return Value(x, y);
|
deba@1553
|
69 |
}
|
deba@1553
|
70 |
|
deba@1553
|
71 |
private:
|
deba@1553
|
72 |
|
deba@1553
|
73 |
DescriptorMap<Graph, typename Graph::Node> descriptor;
|
deba@1553
|
74 |
Value center;
|
deba@1553
|
75 |
double radius;
|
deba@1553
|
76 |
};
|
deba@1553
|
77 |
|
deba@1553
|
78 |
int main() {
|
deba@1553
|
79 |
typedef ListGraph Graph;
|
deba@1553
|
80 |
typedef Graph::Node Node;
|
deba@1553
|
81 |
typedef Graph::Edge Edge;
|
deba@1553
|
82 |
|
deba@1553
|
83 |
// Generating a graph
|
deba@1554
|
84 |
|
deba@1554
|
85 |
std::cout << "Generating graph: " << std::endl;
|
deba@1554
|
86 |
|
deba@1553
|
87 |
Graph graph;
|
deba@1553
|
88 |
|
deba@1553
|
89 |
const int NODE = 16;
|
deba@1553
|
90 |
for (int i = 0; i < NODE; ++i) {
|
deba@1553
|
91 |
graph.addNode();
|
deba@1553
|
92 |
}
|
deba@1553
|
93 |
|
deba@1553
|
94 |
// Creating descriptor map and inverse
|
deba@1553
|
95 |
DescriptorMap<Graph, Node> nodeDesc(graph);
|
deba@1553
|
96 |
DescriptorMap<Graph, Node>::InverseMap nodeInv(nodeDesc);
|
deba@1553
|
97 |
|
deba@1553
|
98 |
// Adding edges
|
deba@1553
|
99 |
// The descriptor map always maps an integer value for each node.
|
deba@1553
|
100 |
// The range of the values is always [0..n - 1] where n is the
|
deba@1553
|
101 |
// number of the nodes of the graph. The inverse map gives back the
|
deba@1553
|
102 |
// the node by its descriptor value.
|
deba@1553
|
103 |
//
|
deba@1553
|
104 |
// The inversemap cannot works without its DescriptorMap because
|
deba@1553
|
105 |
// it holds reference to it.
|
alpar@1556
|
106 |
const int EDGE = (int)(NODE * std::log(double(NODE)));
|
deba@1553
|
107 |
for (int i = 0; i < EDGE; ++i) {
|
deba@2242
|
108 |
int si = rnd[NODE];
|
deba@2242
|
109 |
int ti = rnd[NODE];
|
deba@1553
|
110 |
|
deba@1553
|
111 |
graph.addEdge(nodeInv[si], nodeInv[ti]);
|
deba@1553
|
112 |
}
|
deba@1553
|
113 |
|
deba@1554
|
114 |
GraphWriter<Graph>(std::cout, graph).run();
|
deba@1554
|
115 |
|
deba@1554
|
116 |
std::cout << std::endl;
|
deba@1554
|
117 |
std::cout << "Postscript file: descriptor_map_demo.eps" << std::endl;
|
deba@1554
|
118 |
|
deba@1553
|
119 |
// Make postscript from the graph.
|
deba@1553
|
120 |
|
alpar@2207
|
121 |
CircleMap<Graph> coords(graph, dim2::Point<double>(0.0, 0.0), 10.0);
|
deba@1553
|
122 |
|
deba@1553
|
123 |
graphToEps(graph,"descriptor_map_demo.eps").scaleToA4().
|
deba@1553
|
124 |
title("Generated graph").
|
alpar@1875
|
125 |
copyright("(C) 2006 LEMON Project").
|
deba@1553
|
126 |
coords(coords).
|
deba@1553
|
127 |
nodeScale(1.0).
|
deba@1553
|
128 |
enableParallel().parEdgeDist(1).
|
deba@1553
|
129 |
drawArrows().arrowWidth(1).arrowLength(1).
|
deba@1553
|
130 |
run();
|
deba@1553
|
131 |
|
deba@1553
|
132 |
return 0;
|
deba@1553
|
133 |
}
|