Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
Hypergraph.h
Go to the documentation of this file.
1
34#pragma once
35
37#include <ogdf/basic/Observer.h>
39#include <ogdf/basic/basic.h>
40#include <ogdf/basic/memory.h>
41
42#include <iosfwd>
43#include <string>
44
45namespace ogdf {
46template<class E>
47class List;
48} // namespace ogdf
49
51#define forall_adj_elements(adj, ge) for ((adj) = (v)->firstAdj(); (adj); (adj) = (adj)->succ())
52
54#define forall_hypernodes(v, H) for ((v) = (H).firstHypernode(); (v); (v) = (v)->succ())
55
57#define forall_rev_hypernodes(v, H) for ((v) = (H).lastHypernode(); (v); (v) = (v)->pred())
58
60#define forall_hyperedges(e, H) for ((e) = (H).firstHyperedge(); (e); (e) = (e)->succ())
61
63#define forall_rev_hyperedges(e, H) for ((e) = (H).lastHyperedge(); (e); (e) = (e)->pred())
64
65namespace ogdf {
66
67class AdjHypergraphElement; // IWYU pragma: keep
68class HyperedgeElement; // IWYU pragma: keep
69class Hypergraph; // IWYU pragma: keep
70class HypernodeElement; // IWYU pragma: keep
71
74
77
80
82
87 friend class Hypergraph;
88 friend class GraphListBase;
90
91private:
93 GraphElement* m_element;
94
96
103
106
108 explicit AdjHypergraphElement(GraphElement* pElement)
109 : m_element(pElement), m_twin(nullptr), m_index(0) { }
110
112 AdjHypergraphElement(GraphElement* pElement, int pIndex)
113 : m_element(pElement), m_twin(nullptr), m_index(pIndex) { }
114
115public:
117 int index() const { return m_index; }
118
120
125 GraphElement* element() const { return m_element; }
126
128 adjHypergraphEntry twin() const { return m_twin; }
129
131 adjHypergraphEntry succ() const { return static_cast<adjHypergraphEntry>(m_next); }
132
134 adjHypergraphEntry pred() const { return static_cast<adjHypergraphEntry>(m_prev); }
135
138
141
143
144 friend OGDF_EXPORT std::ostream& operator<<(std::ostream& os, ogdf::adjHypergraphEntry v);
145};
146
149 friend class Hypergraph;
150 friend class GraphListBase;
152
153private:
156
159
162
165
167
170 explicit HyperedgeElement(int pIndex)
171 : m_index(pIndex), m_cardinality(0), m_hypergraph(nullptr) { }
172
173public:
175 int index() const { return m_index; }
176
178 int cardinality() const { return m_cardinality; }
179
181 Hypergraph* hypergraph() const { return m_hypergraph; }
182
184 adjHypergraphEntry firstAdj() const { return m_adjHypernodes.head(); }
185
187 adjHypergraphEntry lastAdj() const { return m_adjHypernodes.tail(); }
188
191
193 template<class NODELIST>
194 void allHypernodes(NODELIST& hypernodes) const {
195 hypernodes.clear();
196 for (adjHypergraphEntry adj = firstAdj(); adj; adj = adj->succ()) {
197 hypernodes.pushBack(reinterpret_cast<hypernode>(adj->element()));
198 }
199 }
200
202 bool incident(hypernode v) const {
203 for (adjHypergraphEntry adj = firstAdj(); adj; adj = adj->succ()) {
204 if (reinterpret_cast<hypernode>(adj->element()) == v) {
205 return true;
206 }
207 }
208 return false;
209 }
210
212 hyperedge succ() const { return static_cast<hyperedge>(m_next); }
213
215 hyperedge pred() const { return static_cast<hyperedge>(m_prev); }
216
218 bool operator==(const hyperedge e) const {
219 return e->index() == m_index && e->hypergraph() == m_hypergraph;
220 }
221
222 friend OGDF_EXPORT std::ostream& operator<<(std::ostream& os, ogdf::hyperedge e);
223
225};
226
229 friend class Hypergraph;
230 friend class GraphListBase;
232
233public:
235 enum class Type {
236 normal = 0x0000001,
237 dummy = 0x0000002,
238 OR = 0x0000003,
239 BUF = 0x0000004,
240 AND = 0x0000005,
241 NOR = 0x0000006,
242 NOT = 0x0000007,
243 XOR = 0x0000008,
244 DFF = 0x0000009,
245 NAND = 0x0000010,
246 INPUT = 0x0000011,
247 OUTPUT = 0x0000012
248 };
249
250private:
253
256
259
262
265
267 explicit HypernodeElement(int pIndex)
268 : m_index(pIndex), m_degree(0), m_type(Type::normal), m_hypergraph(nullptr) { }
269
271 HypernodeElement(int pIndex, Type pType)
272 : m_index(pIndex), m_degree(0), m_type(pType), m_hypergraph(nullptr) { }
273
274public:
276 int index() const { return m_index; }
277
279 int degree() const { return m_degree; }
280
282 Hypergraph* hypergraph() const { return m_hypergraph; }
283
285 Type type() const { return m_type; }
286
288 void type(Type pType) { m_type = pType; }
289
291 adjHypergraphEntry firstAdj() const { return m_adjHyperedges.head(); }
292
294 adjHypergraphEntry lastAdj() const { return m_adjHyperedges.tail(); }
295
297 template<class NODELIST>
298 void allHyperedges(NODELIST& hyperedges) const {
299 hyperedges.clear();
300 for (adjHypergraphEntry adj = firstAdj(); adj; adj = adj->succ()) {
301 hyperedges.pushBack(reinterpret_cast<hyperedge>(adj->element()));
302 }
303 }
304
306 bool adjacent(hypernode v) const {
307 for (adjHypergraphEntry adj = firstAdj(); adj; adj = adj->succ()) {
308 if (reinterpret_cast<hyperedge>(adj->element())->incident(v)) {
309 return true;
310 }
311 }
312 return false;
313 }
314
316 hypernode succ() const { return static_cast<hypernode>(m_next); }
317
319 hypernode pred() const { return static_cast<hypernode>(m_prev); }
320
322 bool operator==(const hypernode v) const {
323 return v->index() == m_index && v->hypergraph() == m_hypergraph;
324 }
325
327
328 friend OGDF_EXPORT std::ostream& operator<<(std::ostream& os, ogdf::hypernode v);
329};
330
332template<typename Key>
334 : public RegistryBase<Key*, HypergraphRegistry<Key>, internal::GraphIterator<Key*>> {
337
338public:
340
342 HypergraphRegistry(Hypergraph* graph, int* nextKeyIndex)
343 : m_pGraph(graph), m_nextKeyIndex(nextKeyIndex) { }
344
345 static inline int keyToIndex(Key* key) { return key->index(); }
346
347 bool isKeyAssociated(Key* key) const {
348 if (key == nullptr) {
349 return false;
350 }
351#ifdef OGDF_DEBUG
352 if (key->hypergraph() == m_pGraph) {
353 OGDF_ASSERT(keyToIndex(key) < this->getArraySize());
354 return true;
355 } else {
356 return false;
357 }
358#else
359 return true;
360#endif
361 }
362
363 int calculateArraySize(int add) const { return calculateTableSize(*m_nextKeyIndex + add); }
364
365 int maxKeyIndex() const { return (*m_nextKeyIndex) - 1; }
366
368 Hypergraph* graphOf() const { return m_pGraph; }
369};
370
373
376
379
382
384template<typename Key, typename Value, bool WithDefault, typename Registry = HypergraphRegistry<Key>>
385class HypergraphRegisteredArray : public RegisteredArray<Registry, Value, WithDefault, Hypergraph> {
387
388public:
389 using RA::RA;
390
393 if (RA::registeredAt() == nullptr) {
394 return nullptr;
395 } else {
396 return RA::registeredAt()->graphOf();
397 }
398 }
399};
400
401#define OGDF_DECL_REG_ARRAY_TYPE(v, c) HypergraphRegisteredArray<HypernodeElement, v, c>
404#undef OGDF_DECL_REG_ARRAY_TYPE
405
406#define OGDF_DECL_REG_ARRAY_TYPE(v, c) HypergraphRegisteredArray<HyperedgeElement, v, c>
409#undef OGDF_DECL_REG_ARRAY_TYPE
410
412
413class OGDF_EXPORT Hypergraph : public Observable<HypergraphObserver, Hypergraph> {
416
419
422
425
428
431
434
437
438public:
441
444
447
449 bool empty() const { return m_nHypernodes == 0; }
450
452 const internal::GraphList<HypernodeElement>& hypernodes() const { return m_hypernodes; }
453
455 const internal::GraphList<HyperedgeElement>& hyperedges() const { return m_hyperedges; }
456
458 int numberOfHypernodes() const { return m_nHypernodes; }
459
461 int numberOfHyperedges() const { return m_nHyperedges; }
462
464 int maxHypernodeIndex() const { return m_hypernodeIdCount - 1; }
465
467 int maxHyperedgeIndex() const { return m_hyperedgeIdCount - 1; }
468
470 hypernode firstHypernode() const { return m_hypernodes.head(); }
471
473 hypernode lastHypernode() const { return m_hypernodes.tail(); }
474
476 hyperedge firstHyperedge() const { return m_hyperedges.head(); }
477
479 hyperedge lastHyperEdge() const { return m_hyperedges.tail(); }
480
483
486
489
492
494
499
501
506 hyperedge newHyperedge(int pIndex, List<hypernode>& hypernodes);
507
509
513
515
519
521 void clear();
522
525
528
530 template<class LIST>
531 void allHypernodes(LIST& hypernodeList) const {
532 hypernodeList.clear();
533 for (hypernode v = m_hypernodes.head(); v; v = v->succ()) {
534 hypernodeList.pushBack(v);
535 }
536 }
537
539 template<class LIST>
540 void allHyperedges(LIST& hyperedgeList) const {
541 hyperedgeList.clear();
542 for (hyperedge e = m_hyperedges.head(); e; e = e->succ()) {
543 hyperedgeList.pushBack(e);
544 }
545 }
546
548 void readBenchHypergraph(std::istream& is);
549
551 void readBenchHypergraph(const char* filename);
552
554 void readPlaHypergraph(std::istream& is);
555
557 void loadPlaHypergraph(const char* fileName);
558
560 bool consistency() const;
561
563 HypergraphRegistry<HypernodeElement>& hypernodeRegistry() { return m_regHypernodeArrays; }
564
567 return m_regHypernodeArrays;
568 }
569
570 operator const HypergraphRegistry<HypernodeElement>&() const { return m_regHypernodeArrays; }
571
573 HypergraphRegistry<HyperedgeElement>& hyperedgeRegistry() { return m_regHyperedgeArrays; }
574
577 return m_regHyperedgeArrays;
578 }
579
580 operator const HypergraphRegistry<HyperedgeElement>&() const { return m_regHyperedgeArrays; }
581
583
584 friend OGDF_EXPORT std::ostream& operator<<(std::ostream& os, ogdf::Hypergraph& H);
585
586 friend OGDF_EXPORT std::istream& operator>>(std::istream& is, ogdf::Hypergraph& H);
587
589
590private:
592
593 int nextEntry(char* buffer, int from, string stop);
594
596};
597
598}
Decralation of GraphElement and GraphList classes.
Simple, safe base classes for C++ observables and observers.
Declaration and implementation of RegisteredArray class.
#define OGDF_DECL_REG_ARRAY(NAME)
Basic declarations, included by all source files.
Class for adjacency list elements.
Definition Hypergraph.h:86
GraphElement * m_element
The associated hyperedge or hypernode.
Definition Hypergraph.h:93
adjHypergraphEntry cyclicSucc() const
Returns the cyclic successor in the adjacency list.
adjHypergraphEntry succ() const
Returns the successor in the adjacency list.
Definition Hypergraph.h:131
int index() const
Returns the index of this adjacency element.
Definition Hypergraph.h:117
adjHypergraphEntry twin() const
Returns the pointer to a twin adjacency list.
Definition Hypergraph.h:128
adjHypergraphEntry m_twin
The corresponding adjacency entry.
Definition Hypergraph.h:102
int m_index
The (unique) index of the adjacency entry.
Definition Hypergraph.h:105
adjHypergraphEntry pred() const
Returns the predecessor in the adjacency list.
Definition Hypergraph.h:134
friend std::ostream & operator<<(std::ostream &os, ogdf::adjHypergraphEntry v)
adjHypergraphEntry cyclicPred() const
Returns the cyclic predecessor in the adjacency list.
AdjHypergraphElement(GraphElement *pElement, int pIndex)
Constructs an adjacency entry for a given hyper{node,edge} and index.
Definition Hypergraph.h:112
GraphElement * element() const
Returns the element associated with this adjacency entry.
Definition Hypergraph.h:125
AdjHypergraphElement(GraphElement *pElement)
Constructs an adjacency element for a given hyper{node,edge}.
Definition Hypergraph.h:108
Class for the representation of hyperedges.
Definition Hypergraph.h:148
internal::GraphList< AdjHypergraphElement > m_adjHypernodes
The adjacency list of the hyperedge.
Definition Hypergraph.h:155
void allHypernodes(NODELIST &hypernodes) const
Returns a list with all incident hypernodes of the hyperedge.
Definition Hypergraph.h:194
Hypergraph * hypergraph() const
Returns the hypergraph containing the hyperedge.
Definition Hypergraph.h:181
adjHypergraphEntry lastAdj() const
Returns the last entry in the adjacency list.
Definition Hypergraph.h:187
int cardinality() const
Returns the number of incident hypernodes.
Definition Hypergraph.h:178
hyperedge pred() const
Returns the predecessor in the list of all hyperedges.
Definition Hypergraph.h:215
int m_cardinality
The number of incidend hypernodes.
Definition Hypergraph.h:161
bool incident(hypernode v) const
Returns true iff v is incident to the hyperedge.
Definition Hypergraph.h:202
friend std::ostream & operator<<(std::ostream &os, ogdf::hyperedge e)
internal::GraphList< AdjHypergraphElement > incidentHypernodes() const
Returns the incident hypernodes of the hyperedge.
Definition Hypergraph.h:190
hyperedge succ() const
Returns the successor in the list of all hyperedges.
Definition Hypergraph.h:212
Hypergraph * m_hypergraph
The hypergraph containing the hyperedge (if any).
Definition Hypergraph.h:164
adjHypergraphEntry firstAdj() const
Returns the first entry in the adjaceny list.
Definition Hypergraph.h:184
bool operator==(const hyperedge e) const
Equality operator.
Definition Hypergraph.h:218
int index() const
Returns the index of a hyperedge.
Definition Hypergraph.h:175
int m_index
The (unique) index of the hyperedge.
Definition Hypergraph.h:158
HyperedgeElement(int pIndex)
Constructs an hyperedge element between hypernodes.
Definition Hypergraph.h:170
~Hypergraph()
Destructor.
void delHyperedge(hyperedge e)
Removes hyperedge e from the hypergraph.
void loadPlaHypergraph(const char *fileName)
Reads hypergraph in pla format from the file.
hyperedge lastHyperEdge() const
Returns the last hyperedge in the list of all hyperedges.
Definition Hypergraph.h:479
hyperedge randomHyperedge() const
Returns a randomly chosen hyperedge.
void clear()
Removes all hypernodes and all hyperedges from the hypergraph.
const internal::GraphList< HypernodeElement > & hypernodes() const
Returns the list of all hypernodes.
Definition Hypergraph.h:452
hyperedge firstHyperedge() const
Returns the first hyperedge in the list of all hyperedges.
Definition Hypergraph.h:476
int maxHyperedgeIndex() const
Returns the largest used hyperedge index.
Definition Hypergraph.h:467
hypernode newHypernode(int pIndex, HypernodeElement::Type pType)
Creates a new hypernode with given pIndex and pType and returns it.
int m_hyperedgeIdCount
The Index that will be assigned to the next created hyperedge.
Definition Hypergraph.h:436
HypergraphRegistry< HypernodeElement > m_regHypernodeArrays
The registered hypernode arrays.
Definition Hypergraph.h:415
int m_hypernodeIdCount
The Index that will be assigned to the next created hypernode.
Definition Hypergraph.h:433
HypergraphRegistry< HyperedgeElement > m_regHyperedgeArrays
The registered hyperedge arrays.
Definition Hypergraph.h:418
internal::GraphList< HyperedgeElement > m_hyperedges
The list of all hyperedges.
Definition Hypergraph.h:424
int m_nHypernodes
The number of hypernodes in the hypergraph.
Definition Hypergraph.h:427
void allHyperedges(LIST &hyperedgeList) const
Returns a list with all hyperedges of the hypergraph.
Definition Hypergraph.h:540
bool empty() const
Returns true iff the hypergraph is empty (ie. contains no hypernodes).
Definition Hypergraph.h:449
hypernode newHypernode(HypernodeElement::Type pType)
Creates a new hypernode with given pType and returns it.
int numberOfHyperedges() const
Returns the number of hyperedges in the hypergraph.
Definition Hypergraph.h:461
internal::GraphList< HypernodeElement > m_hypernodes
The list of all hypernodes.
Definition Hypergraph.h:421
friend std::istream & operator>>(std::istream &is, ogdf::Hypergraph &H)
const HypergraphRegistry< HypernodeElement > & hypernodeRegistry() const
Returns a const reference to the registry of hypernode arrays associated with this hypergraph.
Definition Hypergraph.h:566
void readBenchHypergraph(std::istream &is)
Reads hypergraph in bench format from the input stream.
bool consistency() const
Checks the consistency of the data structure.
Hypergraph & operator=(const Hypergraph &H)
friend std::ostream & operator<<(std::ostream &os, ogdf::Hypergraph &H)
Hypergraph(const Hypergraph &H)
Constructs a hypergraph that is a copy of H.
hypernode lastHypernode() const
Returns the last hypernode in the list of all hypernodes.
Definition Hypergraph.h:473
int nextEntry(char *buffer, int from, string stop)
void allHypernodes(LIST &hypernodeList) const
Returns a list with all hypernodes of the hypergraph.
Definition Hypergraph.h:531
hypernode newHypernode()
Creates a new hypernode and returns it.
int maxHypernodeIndex() const
Returns the largest used hypernode index.
Definition Hypergraph.h:464
int numberOfHypernodes() const
Returns the number of hypernodes in the hypergraph.
Definition Hypergraph.h:458
HypernodeElement::Type gateType(string gate)
hyperedge newHyperedge(int pIndex, List< hypernode > &hypernodes)
Creates a new hyperedge between hypernodes and returns it.
void readPlaHypergraph(std::istream &is)
Reads hypergraph in pla format from the input stream.
void readBenchHypergraph(const char *filename)
Reads hypergraph in bench format from the file.
const HypergraphRegistry< HyperedgeElement > & hyperedgeRegistry() const
Returns a const reference to the registry of hyperedge arrays associated with this hypergraph.
Definition Hypergraph.h:576
hypernode firstHypernode() const
Returns the first hypernode in the list of all hypernodes.
Definition Hypergraph.h:470
const internal::GraphList< HyperedgeElement > & hyperedges() const
Returns the list of all hyperedges.
Definition Hypergraph.h:455
HypergraphRegistry< HypernodeElement > & hypernodeRegistry()
Returns a reference to the registry of hypernode arrays associated with this hypergraph.
Definition Hypergraph.h:563
Hypergraph()
Constructs an empty hypergraph.
void delHypernode(hypernode v)
Removes hypernode v and all incident hyperedges from the hypergraph.
HypergraphRegistry< HyperedgeElement > & hyperedgeRegistry()
Returns a reference to the registry of hyperedge arrays associated with this hypergraph.
Definition Hypergraph.h:573
hyperedge newHyperedge(List< hypernode > &hypernodes)
Creates a new hyperedge btween hypernodes and returns it.
hypernode randomHypernode() const
Returns a randomly chosen hypergraph.
hypernode newHypernode(int pIndex)
Creates a new hypernode with given pIndex and returns it.
int m_nHyperedges
The number of hyperedges in the hypergraph.
Definition Hypergraph.h:430
RegisteredArray for nodes and edges of a hypergraph.
Definition Hypergraph.h:385
Hypergraph * hypergraphOf() const
Returns a pointer to the associated hypergraph.
Definition Hypergraph.h:392
Registry for nodes and edges of a hypergraph.
Definition Hypergraph.h:334
HypergraphRegistry(Hypergraph *graph, int *nextKeyIndex)
Constructor.
Definition Hypergraph.h:342
Hypergraph * graphOf() const
Returns a pointer to the associated hypergraph.
Definition Hypergraph.h:368
bool isKeyAssociated(Key *key) const
Definition Hypergraph.h:347
int calculateArraySize(int add) const
Definition Hypergraph.h:363
static int keyToIndex(Key *key)
Definition Hypergraph.h:345
Class for the representation of hypernodes.
Definition Hypergraph.h:228
HypernodeElement(int pIndex, Type pType)
Constructor.
Definition Hypergraph.h:271
int index() const
Returns the (unique) hypernode index.
Definition Hypergraph.h:276
void type(Type pType)
Sets the type of hypernode.
Definition Hypergraph.h:288
int m_degree
The number of incident hyperedges.
Definition Hypergraph.h:258
int m_index
The (unique) index of the hypernode.
Definition Hypergraph.h:255
hypernode pred() const
Returns the predecessor in the list of all hypernodes.
Definition Hypergraph.h:319
hypernode succ() const
Returns the successor in the list of all hypernodes.
Definition Hypergraph.h:316
Type m_type
The type of the hypernode.
Definition Hypergraph.h:261
adjHypergraphEntry lastAdj() const
Returns the last entry in the adjacency list.
Definition Hypergraph.h:294
HypernodeElement(int pIndex)
Constructor.
Definition Hypergraph.h:267
adjHypergraphEntry firstAdj() const
Returns the first entry in the adjaceny list.
Definition Hypergraph.h:291
Hypergraph * hypergraph() const
Returns the hypergraph containing the hypernode.
Definition Hypergraph.h:282
void allHyperedges(NODELIST &hyperedges) const
Returns a list with all incident hyperedges of the hypernode.
Definition Hypergraph.h:298
Type type() const
Returns the type of hypernode.
Definition Hypergraph.h:285
friend std::ostream & operator<<(std::ostream &os, ogdf::hypernode v)
bool adjacent(hypernode v) const
Returns true iff v is adjacent to the hypernode.
Definition Hypergraph.h:306
internal::GraphList< AdjHypergraphElement > m_adjHyperedges
The adjacency list of the hypernode.
Definition Hypergraph.h:252
int degree() const
Returns the hypernode degree.
Definition Hypergraph.h:279
Hypergraph * m_hypergraph
The hypergraph containing the hypernode (if any).
Definition Hypergraph.h:264
Type
The type of hypernodes.
Definition Hypergraph.h:235
bool operator==(const hypernode v) const
Equality operator.
Definition Hypergraph.h:322
Doubly linked lists (maintaining the length of the list).
Definition List.h:1451
Base class for an observable object that can be tracked by multiple Observer objects.
Definition Observer.h:127
Dynamic arrays indexed with arbitrary keys.
typename std::conditional< WithDefault, internal::RegisteredArrayWithDefault< Registry, Value >, internal::RegisteredArrayWithoutDefault< Registry, Value > >::type RA
Abstract base class for registries.
int getArraySize() const
Returns the current size of all registered arrays.
The base class for objects used by (hyper)graphs.
Definition GraphList.h:60
Lists of graph objects (like nodes, edges, etc.).
Definition GraphList.h:301
T * head() const
Returns the first element in the list.
Definition GraphList.h:324
T * tail() const
Returns the last element in the list.
Definition GraphList.h:327
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF dynamic library (shared object / DLL),...
Definition config.h:117
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition basic.h:52
Declaration of memory manager for allocating small pieces of memory.
The namespace for all OGDF objects.
HypergraphRegistry< HypernodeElement >::iterator begin(const HypergraphRegistry< HypernodeElement > &self)
int calculateTableSize(int actualCount)
The default growth function for registered arrays.
HypergraphRegistry< HypernodeElement >::iterator end(const HypergraphRegistry< HypernodeElement > &self)