[Lemon-commits] Balazs Dezso: Porting full graphs from svn 3498
Lemon HG
hg at lemon.cs.elte.hu
Tue Nov 4 11:27:57 CET 2008
details: http://lemon.cs.elte.hu/hg/lemon/rev/37557a46e298
changeset: 365:37557a46e298
user: Balazs Dezso <deba [at] inf.elte.hu>
date: Thu Aug 14 21:49:39 2008 +0200
description:
Porting full graphs from svn 3498
- the FullGraph is redesigned in implementation
- some improvemnts in documentation
diffstat:
4 files changed, 704 insertions(+), 15 deletions(-)
lemon/Makefile.am | 1
lemon/full_graph.h | 608 ++++++++++++++++++++++++++++++++++++++++++++++++++
test/digraph_test.cc | 38 +++
test/graph_test.cc | 72 ++++-
diffs (truncated from 788 to 300 lines):
diff -r f1158744a112 -r 37557a46e298 lemon/Makefile.am
--- a/lemon/Makefile.am Mon Aug 04 22:00:36 2008 +0200
+++ b/lemon/Makefile.am Thu Aug 14 21:49:39 2008 +0200
@@ -29,6 +29,7 @@
lemon/dijkstra.h \
lemon/dim2.h \
lemon/error.h \
+ lemon/full_graph.h \
lemon/graph_to_eps.h \
lemon/kruskal.h \
lemon/lgf_reader.h \
diff -r f1158744a112 -r 37557a46e298 lemon/full_graph.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/lemon/full_graph.h Thu Aug 14 21:49:39 2008 +0200
@@ -0,0 +1,608 @@
+/* -*- mode: C++; indent-tabs-mode: nil; -*-
+ *
+ * This file is a part of LEMON, a generic C++ optimization library.
+ *
+ * Copyright (C) 2003-2008
+ * 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
+ * purpose.
+ *
+ */
+
+#ifndef LEMON_FULL_GRAPH_H
+#define LEMON_FULL_GRAPH_H
+
+#include <lemon/math.h>
+
+#include <lemon/core.h>
+#include <lemon/bits/graph_extender.h>
+
+///\ingroup graphs
+///\file
+///\brief FullDigraph and FullGraph classes.
+namespace lemon {
+
+ class FullDigraphBase {
+ public:
+
+ typedef FullDigraphBase Graph;
+
+ class Node;
+ class Arc;
+
+ protected:
+
+ int _node_num;
+ int _arc_num;
+
+ FullDigraphBase() {}
+
+ void construct(int n) { _node_num = n; _arc_num = n * n; }
+
+ public:
+
+ typedef True NodeNumTag;
+ typedef True ArcNumTag;
+
+ Node operator()(int ix) const { return Node(ix); }
+ int index(const Node& node) const { return node._id; }
+
+ Arc arc(const Node& s, const Node& t) const {
+ return Arc(s._id * _node_num + t._id);
+ }
+
+ int nodeNum() const { return _node_num; }
+ int arcNum() const { return _arc_num; }
+
+ int maxNodeId() const { return _node_num - 1; }
+ int maxArcId() const { return _arc_num - 1; }
+
+ Node source(Arc arc) const { return arc._id / _node_num; }
+ Node target(Arc arc) const { return arc._id % _node_num; }
+
+
+ static int id(Node node) { return node._id; }
+ static int id(Arc arc) { return arc._id; }
+
+ static Node nodeFromId(int id) { return Node(id);}
+
+ static Arc arcFromId(int id) { return Arc(id);}
+
+ typedef True FindArcTag;
+
+ Arc findArc(Node s, Node t, Arc prev = INVALID) const {
+ return prev != INVALID ? arc(s, t) : INVALID;
+ }
+
+
+ class Node {
+ friend class FullDigraphBase;
+
+ protected:
+ int _id;
+ Node(int id) : _id(id) {}
+ public:
+ Node() {}
+ Node (Invalid) : _id(-1) {}
+ bool operator==(const Node node) const {return _id == node._id;}
+ bool operator!=(const Node node) const {return _id != node._id;}
+ bool operator<(const Node node) const {return _id < node._id;}
+ };
+
+ class Arc {
+ friend class FullDigraphBase;
+
+ protected:
+ int _id; // _node_num * source + target;
+
+ Arc(int id) : _id(id) {}
+
+ public:
+ Arc() { }
+ Arc (Invalid) { _id = -1; }
+ bool operator==(const Arc arc) const {return _id == arc._id;}
+ bool operator!=(const Arc arc) const {return _id != arc._id;}
+ bool operator<(const Arc arc) const {return _id < arc._id;}
+ };
+
+ void first(Node& node) const {
+ node._id = _node_num - 1;
+ }
+
+ static void next(Node& node) {
+ --node._id;
+ }
+
+ void first(Arc& arc) const {
+ arc._id = _arc_num - 1;
+ }
+
+ static void next(Arc& arc) {
+ --arc._id;
+ }
+
+ void firstOut(Arc& arc, const Node& node) const {
+ arc._id = (node._id + 1) * _node_num - 1;
+ }
+
+ void nextOut(Arc& arc) const {
+ if (arc._id % _node_num == 0) arc._id = 0;
+ --arc._id;
+ }
+
+ void firstIn(Arc& arc, const Node& node) const {
+ arc._id = _arc_num + node._id - _node_num;
+ }
+
+ void nextIn(Arc& arc) const {
+ arc._id -= _node_num;
+ if (arc._id < 0) arc._id = -1;
+ }
+
+ };
+
+ typedef DigraphExtender<FullDigraphBase> ExtendedFullDigraphBase;
+
+ /// \ingroup graphs
+ ///
+ /// \brief A full digraph class.
+ ///
+ /// This is a simple and fast directed full graph implementation.
+ /// From each node go arcs to each node (including the source node),
+ /// therefore the number of the arcs in the digraph is the square of
+ /// the node number. The digraph is completely static, so you can
+ /// neither add nor delete either arcs or nodes, and it needs just
+ /// constant space in memory.
+ ///
+ /// Thus it conforms to the \ref concepts::Digraph "Digraph" concept
+ /// and it also has an important extra feature that its maps are
+ /// real \ref concepts::ReferenceMap "reference map"s.
+ /// \sa concepts::Digraph.
+ ///
+ /// \sa FullGraph
+ class FullDigraph : public ExtendedFullDigraphBase {
+ public:
+
+ typedef ExtendedFullDigraphBase Parent;
+
+ /// \brief Constructor
+ FullDigraph() { construct(0); }
+
+ /// \brief Constructor
+ ///
+ /// \param n The number of the nodes.
+ FullDigraph(int n) { construct(n); }
+
+ /// \brief Resize the digraph
+ ///
+ /// Resize the digraph. The function will fully destroy and
+ /// rebuild the digraph. This cause that the maps of the digraph
+ /// will reallocated automatically and the previous values will be
+ /// lost.
+ void resize(int n) {
+ Parent::notifier(Arc()).clear();
+ Parent::notifier(Node()).clear();
+ construct(n);
+ Parent::notifier(Node()).build();
+ Parent::notifier(Arc()).build();
+ }
+
+ /// \brief Returns the node with the given index.
+ ///
+ /// Returns the node with the given index. Because it is a
+ /// static size digraph the node's of the digraph can be indexed
+ /// in the range <tt>[0..nodeNum()-1]</tt> and the index of
+ /// the node can accessed by the \e index() member.
+ Node operator()(int ix) const { return Parent::operator()(ix); }
+
+ /// \brief Returns the index of the node.
+ ///
+ /// Returns the index of the node. Because it is a
+ /// static size digraph the node's of the digraph can be indexed
+ /// in the range <tt>[0..nodeNum()-1]</tt> and the index of
+ /// the node can accessed by the \e index() member.
+ int index(const Node& node) const { return Parent::index(node); }
+
+ /// \brief Returns the arc connects the given nodes.
+ ///
+ /// Returns the arc connects the given nodes.
+ Arc arc(const Node& u, const Node& v) const {
+ return Parent::arc(u, v);
+ }
+
+ /// \brief Number of nodes.
+ int nodeNum() const { return Parent::nodeNum(); }
+ /// \brief Number of arcs.
+ int arcNum() const { return Parent::arcNum(); }
+ };
+
+
+ class FullGraphBase {
+ int _node_num;
+ int _edge_num;
+ public:
+
+ typedef FullGraphBase Graph;
+
+ class Node;
+ class Arc;
+ class Edge;
+
+ protected:
+
+ FullGraphBase() {}
+
+ void construct(int n) { _node_num = n; _edge_num = n * (n - 1) / 2; }
+
+ int _uid(int e) const {
+ int u = e / _node_num;
+ int v = e % _node_num;
+ return u < v ? u : _node_num - 2 - u;
+ }
+
+ int _vid(int e) const {
+ int u = e / _node_num;
+ int v = e % _node_num;
+ return u < v ? v : _node_num - 1 - v;
+ }
+
+ void _uvid(int e, int& u, int& v) const {
+ u = e / _node_num;
+ v = e % _node_num;
+ if (u >= v) {
+ u = _node_num - 2 - u;
+ v = _node_num - 1 - v;
+ }
+ }
+
+ void _stid(int a, int& s, int& t) const {
+ if ((a & 1) == 1) {
+ _uvid(a >> 1, s, t);
+ } else {
+ _uvid(a >> 1, t, s);
+ }
+ }
+
+ int _eid(int u, int v) const {
+ if (u < (_node_num - 1) / 2) {
+ return u * _node_num + v;
+ } else {
+ return (_node_num - 1 - u) * _node_num - v - 1;
+ }
+ }
+
+ public:
+
+
+ Node operator()(int ix) const { return Node(ix); }
+ int index(const Node& node) const { return node._id; }
More information about the Lemon-commits
mailing list