COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/list_bpugraph.h @ 2115:4cd528a30ec1

Last change on this file since 2115:4cd528a30ec1 was 2115:4cd528a30ec1, checked in by Balazs Dezso, 13 years ago

Splitted graph files

File size: 10.6 KB
Line 
1/* -*- C++ -*-
2 *
3 * This file is a part of LEMON, a generic C++ optimization library
4 *
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 *
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
12 *
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
15 * purpose.
16 *
17 */
18
19#ifndef LEMON_LIST_BPUGRAPH_H
20#define LEMON_LIST_BPUGRAPH_H
21
22///\ingroup graphs
23///\file
24///\brief ListBpUGraph classes.
25
26#include <lemon/bits/bpugraph_extender.h>
27
28#include <lemon/error.h>
29
30#include <vector>
31#include <list>
32
33namespace lemon {
34
35  class ListBpUGraphBase {
36  public:
37
38    class NodeSetError : public LogicError {
39      virtual const char* exceptionName() const {
40        return "lemon::ListBpUGraph::NodeSetError";
41      }
42    };
43
44  protected:
45
46    struct NodeT {
47      int first_edge, prev, next;
48    };
49
50    struct UEdgeT {
51      int aNode, prev_out, next_out;
52      int bNode, prev_in, next_in;
53    };
54
55    std::vector<NodeT> aNodes;
56    std::vector<NodeT> bNodes;
57
58    std::vector<UEdgeT> edges;
59
60    int first_anode;
61    int first_free_anode;
62
63    int first_bnode;
64    int first_free_bnode;
65
66    int first_free_edge;
67
68  public:
69 
70    class Node {
71      friend class ListBpUGraphBase;
72    protected:
73      int id;
74
75      explicit Node(int _id) : id(_id) {}
76    public:
77      Node() {}
78      Node(Invalid) { id = -1; }
79      bool operator==(const Node i) const {return id==i.id;}
80      bool operator!=(const Node i) const {return id!=i.id;}
81      bool operator<(const Node i) const {return id<i.id;}
82    };
83
84    class UEdge {
85      friend class ListBpUGraphBase;
86    protected:
87      int id;
88
89      explicit UEdge(int _id) { id = _id;}
90    public:
91      UEdge() {}
92      UEdge (Invalid) { id = -1; }
93      bool operator==(const UEdge i) const {return id==i.id;}
94      bool operator!=(const UEdge i) const {return id!=i.id;}
95      bool operator<(const UEdge i) const {return id<i.id;}
96    };
97
98    ListBpUGraphBase()
99      : first_anode(-1), first_free_anode(-1),
100        first_bnode(-1), first_free_bnode(-1),
101        first_free_edge(-1) {}
102
103    void firstANode(Node& node) const {
104      node.id = first_anode != -1 ? (first_anode << 1) : -1;
105    }
106    void nextANode(Node& node) const {
107      node.id = aNodes[node.id >> 1].next;
108    }
109
110    void firstBNode(Node& node) const {
111      node.id = first_bnode != -1 ? (first_bnode << 1) + 1 : -1;
112    }
113    void nextBNode(Node& node) const {
114      node.id = bNodes[node.id >> 1].next;
115    }
116
117    void first(Node& node) const {
118      if (first_anode != -1) {
119        node.id = (first_anode << 1);
120      } else if (first_bnode != -1) {
121        node.id = (first_bnode << 1) + 1;
122      } else {
123        node.id = -1;
124      }
125    }
126    void next(Node& node) const {
127      if (aNode(node)) {
128        node.id = aNodes[node.id >> 1].next;
129        if (node.id == -1) {
130          if (first_bnode != -1) {
131            node.id = (first_bnode << 1) + 1;
132          }
133        }
134      } else {
135        node.id = bNodes[node.id >> 1].next;
136      }
137    }
138 
139    void first(UEdge& edge) const {
140      int aNodeId = first_anode;
141      while (aNodeId != -1 && aNodes[aNodeId].first_edge == -1) {
142        aNodeId = aNodes[aNodeId].next != -1 ?
143          aNodes[aNodeId].next >> 1 : -1;
144      }
145      if (aNodeId != -1) {
146        edge.id = aNodes[aNodeId].first_edge;
147      } else {
148        edge.id = -1;
149      }
150    }
151    void next(UEdge& edge) const {
152      int aNodeId = edges[edge.id].aNode >> 1;
153      edge.id = edges[edge.id].next_out;
154      if (edge.id == -1) {
155        aNodeId = aNodes[aNodeId].next != -1 ?
156          aNodes[aNodeId].next >> 1 : -1;
157        while (aNodeId != -1 && aNodes[aNodeId].first_edge == -1) {
158          aNodeId = aNodes[aNodeId].next != -1 ?
159          aNodes[aNodeId].next >> 1 : -1;
160        }
161        if (aNodeId != -1) {
162          edge.id = aNodes[aNodeId].first_edge;
163        } else {
164          edge.id = -1;
165        }
166      }
167    }
168
169    void firstFromANode(UEdge& edge, const Node& node) const {
170      LEMON_ASSERT((node.id & 1) == 0, NodeSetError());
171      edge.id = aNodes[node.id >> 1].first_edge;
172    }
173    void nextFromANode(UEdge& edge) const {
174      edge.id = edges[edge.id].next_out;
175    }
176
177    void firstFromBNode(UEdge& edge, const Node& node) const {
178      LEMON_ASSERT((node.id & 1) == 1, NodeSetError());
179      edge.id = bNodes[node.id >> 1].first_edge;
180    }
181    void nextFromBNode(UEdge& edge) const {
182      edge.id = edges[edge.id].next_in;
183    }
184
185    static int id(const Node& node) {
186      return node.id;
187    }
188    static Node nodeFromId(int id) {
189      return Node(id);
190    }
191    int maxNodeId() const {
192      return aNodes.size() > bNodes.size() ?
193        aNodes.size() * 2 - 2 : bNodes.size() * 2 - 1;
194    }
195 
196    static int id(const UEdge& edge) {
197      return edge.id;
198    }
199    static UEdge uEdgeFromId(int id) {
200      return UEdge(id);
201    }
202    int maxUEdgeId() const {
203      return edges.size();
204    }
205 
206    static int aNodeId(const Node& node) {
207      return node.id >> 1;
208    }
209    static Node fromANodeId(int id) {
210      return Node(id << 1);
211    }
212    int maxANodeId() const {
213      return aNodes.size();
214    }
215
216    static int bNodeId(const Node& node) {
217      return node.id >> 1;
218    }
219    static Node fromBNodeId(int id) {
220      return Node((id << 1) + 1);
221    }
222    int maxBNodeId() const {
223      return bNodes.size();
224    }
225
226    Node aNode(const UEdge& edge) const {
227      return Node(edges[edge.id].aNode);
228    }
229    Node bNode(const UEdge& edge) const {
230      return Node(edges[edge.id].bNode);
231    }
232
233    static bool aNode(const Node& node) {
234      return (node.id & 1) == 0;
235    }
236
237    static bool bNode(const Node& node) {
238      return (node.id & 1) == 1;
239    }
240
241    Node addANode() {
242      int aNodeId;
243      if (first_free_anode == -1) {
244        aNodeId = aNodes.size();
245        aNodes.push_back(NodeT());
246      } else {
247        aNodeId = first_free_anode;
248        first_free_anode = aNodes[first_free_anode].next;
249      }
250      if (first_anode != -1) {
251        aNodes[aNodeId].next = first_anode << 1;
252        aNodes[first_anode].prev = aNodeId << 1;
253      } else {
254        aNodes[aNodeId].next = -1;
255      }
256      aNodes[aNodeId].prev = -1;
257      first_anode = aNodeId;
258      aNodes[aNodeId].first_edge = -1;
259      return Node(aNodeId << 1);
260    }
261
262    Node addBNode() {
263      int bNodeId;
264      if (first_free_bnode == -1) {
265        bNodeId = bNodes.size();
266        bNodes.push_back(NodeT());
267      } else {
268        bNodeId = first_free_bnode;
269        first_free_bnode = bNodes[first_free_bnode].next;
270      }
271      if (first_bnode != -1) {
272        bNodes[bNodeId].next = (first_bnode << 1) + 1;
273        bNodes[first_bnode].prev = (bNodeId << 1) + 1;
274      } else {
275        bNodes[bNodeId].next = -1;
276      }
277      first_bnode = bNodeId;
278      bNodes[bNodeId].first_edge = -1;
279      return Node((bNodeId << 1) + 1);
280    }
281
282    UEdge addEdge(const Node& source, const Node& target) {
283      LEMON_ASSERT(((source.id ^ target.id) & 1) == 1, NodeSetError());
284      int edgeId;
285      if (first_free_edge != -1) {
286        edgeId = first_free_edge;
287        first_free_edge = edges[edgeId].next_out;
288      } else {
289        edgeId = edges.size();
290        edges.push_back(UEdgeT());
291      }
292      if ((source.id & 1) == 0) {
293        edges[edgeId].aNode = source.id;
294        edges[edgeId].bNode = target.id;
295      } else {
296        edges[edgeId].aNode = target.id;
297        edges[edgeId].bNode = source.id;
298      }
299      edges[edgeId].next_out = aNodes[edges[edgeId].aNode >> 1].first_edge;
300      edges[edgeId].prev_out = -1;
301      if (aNodes[edges[edgeId].aNode >> 1].first_edge != -1) {
302        edges[aNodes[edges[edgeId].aNode >> 1].first_edge].prev_out = edgeId;
303      }
304      aNodes[edges[edgeId].aNode >> 1].first_edge = edgeId;
305      edges[edgeId].next_in = bNodes[edges[edgeId].bNode >> 1].first_edge;
306      edges[edgeId].prev_in = -1;
307      if (bNodes[edges[edgeId].bNode >> 1].first_edge != -1) {
308        edges[bNodes[edges[edgeId].bNode >> 1].first_edge].prev_in = edgeId;
309      }
310      bNodes[edges[edgeId].bNode >> 1].first_edge = edgeId;
311      return UEdge(edgeId);
312    }
313
314    void erase(const Node& node) {
315      if (aNode(node)) {
316        int aNodeId = node.id >> 1;
317        if (aNodes[aNodeId].prev != -1) {
318          aNodes[aNodes[aNodeId].prev >> 1].next = aNodes[aNodeId].next;
319        } else {
320          first_anode = aNodes[aNodeId].next >> 1;
321        }
322        if (aNodes[aNodeId].next != -1) {
323          aNodes[aNodes[aNodeId].next >> 1].prev = aNodes[aNodeId].prev;
324        }
325        aNodes[aNodeId].next = first_free_anode;
326        first_free_anode = aNodeId;
327      } else {
328        int bNodeId = node.id >> 1;
329        if (bNodes[bNodeId].prev != -1) {
330          bNodes[bNodes[bNodeId].prev >> 1].next = bNodes[bNodeId].next;
331        } else {
332          first_bnode = bNodes[bNodeId].next >> 1;
333        }
334        if (bNodes[bNodeId].next != -1) {
335          bNodes[bNodes[bNodeId].next >> 1].prev = bNodes[bNodeId].prev;
336        }
337        bNodes[bNodeId].next = first_free_bnode;
338        first_free_bnode = bNodeId;
339      }
340    }
341
342    void erase(const UEdge& edge) {
343
344      if (edges[edge.id].prev_out != -1) {
345        edges[edges[edge.id].prev_out].next_out = edges[edge.id].next_out;
346      } else {
347        aNodes[edges[edge.id].aNode >> 1].first_edge = edges[edge.id].next_out;
348      }
349      if (edges[edge.id].next_out != -1) {
350        edges[edges[edge.id].next_out].prev_out = edges[edge.id].prev_out;
351      }
352
353      if (edges[edge.id].prev_in != -1) {
354        edges[edges[edge.id].prev_in].next_in = edges[edge.id].next_in;
355      } else {
356        bNodes[edges[edge.id].bNode >> 1].first_edge = edges[edge.id].next_in;
357      }
358      if (edges[edge.id].next_in != -1) {
359        edges[edges[edge.id].next_in].prev_in = edges[edge.id].prev_in;
360      }
361
362      edges[edge.id].next_out = first_free_edge;
363      first_free_edge = edge.id;
364    }
365
366    void clear() {
367      aNodes.clear();
368      bNodes.clear();
369      edges.clear();
370      first_anode = -1;
371      first_free_anode = -1;
372      first_bnode = -1;
373      first_free_bnode = -1;
374      first_free_edge = -1;
375    }
376
377  };
378
379
380  typedef BpUGraphExtender< ListBpUGraphBase > ExtendedListBpUGraphBase;
381
382  /// \ingroup graphs
383  ///
384  /// \brief A smart bipartite undirected graph class.
385  ///
386  /// This is a bipartite undirected graph implementation.
387  /// It is conforms to the \ref concept::ErasableBpUGraph "ErasableBpUGraph"
388  /// concept.
389  /// \sa concept::BpUGraph.
390  ///
391  class ListBpUGraph : public ExtendedListBpUGraphBase {};
392
393 
394  /// @} 
395} //namespace lemon
396 
397
398#endif
Note: See TracBrowser for help on using the repository browser.