Fix crash when the input file does not contain any nodeset or edgeset.
2 * demo/descriptor_map_demo.cc - Part of LEMON, a generic C++ optimization
5 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
6 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 * Permission to use, modify and distribute this software is granted
9 * provided that this copyright notice appears in all copies. For
10 * precise terms see the accompanying LICENSE file.
12 * This software is provided "AS IS" with no warranty of any kind,
13 * express or implied, and with no claim as to its suitability for any
18 #include <lemon/list_graph.h>
19 #include <lemon/graph_utils.h>
20 #include <lemon/graph_writer.h>
22 #include <lemon/graph_to_eps.h>
30 using namespace lemon;
33 // It gives back a position for each node. The position of the nodes
34 // are on the circle with the given center and radius.
36 // Because we use the descriptor map it will hold the proprty described above
37 // even if a node added or deleted.
38 template <typename Graph>
42 typedef xy<double> Value;
43 typedef typename Graph::Node Key;
45 CircleMap(const Graph& _graph,
46 const Value& _center = Value(0.0, 0.0),
48 : descriptor(_graph), center(_center), radius(_radius) {}
50 Value operator[](const Key& key) const {
51 double angle = descriptor[key] * 2 * M_PI
52 / (double)descriptor.inverse().size();
53 double x = std::cos(angle) * radius + center.x;
54 double y = std::sin(angle) * radius + center.y;
60 DescriptorMap<Graph, typename Graph::Node> descriptor;
66 std::srand(std::time(0));
67 typedef ListGraph Graph;
68 typedef Graph::Node Node;
69 typedef Graph::Edge Edge;
73 std::cout << "Generating graph: " << std::endl;
78 for (int i = 0; i < NODE; ++i) {
82 // Creating descriptor map and inverse
83 DescriptorMap<Graph, Node> nodeDesc(graph);
84 DescriptorMap<Graph, Node>::InverseMap nodeInv(nodeDesc);
87 // The descriptor map always maps an integer value for each node.
88 // The range of the values is always [0..n - 1] where n is the
89 // number of the nodes of the graph. The inverse map gives back the
90 // the node by its descriptor value.
92 // The inversemap cannot works without its DescriptorMap because
93 // it holds reference to it.
94 const int EDGE = (int)(NODE * std::log(double(NODE)));
95 for (int i = 0; i < EDGE; ++i) {
96 int si = (int)(std::rand() / (RAND_MAX + 1.0) * NODE);
97 int ti = (int)(std::rand() / (RAND_MAX + 1.0) * NODE);
99 graph.addEdge(nodeInv[si], nodeInv[ti]);
102 GraphWriter<Graph>(std::cout, graph).run();
104 std::cout << std::endl;
105 std::cout << "Postscript file: descriptor_map_demo.eps" << std::endl;
107 // Make postscript from the graph.
109 CircleMap<Graph> coords(graph, xy<double>(0.0, 0.0), 10.0);
111 graphToEps(graph,"descriptor_map_demo.eps").scaleToA4().
112 title("Generated graph").
113 copyright("(C) 2005 LEMON Project").
116 enableParallel().parEdgeDist(1).
117 drawArrows().arrowWidth(1).arrowLength(1).