/* -*- mode: C++; indent-tabs-mode: nil; -*-
* This file is a part of LEMON, a generic C++ optimization library.
* Copyright (C) 2003-2009
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
* (Egervary Research Group on Combinatorial Optimization, EGRES).
* Permission to use, modify and distribute this software is granted
* provided that this copyright notice appears in all copies. For
* precise terms see the accompanying LICENSE file.
* This software is provided "AS IS" with no warranty of any kind,
* express or implied, and with no claim as to its suitability for any
/// \brief Special plane digraph generator.
/// Graph generator application for various types of plane graphs.
/// for more info on the usage.
#include <lemon/list_graph.h>
#include <lemon/random.h>
#include <lemon/counter.h>
#include <lemon/suurballe.h>
#include <lemon/graph_to_eps.h>
#include <lemon/lgf_writer.h>
#include <lemon/arg_parser.h>
#include <lemon/kruskal.h>
#include <lemon/time_measure.h>
typedef dim2::Point<double> Point;
GRAPH_TYPEDEFS(ListGraph);
ListGraph::NodeMap<Point> coords(g);
for(EdgeIt e(g);e!=INVALID;++e)
tlen+=std::sqrt((coords[g.v(e)]-coords[g.u(e)]).normSquare());
const double EPSILON=1e-8;
bool tsp_improve(Node u, Node v)
double luv=std::sqrt((coords[v]-coords[u]).normSquare());
for(IncEdgeIt e(g,v2);(n=g.runningNode(e))==u2;++e) { }
if(luv+std::sqrt((coords[v2]-coords[u2]).normSquare())-EPSILON>
std::sqrt((coords[u]-coords[u2]).normSquare())+
std::sqrt((coords[v]-coords[v2]).normSquare()))
g.erase(findEdge(g,u,v));
g.erase(findEdge(g,u2,v2));
for(IncEdgeIt e(g,u);e!=INVALID;++e)
if(tsp_improve(u,g.runningNode(e))) return true;
for(NodeIt n(g);n!=INVALID;++n)
if(tsp_improve(n)) b=true;
for(int i=0;i<N;i++) g.addEdge(nodes[i],nodes[(i+1)%N]);
Line(Point _a,Point _b) :a(_a),b(_b) {}
Line(Node _a,Node _b) : a(coords[_a]),b(coords[_b]) {}
Line(const Arc &e) : a(coords[g.source(e)]),b(coords[g.target(e)]) {}
Line(const Edge &e) : a(coords[g.u(e)]),b(coords[g.v(e)]) {}
inline std::ostream& operator<<(std::ostream &os, const Line &l)
os << l.a << "->" << l.b;
bool cross(Line a, Line b)
return (ao*(b.a-a.a))*(ao*(b.b-a.a))<0 &&
(bo*(a.a-b.a))*(bo*(a.b-b.a))<0;
bool pedgeLess(Parc a,Parc b)
namespace _delaunay_bits {
Part(int p, int c, int n) : prev(p), curr(c), next(n) {}
inline std::ostream& operator<<(std::ostream& os, const Part& part) {
os << '(' << part.prev << ',' << part.curr << ',' << part.next << ')';
inline double circle_point(const Point& p, const Point& q, const Point& r) {
double a = p.x * (q.y - r.y) + q.x * (r.y - p.y) + r.x * (p.y - q.y);
if (a == 0) return std::numeric_limits<double>::quiet_NaN();
double d = (p.x * p.x + p.y * p.y) * (q.y - r.y) +
(q.x * q.x + q.y * q.y) * (r.y - p.y) +
(r.x * r.x + r.y * r.y) * (p.y - q.y);
double e = (p.x * p.x + p.y * p.y) * (q.x - r.x) +
(q.x * q.x + q.y * q.y) * (r.x - p.x) +
(r.x * r.x + r.y * r.y) * (p.x - q.x);
double f = (p.x * p.x + p.y * p.y) * (q.x * r.y - r.x * q.y) +
(q.x * q.x + q.y * q.y) * (r.x * p.y - p.x * r.y) +
(r.x * r.x + r.y * r.y) * (p.x * q.y - q.x * p.y);
return d / (2 * a) + std::sqrt((d * d + e * e) / (4 * a * a) + f / a);
inline bool circle_form(const Point& p, const Point& q, const Point& r) {
return rot90(q - p) * (r - q) < 0.0;
inline double intersection(const Point& p, const Point& q, double sx) {
const double epsilon = 1e-8;
if (p.x == q.x) return (p.y + q.y) / 2.0;
if (sx < p.x + epsilon) return p.y;
if (sx < q.x + epsilon) return q.y;
double b = (q.x - sx) * p.y - (p.x - sx) * q.y;
double d = (q.x - sx) * (p.x - sx) * (p - q).normSquare();
return (b - std::sqrt(d)) / a;
YLess(const std::vector<Point>& points, double& sweep)
: _points(points), _sweep(sweep) {}
bool operator()(const Part& l, const Part& r) const {
const double epsilon = 1e-8;
// std::cerr << l << " vs " << r << std::endl;
double lbx = l.prev != -1 ?
intersection(_points[l.prev], _points[l.curr], _sweep) :
- std::numeric_limits<double>::infinity();
double rbx = r.prev != -1 ?
intersection(_points[r.prev], _points[r.curr], _sweep) :
- std::numeric_limits<double>::infinity();
double lex = l.next != -1 ?
intersection(_points[l.curr], _points[l.next], _sweep) :
std::numeric_limits<double>::infinity();
double rex = r.next != -1 ?
intersection(_points[r.curr], _points[r.next], _sweep) :
std::numeric_limits<double>::infinity();
if (lbx > lex) std::swap(lbx, lex);
if (rbx > rex) std::swap(rbx, rex);
if (lex < epsilon + rex && lbx + epsilon < rex) return true;
if (rex < epsilon + lex && rbx + epsilon < lex) return false;
const std::vector<Point>& _points;
typedef std::multimap<double, BeachIt> SpikeHeap;
typedef std::multimap<Part, SpikeHeap::iterator, YLess> Beach;
BeachIt(Beach::iterator iter) : it(iter) {}
Counter cnt("Number of arcs added: ");
using namespace _delaunay_bits;
typedef _delaunay_bits::Part Part;
typedef std::vector<std::pair<double, int> > SiteHeap;
std::vector<Point> points;
for (NodeIt it(g); it != INVALID; ++it) {
points.push_back(coords[it]);
SiteHeap siteheap(points.size());
for (int i = 0; i < int(siteheap.size()); ++i) {
siteheap[i] = std::make_pair(points[i].x, i);
std::sort(siteheap.begin(), siteheap.end());
sweep = siteheap.front().first;
YLess yless(points, sweep);
std::set<std::pair<int, int> > arcs;
while (siteindex < int(siteheap.size()) &&
siteheap[0].first == siteheap[siteindex].first) {
front.push_back(std::make_pair(points[siteheap[siteindex].second].y,
siteheap[siteindex].second));
std::sort(front.begin(), front.end());
for (int i = 0; i < int(front.size()); ++i) {
int prev = (i == 0 ? -1 : front[i - 1].second);
int curr = front[i].second;
int next = (i + 1 == int(front.size()) ? -1 : front[i + 1].second);
beach.insert(std::make_pair(Part(prev, curr, next),
while (siteindex < int(points.size()) || !spikeheap.empty()) {
SpikeHeap::iterator spit = spikeheap.begin();
if (siteindex < int(points.size()) &&
(spit == spikeheap.end() || siteheap[siteindex].first < spit->first)) {
int site = siteheap[siteindex].second;
sweep = siteheap[siteindex].first;
Beach::iterator bit = beach.upper_bound(Part(site, site, site));
if (bit->second != spikeheap.end()) {
spikeheap.erase(bit->second);
int prev = bit->first.prev;
int curr = bit->first.curr;
int next = bit->first.next;
SpikeHeap::iterator pit = spikeheap.end();
circle_form(points[prev], points[curr], points[site])) {
double x = circle_point(points[prev], points[curr], points[site]);
pit = spikeheap.insert(std::make_pair(x, BeachIt(beach.end())));
beach.insert(std::make_pair(Part(prev, curr, site), pit));
beach.insert(std::make_pair(Part(prev, curr, site), pit));
beach.insert(std::make_pair(Part(curr, site, curr), spikeheap.end()));
SpikeHeap::iterator nit = spikeheap.end();
circle_form(points[site], points[curr],points[next])) {
double x = circle_point(points[site], points[curr], points[next]);
nit = spikeheap.insert(std::make_pair(x, BeachIt(beach.end())));
beach.insert(std::make_pair(Part(site, curr, next), nit));
beach.insert(std::make_pair(Part(site, curr, next), nit));
Beach::iterator bit = spit->second.it;
int prev = bit->first.prev;
int curr = bit->first.curr;
int next = bit->first.next;
std::make_pair(prev, curr) : std::make_pair(curr, prev);
if (arcs.find(arc) == arcs.end()) {
g.addEdge(nodes[prev], nodes[curr]);
std::make_pair(curr, next) : std::make_pair(next, curr);
if (arcs.find(arc) == arcs.end()) {
g.addEdge(nodes[curr], nodes[next]);
Beach::iterator pbit = bit; --pbit;
int ppv = pbit->first.prev;
Beach::iterator nbit = bit; ++nbit;
int nnt = nbit->first.next;
if (bit->second != spikeheap.end()) spikeheap.erase(bit->second);
if (pbit->second != spikeheap.end()) spikeheap.erase(pbit->second);
if (nbit->second != spikeheap.end()) spikeheap.erase(nbit->second);
SpikeHeap::iterator pit = spikeheap.end();
if (ppv != -1 && ppv != next &&
circle_form(points[ppv], points[prev], points[next])) {
double x = circle_point(points[ppv], points[prev], points[next]);
if (x < sweep) x = sweep;
pit = spikeheap.insert(std::make_pair(x, BeachIt(beach.end())));
beach.insert(std::make_pair(Part(ppv, prev, next), pit));
beach.insert(std::make_pair(Part(ppv, prev, next), pit));
SpikeHeap::iterator nit = spikeheap.end();
if (nnt != -1 && prev != nnt &&
circle_form(points[prev], points[next], points[nnt])) {
double x = circle_point(points[prev], points[next], points[nnt]);
if (x < sweep) x = sweep;
nit = spikeheap.insert(std::make_pair(x, BeachIt(beach.end())));
beach.insert(std::make_pair(Part(prev, next, nnt), nit));
beach.insert(std::make_pair(Part(prev, next, nnt), nit));
for (Beach::iterator it = beach.begin(); it != beach.end(); ++it) {
int curr = it->first.curr;
int next = it->first.next;
if (next == -1) continue;
std::make_pair(curr, next) : std::make_pair(next, curr);
if (arcs.find(arc) == arcs.end()) {
g.addEdge(nodes[curr], nodes[next]);
Counter cnt("Number of arcs removed: ");
for(std::vector<Edge>::reverse_iterator ei=arcs.rbegin();
if(bfs.predArc(b)==INVALID || bfs.dist(b)>d)
Counter cnt("Number of arcs removed: ");
for(std::vector<Edge>::reverse_iterator ei=arcs.rbegin();
ConstMap<Arc,int> cegy(1);
Suurballe<ListGraph,ConstMap<Arc,int> > sur(g,cegy);
if(k<2 || sur.totalLength()>d)
// else std::cout << "Remove arc " << g.id(a) << "-" << g.id(b) << '\n';
void sparseTriangle(int d)
Counter cnt("Number of arcs added: ");
std::vector<Parc> pedges;
for(NodeIt n(g);n!=INVALID;++n)
for(NodeIt m=++(NodeIt(n));m!=INVALID;++m)
p.len=(coords[m]-coords[n]).normSquare();
std::sort(pedges.begin(),pedges.end(),pedgeLess);
for(std::vector<Parc>::iterator pi=pedges.begin();pi!=pedges.end();++pi)
for(;e!=INVALID && !cross(e,li);++e) ;
ConstMap<Arc,int> cegy(1);
Suurballe<ListGraph,ConstMap<Arc,int> > sur(g,cegy);
int k=sur.run(pi->a,pi->b,2);
if(k<2 || sur.totalLength()>d)
ne=g.addEdge(pi->a,pi->b);
template <typename Graph, typename CoordMap>
typedef typename Graph::Edge Key;
typedef typename CoordMap::Value::Value Value;
LengthSquareMap(const Graph& graph, const CoordMap& coords)
: _graph(graph), _coords(coords) {}
Value operator[](const Key& key) const {
return (_coords[_graph.v(key)] -
_coords[_graph.u(key)]).normSquare();
std::vector<Parc> pedges;
std::cout << T.realTime() << "s: Creating delaunay triangulation...\n";
std::cout << T.realTime() << "s: Calculating spanning tree...\n";
LengthSquareMap<ListGraph, ListGraph::NodeMap<Point> > ls(g, coords);
ListGraph::EdgeMap<bool> tree(g);
std::cout << T.realTime() << "s: Removing non tree arcs...\n";
std::vector<Edge> remove;
for (EdgeIt e(g); e != INVALID; ++e) {
if (!tree[e]) remove.push_back(e);
for(int i = 0; i < int(remove.size()); ++i) {
std::cout << T.realTime() << "s: Done\n";
std::cout << "Find a tree..." << std::endl;
std::cout << "Total arc length (tree) : " << totalLen() << std::endl;
std::cout << "Make it Euler..." << std::endl;
for(NodeIt n(g);n!=INVALID;++n)
if(countIncEdges(g,n)%2==1) leafs.push_back(n);
// for(unsigned int i=0;i<leafs.size();i+=2)
// g.addArc(leafs[i],leafs[i+1]);
std::vector<Parc> pedges;
for(unsigned int i=0;i<leafs.size()-1;i++)
for(unsigned int j=i+1;j<leafs.size();j++)
p.len=(coords[m]-coords[n]).normSquare();
std::sort(pedges.begin(),pedges.end(),pedgeLess);
for(unsigned int i=0;i<pedges.size();i++)
if(countIncEdges(g,pedges[i].a)%2 &&
countIncEdges(g,pedges[i].b)%2)
g.addEdge(pedges[i].a,pedges[i].b);
for(NodeIt n(g);n!=INVALID;++n)
if(countIncEdges(g,n)%2 || countIncEdges(g,n)==0 )
std::cout << "GEBASZ!!!" << std::endl;
for(EdgeIt e(g);e!=INVALID;++e)
std::cout << "LOOP GEBASZ!!!" << std::endl;
std::cout << "Number of arcs : " << countEdges(g) << std::endl;
std::cout << "Total arc length (euler) : " << totalLen() << std::endl;
ListGraph::EdgeMap<Arc> enext(g);
// std::cout << "Tour arc: " << g.id(Edge(e)) << std::endl;
// std::cout << "Tour arc: " << g.id(Edge(e)) << std::endl;
std::cout << "Creating a tour from that..." << std::endl;
int nnum = countNodes(g);
int ednum = countEdges(g);
for(Arc p=enext[EdgeIt(g)];ednum>nnum;p=enext[p])
// std::cout << "Checking arc " << g.id(p) << std::endl;
Node n1=g.oppositeNode(n2,e);
Node n3=g.oppositeNode(n2,f);
if(countIncEdges(g,n2)>2)
// std::cout << "Remove an Arc" << std::endl;
Arc ne=g.direct(g.addEdge(n1,n3),n1);
std::cout << "Total arc length (tour) : " << totalLen() << std::endl;
std::cout << "2-opt the tour..." << std::endl;
std::cout << "Total arc length (2-opt tour) : " << totalLen() << std::endl;
int main(int argc,const char **argv)
bool disc_d, square_d, gauss_d;
// bool tsp_a,two_a,tree_a;
std::string ndist("disc");
ap.refOption("n", "Number of nodes (default is 100)", N)
.intOption("g", "Girth parameter (default is 10)", 10)
.refOption("cities", "Number of cities (default is 1)", num_of_cities)
.refOption("area", "Full relative area of the cities (default is 1)", area)
.refOption("disc", "Nodes are evenly distributed on a unit disc (default)",disc_d)
.optionGroup("dist", "disc")
.refOption("square", "Nodes are evenly distributed on a unit square", square_d)
.optionGroup("dist", "square")
"Nodes are located according to a two-dim gauss distribution",
.optionGroup("dist", "gauss")
// .mandatoryGroup("dist")
.boolOption("eps", "Also generate .eps output (prefix.eps)")
.boolOption("nonodes", "Draw the edges only in the generated .eps")
.boolOption("dir", "Directed digraph is generated (each arcs are replaced by two directed ones)")
.boolOption("2con", "Create a two connected planar digraph")
.optionGroup("alg","2con")
.boolOption("tree", "Create a min. cost spanning tree")
.optionGroup("alg","tree")
.boolOption("tsp", "Create a TSP tour")
.optionGroup("alg","tsp")
.boolOption("tsp2", "Create a TSP tour (tree based)")
.optionGroup("alg","tsp2")
.boolOption("dela", "Delaunay triangulation digraph")
.optionGroup("alg","dela")
.boolOption("rand", "Use time seed for random number generator")
.optionGroup("rand", "rand")
.intOption("seed", "Random seed", -1)
.optionGroup("rand", "seed")
.other("[prefix]","Prefix of the output files. Default is 'lgf-gen-out'")
std::cout << "Random number seed: " << seed << std::endl;
std::cout << "Random number seed: " << seed << std::endl;
switch(ap.files().size())
std::cerr << "\nAt most one prefix can be given\n\n";
std::vector<double> sizes;
std::vector<double> cum_sizes;
for(int s=0;s<num_of_cities;s++)
// sum_sizes+=rnd.exponential();
cum_sizes.push_back(sum_sizes);
for(int s=0;s<num_of_cities;s++)
Point center=(num_of_cities==1?Point(0,0):rnd.disc());
for(;i<N*(cum_sizes[s]/sum_sizes);i++) {
coords[n]=center+rnd.gauss2()*area*
std::sqrt(sizes[s]/sum_sizes);
for(;i<N*(cum_sizes[s]/sum_sizes);i++) {
coords[n]=center+Point(rnd()*2-1,rnd()*2-1)*area*
std::sqrt(sizes[s]/sum_sizes);
for(;i<N*(cum_sizes[s]/sum_sizes);i++) {
coords[n]=center+rnd.disc()*area*
std::sqrt(sizes[s]/sum_sizes);
// for (ListGraph::NodeIt n(g); n != INVALID; ++n) {
// std::cerr << coords[n] << std::endl;
std::cout << "#2-opt improvements: " << tsp_impr_num << std::endl;
std::cout << "#2-opt improvements: " << tsp_impr_num << std::endl;
std::cout << "Make triangles\n";
std::cout << "Make it sparser\n";
std::cout << "Number of nodes : " << countNodes(g) << std::endl;
std::cout << "Number of arcs : " << countEdges(g) << std::endl;
for(EdgeIt e(g);e!=INVALID;++e)
tlen+=std::sqrt((coords[g.v(e)]-coords[g.u(e)]).normSquare());
std::cout << "Total arc length : " << tlen << std::endl;
graphToEps(g,prefix+".eps").scaleToA4().
scale(600).nodeScale(.005).arcWidthScale(.001).preScale(false).
coords(coords).hideNodes(ap.given("nonodes")).run();
DigraphWriter<ListGraph>(g,prefix+".lgf").
nodeMap("coordinates_x",scaleMap(xMap(coords),600)).
nodeMap("coordinates_y",scaleMap(yMap(coords),600)).
else GraphWriter<ListGraph>(g,prefix+".lgf").
nodeMap("coordinates_x",scaleMap(xMap(coords),600)).
nodeMap("coordinates_y",scaleMap(yMap(coords),600)).