iMSTK
Interactive Medical Simulation Toolkit
imstkGraph.h
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #pragma once
8 
9 #include <cstdlib>
10 #include <unordered_set>
11 #include <vector>
12 
13 namespace imstk
14 {
18 class Graph
19 {
20 using edgeType = std::unordered_set<size_t>;
21 using graphColorsType = std::pair<std::vector<unsigned short>, unsigned short>;
22 public:
23  enum class ColoringMethod
24  {
25  Greedy,
26  WelshPowell
27  };
28 
29  Graph(const size_t size) { m_adjList.resize(size); }
30  virtual ~Graph() = default;
31 
35  void addEdge(const size_t v, const size_t w);
36 
40  void getEdges(const size_t v, edgeType& edges) const;
41 
45  size_t size() const { return m_adjList.size(); }
46 
50  void print() const;
51 
55  void setDefaultColoringMethod(ColoringMethod method) { m_ColoringMethod = method; }
56 
61  graphColorsType doColoring(ColoringMethod method = ColoringMethod::WelshPowell,
62  bool print = false) const;
63 
64 protected:
69  graphColorsType doColoringGreedy(bool print = false) const;
70 
75  graphColorsType doColoringWelshPowell(bool print = false) const;
76 
77  std::vector<edgeType> m_adjList;
78  ColoringMethod m_ColoringMethod = ColoringMethod::WelshPowell;
79 };
80 } // namespace imstk
void print() const
print adjacency list representation of graph
Definition: imstkGraph.cpp:37
Compound Geometry.
void getEdges(const size_t v, edgeType &edges) const
Get edges surrounding a node.
Definition: imstkGraph.cpp:31
graphColorsType doColoringWelshPowell(bool print=false) const
Colorize using Welsh-Powell algorithm and print the assignment of colors.
Definition: imstkGraph.cpp:62
void setDefaultColoringMethod(ColoringMethod method)
Set the default colorizing method.
Definition: imstkGraph.h:55
class to represent a graph object
Definition: imstkGraph.h:18
graphColorsType doColoring(ColoringMethod method=ColoringMethod::WelshPowell, bool print=false) const
Colorize using the given method and prints the assignment of colors.
Definition: imstkGraph.cpp:54
void addEdge(const size_t v, const size_t w)
Add edge to the graph.
Definition: imstkGraph.cpp:17
graphColorsType doColoringGreedy(bool print=false) const
Colorize using greedy algorithm and print the assignment of colors.
Definition: imstkGraph.cpp:152
std::vector< edgeType > m_adjList
A array of std::vectors to represent adjacency list.
Definition: imstkGraph.h:77
size_t size() const
Get size of the graph.
Definition: imstkGraph.h:45