Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
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>
38#include <ogdf/basic/List.h>
39#include <ogdf/basic/basic.h>
41
42namespace ogdf {
43
49public:
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
113protected:
116
124 using Index = int;
125
129
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
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 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
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}
Declaration and implementation of Array class and Array algorithms.
Includes declaration of graph class.
Decralation of GraphElement and GraphList classes.
Declaration and implementation of NodeSet, EdgeSet, and AdjEntrySet classes.
Declaration of doubly linked lists and iterators.
Basic declarations, included by all source files.
Class for adjacency list elements.
Definition Graph_d.h:143
The parameterized class Array implements dynamic arrays of type E.
Definition Array.h:219
INDEX size() const
Returns the size (number of elements) of the array.
Definition Array.h:302
Data type for general directed graphs (adjacency list representation).
Definition Graph_d.h:866
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition Graph_d.h:929
Doubly linked lists (maintaining the length of the list).
Definition List.h:1451
iterator emplaceBack(Args &&... args)
Adds a new element at the end of the list.
Definition List.h:1554
void clear()
Removes all elements from the list.
Definition List.h:1626
Approximation algorithms for the node coloring problem in graphs.
virtual int getMaximumDegreeNode(const Graph &graph, node &maxDegreeNode) const
Searches for a maximum degree node in the graph.
unsigned int NodeColor
Data type of the node colors.
int searchBinary(SearchWrapper *searchWrapper, int start, int end) const
Performs a binary search in a specified range with a given oracle.
NodeColoringModule()
Default constructor.
virtual NodeColor getMaximumNodeColor(NodeArray< NodeColor > &colors)
Calculates the maximum node color index used in a certain node coloring.
virtual int getMinimumDegreeNodes(const Graph &graph, List< node > &minDegreeNodes) const
Searches for all nodes with minimum degree in the graph.
virtual NodeColor call(const Graph &graph, NodeArray< NodeColor > &colors, NodeColor start=0)=0
The actual algorithm call.
virtual ~NodeColoringModule()
Destructor.
SearchProcedure
Declares the search procedures.
RamseyProcedure m_ramseyProcedure
The RamseyProcedure to be used to select nodes.
virtual void ramseyAlgorithm(const Graph &graph, List< node > &clique, List< node > &independentSet) const
Performs the Ramsey algorithm for finding heuristically large cliques and independents sets in a grap...
virtual void preprocessGraph(Graph &graph) const
Preprocesses a graph so that a coloring algorithm can be applied.
void getNeighbors(const Graph &graph, LISTITERATOR nodes, List< node > &neighbors) const
Calculates the set of neighbors Y for a given set of nodes X.
RamseyProcedure
Declares the procedure of finding nodes in Ramsey's algorithm.
virtual void reverseNodeTable(const Graph &graphOrig, const Graph &graphNew, const NodeArray< node > &orig2New, NodeArray< node > &new2Orig) const
Reverses the mapping between the nodes sets of a graph and a subgraph.
bool checkIndependentSet(const Graph &graph, const CONTAINER &nodes) const
Checks if subgraph induced by the given nodes forms an independent set.
int getNeighborDegrees(const node &v) const
Calculates the sum of neighbor nodes degrees for a given node v.
virtual int getMaximumDegreeNodes(const Graph &graph, List< node > &maxDegreeNodes) const
Searches for all nodes with maximum degree in the graph.
int searchWigderson(SearchWrapper *searchWrapper) const
Performs a the search procedure specified by Wigderson.
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.
virtual void createBuckets(const Graph &graph, int size, Array< Array< node > > &buckets) const
Creates a partitioning of the node set into buckets of a given size.
virtual int getMinimumDegreeNode(const Graph &graph, node &minDegreeNode) const
Searches for a minimum degree node in the graph.
void mergeNodeLists(const Graph &graph, LISTITERATOR firstList, LISTITERATOR secondList, List< node > &mergedList) const
Merges two lists of nodes and deletes the duplicates.
int searchLinear(SearchWrapper *searchWrapper, int start, int end) const
Performs a linear search in a specified range with a given oracle.
virtual void cliqueRemoval(const Graph &graph, List< node > &independentSet) const
Applies the clique removal algorithm for finding a large independent set in a given graph.
virtual bool checkColoring(const Graph &graph, const NodeArray< NodeColor > &colors) const
Checks if the given node coloring is valid.
Class for the representation of nodes.
Definition Graph_d.h:241
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition Graph_d.h:272
const Graph * graphOf() const
Returns the graph containing this node (debug only).
Definition Graph_d.h:345
Node sets.
Definition GraphSets.h:54
bool isMember(element_type v) const
Returns true iff element v is contained in this set.
void insert(element_type v)
Inserts element v into this set.
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition Graph_d.h:659
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF dynamic library (shared object / DLL),...
Definition config.h:117
void makeSimpleUndirected(Graph &G)
Removes all self-loops and all but one edge of each bundle of undirected parallel edges.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition basic.h:52
The namespace for all OGDF objects.
Declaration of simple graph algorithms.
Wraps the search for the minimum parameter k so that the same code can be reused for all algorithms.
virtual bool step(int k)=0
Performs a step in the search procedure.
Struct to iterate over all node subsets of a given size.
List< node > currentSubset()
Returns the current subset.
SubsetIterator(Array< node > &set, Index subsetSize)
Creates the SubsetIterator with a given set to iterate and the size of the subsets.
bool isOk()
Returns if the iterator, i.e.
bool advance()
Advances the iterator so that the next subset can be queried.