1 //===-------------------- Graph.h - PBQP Graph ------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // PBQP Graph class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 
15 #ifndef LLVM_CODEGEN_PBQP_GRAPH_H
16 #define LLVM_CODEGEN_PBQP_GRAPH_H
17 
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/Support/Debug.h"
20 #include <algorithm>
21 #include <cassert>
22 #include <limits>
23 #include <utility>
24 #include <vector>
25 
26 namespace llvm {
27 namespace PBQP {
28 
29   class GraphBase {
30   public:
31     typedef unsigned NodeId;
32     typedef unsigned EdgeId;
33 
34     /// @brief Returns a value representing an invalid (non-existent) node.
35     static NodeId invalidNodeId() {
36       return std::numeric_limits<NodeId>::max();
37     }
38 
39     /// @brief Returns a value representing an invalid (non-existent) edge.
40     static EdgeId invalidEdgeId() {
41       return std::numeric_limits<EdgeId>::max();
42     }
43   };
44 
45   /// PBQP Graph class.
46   /// Instances of this class describe PBQP problems.
47   ///
48   template <typename SolverT>
49   class Graph : public GraphBase {
50   private:
51     typedef typename SolverT::CostAllocator CostAllocator;
52   public:
53     typedef typename SolverT::RawVector RawVector;
54     typedef typename SolverT::RawMatrix RawMatrix;
55     typedef typename SolverT::Vector Vector;
56     typedef typename SolverT::Matrix Matrix;
57     typedef typename CostAllocator::VectorPtr VectorPtr;
58     typedef typename CostAllocator::MatrixPtr MatrixPtr;
59     typedef typename SolverT::NodeMetadata NodeMetadata;
60     typedef typename SolverT::EdgeMetadata EdgeMetadata;
61     typedef typename SolverT::GraphMetadata GraphMetadata;
62 
63   private:
64 
65     class NodeEntry {
66     public:
67       typedef std::vector<EdgeId> AdjEdgeList;
68       typedef AdjEdgeList::size_type AdjEdgeIdx;
69       typedef AdjEdgeList::const_iterator AdjEdgeItr;
70 
71       static AdjEdgeIdx getInvalidAdjEdgeIdx() {
72         return std::numeric_limits<AdjEdgeIdx>::max();
73       }
74 
75       NodeEntry(VectorPtr Costs) : Costs(std::move(Costs)) {}
76 
77       AdjEdgeIdx addAdjEdgeId(EdgeId EId) {
78         AdjEdgeIdx Idx = AdjEdgeIds.size();
79         AdjEdgeIds.push_back(EId);
80         return Idx;
81       }
82 
83       void removeAdjEdgeId(Graph &G, NodeId ThisNId, AdjEdgeIdx Idx) {
84         // Swap-and-pop for fast removal.
85         //   1) Update the adj index of the edge currently at back().
86         //   2) Move last Edge down to Idx.
87         //   3) pop_back()
88         // If Idx == size() - 1 then the setAdjEdgeIdx and swap are
89         // redundant, but both operations are cheap.
90         G.getEdge(AdjEdgeIds.back()).setAdjEdgeIdx(ThisNId, Idx);
91         AdjEdgeIds[Idx] = AdjEdgeIds.back();
92         AdjEdgeIds.pop_back();
93       }
94 
95       const AdjEdgeList& getAdjEdgeIds() const { return AdjEdgeIds; }
96 
97       VectorPtr Costs;
98       NodeMetadata Metadata;
99     private:
100       AdjEdgeList AdjEdgeIds;
101     };
102 
103     class EdgeEntry {
104     public:
105       EdgeEntry(NodeId N1Id, NodeId N2Id, MatrixPtr Costs)
106           : Costs(std::move(Costs)) {
107         NIds[0] = N1Id;
108         NIds[1] = N2Id;
109         ThisEdgeAdjIdxs[0] = NodeEntry::getInvalidAdjEdgeIdx();
110         ThisEdgeAdjIdxs[1] = NodeEntry::getInvalidAdjEdgeIdx();
111       }
112 
113       void invalidate() {
114         NIds[0] = NIds[1] = Graph::invalidNodeId();
115         ThisEdgeAdjIdxs[0] = ThisEdgeAdjIdxs[1] =
116           NodeEntry::getInvalidAdjEdgeIdx();
117         Costs = nullptr;
118       }
119 
120       void connectToN(Graph &G, EdgeId ThisEdgeId, unsigned NIdx) {
121         assert(ThisEdgeAdjIdxs[NIdx] == NodeEntry::getInvalidAdjEdgeIdx() &&
122                "Edge already connected to NIds[NIdx].");
123         NodeEntry &N = G.getNode(NIds[NIdx]);
124         ThisEdgeAdjIdxs[NIdx] = N.addAdjEdgeId(ThisEdgeId);
125       }
126 
127       void connectTo(Graph &G, EdgeId ThisEdgeId, NodeId NId) {
128         if (NId == NIds[0])
129           connectToN(G, ThisEdgeId, 0);
130         else {
131           assert(NId == NIds[1] && "Edge does not connect NId.");
132           connectToN(G, ThisEdgeId, 1);
133         }
134       }
135 
136       void connect(Graph &G, EdgeId ThisEdgeId) {
137         connectToN(G, ThisEdgeId, 0);
138         connectToN(G, ThisEdgeId, 1);
139       }
140 
141       void setAdjEdgeIdx(NodeId NId, typename NodeEntry::AdjEdgeIdx NewIdx) {
142         if (NId == NIds[0])
143           ThisEdgeAdjIdxs[0] = NewIdx;
144         else {
145           assert(NId == NIds[1] && "Edge not connected to NId");
146           ThisEdgeAdjIdxs[1] = NewIdx;
147         }
148       }
149 
150       void disconnectFromN(Graph &G, unsigned NIdx) {
151         assert(ThisEdgeAdjIdxs[NIdx] != NodeEntry::getInvalidAdjEdgeIdx() &&
152                "Edge not connected to NIds[NIdx].");
153         NodeEntry &N = G.getNode(NIds[NIdx]);
154         N.removeAdjEdgeId(G, NIds[NIdx], ThisEdgeAdjIdxs[NIdx]);
155         ThisEdgeAdjIdxs[NIdx] = NodeEntry::getInvalidAdjEdgeIdx();
156       }
157 
158       void disconnectFrom(Graph &G, NodeId NId) {
159         if (NId == NIds[0])
160           disconnectFromN(G, 0);
161         else {
162           assert(NId == NIds[1] && "Edge does not connect NId");
163           disconnectFromN(G, 1);
164         }
165       }
166 
167       NodeId getN1Id() const { return NIds[0]; }
168       NodeId getN2Id() const { return NIds[1]; }
169       MatrixPtr Costs;
170       EdgeMetadata Metadata;
171     private:
172       NodeId NIds[2];
173       typename NodeEntry::AdjEdgeIdx ThisEdgeAdjIdxs[2];
174     };
175 
176     // ----- MEMBERS -----
177 
178     GraphMetadata Metadata;
179     CostAllocator CostAlloc;
180     SolverT *Solver;
181 
182     typedef std::vector<NodeEntry> NodeVector;
183     typedef std::vector<NodeId> FreeNodeVector;
184     NodeVector Nodes;
185     FreeNodeVector FreeNodeIds;
186 
187     typedef std::vector<EdgeEntry> EdgeVector;
188     typedef std::vector<EdgeId> FreeEdgeVector;
189     EdgeVector Edges;
190     FreeEdgeVector FreeEdgeIds;
191 
192     // ----- INTERNAL METHODS -----
193 
194     NodeEntry &getNode(NodeId NId) {
195       assert(NId < Nodes.size() && "Out of bound NodeId");
196       return Nodes[NId];
197     }
198     const NodeEntry &getNode(NodeId NId) const {
199       assert(NId < Nodes.size() && "Out of bound NodeId");
200       return Nodes[NId];
201     }
202 
203     EdgeEntry& getEdge(EdgeId EId) { return Edges[EId]; }
204     const EdgeEntry& getEdge(EdgeId EId) const { return Edges[EId]; }
205 
206     NodeId addConstructedNode(NodeEntry N) {
207       NodeId NId = 0;
208       if (!FreeNodeIds.empty()) {
209         NId = FreeNodeIds.back();
210         FreeNodeIds.pop_back();
211         Nodes[NId] = std::move(N);
212       } else {
213         NId = Nodes.size();
214         Nodes.push_back(std::move(N));
215       }
216       return NId;
217     }
218 
219     EdgeId addConstructedEdge(EdgeEntry E) {
220       assert(findEdge(E.getN1Id(), E.getN2Id()) == invalidEdgeId() &&
221              "Attempt to add duplicate edge.");
222       EdgeId EId = 0;
223       if (!FreeEdgeIds.empty()) {
224         EId = FreeEdgeIds.back();
225         FreeEdgeIds.pop_back();
226         Edges[EId] = std::move(E);
227       } else {
228         EId = Edges.size();
229         Edges.push_back(std::move(E));
230       }
231 
232       EdgeEntry &NE = getEdge(EId);
233 
234       // Add the edge to the adjacency sets of its nodes.
235       NE.connect(*this, EId);
236       return EId;
237     }
238 
239     Graph(const Graph &Other) {}
240     void operator=(const Graph &Other) {}
241 
242   public:
243 
244     typedef typename NodeEntry::AdjEdgeItr AdjEdgeItr;
245 
246     class NodeItr {
247     public:
248       typedef std::forward_iterator_tag iterator_category;
249       typedef NodeId value_type;
250       typedef int difference_type;
251       typedef NodeId* pointer;
252       typedef NodeId& reference;
253 
254       NodeItr(NodeId CurNId, const Graph &G)
255         : CurNId(CurNId), EndNId(G.Nodes.size()), FreeNodeIds(G.FreeNodeIds) {
256         this->CurNId = findNextInUse(CurNId); // Move to first in-use node id
257       }
258 
259       bool operator==(const NodeItr &O) const { return CurNId == O.CurNId; }
260       bool operator!=(const NodeItr &O) const { return !(*this == O); }
261       NodeItr& operator++() { CurNId = findNextInUse(++CurNId); return *this; }
262       NodeId operator*() const { return CurNId; }
263 
264     private:
265       NodeId findNextInUse(NodeId NId) const {
266         while (NId < EndNId && is_contained(FreeNodeIds, NId)) {
267           ++NId;
268         }
269         return NId;
270       }
271 
272       NodeId CurNId, EndNId;
273       const FreeNodeVector &FreeNodeIds;
274     };
275 
276     class EdgeItr {
277     public:
278       EdgeItr(EdgeId CurEId, const Graph &G)
279         : CurEId(CurEId), EndEId(G.Edges.size()), FreeEdgeIds(G.FreeEdgeIds) {
280         this->CurEId = findNextInUse(CurEId); // Move to first in-use edge id
281       }
282 
283       bool operator==(const EdgeItr &O) const { return CurEId == O.CurEId; }
284       bool operator!=(const EdgeItr &O) const { return !(*this == O); }
285       EdgeItr& operator++() { CurEId = findNextInUse(++CurEId); return *this; }
286       EdgeId operator*() const { return CurEId; }
287 
288     private:
289       EdgeId findNextInUse(EdgeId EId) const {
290         while (EId < EndEId && is_contained(FreeEdgeIds, EId)) {
291           ++EId;
292         }
293         return EId;
294       }
295 
296       EdgeId CurEId, EndEId;
297       const FreeEdgeVector &FreeEdgeIds;
298     };
299 
300     class NodeIdSet {
301     public:
302       NodeIdSet(const Graph &G) : G(G) { }
303       NodeItr begin() const { return NodeItr(0, G); }
304       NodeItr end() const { return NodeItr(G.Nodes.size(), G); }
305       bool empty() const { return G.Nodes.empty(); }
306       typename NodeVector::size_type size() const {
307         return G.Nodes.size() - G.FreeNodeIds.size();
308       }
309     private:
310       const Graph& G;
311     };
312 
313     class EdgeIdSet {
314     public:
315       EdgeIdSet(const Graph &G) : G(G) { }
316       EdgeItr begin() const { return EdgeItr(0, G); }
317       EdgeItr end() const { return EdgeItr(G.Edges.size(), G); }
318       bool empty() const { return G.Edges.empty(); }
319       typename NodeVector::size_type size() const {
320         return G.Edges.size() - G.FreeEdgeIds.size();
321       }
322     private:
323       const Graph& G;
324     };
325 
326     class AdjEdgeIdSet {
327     public:
328       AdjEdgeIdSet(const NodeEntry &NE) : NE(NE) { }
329       typename NodeEntry::AdjEdgeItr begin() const {
330         return NE.getAdjEdgeIds().begin();
331       }
332       typename NodeEntry::AdjEdgeItr end() const {
333         return NE.getAdjEdgeIds().end();
334       }
335       bool empty() const { return NE.getAdjEdgeIds().empty(); }
336       typename NodeEntry::AdjEdgeList::size_type size() const {
337         return NE.getAdjEdgeIds().size();
338       }
339     private:
340       const NodeEntry &NE;
341     };
342 
343     /// @brief Construct an empty PBQP graph.
344     Graph() : Solver(nullptr) {}
345 
346     /// @brief Construct an empty PBQP graph with the given graph metadata.
347     Graph(GraphMetadata Metadata)
348         : Metadata(std::move(Metadata)), Solver(nullptr) {}
349 
350     /// @brief Get a reference to the graph metadata.
351     GraphMetadata& getMetadata() { return Metadata; }
352 
353     /// @brief Get a const-reference to the graph metadata.
354     const GraphMetadata& getMetadata() const { return Metadata; }
355 
356     /// @brief Lock this graph to the given solver instance in preparation
357     /// for running the solver. This method will call solver.handleAddNode for
358     /// each node in the graph, and handleAddEdge for each edge, to give the
359     /// solver an opportunity to set up any requried metadata.
360     void setSolver(SolverT &S) {
361       assert(!Solver && "Solver already set. Call unsetSolver().");
362       Solver = &S;
363       for (auto NId : nodeIds())
364         Solver->handleAddNode(NId);
365       for (auto EId : edgeIds())
366         Solver->handleAddEdge(EId);
367     }
368 
369     /// @brief Release from solver instance.
370     void unsetSolver() {
371       assert(Solver && "Solver not set.");
372       Solver = nullptr;
373     }
374 
375     /// @brief Add a node with the given costs.
376     /// @param Costs Cost vector for the new node.
377     /// @return Node iterator for the added node.
378     template <typename OtherVectorT>
379     NodeId addNode(OtherVectorT Costs) {
380       // Get cost vector from the problem domain
381       VectorPtr AllocatedCosts = CostAlloc.getVector(std::move(Costs));
382       NodeId NId = addConstructedNode(NodeEntry(AllocatedCosts));
383       if (Solver)
384         Solver->handleAddNode(NId);
385       return NId;
386     }
387 
388     /// @brief Add a node bypassing the cost allocator.
389     /// @param Costs Cost vector ptr for the new node (must be convertible to
390     ///        VectorPtr).
391     /// @return Node iterator for the added node.
392     ///
393     ///   This method allows for fast addition of a node whose costs don't need
394     /// to be passed through the cost allocator. The most common use case for
395     /// this is when duplicating costs from an existing node (when using a
396     /// pooling allocator). These have already been uniqued, so we can avoid
397     /// re-constructing and re-uniquing them by attaching them directly to the
398     /// new node.
399     template <typename OtherVectorPtrT>
400     NodeId addNodeBypassingCostAllocator(OtherVectorPtrT Costs) {
401       NodeId NId = addConstructedNode(NodeEntry(Costs));
402       if (Solver)
403         Solver->handleAddNode(NId);
404       return NId;
405     }
406 
407     /// @brief Add an edge between the given nodes with the given costs.
408     /// @param N1Id First node.
409     /// @param N2Id Second node.
410     /// @param Costs Cost matrix for new edge.
411     /// @return Edge iterator for the added edge.
412     template <typename OtherVectorT>
413     EdgeId addEdge(NodeId N1Id, NodeId N2Id, OtherVectorT Costs) {
414       assert(getNodeCosts(N1Id).getLength() == Costs.getRows() &&
415              getNodeCosts(N2Id).getLength() == Costs.getCols() &&
416              "Matrix dimensions mismatch.");
417       // Get cost matrix from the problem domain.
418       MatrixPtr AllocatedCosts = CostAlloc.getMatrix(std::move(Costs));
419       EdgeId EId = addConstructedEdge(EdgeEntry(N1Id, N2Id, AllocatedCosts));
420       if (Solver)
421         Solver->handleAddEdge(EId);
422       return EId;
423     }
424 
425     /// @brief Add an edge bypassing the cost allocator.
426     /// @param N1Id First node.
427     /// @param N2Id Second node.
428     /// @param Costs Cost matrix for new edge.
429     /// @return Edge iterator for the added edge.
430     ///
431     ///   This method allows for fast addition of an edge whose costs don't need
432     /// to be passed through the cost allocator. The most common use case for
433     /// this is when duplicating costs from an existing edge (when using a
434     /// pooling allocator). These have already been uniqued, so we can avoid
435     /// re-constructing and re-uniquing them by attaching them directly to the
436     /// new edge.
437     template <typename OtherMatrixPtrT>
438     NodeId addEdgeBypassingCostAllocator(NodeId N1Id, NodeId N2Id,
439                                          OtherMatrixPtrT Costs) {
440       assert(getNodeCosts(N1Id).getLength() == Costs->getRows() &&
441              getNodeCosts(N2Id).getLength() == Costs->getCols() &&
442              "Matrix dimensions mismatch.");
443       // Get cost matrix from the problem domain.
444       EdgeId EId = addConstructedEdge(EdgeEntry(N1Id, N2Id, Costs));
445       if (Solver)
446         Solver->handleAddEdge(EId);
447       return EId;
448     }
449 
450     /// @brief Returns true if the graph is empty.
451     bool empty() const { return NodeIdSet(*this).empty(); }
452 
453     NodeIdSet nodeIds() const { return NodeIdSet(*this); }
454     EdgeIdSet edgeIds() const { return EdgeIdSet(*this); }
455 
456     AdjEdgeIdSet adjEdgeIds(NodeId NId) { return AdjEdgeIdSet(getNode(NId)); }
457 
458     /// @brief Get the number of nodes in the graph.
459     /// @return Number of nodes in the graph.
460     unsigned getNumNodes() const { return NodeIdSet(*this).size(); }
461 
462     /// @brief Get the number of edges in the graph.
463     /// @return Number of edges in the graph.
464     unsigned getNumEdges() const { return EdgeIdSet(*this).size(); }
465 
466     /// @brief Set a node's cost vector.
467     /// @param NId Node to update.
468     /// @param Costs New costs to set.
469     template <typename OtherVectorT>
470     void setNodeCosts(NodeId NId, OtherVectorT Costs) {
471       VectorPtr AllocatedCosts = CostAlloc.getVector(std::move(Costs));
472       if (Solver)
473         Solver->handleSetNodeCosts(NId, *AllocatedCosts);
474       getNode(NId).Costs = AllocatedCosts;
475     }
476 
477     /// @brief Get a VectorPtr to a node's cost vector. Rarely useful - use
478     ///        getNodeCosts where possible.
479     /// @param NId Node id.
480     /// @return VectorPtr to node cost vector.
481     ///
482     ///   This method is primarily useful for duplicating costs quickly by
483     /// bypassing the cost allocator. See addNodeBypassingCostAllocator. Prefer
484     /// getNodeCosts when dealing with node cost values.
485     const VectorPtr& getNodeCostsPtr(NodeId NId) const {
486       return getNode(NId).Costs;
487     }
488 
489     /// @brief Get a node's cost vector.
490     /// @param NId Node id.
491     /// @return Node cost vector.
492     const Vector& getNodeCosts(NodeId NId) const {
493       return *getNodeCostsPtr(NId);
494     }
495 
496     NodeMetadata& getNodeMetadata(NodeId NId) {
497       return getNode(NId).Metadata;
498     }
499 
500     const NodeMetadata& getNodeMetadata(NodeId NId) const {
501       return getNode(NId).Metadata;
502     }
503 
504     typename NodeEntry::AdjEdgeList::size_type getNodeDegree(NodeId NId) const {
505       return getNode(NId).getAdjEdgeIds().size();
506     }
507 
508     /// @brief Update an edge's cost matrix.
509     /// @param EId Edge id.
510     /// @param Costs New cost matrix.
511     template <typename OtherMatrixT>
512     void updateEdgeCosts(EdgeId EId, OtherMatrixT Costs) {
513       MatrixPtr AllocatedCosts = CostAlloc.getMatrix(std::move(Costs));
514       if (Solver)
515         Solver->handleUpdateCosts(EId, *AllocatedCosts);
516       getEdge(EId).Costs = AllocatedCosts;
517     }
518 
519     /// @brief Get a MatrixPtr to a node's cost matrix. Rarely useful - use
520     ///        getEdgeCosts where possible.
521     /// @param EId Edge id.
522     /// @return MatrixPtr to edge cost matrix.
523     ///
524     ///   This method is primarily useful for duplicating costs quickly by
525     /// bypassing the cost allocator. See addNodeBypassingCostAllocator. Prefer
526     /// getEdgeCosts when dealing with edge cost values.
527     const MatrixPtr& getEdgeCostsPtr(EdgeId EId) const {
528       return getEdge(EId).Costs;
529     }
530 
531     /// @brief Get an edge's cost matrix.
532     /// @param EId Edge id.
533     /// @return Edge cost matrix.
534     const Matrix& getEdgeCosts(EdgeId EId) const {
535       return *getEdge(EId).Costs;
536     }
537 
538     EdgeMetadata& getEdgeMetadata(EdgeId EId) {
539       return getEdge(EId).Metadata;
540     }
541 
542     const EdgeMetadata& getEdgeMetadata(EdgeId EId) const {
543       return getEdge(EId).Metadata;
544     }
545 
546     /// @brief Get the first node connected to this edge.
547     /// @param EId Edge id.
548     /// @return The first node connected to the given edge.
549     NodeId getEdgeNode1Id(EdgeId EId) const {
550       return getEdge(EId).getN1Id();
551     }
552 
553     /// @brief Get the second node connected to this edge.
554     /// @param EId Edge id.
555     /// @return The second node connected to the given edge.
556     NodeId getEdgeNode2Id(EdgeId EId) const {
557       return getEdge(EId).getN2Id();
558     }
559 
560     /// @brief Get the "other" node connected to this edge.
561     /// @param EId Edge id.
562     /// @param NId Node id for the "given" node.
563     /// @return The iterator for the "other" node connected to this edge.
564     NodeId getEdgeOtherNodeId(EdgeId EId, NodeId NId) {
565       EdgeEntry &E = getEdge(EId);
566       if (E.getN1Id() == NId) {
567         return E.getN2Id();
568       } // else
569       return E.getN1Id();
570     }
571 
572     /// @brief Get the edge connecting two nodes.
573     /// @param N1Id First node id.
574     /// @param N2Id Second node id.
575     /// @return An id for edge (N1Id, N2Id) if such an edge exists,
576     ///         otherwise returns an invalid edge id.
577     EdgeId findEdge(NodeId N1Id, NodeId N2Id) {
578       for (auto AEId : adjEdgeIds(N1Id)) {
579         if ((getEdgeNode1Id(AEId) == N2Id) ||
580             (getEdgeNode2Id(AEId) == N2Id)) {
581           return AEId;
582         }
583       }
584       return invalidEdgeId();
585     }
586 
587     /// @brief Remove a node from the graph.
588     /// @param NId Node id.
589     void removeNode(NodeId NId) {
590       if (Solver)
591         Solver->handleRemoveNode(NId);
592       NodeEntry &N = getNode(NId);
593       // TODO: Can this be for-each'd?
594       for (AdjEdgeItr AEItr = N.adjEdgesBegin(),
595              AEEnd = N.adjEdgesEnd();
596            AEItr != AEEnd;) {
597         EdgeId EId = *AEItr;
598         ++AEItr;
599         removeEdge(EId);
600       }
601       FreeNodeIds.push_back(NId);
602     }
603 
604     /// @brief Disconnect an edge from the given node.
605     ///
606     /// Removes the given edge from the adjacency list of the given node.
607     /// This operation leaves the edge in an 'asymmetric' state: It will no
608     /// longer appear in an iteration over the given node's (NId's) edges, but
609     /// will appear in an iteration over the 'other', unnamed node's edges.
610     ///
611     /// This does not correspond to any normal graph operation, but exists to
612     /// support efficient PBQP graph-reduction based solvers. It is used to
613     /// 'effectively' remove the unnamed node from the graph while the solver
614     /// is performing the reduction. The solver will later call reconnectNode
615     /// to restore the edge in the named node's adjacency list.
616     ///
617     /// Since the degree of a node is the number of connected edges,
618     /// disconnecting an edge from a node 'u' will cause the degree of 'u' to
619     /// drop by 1.
620     ///
621     /// A disconnected edge WILL still appear in an iteration over the graph
622     /// edges.
623     ///
624     /// A disconnected edge should not be removed from the graph, it should be
625     /// reconnected first.
626     ///
627     /// A disconnected edge can be reconnected by calling the reconnectEdge
628     /// method.
629     void disconnectEdge(EdgeId EId, NodeId NId) {
630       if (Solver)
631         Solver->handleDisconnectEdge(EId, NId);
632 
633       EdgeEntry &E = getEdge(EId);
634       E.disconnectFrom(*this, NId);
635     }
636 
637     /// @brief Convenience method to disconnect all neighbours from the given
638     ///        node.
639     void disconnectAllNeighborsFromNode(NodeId NId) {
640       for (auto AEId : adjEdgeIds(NId))
641         disconnectEdge(AEId, getEdgeOtherNodeId(AEId, NId));
642     }
643 
644     /// @brief Re-attach an edge to its nodes.
645     ///
646     /// Adds an edge that had been previously disconnected back into the
647     /// adjacency set of the nodes that the edge connects.
648     void reconnectEdge(EdgeId EId, NodeId NId) {
649       EdgeEntry &E = getEdge(EId);
650       E.connectTo(*this, EId, NId);
651       if (Solver)
652         Solver->handleReconnectEdge(EId, NId);
653     }
654 
655     /// @brief Remove an edge from the graph.
656     /// @param EId Edge id.
657     void removeEdge(EdgeId EId) {
658       if (Solver)
659         Solver->handleRemoveEdge(EId);
660       EdgeEntry &E = getEdge(EId);
661       E.disconnect();
662       FreeEdgeIds.push_back(EId);
663       Edges[EId].invalidate();
664     }
665 
666     /// @brief Remove all nodes and edges from the graph.
667     void clear() {
668       Nodes.clear();
669       FreeNodeIds.clear();
670       Edges.clear();
671       FreeEdgeIds.clear();
672     }
673   };
674 
675 }  // namespace PBQP
676 }  // namespace llvm
677 
678 #endif // LLVM_CODEGEN_PBQP_GRAPH_HPP
679