3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
19 #ifndef LEMON_LIST_GRAPH_H
20 #define LEMON_LIST_GRAPH_H
24 ///\brief ListGraph, ListUGraph classes.
26 #include <lemon/bits/base_extender.h>
27 #include <lemon/bits/graph_extender.h>
29 #include <lemon/error.h>
40 int first_in, first_out;
46 int prev_in, prev_out;
47 int next_in, next_out;
50 std::vector<NodeT> nodes;
56 std::vector<EdgeT> edges;
62 typedef ListGraphBase Graph;
65 friend class ListGraphBase;
69 explicit Node(int pid) { id = pid;}
73 Node (Invalid) { id = -1; }
74 bool operator==(const Node& node) const {return id == node.id;}
75 bool operator!=(const Node& node) const {return id != node.id;}
76 bool operator<(const Node& node) const {return id < node.id;}
80 friend class ListGraphBase;
84 explicit Edge(int pid) { id = pid;}
88 Edge (Invalid) { id = -1; }
89 bool operator==(const Edge& edge) const {return id == edge.id;}
90 bool operator!=(const Edge& edge) const {return id != edge.id;}
91 bool operator<(const Edge& edge) const {return id < edge.id;}
97 : nodes(), first_node(-1),
98 first_free_node(-1), edges(), first_free_edge(-1) {}
105 int maxNodeId() const { return nodes.size()-1; }
111 int maxEdgeId() const { return edges.size()-1; }
113 Node source(Edge e) const { return Node(edges[e.id].source); }
114 Node target(Edge e) const { return Node(edges[e.id].target); }
117 void first(Node& node) const {
118 node.id = first_node;
121 void next(Node& node) const {
122 node.id = nodes[node.id].next;
126 void first(Edge& e) const {
129 n!=-1 && nodes[n].first_in == -1;
131 e.id = (n == -1) ? -1 : nodes[n].first_in;
134 void next(Edge& edge) const {
135 if (edges[edge.id].next_in != -1) {
136 edge.id = edges[edge.id].next_in;
139 for(n = nodes[edges[edge.id].target].next;
140 n!=-1 && nodes[n].first_in == -1;
142 edge.id = (n == -1) ? -1 : nodes[n].first_in;
146 void firstOut(Edge &e, const Node& v) const {
147 e.id = nodes[v.id].first_out;
149 void nextOut(Edge &e) const {
150 e.id=edges[e.id].next_out;
153 void firstIn(Edge &e, const Node& v) const {
154 e.id = nodes[v.id].first_in;
156 void nextIn(Edge &e) const {
157 e.id=edges[e.id].next_in;
161 static int id(Node v) { return v.id; }
162 static int id(Edge e) { return e.id; }
164 static Node nodeFromId(int id) { return Node(id);}
165 static Edge edgeFromId(int id) { return Edge(id);}
167 /// Adds a new node to the graph.
169 /// \warning It adds the new node to the front of the list.
170 /// (i.e. the lastly added node becomes the first.)
174 if(first_free_node==-1) {
176 nodes.push_back(NodeT());
179 first_free_node = nodes[n].next;
182 nodes[n].next = first_node;
183 if(first_node != -1) nodes[first_node].prev = n;
187 nodes[n].first_in = nodes[n].first_out = -1;
192 Edge addEdge(Node u, Node v) {
195 if (first_free_edge == -1) {
197 edges.push_back(EdgeT());
200 first_free_edge = edges[n].next_in;
203 edges[n].source = u.id;
204 edges[n].target = v.id;
206 edges[n].next_out = nodes[u.id].first_out;
207 if(nodes[u.id].first_out != -1) {
208 edges[nodes[u.id].first_out].prev_out = n;
211 edges[n].next_in = nodes[v.id].first_in;
212 if(nodes[v.id].first_in != -1) {
213 edges[nodes[v.id].first_in].prev_in = n;
216 edges[n].prev_in = edges[n].prev_out = -1;
218 nodes[u.id].first_out = nodes[v.id].first_in = n;
223 void erase(const Node& node) {
226 if(nodes[n].next != -1) {
227 nodes[nodes[n].next].prev = nodes[n].prev;
230 if(nodes[n].prev != -1) {
231 nodes[nodes[n].prev].next = nodes[n].next;
233 first_node = nodes[n].next;
236 nodes[n].next = first_free_node;
241 void erase(const Edge& edge) {
244 if(edges[n].next_in!=-1) {
245 edges[edges[n].next_in].prev_in = edges[n].prev_in;
248 if(edges[n].prev_in!=-1) {
249 edges[edges[n].prev_in].next_in = edges[n].next_in;
251 nodes[edges[n].target].first_in = edges[n].next_in;
255 if(edges[n].next_out!=-1) {
256 edges[edges[n].next_out].prev_out = edges[n].prev_out;
259 if(edges[n].prev_out!=-1) {
260 edges[edges[n].prev_out].next_out = edges[n].next_out;
262 nodes[edges[n].source].first_out = edges[n].next_out;
265 edges[n].next_in = first_free_edge;
273 first_node = first_free_node = first_free_edge = -1;
277 void changeTarget(Edge e, Node n)
279 if(edges[e.id].next_in != -1)
280 edges[edges[e.id].next_in].prev_in = edges[e.id].prev_in;
281 if(edges[e.id].prev_in != -1)
282 edges[edges[e.id].prev_in].next_in = edges[e.id].next_in;
283 else nodes[edges[e.id].target].first_in = edges[e.id].next_in;
284 if (nodes[n.id].first_in != -1) {
285 edges[nodes[n.id].first_in].prev_in = e.id;
287 edges[e.id].target = n.id;
288 edges[e.id].prev_in = -1;
289 edges[e.id].next_in = nodes[n.id].first_in;
290 nodes[n.id].first_in = e.id;
292 void changeSource(Edge e, Node n)
294 if(edges[e.id].next_out != -1)
295 edges[edges[e.id].next_out].prev_out = edges[e.id].prev_out;
296 if(edges[e.id].prev_out != -1)
297 edges[edges[e.id].prev_out].next_out = edges[e.id].next_out;
298 else nodes[edges[e.id].source].first_out = edges[e.id].next_out;
299 if (nodes[n.id].first_out != -1) {
300 edges[nodes[n.id].first_out].prev_out = e.id;
302 edges[e.id].source = n.id;
303 edges[e.id].prev_out = -1;
304 edges[e.id].next_out = nodes[n.id].first_out;
305 nodes[n.id].first_out = e.id;
310 typedef GraphExtender<ListGraphBase> ExtendedListGraphBase;
312 /// \addtogroup graphs
315 ///A list graph class.
317 ///This is a simple and fast graph implementation.
319 ///It conforms to the \ref concept::Graph "Graph concept" and it
320 ///also provides several additional useful extra functionalities.
321 ///The most of the member functions and nested classes are
322 ///documented only in the concept class.
323 ///\sa concept::Graph.
325 class ListGraph : public ExtendedListGraphBase {
328 typedef ExtendedListGraphBase Parent;
330 ///Add a new node to the graph.
332 /// \return the new node.
334 Node addNode() { return Parent::addNode(); }
336 ///Add a new edge to the graph.
338 ///Add a new edge to the graph with source node \c s
339 ///and target node \c t.
340 ///\return the new edge.
341 Edge addEdge(const Node& s, const Node& t) {
342 return Parent::addEdge(s, t);
345 /// Changes the target of \c e to \c n
347 /// Changes the target of \c e to \c n
349 ///\note The <tt>Edge</tt>s and <tt>OutEdgeIt</tt>s referencing
350 ///the changed edge remain valid. However <tt>InEdgeIt</tt>s are
352 void changeTarget(Edge e, Node n) {
353 Parent::changeTarget(e,n);
355 /// Changes the source of \c e to \c n
357 /// Changes the source of \c e to \c n
359 ///\note The <tt>Edge</tt>s and <tt>InEdgeIt</tt>s referencing
360 ///the changed edge remain valid. However <tt>OutEdgeIt</tt>s are
362 void changeSource(Edge e, Node n) {
363 Parent::changeSource(e,n);
366 /// Invert the direction of an edge.
368 ///\note The <tt>Edge</tt>s referencing the changed edge remain
369 ///valid. However <tt>OutEdgeIt</tt>s and <tt>InEdgeIt</tt>s are
371 void reverseEdge(Edge e) {
373 changeTarget(e,source(e));
377 /// \brief Using this it is possible to avoid the superfluous memory
380 ///Using this it is possible to avoid the superfluous memory
381 ///allocation: if you know that the graph you want to build will
382 ///contain at least 10 million nodes then it is worth to reserve
383 ///space for this amount before starting to build the graph.
384 void reserveNode(int n) { nodes.reserve(n); };
386 /// \brief Using this it is possible to avoid the superfluous memory
389 ///Using this it is possible to avoid the superfluous memory
390 ///allocation: see the \ref reserveNode function.
391 void reserveEdge(int n) { edges.reserve(n); };
394 ///Contract two nodes.
396 ///This function contracts two nodes.
398 ///Node \p b will be removed but instead of deleting
399 ///incident edges, they will be joined to \p a.
400 ///The last parameter \p r controls whether to remove loops. \c true
401 ///means that loops will be removed.
403 ///\note The <tt>Edge</tt>s
404 ///referencing a moved edge remain
405 ///valid. However <tt>InEdge</tt>s and <tt>OutEdge</tt>s
406 ///may be invalidated.
407 void contract(Node a, Node b, bool r = true)
409 for(OutEdgeIt e(*this,b);e!=INVALID;) {
412 if(r && target(e)==a) erase(e);
413 else changeSource(e,a);
416 for(InEdgeIt e(*this,b);e!=INVALID;) {
419 if(r && source(e)==a) erase(e);
420 else changeTarget(e,a);
428 ///This function splits a node. First a new node is added to the graph,
429 ///then the source of each outgoing edge of \c n is moved to this new node.
430 ///If \c connect is \c true (this is the default value), then a new edge
431 ///from \c n to the newly created node is also added.
432 ///\return The newly created node.
434 ///\note The <tt>Edge</tt>s
435 ///referencing a moved edge remain
436 ///valid. However <tt>InEdge</tt>s and <tt>OutEdge</tt>s
437 ///may be invalidated.
438 ///\warning This functionality cannot be used together with the Snapshot
440 ///\todo It could be implemented in a bit faster way.
441 Node split(Node n, bool connect = true) {
443 for(OutEdgeIt e(*this,n);e!=INVALID;) {
449 if (connect) addEdge(n,b);
455 ///This function splits an edge. First a new node \c b is added to
456 ///the graph, then the original edge is re-targeted to \c
457 ///b. Finally an edge from \c b to the original target is added.
458 ///\return The newly created node.
459 ///\warning This functionality
460 ///cannot be used together with the Snapshot feature.
463 addEdge(b,target(e));
468 /// \brief Class to make a snapshot of the graph and restore
471 /// Class to make a snapshot of the graph and to restore it
474 /// The newly added nodes and edges can be removed using the
475 /// restore() function.
477 /// \warning Edge and node deletions cannot be restored.
481 class UnsupportedOperation : public LogicError {
483 virtual const char* exceptionName() const {
484 return "lemon::ListGraph::Snapshot::UnsupportedOperation";
491 typedef Parent::NodeNotifier NodeNotifier;
493 class NodeObserverProxy : public NodeNotifier::ObserverBase {
496 NodeObserverProxy(Snapshot& _snapshot)
497 : snapshot(_snapshot) {}
499 using NodeNotifier::ObserverBase::attach;
500 using NodeNotifier::ObserverBase::detach;
501 using NodeNotifier::ObserverBase::attached;
505 virtual void add(const Node& node) {
506 snapshot.addNode(node);
508 virtual void add(const std::vector<Node>& nodes) {
509 for (int i = nodes.size() - 1; i >= 0; ++i) {
510 snapshot.addNode(nodes[i]);
513 virtual void erase(const Node& node) {
514 snapshot.eraseNode(node);
516 virtual void erase(const std::vector<Node>& nodes) {
517 for (int i = 0; i < (int)nodes.size(); ++i) {
518 if (!snapshot.eraseNode(nodes[i])) break;
521 virtual void build() {
522 NodeNotifier* notifier = getNotifier();
524 std::vector<Node> nodes;
525 for (notifier->first(node); node != INVALID; notifier->next(node)) {
526 nodes.push_back(node);
528 for (int i = nodes.size() - 1; i >= 0; --i) {
529 snapshot.addNode(nodes[i]);
532 virtual void clear() {
533 NodeNotifier* notifier = getNotifier();
535 for (notifier->first(node); node != INVALID; notifier->next(node)) {
536 if (!snapshot.eraseNode(node)) break;
543 class EdgeObserverProxy : public EdgeNotifier::ObserverBase {
546 EdgeObserverProxy(Snapshot& _snapshot)
547 : snapshot(_snapshot) {}
549 using EdgeNotifier::ObserverBase::attach;
550 using EdgeNotifier::ObserverBase::detach;
551 using EdgeNotifier::ObserverBase::attached;
555 virtual void add(const Edge& edge) {
556 snapshot.addEdge(edge);
558 virtual void add(const std::vector<Edge>& edges) {
559 for (int i = edges.size() - 1; i >= 0; ++i) {
560 snapshot.addEdge(edges[i]);
563 virtual void erase(const Edge& edge) {
564 snapshot.eraseEdge(edge);
566 virtual void erase(const std::vector<Edge>& edges) {
567 for (int i = 0; i < (int)edges.size(); ++i) {
568 if (!snapshot.eraseEdge(edges[i])) break;
571 virtual void build() {
572 EdgeNotifier* notifier = getNotifier();
574 std::vector<Edge> edges;
575 for (notifier->first(edge); edge != INVALID; notifier->next(edge)) {
576 edges.push_back(edge);
578 for (int i = edges.size() - 1; i >= 0; --i) {
579 snapshot.addEdge(edges[i]);
582 virtual void clear() {
583 EdgeNotifier* notifier = getNotifier();
585 for (notifier->first(edge); edge != INVALID; notifier->next(edge)) {
586 if (!snapshot.eraseEdge(edge)) break;
595 NodeObserverProxy node_observer_proxy;
596 EdgeObserverProxy edge_observer_proxy;
598 std::list<Node> added_nodes;
599 std::list<Edge> added_edges;
602 void addNode(const Node& node) {
603 added_nodes.push_front(node);
605 bool eraseNode(const Node& node) {
606 std::list<Node>::iterator it =
607 std::find(added_nodes.begin(), added_nodes.end(), node);
608 if (it == added_nodes.end()) {
612 added_nodes.erase(it);
617 void addEdge(const Edge& edge) {
618 added_edges.push_front(edge);
620 bool eraseEdge(const Edge& edge) {
621 std::list<Edge>::iterator it =
622 std::find(added_edges.begin(), added_edges.end(), edge);
623 if (it == added_edges.end()) {
627 added_edges.erase(it);
632 void attach(ListGraph &_graph) {
634 node_observer_proxy.attach(graph->getNotifier(Node()));
635 edge_observer_proxy.attach(graph->getNotifier(Edge()));
639 node_observer_proxy.detach();
640 edge_observer_proxy.detach();
651 /// \brief Default constructur.
653 /// Default constructor.
654 /// To actually make a snapshot you must call save().
656 : graph(0), node_observer_proxy(*this),
657 edge_observer_proxy(*this) {}
659 /// \brief Constructor that immediately makes a snapshot.
661 /// This constructor immediately makes a snapshot of the graph.
662 /// \param _graph The graph we make a snapshot of.
663 Snapshot(ListGraph &_graph)
664 : node_observer_proxy(*this),
665 edge_observer_proxy(*this) {
669 /// \brief Make a snapshot.
671 /// Make a snapshot of the graph.
673 /// This function can be called more than once. In case of a repeated
674 /// call, the previous snapshot gets lost.
675 /// \param _graph The graph we make the snapshot of.
676 void save(ListGraph &_graph) {
681 /// \brief Undo the changes until the last snapshot.
683 /// Undo the changes until last snapshot created by save().
685 /// \todo This function might be called undo().
688 while(!added_edges.empty()) {
689 graph->erase(added_edges.front());
690 added_edges.pop_front();
692 while(!added_nodes.empty()) {
693 graph->erase(added_nodes.front());
694 added_nodes.pop_front();
698 /// \brief Gives back true when the snapshot is valid.
700 /// Gives back true when the snapshot is valid.
702 return node_observer_proxy.attached();
710 /**************** Undirected List Graph ****************/
712 typedef UGraphExtender<UndirGraphExtender<ListGraphBase> >
713 ExtendedListUGraphBase;
715 /// \addtogroup graphs
718 ///An undirected list graph class.
720 ///This is a simple and fast undirected graph implementation.
722 ///It conforms to the
723 ///\ref concept::UGraph "UGraph concept".
725 ///\sa concept::UGraph.
727 ///\todo Snapshot, reverseEdge(), changeTarget(), changeSource(), contract()
728 ///haven't been implemented yet.
730 class ListUGraph : public ExtendedListUGraphBase {
732 typedef ExtendedListUGraphBase Parent;
733 /// \brief Add a new node to the graph.
735 /// \return the new node.
737 Node addNode() { return Parent::addNode(); }
739 /// \brief Add a new edge to the graph.
741 /// Add a new edge to the graph with source node \c s
742 /// and target node \c t.
743 /// \return the new undirected edge.
744 UEdge addEdge(const Node& s, const Node& t) {
745 return Parent::addEdge(s, t);
747 /// \brief Changes the target of \c e to \c n
749 /// Changes the target of \c e to \c n
751 /// \note The <tt>Edge</tt>'s and <tt>OutEdge</tt>'s
752 /// referencing the changed edge remain
753 /// valid. However <tt>InEdge</tt>'s are invalidated.
754 void changeTarget(UEdge e, Node n) {
755 Parent::changeTarget(e,n);
757 /// Changes the source of \c e to \c n
759 /// Changes the source of \c e to \c n
761 ///\note The <tt>Edge</tt>'s and <tt>InEdge</tt>'s
762 ///referencing the changed edge remain
763 ///valid. However <tt>OutEdge</tt>'s are invalidated.
764 void changeSource(UEdge e, Node n) {
765 Parent::changeSource(e,n);
767 /// \brief Contract two nodes.
769 /// This function contracts two nodes.
771 /// Node \p b will be removed but instead of deleting
772 /// its neighboring edges, they will be joined to \p a.
773 /// The last parameter \p r controls whether to remove loops. \c true
774 /// means that loops will be removed.
776 /// \note The <tt>Edge</tt>s
777 /// referencing a moved edge remain
779 void contract(Node a, Node b, bool r = true) {
780 for(IncEdgeIt e(*this, b); e!=INVALID;) {
781 IncEdgeIt f = e; ++f;
782 if (r && runningNode(e) == a) {
784 } else if (source(e) == b) {
796 class ListBpUGraphBase {
799 class NodeSetError : public LogicError {
800 virtual const char* exceptionName() const {
801 return "lemon::ListBpUGraph::NodeSetError";
808 int first_edge, prev, next;
812 int aNode, prev_out, next_out;
813 int bNode, prev_in, next_in;
816 std::vector<NodeT> aNodes;
817 std::vector<NodeT> bNodes;
819 std::vector<UEdgeT> edges;
822 int first_free_anode;
825 int first_free_bnode;
832 friend class ListBpUGraphBase;
836 explicit Node(int _id) : id(_id) {}
839 Node(Invalid) { id = -1; }
840 bool operator==(const Node i) const {return id==i.id;}
841 bool operator!=(const Node i) const {return id!=i.id;}
842 bool operator<(const Node i) const {return id<i.id;}
846 friend class ListBpUGraphBase;
850 explicit UEdge(int _id) { id = _id;}
853 UEdge (Invalid) { id = -1; }
854 bool operator==(const UEdge i) const {return id==i.id;}
855 bool operator!=(const UEdge i) const {return id!=i.id;}
856 bool operator<(const UEdge i) const {return id<i.id;}
860 : first_anode(-1), first_free_anode(-1),
861 first_bnode(-1), first_free_bnode(-1),
862 first_free_edge(-1) {}
864 void firstANode(Node& node) const {
865 node.id = first_anode != -1 ? (first_anode << 1) : -1;
867 void nextANode(Node& node) const {
868 node.id = aNodes[node.id >> 1].next;
871 void firstBNode(Node& node) const {
872 node.id = first_bnode != -1 ? (first_bnode << 1) + 1 : -1;
874 void nextBNode(Node& node) const {
875 node.id = bNodes[node.id >> 1].next;
878 void first(Node& node) const {
879 if (first_anode != -1) {
880 node.id = (first_anode << 1);
881 } else if (first_bnode != -1) {
882 node.id = (first_bnode << 1) + 1;
887 void next(Node& node) const {
889 node.id = aNodes[node.id >> 1].next;
891 if (first_bnode != -1) {
892 node.id = (first_bnode << 1) + 1;
896 node.id = bNodes[node.id >> 1].next;
900 void first(UEdge& edge) const {
901 int aNodeId = first_anode;
902 while (aNodeId != -1 && aNodes[aNodeId].first_edge == -1) {
903 aNodeId = aNodes[aNodeId].next != -1 ?
904 aNodes[aNodeId].next >> 1 : -1;
907 edge.id = aNodes[aNodeId].first_edge;
912 void next(UEdge& edge) const {
913 int aNodeId = edges[edge.id].aNode >> 1;
914 edge.id = edges[edge.id].next_out;
916 aNodeId = aNodes[aNodeId].next != -1 ?
917 aNodes[aNodeId].next >> 1 : -1;
918 while (aNodeId != -1 && aNodes[aNodeId].first_edge == -1) {
919 aNodeId = aNodes[aNodeId].next != -1 ?
920 aNodes[aNodeId].next >> 1 : -1;
923 edge.id = aNodes[aNodeId].first_edge;
930 void firstFromANode(UEdge& edge, const Node& node) const {
931 LEMON_ASSERT((node.id & 1) == 0, NodeSetError());
932 edge.id = aNodes[node.id >> 1].first_edge;
934 void nextFromANode(UEdge& edge) const {
935 edge.id = edges[edge.id].next_out;
938 void firstFromBNode(UEdge& edge, const Node& node) const {
939 LEMON_ASSERT((node.id & 1) == 1, NodeSetError());
940 edge.id = bNodes[node.id >> 1].first_edge;
942 void nextFromBNode(UEdge& edge) const {
943 edge.id = edges[edge.id].next_in;
946 static int id(const Node& node) {
949 static Node nodeFromId(int id) {
952 int maxNodeId() const {
953 return aNodes.size() > bNodes.size() ?
954 aNodes.size() * 2 - 2 : bNodes.size() * 2 - 1;
957 static int id(const UEdge& edge) {
960 static UEdge uEdgeFromId(int id) {
963 int maxUEdgeId() const {
967 static int aNodeId(const Node& node) {
970 static Node fromANodeId(int id) {
971 return Node(id << 1);
973 int maxANodeId() const {
974 return aNodes.size();
977 static int bNodeId(const Node& node) {
980 static Node fromBNodeId(int id) {
981 return Node((id << 1) + 1);
983 int maxBNodeId() const {
984 return bNodes.size();
987 Node aNode(const UEdge& edge) const {
988 return Node(edges[edge.id].aNode);
990 Node bNode(const UEdge& edge) const {
991 return Node(edges[edge.id].bNode);
994 static bool aNode(const Node& node) {
995 return (node.id & 1) == 0;
998 static bool bNode(const Node& node) {
999 return (node.id & 1) == 1;
1004 if (first_free_anode == -1) {
1005 aNodeId = aNodes.size();
1006 aNodes.push_back(NodeT());
1008 aNodeId = first_free_anode;
1009 first_free_anode = aNodes[first_free_anode].next;
1011 if (first_anode != -1) {
1012 aNodes[aNodeId].next = first_anode << 1;
1013 aNodes[first_anode].prev = aNodeId << 1;
1015 aNodes[aNodeId].next = -1;
1017 aNodes[aNodeId].prev = -1;
1018 first_anode = aNodeId;
1019 aNodes[aNodeId].first_edge = -1;
1020 return Node(aNodeId << 1);
1025 if (first_free_bnode == -1) {
1026 bNodeId = bNodes.size();
1027 bNodes.push_back(NodeT());
1029 bNodeId = first_free_bnode;
1030 first_free_bnode = bNodes[first_free_bnode].next;
1032 if (first_bnode != -1) {
1033 bNodes[bNodeId].next = (first_bnode << 1) + 1;
1034 bNodes[first_bnode].prev = (bNodeId << 1) + 1;
1036 bNodes[bNodeId].next = -1;
1038 first_bnode = bNodeId;
1039 bNodes[bNodeId].first_edge = -1;
1040 return Node((bNodeId << 1) + 1);
1043 UEdge addEdge(const Node& source, const Node& target) {
1044 LEMON_ASSERT(((source.id ^ target.id) & 1) == 1, NodeSetError());
1046 if (first_free_edge != -1) {
1047 edgeId = first_free_edge;
1048 first_free_edge = edges[edgeId].next_out;
1050 edgeId = edges.size();
1051 edges.push_back(UEdgeT());
1053 if ((source.id & 1) == 0) {
1054 edges[edgeId].aNode = source.id;
1055 edges[edgeId].bNode = target.id;
1057 edges[edgeId].aNode = target.id;
1058 edges[edgeId].bNode = source.id;
1060 edges[edgeId].next_out = aNodes[edges[edgeId].aNode >> 1].first_edge;
1061 edges[edgeId].prev_out = -1;
1062 if (aNodes[edges[edgeId].aNode >> 1].first_edge != -1) {
1063 edges[aNodes[edges[edgeId].aNode >> 1].first_edge].prev_out = edgeId;
1065 aNodes[edges[edgeId].aNode >> 1].first_edge = edgeId;
1066 edges[edgeId].next_in = bNodes[edges[edgeId].bNode >> 1].first_edge;
1067 edges[edgeId].prev_in = -1;
1068 if (bNodes[edges[edgeId].bNode >> 1].first_edge != -1) {
1069 edges[bNodes[edges[edgeId].bNode >> 1].first_edge].prev_in = edgeId;
1071 bNodes[edges[edgeId].bNode >> 1].first_edge = edgeId;
1072 return UEdge(edgeId);
1075 void erase(const Node& node) {
1077 int aNodeId = node.id >> 1;
1078 if (aNodes[aNodeId].prev != -1) {
1079 aNodes[aNodes[aNodeId].prev >> 1].next = aNodes[aNodeId].next;
1081 first_anode = aNodes[aNodeId].next >> 1;
1083 if (aNodes[aNodeId].next != -1) {
1084 aNodes[aNodes[aNodeId].next >> 1].prev = aNodes[aNodeId].prev;
1086 aNodes[aNodeId].next = first_free_anode;
1087 first_free_anode = aNodeId;
1089 int bNodeId = node.id >> 1;
1090 if (bNodes[bNodeId].prev != -1) {
1091 bNodes[bNodes[bNodeId].prev >> 1].next = bNodes[bNodeId].next;
1093 first_bnode = bNodes[bNodeId].next >> 1;
1095 if (bNodes[bNodeId].next != -1) {
1096 bNodes[bNodes[bNodeId].next >> 1].prev = bNodes[bNodeId].prev;
1098 bNodes[bNodeId].next = first_free_bnode;
1099 first_free_bnode = bNodeId;
1103 void erase(const UEdge& edge) {
1105 if (edges[edge.id].prev_out != -1) {
1106 edges[edges[edge.id].prev_out].next_out = edges[edge.id].next_out;
1108 aNodes[edges[edge.id].aNode >> 1].first_edge = edges[edge.id].next_out;
1110 if (edges[edge.id].next_out != -1) {
1111 edges[edges[edge.id].next_out].prev_out = edges[edge.id].prev_out;
1114 if (edges[edge.id].prev_in != -1) {
1115 edges[edges[edge.id].prev_in].next_in = edges[edge.id].next_in;
1117 bNodes[edges[edge.id].bNode >> 1].first_edge = edges[edge.id].next_in;
1119 if (edges[edge.id].next_in != -1) {
1120 edges[edges[edge.id].next_in].prev_in = edges[edge.id].prev_in;
1123 edges[edge.id].next_out = first_free_edge;
1124 first_free_edge = edge.id;
1132 first_free_anode = -1;
1134 first_free_bnode = -1;
1135 first_free_edge = -1;
1141 typedef BpUGraphExtender< ListBpUGraphBase > ExtendedListBpUGraphBase;
1145 /// \brief A smart bipartite undirected graph class.
1147 /// This is a bipartite undirected graph implementation.
1148 /// It is conforms to the \ref concept::BpUGraph "BpUGraph concept".
1149 /// \sa concept::BpUGraph.
1151 class ListBpUGraph : public ExtendedListBpUGraphBase {};