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/Graph.h>
35 #include <ogdf/basic/NodeSet.h>
37 
38 namespace ogdf {
39 
45 public:
49  using NodeColor = unsigned int;
50 
54  enum class SearchProcedure {
55  linearSearch,
56  binarySearch,
57  wigdersonSearch
58  };
59 
64  enum class RamseyProcedure {
65  smallestIndex,
66  smallestDegree,
67  largestDegree,
68  extremalDegree,
69  };
70 
75  NodeColoringModule() : m_ramseyProcedure(RamseyProcedure::smallestIndex) { }
76 
85  virtual NodeColor call(const Graph& graph, NodeArray<NodeColor>& colors, NodeColor start = 0) = 0;
86 
90  virtual ~NodeColoringModule() { }
91 
99  virtual bool checkColoring(const Graph& graph, const NodeArray<NodeColor>& colors) const;
100 
107  virtual void preprocessGraph(Graph& graph) const { makeSimpleUndirected(graph); }
108 
109 protected:
112 
116  struct SubsetIterator {
120  using Index = int;
121 
125 
133  SubsetIterator(Array<node>& set, Index subsetSize)
134  : m_set(set), m_indices(subsetSize), m_numElements(subsetSize) {
135  OGDF_ASSERT(subsetSize <= set.size());
136  for (Index i = 0; i < m_numElements; i++) {
137  m_indices[i] = i;
138  }
139  }
140 
146  bool isOk() {
147  for (Index i = 0; i < m_numElements; i++) {
148  if (m_indices[i] > (m_set.size() - m_numElements + i)) {
149  return false;
150  }
151  }
152  return true;
153  }
154 
161  List<node> result;
162  for (Index i = 0; i < m_numElements; i++) {
163  result.emplaceBack(m_set[m_indices[i]]);
164  }
165  return result;
166  }
167 
174  bool advance() {
175  for (Index i = m_numElements - 1; i >= 0; i--) {
176  m_indices[i]++;
177  if (m_indices[i] <= (m_set.size() + i - m_numElements)) {
178  for (int j = i + 1; j < m_numElements; j++) {
179  m_indices[j] = m_indices[i] + j - i;
180  }
181  return true;
182  }
183  }
184  return false;
185  }
186  };
187 
192  struct SearchWrapper {
200  virtual bool step(int k) = 0;
201  };
202 
211  template<class CONTAINER>
212  bool checkIndependentSet(const Graph& graph, const CONTAINER& nodes) const {
213  NodeSet<false> nodeSet(graph);
214  for (node v : nodes) {
215  nodeSet.insert(v);
216  }
217  for (node v : nodes) {
218  for (adjEntry adj : v->adjEntries) {
219  if (nodeSet.isMember(adj->twinNode()) && adj->twinNode() != v) {
220  return false;
221  }
222  }
223  }
224  return true;
225  }
226 
235  virtual void createBuckets(const Graph& graph, int size, Array<Array<node>>& buckets) const;
236 
246  template<class LISTITERATOR>
247  void getNeighbors(const Graph& graph, LISTITERATOR nodes, List<node>& neighbors) const {
248  neighbors.clear();
249  NodeArray<bool> isInserted(graph, false);
250 
251  for (LISTITERATOR its = nodes; its.valid(); its++) {
252  node v = (*its);
253  OGDF_ASSERT(v != nullptr);
254  OGDF_ASSERT(v->graphOf() == &graph);
255 
256  for (adjEntry adj : v->adjEntries) {
257  node twin = adj->twinNode();
258  if (!isInserted[twin]) {
259  neighbors.emplaceBack(twin);
260  isInserted[twin] = true;
261  }
262  }
263  }
264  }
265 
273  int getNeighborDegrees(const node& v) const;
274 
285  template<class LISTITERATOR>
286  void getNeighborsComplement(const Graph& graph, LISTITERATOR nodes,
287  List<node>& complementNeighbors) const {
288  // Preparations step
289  complementNeighbors.clear();
290  NodeArray<bool> isComplement(graph, true);
291 
292  // Mark all given nodes as false
293  for (LISTITERATOR its = nodes; its.valid(); its++) {
294  node v = (*its);
295  OGDF_ASSERT(v != nullptr);
296  OGDF_ASSERT(v->graphOf() == &graph);
297  isComplement[v] = false;
298  }
299 
300  // Mark all neighbors of the given nodes as false
301  List<node> neighbors;
302  getNeighbors<LISTITERATOR>(graph, nodes, neighbors);
303  for (node& v : neighbors) {
304  isComplement[v] = false;
305  }
306 
307  // Store all nodes which are part of the complement
308  for (node v : graph.nodes) {
309  if (isComplement[v]) {
310  complementNeighbors.emplaceBack(v);
311  }
312  }
313  }
314 
324  template<class LISTITERATOR>
325  void mergeNodeLists(const Graph& graph, LISTITERATOR firstList, LISTITERATOR secondList,
326  List<node>& mergedList) const {
327  mergedList.clear();
328  NodeArray<bool> isInserted(graph, false);
329  List<LISTITERATOR> iterators = {firstList, secondList};
330  for (auto& iterator : iterators) {
331  for (LISTITERATOR its = iterator; its.valid(); its++) {
332  node v = (*its);
333  OGDF_ASSERT(v != nullptr);
334  OGDF_ASSERT(v->graphOf() == &graph);
335  if (!isInserted[v]) {
336  mergedList.emplaceBack(v);
337  isInserted[v] = true;
338  }
339  }
340  }
341  }
342 
350  virtual int getMaximumDegreeNode(const Graph& graph, node& maxDegreeNode) const;
351 
359  virtual int getMaximumDegreeNodes(const Graph& graph, List<node>& maxDegreeNodes) const;
360 
368  virtual int getMinimumDegreeNode(const Graph& graph, node& minDegreeNode) const;
369 
377  virtual int getMinimumDegreeNodes(const Graph& graph, List<node>& minDegreeNodes) const;
378 
384  virtual NodeColor getMaximumNodeColor(NodeArray<NodeColor>& colors);
385 
395  virtual void reverseNodeTable(const Graph& graphOrig, const Graph& graphNew,
396  const NodeArray<node>& orig2New, NodeArray<node>& new2Orig) const;
397 
406  virtual void ramseyAlgorithm(const Graph& graph, List<node>& clique,
407  List<node>& independentSet) const;
408 
416  virtual void cliqueRemoval(const Graph& graph, List<node>& independentSet) const;
417 
426  int searchLinear(SearchWrapper* searchWrapper, int start, int end) const;
427 
436  int searchBinary(SearchWrapper* searchWrapper, int start, int end) const;
437 
444  int searchWigderson(SearchWrapper* searchWrapper) const;
445 };
446 }
NodeSet.h
Declaration and implementation of ogdf::NodeSet.
ogdf
The namespace for all OGDF objects.
Definition: AugmentationModule.h:36
ogdf::NodeColoringModule::SubsetIterator::currentSubset
List< node > currentSubset()
Returns the current subset.
Definition: NodeColoringModule.h:160
Graph.h
Includes declaration of graph class.
ogdf::NodeColoringModule::SubsetIterator::isOk
bool isOk()
Returns if the iterator, i.e.
Definition: NodeColoringModule.h:146
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:54
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:247
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:425
ogdf::AdjElement
Class for adjacency list elements.
Definition: Graph_d.h:135
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:133
ogdf::NodeColoringModule::NodeColoringModule
NodeColoringModule()
Default constructor.
Definition: NodeColoringModule.h:75
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:124
ogdf::Graph::nodes
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition: Graph_d.h:924
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:325
ogdf::NodeColoringModule::SubsetIterator::m_set
Array< node > & m_set
Definition: NodeColoringModule.h:122
ogdf::NodeColoringModule
Approximation algorithms for the node coloring problem in graphs.
Definition: NodeColoringModule.h:44
ogdf::NodeColoringModule::SubsetIterator::m_indices
Array< Index > m_indices
Definition: NodeColoringModule.h:123
ogdf::Array< node >
ogdf::RegisteredSet::insert
void insert(element_type v)
Inserts element v into this set.
Definition: RegisteredSet.h:84
ogdf::NodeElement::adjEntries
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition: Graph_d.h:264
ogdf::NodeColoringModule::preprocessGraph
virtual void preprocessGraph(Graph &graph) const
Preprocesses a graph so that a coloring algorithm can be applied.
Definition: NodeColoringModule.h:107
ogdf::List
Doubly linked lists (maintaining the length of the list).
Definition: List.h:42
ogdf::internal::GraphRegisteredArray
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition: Graph_d.h:651
ogdf::Graph
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:862
ogdf::RegisteredSet::isMember
bool isMember(element_type v) const
Returns true iff element v is contained in this set.
Definition: RegisteredSet.h:136
ogdf::NodeColoringModule::~NodeColoringModule
virtual ~NodeColoringModule()
Destructor.
Definition: NodeColoringModule.h:90
ogdf::AdjElement::twinNode
node twinNode() const
Returns the associated node of the corresponding adjacency entry (shorthand for twin()->theNode()).
Definition: Graph_d.h:168
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:286
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
ogdf::NodeColoringModule::NodeColor
unsigned int NodeColor
Data type of the node colors.
Definition: NodeColoringModule.h:49
ogdf::NodeColoringModule::SubsetIterator
Struct to iterate over all node subsets of a given size.
Definition: NodeColoringModule.h:116
ogdf::NodeElement::graphOf
const Graph * graphOf() const
Returns the graph containing this node (debug only).
Definition: Graph_d.h:337
ogdf::Array::size
INDEX size() const
Returns the size (number of elements) of the array.
Definition: Array.h:297
ogdf::NodeColoringModule::m_ramseyProcedure
RamseyProcedure m_ramseyProcedure
The RamseyProcedure to be used to select nodes.
Definition: NodeColoringModule.h:111
ogdf::NodeColoringModule::RamseyProcedure
RamseyProcedure
Declares the procedure of finding nodes in Ramsey's algorithm.
Definition: NodeColoringModule.h:64
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:212
ogdf::NodeColoringModule::SearchProcedure
SearchProcedure
Declares the search procedures.
Definition: NodeColoringModule.h:54
ogdf::NodeColoringModule::SubsetIterator::advance
bool advance()
Advances the iterator so that the next subset can be queried.
Definition: NodeColoringModule.h:174
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:233
simple_graph_alg.h
Declaration of simple graph algorithms.
ogdf::NodeColoringModule::SubsetIterator::Index
int Index
Data type for the indices.
Definition: NodeColoringModule.h:120
ogdf::List::emplaceBack
iterator emplaceBack(Args &&... args)
Adds a new element at the end of the list.
Definition: List.h:1544
ogdf::List::clear
void clear()
Removes all elements from the list.
Definition: List.h:1616
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:192