7 #include "imstkGraph.h" 8 #include "imstkLogger.h" 9 #include "imstkParallelUtils.h" 26 LOG(WARNING) <<
"Vertex id exceeds the graph size: cannot add edge!" << std::endl;
39 std::cout <<
"Graph: " <<
"\nTotal nodes: " <<
m_adjList.size() <<
"\nAdjacency:" << std::endl;
41 for (
size_t i = 0; i <
m_adjList.size(); i++)
43 std::cout <<
"\t[" << i <<
"] : ";
47 std::cout << v <<
" ";
49 std::cout << std::endl;
53 Graph::graphColorsType
56 return method == ColoringMethod::WelshPowell ?
61 Graph::graphColorsType
66 using ColorType =
unsigned short;
67 const ColorType INVALID = std::numeric_limits<unsigned short>::max();
69 std::vector<ColorType> colors(numNodes, INVALID);
72 std::vector<size_t> neighborCounts(numNodes);
73 ParallelUtils::parallelFor(numNodes,
76 neighborCounts[idx] =
m_adjList[idx].size();
79 std::vector<size_t> coloringOrder(numNodes);
80 std::iota(coloringOrder.begin(), coloringOrder.end(),
static_cast<size_t>(0));
83 tbb::parallel_sort(coloringOrder.begin(), coloringOrder.end(),
84 [&](
const size_t idx0,
const size_t idx1) {
85 return neighborCounts[idx0] > neighborCounts[idx1];
89 while (coloringOrder.size() > 0)
91 colors[coloringOrder[0]] = color;
94 for (
size_t i = 1; i < coloringOrder.size(); ++i)
96 const auto u = coloringOrder[i];
101 if (colors[v] == color)
118 for (
size_t readIdx = 1; readIdx < coloringOrder.size(); ++readIdx)
121 if (colors[coloringOrder[readIdx]] == INVALID)
123 coloringOrder[writeIdx++] = coloringOrder[readIdx];
126 coloringOrder.resize(writeIdx);
132 std::map<size_t, size_t> verticesPerColor;
133 std::cout <<
"Num. of nodes: " << numNodes <<
" | Num. of colors: " << color << std::endl;
134 for (
size_t i = 0; i < numNodes; ++i)
137 verticesPerColor[colors[i]]++;
139 std::cout << std::endl;
140 std::cout <<
"Vertices per color: " << std::endl;
141 for (
const auto& kv : verticesPerColor)
143 std::cout <<
"C: " << kv.first <<
" - " << kv.second << std::endl;
145 std::cout << std::endl;
148 return std::make_pair(colors, color);
151 std::pair<std::vector<unsigned short>,
unsigned short>
155 std::vector<unsigned short> colors(numNodes, std::numeric_limits<unsigned short>::max());
156 std::vector<bool> available(numNodes,
false);
159 unsigned short numColors = 0;
162 for (
size_t u = 1; u < numNodes; ++u)
168 if (colors[i] != std::numeric_limits<unsigned short>::max())
170 available[colors[i]] =
true;
176 for (cr = 0; cr < numNodes; cr++)
184 if (cr + 1 > numColors)
192 if (colors[i] != std::numeric_limits<unsigned short>::max())
194 available[colors[i]] =
false;
202 std::map<size_t, size_t> verticesPerColor;
203 std::cout <<
"Num. of nodes: " << numNodes <<
" | Num. of colors: " << numColors << std::endl;
204 for (
size_t i = 0; i < numNodes; ++i)
206 std::cout <<
"V " << i <<
"-C " << colors[i] <<
" | ";
207 verticesPerColor[colors[i]]++;
209 std::cout << std::endl;
210 std::cout <<
"Vertices per color: " << std::endl;
211 for (
const auto& kv : verticesPerColor)
213 std::cout <<
"C: " << kv.first <<
" - " << kv.second << std::endl;
215 std::cout << std::endl;
218 return std::make_pair(colors, numColors);
void print() const
print adjacency list representation of graph
void getEdges(const size_t v, edgeType &edges) const
Get edges surrounding a node.
graphColorsType doColoringWelshPowell(bool print=false) const
Colorize using Welsh-Powell algorithm and print the assignment of colors.
graphColorsType doColoring(ColoringMethod method=ColoringMethod::WelshPowell, bool print=false) const
Colorize using the given method and prints the assignment of colors.
void addEdge(const size_t v, const size_t w)
Add edge to the graph.
graphColorsType doColoringGreedy(bool print=false) const
Colorize using greedy algorithm and print the assignment of colors.
std::vector< edgeType > m_adjList
A array of std::vectors to represent adjacency list.