| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2009 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_LIST_GRAPH_H |
| 20 | 20 |
#define LEMON_LIST_GRAPH_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup graphs |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief ListDigraph, ListGraph classes. |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/core.h> |
| 27 | 27 |
#include <lemon/error.h> |
| 28 | 28 |
#include <lemon/bits/graph_extender.h> |
| 29 | 29 |
|
| 30 | 30 |
#include <vector> |
| 31 | 31 |
#include <list> |
| 32 | 32 |
|
| 33 | 33 |
namespace lemon {
|
| 34 | 34 |
|
| 35 | 35 |
class ListDigraphBase {
|
| 36 | 36 |
|
| 37 | 37 |
protected: |
| 38 | 38 |
struct NodeT {
|
| 39 | 39 |
int first_in, first_out; |
| 40 | 40 |
int prev, next; |
| 41 | 41 |
}; |
| 42 | 42 |
|
| 43 | 43 |
struct ArcT {
|
| 44 | 44 |
int target, source; |
| 45 | 45 |
int prev_in, prev_out; |
| 46 | 46 |
int next_in, next_out; |
| 47 | 47 |
}; |
| 48 | 48 |
|
| 49 | 49 |
std::vector<NodeT> nodes; |
| 50 | 50 |
|
| 51 | 51 |
int first_node; |
| 52 | 52 |
|
| 53 | 53 |
int first_free_node; |
| 54 | 54 |
|
| 55 | 55 |
std::vector<ArcT> arcs; |
| 56 | 56 |
|
| 57 | 57 |
int first_free_arc; |
| 58 | 58 |
|
| 59 | 59 |
public: |
| 60 | 60 |
|
| 61 | 61 |
typedef ListDigraphBase Digraph; |
| 62 | 62 |
|
| 63 | 63 |
class Node {
|
| 64 | 64 |
friend class ListDigraphBase; |
| 65 | 65 |
protected: |
| 66 | 66 |
|
| 67 | 67 |
int id; |
| 68 | 68 |
explicit Node(int pid) { id = pid;}
|
| 69 | 69 |
|
| 70 | 70 |
public: |
| 71 | 71 |
Node() {}
|
| 72 | 72 |
Node (Invalid) { id = -1; }
|
| 73 | 73 |
bool operator==(const Node& node) const {return id == node.id;}
|
| 74 | 74 |
bool operator!=(const Node& node) const {return id != node.id;}
|
| 75 | 75 |
bool operator<(const Node& node) const {return id < node.id;}
|
| 76 | 76 |
}; |
| 77 | 77 |
|
| 78 | 78 |
class Arc {
|
| 79 | 79 |
friend class ListDigraphBase; |
| 80 | 80 |
protected: |
| 81 | 81 |
|
| 82 | 82 |
int id; |
| 83 | 83 |
explicit Arc(int pid) { id = pid;}
|
| 84 | 84 |
|
| 85 | 85 |
public: |
| 86 | 86 |
Arc() {}
|
| 87 | 87 |
Arc (Invalid) { id = -1; }
|
| 88 | 88 |
bool operator==(const Arc& arc) const {return id == arc.id;}
|
| 89 | 89 |
bool operator!=(const Arc& arc) const {return id != arc.id;}
|
| 90 | 90 |
bool operator<(const Arc& arc) const {return id < arc.id;}
|
| 91 | 91 |
}; |
| 92 | 92 |
|
| 93 | 93 |
|
| 94 | 94 |
|
| 95 | 95 |
ListDigraphBase() |
| 96 | 96 |
: nodes(), first_node(-1), |
| 97 | 97 |
first_free_node(-1), arcs(), first_free_arc(-1) {}
|
| 98 | 98 |
|
| 99 | 99 |
|
| 100 | 100 |
int maxNodeId() const { return nodes.size()-1; }
|
| 101 | 101 |
int maxArcId() const { return arcs.size()-1; }
|
| 102 | 102 |
|
| 103 | 103 |
Node source(Arc e) const { return Node(arcs[e.id].source); }
|
| 104 | 104 |
Node target(Arc e) const { return Node(arcs[e.id].target); }
|
| 105 | 105 |
|
| 106 | 106 |
|
| 107 | 107 |
void first(Node& node) const {
|
| 108 | 108 |
node.id = first_node; |
| 109 | 109 |
} |
| 110 | 110 |
|
| 111 | 111 |
void next(Node& node) const {
|
| 112 | 112 |
node.id = nodes[node.id].next; |
| 113 | 113 |
} |
| 114 | 114 |
|
| 115 | 115 |
|
| 116 | 116 |
void first(Arc& arc) const {
|
| 117 | 117 |
int n; |
| 118 | 118 |
for(n = first_node; |
| 119 |
n!=-1 && nodes[n]. |
|
| 119 |
n != -1 && nodes[n].first_out == -1; |
|
| 120 | 120 |
n = nodes[n].next) {}
|
| 121 |
arc.id = (n == -1) ? -1 : nodes[n]. |
|
| 121 |
arc.id = (n == -1) ? -1 : nodes[n].first_out; |
|
| 122 | 122 |
} |
| 123 | 123 |
|
| 124 | 124 |
void next(Arc& arc) const {
|
| 125 |
if (arcs[arc.id].next_in != -1) {
|
|
| 126 |
arc.id = arcs[arc.id].next_in; |
|
| 125 |
if (arcs[arc.id].next_out != -1) {
|
|
| 126 |
arc.id = arcs[arc.id].next_out; |
|
| 127 | 127 |
} else {
|
| 128 | 128 |
int n; |
| 129 |
for(n = nodes[arcs[arc.id].target].next; |
|
| 130 |
n!=-1 && nodes[n].first_in == -1; |
|
| 129 |
for(n = nodes[arcs[arc.id].source].next; |
|
| 130 |
n != -1 && nodes[n].first_out == -1; |
|
| 131 | 131 |
n = nodes[n].next) {}
|
| 132 |
arc.id = (n == -1) ? -1 : nodes[n]. |
|
| 132 |
arc.id = (n == -1) ? -1 : nodes[n].first_out; |
|
| 133 | 133 |
} |
| 134 | 134 |
} |
| 135 | 135 |
|
| 136 | 136 |
void firstOut(Arc &e, const Node& v) const {
|
| 137 | 137 |
e.id = nodes[v.id].first_out; |
| 138 | 138 |
} |
| 139 | 139 |
void nextOut(Arc &e) const {
|
| 140 | 140 |
e.id=arcs[e.id].next_out; |
| 141 | 141 |
} |
| 142 | 142 |
|
| 143 | 143 |
void firstIn(Arc &e, const Node& v) const {
|
| 144 | 144 |
e.id = nodes[v.id].first_in; |
| 145 | 145 |
} |
| 146 | 146 |
void nextIn(Arc &e) const {
|
| 147 | 147 |
e.id=arcs[e.id].next_in; |
| 148 | 148 |
} |
| 149 | 149 |
|
| 150 | 150 |
|
| 151 | 151 |
static int id(Node v) { return v.id; }
|
| 152 | 152 |
static int id(Arc e) { return e.id; }
|
| 153 | 153 |
|
| 154 | 154 |
static Node nodeFromId(int id) { return Node(id);}
|
| 155 | 155 |
static Arc arcFromId(int id) { return Arc(id);}
|
| 156 | 156 |
|
| 157 | 157 |
bool valid(Node n) const {
|
| 158 | 158 |
return n.id >= 0 && n.id < static_cast<int>(nodes.size()) && |
| 159 | 159 |
nodes[n.id].prev != -2; |
| 160 | 160 |
} |
| 161 | 161 |
|
| 162 | 162 |
bool valid(Arc a) const {
|
| 163 | 163 |
return a.id >= 0 && a.id < static_cast<int>(arcs.size()) && |
| 164 | 164 |
arcs[a.id].prev_in != -2; |
| 165 | 165 |
} |
| 166 | 166 |
|
| 167 | 167 |
Node addNode() {
|
| 168 | 168 |
int n; |
| 169 | 169 |
|
| 170 | 170 |
if(first_free_node==-1) {
|
| 171 | 171 |
n = nodes.size(); |
| 172 | 172 |
nodes.push_back(NodeT()); |
| 173 | 173 |
} else {
|
| 174 | 174 |
n = first_free_node; |
| 175 | 175 |
first_free_node = nodes[n].next; |
| 176 | 176 |
} |
| 177 | 177 |
|
| 178 | 178 |
nodes[n].next = first_node; |
| 179 | 179 |
if(first_node != -1) nodes[first_node].prev = n; |
| 180 | 180 |
first_node = n; |
| 181 | 181 |
nodes[n].prev = -1; |
| 182 | 182 |
|
| 183 | 183 |
nodes[n].first_in = nodes[n].first_out = -1; |
| 184 | 184 |
|
| 185 | 185 |
return Node(n); |
| 186 | 186 |
} |
| 187 | 187 |
|
| 188 | 188 |
Arc addArc(Node u, Node v) {
|
| 189 | 189 |
int n; |
| 190 | 190 |
|
| 191 | 191 |
if (first_free_arc == -1) {
|
| 192 | 192 |
n = arcs.size(); |
| 193 | 193 |
arcs.push_back(ArcT()); |
| 194 | 194 |
} else {
|
| 195 | 195 |
n = first_free_arc; |
| 196 | 196 |
first_free_arc = arcs[n].next_in; |
| 197 | 197 |
} |
| 198 | 198 |
|
| 199 | 199 |
arcs[n].source = u.id; |
| 200 | 200 |
arcs[n].target = v.id; |
| 201 | 201 |
|
| 202 | 202 |
arcs[n].next_out = nodes[u.id].first_out; |
| 203 | 203 |
if(nodes[u.id].first_out != -1) {
|
| 204 | 204 |
arcs[nodes[u.id].first_out].prev_out = n; |
| 205 | 205 |
} |
| 206 | 206 |
|
| 207 | 207 |
arcs[n].next_in = nodes[v.id].first_in; |
| 208 | 208 |
if(nodes[v.id].first_in != -1) {
|
| 209 | 209 |
arcs[nodes[v.id].first_in].prev_in = n; |
| 210 | 210 |
} |
| 211 | 211 |
|
| 212 | 212 |
arcs[n].prev_in = arcs[n].prev_out = -1; |
| 213 | 213 |
|
| 214 | 214 |
nodes[u.id].first_out = nodes[v.id].first_in = n; |
| 215 | 215 |
|
| 216 | 216 |
return Arc(n); |
| 217 | 217 |
} |
| 218 | 218 |
|
| 219 | 219 |
void erase(const Node& node) {
|
| 220 | 220 |
int n = node.id; |
| 221 | 221 |
|
| 222 | 222 |
if(nodes[n].next != -1) {
|
| 223 | 223 |
nodes[nodes[n].next].prev = nodes[n].prev; |
| 224 | 224 |
} |
| 225 | 225 |
|
| 226 | 226 |
if(nodes[n].prev != -1) {
|
| 227 | 227 |
nodes[nodes[n].prev].next = nodes[n].next; |
| 228 | 228 |
} else {
|
| 229 | 229 |
first_node = nodes[n].next; |
| 230 | 230 |
} |
| 231 | 231 |
|
| 232 | 232 |
nodes[n].next = first_free_node; |
| 233 | 233 |
first_free_node = n; |
| 234 | 234 |
nodes[n].prev = -2; |
| 235 | 235 |
|
| 236 | 236 |
} |
| 237 | 237 |
|
| 238 | 238 |
void erase(const Arc& arc) {
|
| 239 | 239 |
int n = arc.id; |
| 240 | 240 |
|
| 241 | 241 |
if(arcs[n].next_in!=-1) {
|
| 242 | 242 |
arcs[arcs[n].next_in].prev_in = arcs[n].prev_in; |
| 243 | 243 |
} |
| 244 | 244 |
|
| 245 | 245 |
if(arcs[n].prev_in!=-1) {
|
| 246 | 246 |
arcs[arcs[n].prev_in].next_in = arcs[n].next_in; |
| 247 | 247 |
} else {
|
| 248 | 248 |
nodes[arcs[n].target].first_in = arcs[n].next_in; |
| 249 | 249 |
} |
| 250 | 250 |
|
| 251 | 251 |
|
| 252 | 252 |
if(arcs[n].next_out!=-1) {
|
| 253 | 253 |
arcs[arcs[n].next_out].prev_out = arcs[n].prev_out; |
| 254 | 254 |
} |
| 255 | 255 |
|
| 256 | 256 |
if(arcs[n].prev_out!=-1) {
|
| 257 | 257 |
arcs[arcs[n].prev_out].next_out = arcs[n].next_out; |
| 258 | 258 |
} else {
|
| 259 | 259 |
nodes[arcs[n].source].first_out = arcs[n].next_out; |
| 260 | 260 |
} |
| 261 | 261 |
|
| 262 | 262 |
arcs[n].next_in = first_free_arc; |
| 263 | 263 |
first_free_arc = n; |
| 264 | 264 |
arcs[n].prev_in = -2; |
| 265 | 265 |
} |
| 266 | 266 |
|
| 267 | 267 |
void clear() {
|
| 268 | 268 |
arcs.clear(); |
| 269 | 269 |
nodes.clear(); |
| 270 | 270 |
first_node = first_free_node = first_free_arc = -1; |
| 271 | 271 |
} |
| 272 | 272 |
|
| 273 | 273 |
protected: |
| 274 | 274 |
void changeTarget(Arc e, Node n) |
| 275 | 275 |
{
|
| 276 | 276 |
if(arcs[e.id].next_in != -1) |
| 277 | 277 |
arcs[arcs[e.id].next_in].prev_in = arcs[e.id].prev_in; |
| 278 | 278 |
if(arcs[e.id].prev_in != -1) |
| 279 | 279 |
arcs[arcs[e.id].prev_in].next_in = arcs[e.id].next_in; |
| 280 | 280 |
else nodes[arcs[e.id].target].first_in = arcs[e.id].next_in; |
| 281 | 281 |
if (nodes[n.id].first_in != -1) {
|
| 282 | 282 |
arcs[nodes[n.id].first_in].prev_in = e.id; |
| 283 | 283 |
} |
| 284 | 284 |
arcs[e.id].target = n.id; |
| 285 | 285 |
arcs[e.id].prev_in = -1; |
| 286 | 286 |
arcs[e.id].next_in = nodes[n.id].first_in; |
| 287 | 287 |
nodes[n.id].first_in = e.id; |
| 288 | 288 |
} |
| 289 | 289 |
void changeSource(Arc e, Node n) |
| 290 | 290 |
{
|
| 291 | 291 |
if(arcs[e.id].next_out != -1) |
| 292 | 292 |
arcs[arcs[e.id].next_out].prev_out = arcs[e.id].prev_out; |
| 293 | 293 |
if(arcs[e.id].prev_out != -1) |
| 294 | 294 |
arcs[arcs[e.id].prev_out].next_out = arcs[e.id].next_out; |
| 295 | 295 |
else nodes[arcs[e.id].source].first_out = arcs[e.id].next_out; |
| 296 | 296 |
if (nodes[n.id].first_out != -1) {
|
| 297 | 297 |
arcs[nodes[n.id].first_out].prev_out = e.id; |
| 298 | 298 |
} |
| 299 | 299 |
arcs[e.id].source = n.id; |
| 300 | 300 |
arcs[e.id].prev_out = -1; |
| 301 | 301 |
arcs[e.id].next_out = nodes[n.id].first_out; |
| 302 | 302 |
nodes[n.id].first_out = e.id; |
| 303 | 303 |
} |
| 304 | 304 |
|
| 305 | 305 |
}; |
| 306 | 306 |
|
| 307 | 307 |
typedef DigraphExtender<ListDigraphBase> ExtendedListDigraphBase; |
| 308 | 308 |
|
| 309 | 309 |
/// \addtogroup graphs |
| 310 | 310 |
/// @{
|
| 311 | 311 |
|
| 312 | 312 |
///A general directed graph structure. |
| 313 | 313 |
|
| 314 | 314 |
///\ref ListDigraph is a simple and fast <em>directed graph</em> |
| 315 | 315 |
///implementation based on static linked lists that are stored in |
| 316 | 316 |
///\c std::vector structures. |
| 317 | 317 |
/// |
| 318 | 318 |
///It conforms to the \ref concepts::Digraph "Digraph concept" and it |
| 319 | 319 |
///also provides several useful additional functionalities. |
| 320 | 320 |
///Most of the member functions and nested classes are documented |
| 321 | 321 |
///only in the concept class. |
| 322 | 322 |
/// |
| 323 | 323 |
///\sa concepts::Digraph |
| 324 | 324 |
|
0 comments (0 inline)