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 /// Adds a new node to the graph.
171 /// \warning It adds the new node to the front of the list.
172 /// (i.e. the lastly added node becomes the first.)
176 if(first_free_node==-1) {
178 nodes.push_back(NodeT());
181 first_free_node = nodes[n].next;
184 nodes[n].next = first_node;
185 if(first_node != -1) nodes[first_node].prev = n;
189 nodes[n].first_in = nodes[n].first_out = -1;
194 Edge addEdge(Node u, Node v) {
197 if (first_free_edge == -1) {
199 edges.push_back(EdgeT());
202 first_free_edge = edges[n].next_in;
205 edges[n].source = u.id;
206 edges[n].target = v.id;
208 edges[n].next_out = nodes[u.id].first_out;
209 if(nodes[u.id].first_out != -1) {
210 edges[nodes[u.id].first_out].prev_out = n;
213 edges[n].next_in = nodes[v.id].first_in;
214 if(nodes[v.id].first_in != -1) {
215 edges[nodes[v.id].first_in].prev_in = n;
218 edges[n].prev_in = edges[n].prev_out = -1;
220 nodes[u.id].first_out = nodes[v.id].first_in = n;
225 void erase(const Node& node) {
228 if(nodes[n].next != -1) {
229 nodes[nodes[n].next].prev = nodes[n].prev;
232 if(nodes[n].prev != -1) {
233 nodes[nodes[n].prev].next = nodes[n].next;
235 first_node = nodes[n].next;
238 nodes[n].next = first_free_node;
243 void erase(const Edge& edge) {
246 if(edges[n].next_in!=-1) {
247 edges[edges[n].next_in].prev_in = edges[n].prev_in;
250 if(edges[n].prev_in!=-1) {
251 edges[edges[n].prev_in].next_in = edges[n].next_in;
253 nodes[edges[n].target].first_in = edges[n].next_in;
257 if(edges[n].next_out!=-1) {
258 edges[edges[n].next_out].prev_out = edges[n].prev_out;
261 if(edges[n].prev_out!=-1) {
262 edges[edges[n].prev_out].next_out = edges[n].next_out;
264 nodes[edges[n].source].first_out = edges[n].next_out;
267 edges[n].next_in = first_free_edge;
275 first_node = first_free_node = first_free_edge = -1;
279 void changeTarget(Edge e, Node n)
281 if(edges[e.id].next_in != -1)
282 edges[edges[e.id].next_in].prev_in = edges[e.id].prev_in;
283 if(edges[e.id].prev_in != -1)
284 edges[edges[e.id].prev_in].next_in = edges[e.id].next_in;
285 else nodes[edges[e.id].target].first_in = edges[e.id].next_in;
286 if (nodes[n.id].first_in != -1) {
287 edges[nodes[n.id].first_in].prev_in = e.id;
289 edges[e.id].target = n.id;
290 edges[e.id].prev_in = -1;
291 edges[e.id].next_in = nodes[n.id].first_in;
292 nodes[n.id].first_in = e.id;
294 void changeSource(Edge e, Node n)
296 if(edges[e.id].next_out != -1)
297 edges[edges[e.id].next_out].prev_out = edges[e.id].prev_out;
298 if(edges[e.id].prev_out != -1)
299 edges[edges[e.id].prev_out].next_out = edges[e.id].next_out;
300 else nodes[edges[e.id].source].first_out = edges[e.id].next_out;
301 if (nodes[n.id].first_out != -1) {
302 edges[nodes[n.id].first_out].prev_out = e.id;
304 edges[e.id].source = n.id;
305 edges[e.id].prev_out = -1;
306 edges[e.id].next_out = nodes[n.id].first_out;
307 nodes[n.id].first_out = e.id;
312 typedef GraphExtender<ListGraphBase> ExtendedListGraphBase;
314 /// \addtogroup graphs
317 ///A list graph class.
319 ///This is a simple and fast graph implementation.
321 ///It conforms to the \ref concept::Graph "Graph concept" and it
322 ///also provides several additional useful extra functionalities.
323 ///The most of the member functions and nested classes are
324 ///documented only in the concept class.
325 ///\sa concept::Graph.
327 class ListGraph : public ExtendedListGraphBase {
329 ///ListGraph is \e not copy constructible. Use GraphCopy() instead.
331 ///ListGraph is \e not copy constructible. Use GraphCopy() instead.
333 ListGraph(const ListGraph &) :ExtendedListGraphBase() {};
334 ///\brief Assignment of ListGraph to another one is \e not allowed.
335 ///Use GraphCopy() instead.
337 ///Assignment of ListGraph to another one is \e not allowed.
338 ///Use GraphCopy() instead.
339 void operator=(const ListGraph &) {}
342 typedef ExtendedListGraphBase Parent;
350 ///Add a new node to the graph.
352 /// \return the new node.
354 Node addNode() { return Parent::addNode(); }
356 ///Add a new edge to the graph.
358 ///Add a new edge to the graph with source node \c s
359 ///and target node \c t.
360 ///\return the new edge.
361 Edge addEdge(const Node& s, const Node& t) {
362 return Parent::addEdge(s, t);
365 /// Changes the target of \c e to \c n
367 /// Changes the target of \c e to \c n
369 ///\note The <tt>Edge</tt>s and <tt>OutEdgeIt</tt>s referencing
370 ///the changed edge remain valid. However <tt>InEdgeIt</tt>s are
372 ///\warning This functionality cannot be used together with the Snapshot
374 void changeTarget(Edge e, Node n) {
375 Parent::changeTarget(e,n);
377 /// Changes the source of \c e to \c n
379 /// Changes the source of \c e to \c n
381 ///\note The <tt>Edge</tt>s and <tt>InEdgeIt</tt>s referencing
382 ///the changed edge remain valid. However <tt>OutEdgeIt</tt>s are
384 ///\warning This functionality cannot be used together with the Snapshot
386 void changeSource(Edge e, Node n) {
387 Parent::changeSource(e,n);
390 /// Invert the direction of an edge.
392 ///\note The <tt>Edge</tt>s referencing the changed edge remain
393 ///valid. However <tt>OutEdgeIt</tt>s and <tt>InEdgeIt</tt>s are
395 ///\warning This functionality cannot be used together with the Snapshot
397 void reverseEdge(Edge e) {
399 changeTarget(e,source(e));
403 /// \brief Using this it is possible to avoid the superfluous memory
406 ///Using this it is possible to avoid the superfluous memory
407 ///allocation: if you know that the graph you want to build will
408 ///contain at least 10 million nodes then it is worth reserving
409 ///space for this amount before starting to build the graph.
410 void reserveNode(int n) { nodes.reserve(n); };
412 /// \brief Using this it is possible to avoid the superfluous memory
415 ///Using this it is possible to avoid the superfluous memory
416 ///allocation: see the \ref reserveNode function.
417 void reserveEdge(int n) { edges.reserve(n); };
420 ///Contract two nodes.
422 ///This function contracts two nodes.
424 ///Node \p b will be removed but instead of deleting
425 ///incident edges, they will be joined to \p a.
426 ///The last parameter \p r controls whether to remove loops. \c true
427 ///means that loops will be removed.
429 ///\note The <tt>Edge</tt>s
430 ///referencing a moved edge remain
431 ///valid. However <tt>InEdge</tt>s and <tt>OutEdge</tt>s
432 ///may be invalidated.
433 ///\warning This functionality cannot be used together with the Snapshot
435 void contract(Node a, Node b, bool r = true)
437 for(OutEdgeIt e(*this,b);e!=INVALID;) {
440 if(r && target(e)==a) erase(e);
441 else changeSource(e,a);
444 for(InEdgeIt e(*this,b);e!=INVALID;) {
447 if(r && source(e)==a) erase(e);
448 else changeTarget(e,a);
456 ///This function splits a node. First a new node is added to the graph,
457 ///then the source of each outgoing edge of \c n is moved to this new node.
458 ///If \c connect is \c true (this is the default value), then a new edge
459 ///from \c n to the newly created node is also added.
460 ///\return The newly created node.
462 ///\note The <tt>Edge</tt>s
463 ///referencing a moved edge remain
464 ///valid. However <tt>InEdge</tt>s and <tt>OutEdge</tt>s
465 ///may be invalidated.
466 ///\warning This functionality cannot be used together with the Snapshot
468 ///\todo It could be implemented in a bit faster way.
469 Node split(Node n, bool connect = true) {
471 for(OutEdgeIt e(*this,n);e!=INVALID;) {
477 if (connect) addEdge(n,b);
483 ///This function splits an edge. First a new node \c b is added to
484 ///the graph, then the original edge is re-targeted to \c
485 ///b. Finally an edge from \c b to the original target is added.
486 ///\return The newly created node.
487 ///\warning This functionality
488 ///cannot be used together with the Snapshot feature.
491 addEdge(b,target(e));
496 /// \brief Class to make a snapshot of the graph and restore
499 /// Class to make a snapshot of the graph and to restore it
502 /// The newly added nodes and edges can be removed using the
503 /// restore() function.
505 /// \warning Edge and node deletions cannot be restored.
509 class UnsupportedOperation : public LogicError {
511 virtual const char* what() const throw() {
512 return "lemon::ListGraph::Snapshot::UnsupportedOperation";
519 typedef Parent::NodeNotifier NodeNotifier;
521 class NodeObserverProxy : public NodeNotifier::ObserverBase {
524 NodeObserverProxy(Snapshot& _snapshot)
525 : snapshot(_snapshot) {}
527 using NodeNotifier::ObserverBase::attach;
528 using NodeNotifier::ObserverBase::detach;
529 using NodeNotifier::ObserverBase::attached;
533 virtual void add(const Node& node) {
534 snapshot.addNode(node);
536 virtual void add(const std::vector<Node>& nodes) {
537 for (int i = nodes.size() - 1; i >= 0; ++i) {
538 snapshot.addNode(nodes[i]);
541 virtual void erase(const Node& node) {
542 snapshot.eraseNode(node);
544 virtual void erase(const std::vector<Node>& nodes) {
545 for (int i = 0; i < (int)nodes.size(); ++i) {
546 if (!snapshot.eraseNode(nodes[i])) break;
549 virtual void build() {
550 NodeNotifier* notifier = getNotifier();
552 std::vector<Node> nodes;
553 for (notifier->first(node); node != INVALID; notifier->next(node)) {
554 nodes.push_back(node);
556 for (int i = nodes.size() - 1; i >= 0; --i) {
557 snapshot.addNode(nodes[i]);
560 virtual void clear() {
561 NodeNotifier* notifier = getNotifier();
563 for (notifier->first(node); node != INVALID; notifier->next(node)) {
564 if (!snapshot.eraseNode(node)) break;
571 class EdgeObserverProxy : public EdgeNotifier::ObserverBase {
574 EdgeObserverProxy(Snapshot& _snapshot)
575 : snapshot(_snapshot) {}
577 using EdgeNotifier::ObserverBase::attach;
578 using EdgeNotifier::ObserverBase::detach;
579 using EdgeNotifier::ObserverBase::attached;
583 virtual void add(const Edge& edge) {
584 snapshot.addEdge(edge);
586 virtual void add(const std::vector<Edge>& edges) {
587 for (int i = edges.size() - 1; i >= 0; ++i) {
588 snapshot.addEdge(edges[i]);
591 virtual void erase(const Edge& edge) {
592 snapshot.eraseEdge(edge);
594 virtual void erase(const std::vector<Edge>& edges) {
595 for (int i = 0; i < (int)edges.size(); ++i) {
596 if (!snapshot.eraseEdge(edges[i])) break;
599 virtual void build() {
600 EdgeNotifier* notifier = getNotifier();
602 std::vector<Edge> edges;
603 for (notifier->first(edge); edge != INVALID; notifier->next(edge)) {
604 edges.push_back(edge);
606 for (int i = edges.size() - 1; i >= 0; --i) {
607 snapshot.addEdge(edges[i]);
610 virtual void clear() {
611 EdgeNotifier* notifier = getNotifier();
613 for (notifier->first(edge); edge != INVALID; notifier->next(edge)) {
614 if (!snapshot.eraseEdge(edge)) break;
623 NodeObserverProxy node_observer_proxy;
624 EdgeObserverProxy edge_observer_proxy;
626 std::list<Node> added_nodes;
627 std::list<Edge> added_edges;
630 void addNode(const Node& node) {
631 added_nodes.push_front(node);
633 bool eraseNode(const Node& node) {
634 std::list<Node>::iterator it =
635 std::find(added_nodes.begin(), added_nodes.end(), node);
636 if (it == added_nodes.end()) {
640 added_nodes.erase(it);
645 void addEdge(const Edge& edge) {
646 added_edges.push_front(edge);
648 bool eraseEdge(const Edge& edge) {
649 std::list<Edge>::iterator it =
650 std::find(added_edges.begin(), added_edges.end(), edge);
651 if (it == added_edges.end()) {
655 added_edges.erase(it);
660 void attach(ListGraph &_graph) {
662 node_observer_proxy.attach(graph->getNotifier(Node()));
663 edge_observer_proxy.attach(graph->getNotifier(Edge()));
667 node_observer_proxy.detach();
668 edge_observer_proxy.detach();
679 /// \brief Default constructur.
681 /// Default constructor.
682 /// To actually make a snapshot you must call save().
684 : graph(0), node_observer_proxy(*this),
685 edge_observer_proxy(*this) {}
687 /// \brief Constructor that immediately makes a snapshot.
689 /// This constructor immediately makes a snapshot of the graph.
690 /// \param _graph The graph we make a snapshot of.
691 Snapshot(ListGraph &_graph)
692 : node_observer_proxy(*this),
693 edge_observer_proxy(*this) {
697 /// \brief Make a snapshot.
699 /// Make a snapshot of the graph.
701 /// This function can be called more than once. In case of a repeated
702 /// call, the previous snapshot gets lost.
703 /// \param _graph The graph we make the snapshot of.
704 void save(ListGraph &_graph) {
709 /// \brief Undo the changes until the last snapshot.
711 /// Undo the changes until the last snapshot created by save().
714 while(!added_edges.empty()) {
715 graph->erase(added_edges.front());
716 added_edges.pop_front();
718 while(!added_nodes.empty()) {
719 graph->erase(added_nodes.front());
720 added_nodes.pop_front();
724 /// \brief Gives back true when the snapshot is valid.
726 /// Gives back true when the snapshot is valid.
728 return node_observer_proxy.attached();
736 /**************** Undirected List Graph ****************/
738 typedef UGraphExtender<UndirGraphExtender<ListGraphBase> >
739 ExtendedListUGraphBase;
741 /// \addtogroup graphs
744 ///An undirected list graph class.
746 ///This is a simple and fast undirected graph implementation.
748 ///It conforms to the
749 ///\ref concept::UGraph "UGraph concept".
751 ///\sa concept::UGraph.
753 ///\todo Snapshot, reverseEdge(), changeTarget(), changeSource(), contract()
754 ///haven't been implemented yet.
756 class ListUGraph : public ExtendedListUGraphBase {
758 ///ListUGraph is \e not copy constructible. Use UGraphCopy() instead.
760 ///ListUGraph is \e not copy constructible. Use UGraphCopy() instead.
762 ListUGraph(const ListUGraph &) :ExtendedListUGraphBase() {};
763 ///\brief Assignment of ListUGraph to another one is \e not allowed.
764 ///Use UGraphCopy() instead.
766 ///Assignment of ListUGraph to another one is \e not allowed.
767 ///Use UGraphCopy() instead.
768 void operator=(const ListUGraph &) {}
776 typedef ExtendedListUGraphBase Parent;
777 /// \brief Add a new node to the graph.
779 /// \return the new node.
781 Node addNode() { return Parent::addNode(); }
783 /// \brief Add a new edge to the graph.
785 /// Add a new edge to the graph with source node \c s
786 /// and target node \c t.
787 /// \return the new undirected edge.
788 UEdge addEdge(const Node& s, const Node& t) {
789 return Parent::addEdge(s, t);
791 /// \brief Changes the target of \c e to \c n
793 /// Changes the target of \c e to \c n
795 /// \note The <tt>Edge</tt>'s and <tt>OutEdge</tt>'s
796 /// referencing the changed edge remain
797 /// valid. However <tt>InEdge</tt>'s are invalidated.
798 void changeTarget(UEdge e, Node n) {
799 Parent::changeTarget(e,n);
801 /// Changes the source of \c e to \c n
803 /// Changes the source of \c e to \c n
805 ///\note The <tt>Edge</tt>'s and <tt>InEdge</tt>'s
806 ///referencing the changed edge remain
807 ///valid. However <tt>OutEdge</tt>'s are invalidated.
808 void changeSource(UEdge e, Node n) {
809 Parent::changeSource(e,n);
811 /// \brief Contract two nodes.
813 /// This function contracts two nodes.
815 /// Node \p b will be removed but instead of deleting
816 /// its neighboring edges, they will be joined to \p a.
817 /// The last parameter \p r controls whether to remove loops. \c true
818 /// means that loops will be removed.
820 /// \note The <tt>Edge</tt>s
821 /// referencing a moved edge remain
823 void contract(Node a, Node b, bool r = true) {
824 for(IncEdgeIt e(*this, b); e!=INVALID;) {
825 IncEdgeIt f = e; ++f;
826 if (r && runningNode(e) == a) {
828 } else if (source(e) == b) {
840 class ListBpUGraphBase {
843 class NodeSetError : public LogicError {
844 virtual const char* what() const throw() {
845 return "lemon::ListBpUGraph::NodeSetError";
852 int first_edge, prev, next;
856 int aNode, prev_out, next_out;
857 int bNode, prev_in, next_in;
860 std::vector<NodeT> aNodes;
861 std::vector<NodeT> bNodes;
863 std::vector<UEdgeT> edges;
866 int first_free_anode;
869 int first_free_bnode;
876 friend class ListBpUGraphBase;
880 explicit Node(int _id) : id(_id) {}
883 Node(Invalid) { id = -1; }
884 bool operator==(const Node i) const {return id==i.id;}
885 bool operator!=(const Node i) const {return id!=i.id;}
886 bool operator<(const Node i) const {return id<i.id;}
890 friend class ListBpUGraphBase;
894 explicit UEdge(int _id) { id = _id;}
897 UEdge (Invalid) { id = -1; }
898 bool operator==(const UEdge i) const {return id==i.id;}
899 bool operator!=(const UEdge i) const {return id!=i.id;}
900 bool operator<(const UEdge i) const {return id<i.id;}
904 : first_anode(-1), first_free_anode(-1),
905 first_bnode(-1), first_free_bnode(-1),
906 first_free_edge(-1) {}
908 void firstANode(Node& node) const {
909 node.id = first_anode != -1 ? (first_anode << 1) : -1;
911 void nextANode(Node& node) const {
912 node.id = aNodes[node.id >> 1].next;
915 void firstBNode(Node& node) const {
916 node.id = first_bnode != -1 ? (first_bnode << 1) + 1 : -1;
918 void nextBNode(Node& node) const {
919 node.id = bNodes[node.id >> 1].next;
922 void first(Node& node) const {
923 if (first_anode != -1) {
924 node.id = (first_anode << 1);
925 } else if (first_bnode != -1) {
926 node.id = (first_bnode << 1) + 1;
931 void next(Node& node) const {
933 node.id = aNodes[node.id >> 1].next;
935 if (first_bnode != -1) {
936 node.id = (first_bnode << 1) + 1;
940 node.id = bNodes[node.id >> 1].next;
944 void first(UEdge& edge) const {
945 int aNodeId = first_anode;
946 while (aNodeId != -1 && aNodes[aNodeId].first_edge == -1) {
947 aNodeId = aNodes[aNodeId].next != -1 ?
948 aNodes[aNodeId].next >> 1 : -1;
951 edge.id = aNodes[aNodeId].first_edge;
956 void next(UEdge& edge) const {
957 int aNodeId = edges[edge.id].aNode >> 1;
958 edge.id = edges[edge.id].next_out;
960 aNodeId = aNodes[aNodeId].next != -1 ?
961 aNodes[aNodeId].next >> 1 : -1;
962 while (aNodeId != -1 && aNodes[aNodeId].first_edge == -1) {
963 aNodeId = aNodes[aNodeId].next != -1 ?
964 aNodes[aNodeId].next >> 1 : -1;
967 edge.id = aNodes[aNodeId].first_edge;
974 void firstFromANode(UEdge& edge, const Node& node) const {
975 LEMON_ASSERT((node.id & 1) == 0, NodeSetError());
976 edge.id = aNodes[node.id >> 1].first_edge;
978 void nextFromANode(UEdge& edge) const {
979 edge.id = edges[edge.id].next_out;
982 void firstFromBNode(UEdge& edge, const Node& node) const {
983 LEMON_ASSERT((node.id & 1) == 1, NodeSetError());
984 edge.id = bNodes[node.id >> 1].first_edge;
986 void nextFromBNode(UEdge& edge) const {
987 edge.id = edges[edge.id].next_in;
990 static int id(const Node& node) {
993 static Node nodeFromId(int id) {
996 int maxNodeId() const {
997 return aNodes.size() > bNodes.size() ?
998 aNodes.size() * 2 - 2 : bNodes.size() * 2 - 1;
1001 static int id(const UEdge& edge) {
1004 static UEdge uEdgeFromId(int id) {
1007 int maxUEdgeId() const {
1008 return edges.size();
1011 static int aNodeId(const Node& node) {
1012 return node.id >> 1;
1014 static Node fromANodeId(int id) {
1015 return Node(id << 1);
1017 int maxANodeId() const {
1018 return aNodes.size();
1021 static int bNodeId(const Node& node) {
1022 return node.id >> 1;
1024 static Node fromBNodeId(int id) {
1025 return Node((id << 1) + 1);
1027 int maxBNodeId() const {
1028 return bNodes.size();
1031 Node aNode(const UEdge& edge) const {
1032 return Node(edges[edge.id].aNode);
1034 Node bNode(const UEdge& edge) const {
1035 return Node(edges[edge.id].bNode);
1038 static bool aNode(const Node& node) {
1039 return (node.id & 1) == 0;
1042 static bool bNode(const Node& node) {
1043 return (node.id & 1) == 1;
1048 if (first_free_anode == -1) {
1049 aNodeId = aNodes.size();
1050 aNodes.push_back(NodeT());
1052 aNodeId = first_free_anode;
1053 first_free_anode = aNodes[first_free_anode].next;
1055 if (first_anode != -1) {
1056 aNodes[aNodeId].next = first_anode << 1;
1057 aNodes[first_anode].prev = aNodeId << 1;
1059 aNodes[aNodeId].next = -1;
1061 aNodes[aNodeId].prev = -1;
1062 first_anode = aNodeId;
1063 aNodes[aNodeId].first_edge = -1;
1064 return Node(aNodeId << 1);
1069 if (first_free_bnode == -1) {
1070 bNodeId = bNodes.size();
1071 bNodes.push_back(NodeT());
1073 bNodeId = first_free_bnode;
1074 first_free_bnode = bNodes[first_free_bnode].next;
1076 if (first_bnode != -1) {
1077 bNodes[bNodeId].next = (first_bnode << 1) + 1;
1078 bNodes[first_bnode].prev = (bNodeId << 1) + 1;
1080 bNodes[bNodeId].next = -1;
1082 first_bnode = bNodeId;
1083 bNodes[bNodeId].first_edge = -1;
1084 return Node((bNodeId << 1) + 1);
1087 UEdge addEdge(const Node& source, const Node& target) {
1088 LEMON_ASSERT(((source.id ^ target.id) & 1) == 1, NodeSetError());
1090 if (first_free_edge != -1) {
1091 edgeId = first_free_edge;
1092 first_free_edge = edges[edgeId].next_out;
1094 edgeId = edges.size();
1095 edges.push_back(UEdgeT());
1097 if ((source.id & 1) == 0) {
1098 edges[edgeId].aNode = source.id;
1099 edges[edgeId].bNode = target.id;
1101 edges[edgeId].aNode = target.id;
1102 edges[edgeId].bNode = source.id;
1104 edges[edgeId].next_out = aNodes[edges[edgeId].aNode >> 1].first_edge;
1105 edges[edgeId].prev_out = -1;
1106 if (aNodes[edges[edgeId].aNode >> 1].first_edge != -1) {
1107 edges[aNodes[edges[edgeId].aNode >> 1].first_edge].prev_out = edgeId;
1109 aNodes[edges[edgeId].aNode >> 1].first_edge = edgeId;
1110 edges[edgeId].next_in = bNodes[edges[edgeId].bNode >> 1].first_edge;
1111 edges[edgeId].prev_in = -1;
1112 if (bNodes[edges[edgeId].bNode >> 1].first_edge != -1) {
1113 edges[bNodes[edges[edgeId].bNode >> 1].first_edge].prev_in = edgeId;
1115 bNodes[edges[edgeId].bNode >> 1].first_edge = edgeId;
1116 return UEdge(edgeId);
1119 void erase(const Node& node) {
1121 int aNodeId = node.id >> 1;
1122 if (aNodes[aNodeId].prev != -1) {
1123 aNodes[aNodes[aNodeId].prev >> 1].next = aNodes[aNodeId].next;
1125 first_anode = aNodes[aNodeId].next >> 1;
1127 if (aNodes[aNodeId].next != -1) {
1128 aNodes[aNodes[aNodeId].next >> 1].prev = aNodes[aNodeId].prev;
1130 aNodes[aNodeId].next = first_free_anode;
1131 first_free_anode = aNodeId;
1133 int bNodeId = node.id >> 1;
1134 if (bNodes[bNodeId].prev != -1) {
1135 bNodes[bNodes[bNodeId].prev >> 1].next = bNodes[bNodeId].next;
1137 first_bnode = bNodes[bNodeId].next >> 1;
1139 if (bNodes[bNodeId].next != -1) {
1140 bNodes[bNodes[bNodeId].next >> 1].prev = bNodes[bNodeId].prev;
1142 bNodes[bNodeId].next = first_free_bnode;
1143 first_free_bnode = bNodeId;
1147 void erase(const UEdge& edge) {
1149 if (edges[edge.id].prev_out != -1) {
1150 edges[edges[edge.id].prev_out].next_out = edges[edge.id].next_out;
1152 aNodes[edges[edge.id].aNode >> 1].first_edge = edges[edge.id].next_out;
1154 if (edges[edge.id].next_out != -1) {
1155 edges[edges[edge.id].next_out].prev_out = edges[edge.id].prev_out;
1158 if (edges[edge.id].prev_in != -1) {
1159 edges[edges[edge.id].prev_in].next_in = edges[edge.id].next_in;
1161 bNodes[edges[edge.id].bNode >> 1].first_edge = edges[edge.id].next_in;
1163 if (edges[edge.id].next_in != -1) {
1164 edges[edges[edge.id].next_in].prev_in = edges[edge.id].prev_in;
1167 edges[edge.id].next_out = first_free_edge;
1168 first_free_edge = edge.id;
1173 ///\bug Undocumented
1174 ///\bug Doesn't destruct the maps.
1180 first_free_anode = -1;
1182 first_free_bnode = -1;
1183 first_free_edge = -1;
1189 typedef BpUGraphExtender< ListBpUGraphBase > ExtendedListBpUGraphBase;
1193 /// \brief A smart bipartite undirected graph class.
1195 /// This is a bipartite undirected graph implementation.
1196 /// It is conforms to the \ref concept::BpUGraph "BpUGraph concept".
1197 /// \sa concept::BpUGraph.
1199 class ListBpUGraph : public ExtendedListBpUGraphBase {};