Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

NodeColoringModule.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/Array.h>
35 #include <ogdf/basic/Graph.h>
36 #include <ogdf/basic/GraphList.h>
37 #include <ogdf/basic/GraphSets.h>
38 #include <ogdf/basic/List.h>
39 #include <ogdf/basic/basic.h>
41 
42 namespace ogdf {
43 
49 public:
53  using NodeColor = unsigned int;
54 
58  enum class SearchProcedure {
59  linearSearch,
60  binarySearch,
61  wigdersonSearch
62  };
63 
68  enum class RamseyProcedure {
69  smallestIndex,
70  smallestDegree,
71  largestDegree,
72  extremalDegree,
73  };
74 
79  NodeColoringModule() : m_ramseyProcedure(RamseyProcedure::smallestIndex) { }
80 
89  virtual NodeColor call(const Graph& graph, NodeArray<NodeColor>& colors, NodeColor start = 0) = 0;
90 
94  virtual ~NodeColoringModule() { }
95 
103  virtual bool checkColoring(const Graph& graph, const NodeArray<NodeColor>& colors) const;
104 
111  virtual void preprocessGraph(Graph& graph) const { makeSimpleUndirected(graph); }
112 
113 protected:
116 
120  struct SubsetIterator {
124  using Index = int;
125 
129 
137  SubsetIterator(Array<node>& set, Index subsetSize)
138  : m_set(set), m_indices(subsetSize), m_numElements(subsetSize) {
139  OGDF_ASSERT(subsetSize <= set.size());
140  for (Index i = 0; i < m_numElements; i++) {
141  m_indices[i] = i;
142  }
143  }
144 
150  bool isOk() {
151  for (Index i = 0; i < m_numElements; i++) {
152  if (m_indices[i] > (m_set.size() - m_numElements + i)) {
153  return false;
154  }
155  }
156  return true;
157  }
158 
165  List<node> result;
166  for (Index i = 0; i < m_numElements; i++) {
167  result.emplaceBack(m_set[m_indices[i]]);
168  }
169  return result;
170  }
171 
178  bool advance() {
179  for (Index i = m_numElements - 1; i >= 0; i--) {
180  m_indices[i]++;
181  if (m_indices[i] <= (m_set.size() + i - m_numElements)) {
182  for (int j = i + 1; j < m_numElements; j++) {
183  m_indices[j] = m_indices[i] + j - i;
184  }
185  return true;
186  }
187  }
188  return false;
189  }
190  };
191 
196  struct SearchWrapper {
204  virtual bool step(int k) = 0;
205  };
206 
215  template<class CONTAINER>
216  bool checkIndependentSet(const Graph& graph, const CONTAINER& nodes) const {
217  NodeSet<false> nodeSet(graph);
218  for (node v : nodes) {
219  nodeSet.insert(v);
220  }
221  for (node v : nodes) {
222  for (adjEntry adj : v->adjEntries) {
223  if (nodeSet.isMember(adj->twinNode()) && adj->twinNode() != v) {
224  return false;
225  }
226  }
227  }
228  return true;
229  }
230 
239  virtual void createBuckets(const Graph& graph, int size, Array<Array<node>>& buckets) const;
240 
250  template<class LISTITERATOR>
251  void getNeighbors(const Graph& graph, LISTITERATOR nodes, List<node>& neighbors) const {
252  neighbors.clear();
253  NodeArray<bool> isInserted(graph, false);
254 
255  for (LISTITERATOR its = nodes; its.valid(); its++) {
256  node v = (*its);
257  OGDF_ASSERT(v != nullptr);
258  OGDF_ASSERT(v->graphOf() == &graph);
259 
260  for (adjEntry adj : v->adjEntries) {
261  node twin = adj->twinNode();
262  if (!isInserted[twin]) {
263  neighbors.emplaceBack(twin);
264  isInserted[twin] = true;
265  }
266  }
267  }
268  }
269 
277  int getNeighborDegrees(const node& v) const;
278 
289  template<class LISTITERATOR>
290  void getNeighborsComplement(const Graph& graph, LISTITERATOR nodes,
291  List<node>& complementNeighbors) const {
292  // Preparations step
293  complementNeighbors.clear();
294  NodeArray<bool> isComplement(graph, true);
295 
296  // Mark all given nodes as false
297  for (LISTITERATOR its = nodes; its.valid(); its++) {
298  node v = (*its);
299  OGDF_ASSERT(v != nullptr);
300  OGDF_ASSERT(v->graphOf() == &graph);
301  isComplement[v] = false;
302  }
303 
304  // Mark all neighbors of the given nodes as false
305  List<node> neighbors;
306  getNeighbors<LISTITERATOR>(graph, nodes, neighbors);
307  for (node& v : neighbors) {
308  isComplement[v] = false;
309  }
310 
311  // Store all nodes which are part of the complement
312  for (node v : graph.nodes) {
313  if (isComplement[v]) {
314  complementNeighbors.emplaceBack(v);
315  }
316  }
317  }
318 
328  template<class LISTITERATOR>
329  void mergeNodeLists(const Graph& graph, LISTITERATOR firstList, LISTITERATOR secondList,
330  List<node>& mergedList) const {
331  mergedList.clear();
332  NodeArray<bool> isInserted(graph, false);
333  List<LISTITERATOR> iterators = {firstList, secondList};
334  for (auto& iterator : iterators) {
335  for (LISTITERATOR its = iterator; its.valid(); its++) {
336  node v = (*its);
337  OGDF_ASSERT(v != nullptr);
338  OGDF_ASSERT(v->graphOf() == &graph);
339  if (!isInserted[v]) {
340  mergedList.emplaceBack(v);
341  isInserted[v] = true;
342  }
343  }
344  }
345  }
346 
354  virtual int getMaximumDegreeNode(const Graph& graph, node& maxDegreeNode) const;
355 
363  virtual int getMaximumDegreeNodes(const Graph& graph, List<node>& maxDegreeNodes) const;
364 
372  virtual int getMinimumDegreeNode(const Graph& graph, node& minDegreeNode) const;
373 
381  virtual int getMinimumDegreeNodes(const Graph& graph, List<node>& minDegreeNodes) const;
382 
388  virtual NodeColor getMaximumNodeColor(NodeArray<NodeColor>& colors);
389 
399  virtual void reverseNodeTable(const Graph& graphOrig, const Graph& graphNew,
400  const NodeArray<node>& orig2New, NodeArray<node>& new2Orig) const;
401 
410  virtual void ramseyAlgorithm(const Graph& graph, List<node>& clique,
411  List<node>& independentSet) const;
412 
420  virtual void cliqueRemoval(const Graph& graph, List<node>& independentSet) const;
421 
430  int searchLinear(SearchWrapper* searchWrapper, int start, int end) const;
431 
440  int searchBinary(SearchWrapper* searchWrapper, int start, int end) const;
441 
448  int searchWigderson(SearchWrapper* searchWrapper) const;
449 };
450 }
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
ogdf::NodeColoringModule::SubsetIterator::currentSubset
List< node > currentSubset()
Returns the current subset.
Definition: NodeColoringModule.h:164
Graph.h
Includes declaration of graph class.
ogdf::NodeColoringModule::SubsetIterator::isOk
bool isOk()
Returns if the iterator, i.e.
Definition: NodeColoringModule.h:150
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:66
ogdf::NodeColoringModule::getNeighbors
void getNeighbors(const Graph &graph, LISTITERATOR nodes, List< node > &neighbors) const
Calculates the set of neighbors Y for a given set of nodes X.
Definition: NodeColoringModule.h:251
ogdf::NodeSet< false >
ogdf::makeSimpleUndirected
void makeSimpleUndirected(Graph &G)
Removes all self-loops and all but one edge of each bundle of undirected parallel edges.
Definition: simple_graph_alg.h:433
GraphSets.h
Declaration and implementation of NodeSet, EdgeSet, and AdjEntrySet classes.
ogdf::AdjElement
Class for adjacency list elements.
Definition: Graph_d.h:142
ogdf::NodeColoringModule::SubsetIterator::SubsetIterator
SubsetIterator(Array< node > &set, Index subsetSize)
Creates the SubsetIterator with a given set to iterate and the size of the subsets.
Definition: NodeColoringModule.h:137
ogdf::NodeColoringModule::NodeColoringModule
NodeColoringModule()
Default constructor.
Definition: NodeColoringModule.h:79
ogdf::colors
const std::array< Color, 63 > colors
An array of 63 different colors to cycle through.
ogdf::NodeColoringModule::SubsetIterator::m_numElements
Index m_numElements
Definition: NodeColoringModule.h:128
ogdf::Graph::nodes
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition: Graph_d.h:932
ogdf::NodeColoringModule::mergeNodeLists
void mergeNodeLists(const Graph &graph, LISTITERATOR firstList, LISTITERATOR secondList, List< node > &mergedList) const
Merges two lists of nodes and deletes the duplicates.
Definition: NodeColoringModule.h:329
ogdf::NodeColoringModule::SubsetIterator::m_set
Array< node > & m_set
Definition: NodeColoringModule.h:126
ogdf::NodeColoringModule
Approximation algorithms for the node coloring problem in graphs.
Definition: NodeColoringModule.h:48
ogdf::NodeColoringModule::SubsetIterator::m_indices
Array< Index > m_indices
Definition: NodeColoringModule.h:127
ogdf::Array< node >
GraphList.h
Decralation of GraphElement and GraphList classes.
ogdf::RegisteredSet::insert
void insert(element_type v)
Inserts element v into this set.
Definition: RegisteredSet.h:86
ogdf::NodeElement::adjEntries
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition: Graph_d.h:271
ogdf::NodeColoringModule::preprocessGraph
virtual void preprocessGraph(Graph &graph) const
Preprocesses a graph so that a coloring algorithm can be applied.
Definition: NodeColoringModule.h:111
ogdf::List
Doubly linked lists (maintaining the length of the list).
Definition: DfsMakeBiconnected.h:40
ogdf::internal::GraphRegisteredArray
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition: Graph_d.h:658
ogdf::Graph
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:869
ogdf::RegisteredSet::isMember
bool isMember(element_type v) const
Returns true iff element v is contained in this set.
Definition: RegisteredSet.h:138
ogdf::NodeColoringModule::~NodeColoringModule
virtual ~NodeColoringModule()
Destructor.
Definition: NodeColoringModule.h:94
ogdf::AdjElement::twinNode
node twinNode() const
Returns the associated node of the corresponding adjacency entry (shorthand for twin()->theNode()).
Definition: Graph_d.h:175
ogdf::NodeColoringModule::getNeighborsComplement
void getNeighborsComplement(const Graph &graph, LISTITERATOR nodes, List< node > &complementNeighbors) const
Calculates the set of complement neighbors Y for a given set of nodes X.
Definition: NodeColoringModule.h:290
basic.h
Basic declarations, included by all source files.
ogdf::end
HypergraphRegistry< HypernodeElement >::iterator end(const HypergraphRegistry< HypernodeElement > &self)
OGDF_EXPORT
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF DLL.
Definition: config.h:101
Array.h
Declaration and implementation of Array class and Array algorithms.
ogdf::NodeColoringModule::NodeColor
unsigned int NodeColor
Data type of the node colors.
Definition: NodeColoringModule.h:53
ogdf::NodeColoringModule::SubsetIterator
Struct to iterate over all node subsets of a given size.
Definition: NodeColoringModule.h:120
List.h
Declaration of doubly linked lists and iterators.
ogdf::NodeElement::graphOf
const Graph * graphOf() const
Returns the graph containing this node (debug only).
Definition: Graph_d.h:344
ogdf::Array::size
INDEX size() const
Returns the size (number of elements) of the array.
Definition: Array.h:302
ogdf::NodeColoringModule::m_ramseyProcedure
RamseyProcedure m_ramseyProcedure
The RamseyProcedure to be used to select nodes.
Definition: NodeColoringModule.h:115
ogdf::NodeColoringModule::RamseyProcedure
RamseyProcedure
Declares the procedure of finding nodes in Ramsey's algorithm.
Definition: NodeColoringModule.h:68
ogdf::NodeColoringModule::checkIndependentSet
bool checkIndependentSet(const Graph &graph, const CONTAINER &nodes) const
Checks if subgraph induced by the given nodes forms an independent set.
Definition: NodeColoringModule.h:216
ogdf::NodeColoringModule::SearchProcedure
SearchProcedure
Declares the search procedures.
Definition: NodeColoringModule.h:58
ogdf::NodeColoringModule::SubsetIterator::advance
bool advance()
Advances the iterator so that the next subset can be queried.
Definition: NodeColoringModule.h:178
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:240
simple_graph_alg.h
Declaration of simple graph algorithms.
ogdf::NodeColoringModule::SubsetIterator::Index
int Index
Data type for the indices.
Definition: NodeColoringModule.h:124
ogdf::List::emplaceBack
iterator emplaceBack(Args &&... args)
Adds a new element at the end of the list.
Definition: List.h:1554
ogdf::List::clear
void clear()
Removes all elements from the list.
Definition: List.h:1626
ogdf::NodeColoringModule::SearchWrapper
Wraps the search for the minimum parameter k so that the same code can be reused for all algorithms.
Definition: NodeColoringModule.h:196