20 #ifndef __CXXGRAPH_PARTITION_H__
21 #define __CXXGRAPH_PARTITION_H__
26 #include <unordered_set>
28 #include "Utility/Typedef.hpp"
29 #include "PartitioningStats.hpp"
37 using T_EdgeSet = std::unordered_set<const Edge<T> *>;
38 namespace PARTITIONING
41 std::ostream &operator<<(std::ostream &o,
const Partition<T> &partition);
50 Partition(
unsigned int partitionId,
const T_EdgeSet<T> &edgeSet);
66 unsigned int partitionId = 0;
87 static unsigned int getMaxEdgesLoad(
const PartitionMap<T> &partitionMap);
97 static unsigned int getMinEdgesLoad(
const PartitionMap<T> &partitionMap);
106 template <
typename T>
107 static unsigned int getMaxNodesLoad(
const PartitionMap<T> &partitionMap);
116 template <
typename T>
117 static unsigned int getMinNodesLoad(
const PartitionMap<T> &partitionMap);
126 template <
typename T>
127 static unsigned int getNumberOfEdges(
const PartitionMap<T> &partitionMap);
136 template <
typename T>
137 static unsigned int getNumberOfNodes(
const PartitionMap<T> &partitionMap);
146 template <
typename T>
147 static unsigned int getNumberOfReplicatedEdges(
const PartitionMap<T> &partitionMap);
156 template <
typename T>
157 static unsigned int getNumberOfReplicatedNodes(
const PartitionMap<T> &partitionMap);
159 template <
typename T>
165 template <
typename T>
166 Partition<T>::Partition(
unsigned int partitionId) :
Graph<T>()
168 this->partitionId = partitionId;
171 template <
typename T>
172 Partition<T>::Partition(
const T_EdgeSet<T> &edgeSet) : Graph<T>(edgeSet)
177 template <
typename T>
178 Partition<T>::Partition(
unsigned int partitionId,
const T_EdgeSet<T> &edgeSet) : Graph<T>(edgeSet)
180 this->partitionId = partitionId;
183 template <
typename T>
189 template <
typename T>
192 this->partitionId = partitionId;
195 template <
typename T>
199 result.numberOfPartitions = partitionMap.size();
200 result.numberOfNodes = getNumberOfNodes(partitionMap);
201 result.numberOfEdges = getNumberOfEdges(partitionMap);
202 result.replicatedNodesCount = getNumberOfReplicatedNodes(partitionMap);
203 result.replicatedEdgesCount = getNumberOfReplicatedEdges(partitionMap);
204 result.maxEdgesLoad = getMaxEdgesLoad(partitionMap);
205 result.minEdgesLoad = getMinEdgesLoad(partitionMap);
206 result.maxNodesLoad = getMaxNodesLoad(partitionMap);
207 result.minNodesLoad = getMinNodesLoad(partitionMap);
208 result.edgesReplicationFactor =
static_cast<double>(result.replicatedEdgesCount / result.numberOfEdges);
209 result.nodesReplicationFactor =
static_cast<double>(result.replicatedNodesCount / result.numberOfNodes);
210 result.balanceEdgesFactor =
static_cast<double>((result.maxEdgesLoad - result.minEdgesLoad) / (result.maxEdgesLoad));
211 result.balanceNodesFactor =
static_cast<double>((result.maxNodesLoad - result.minNodesLoad) / (result.maxNodesLoad));
215 template <
typename T>
216 unsigned int getMaxEdgesLoad(
const PartitionMap<T> &partitionMap)
218 unsigned int maxLoad = 0;
219 for (
const auto& it : partitionMap)
221 if (it.second->getEdgeSet().size() > maxLoad)
223 maxLoad = it.second->getEdgeSet().size();
229 template <
typename T>
230 unsigned int getMinEdgesLoad(
const PartitionMap<T> &partitionMap)
232 unsigned int minLoad = std::numeric_limits<unsigned int>::max();
233 for (
const auto& it : partitionMap)
235 if (it.second->getEdgeSet().size() < minLoad)
237 minLoad = it.second->getEdgeSet().size();
243 template <
typename T>
244 unsigned int getMaxNodesLoad(
const PartitionMap<T> &partitionMap)
246 unsigned int maxLoad = 0;
247 for (
const auto& it : partitionMap)
249 if (it.second->getNodeSet().size() > maxLoad)
251 maxLoad = it.second->getNodeSet().size();
257 template <
typename T>
258 unsigned int getMinNodesLoad(
const PartitionMap<T> &partitionMap)
260 unsigned int minLoad = std::numeric_limits<unsigned int>::max();
261 for (
const auto& it : partitionMap)
263 if (it.second->getNodeSet().size() < minLoad)
265 minLoad = it.second->getNodeSet().size();
271 template <
typename T>
272 unsigned int getNumberOfEdges(
const PartitionMap<T> &partitionMap)
274 unsigned int numberOfEdges = 0;
275 T_EdgeSet<T> edgeSet;
277 for (
const auto& it : partitionMap)
279 const T_EdgeSet<T> partitionEdgeSet = it.second->getEdgeSet();
280 for (
const auto& it2 : partitionEdgeSet)
282 if (std::find_if(edgeSet.begin(), edgeSet.end(), [it2](
const Edge<T> *edge)
283 { return (*it2 == *edge); }) == edgeSet.end())
290 return edgeSet.size();
293 template <
typename T>
294 unsigned int getNumberOfNodes(
const PartitionMap<T> &partitionMap)
296 unsigned int numberOfNodes = 0;
297 std::deque<const Node<T> *> nodeSet;
299 for (
const auto& it : partitionMap)
301 const std::deque<const Node<T> *> partitionNodeSet = it->second->getNodeSet();
302 for (
const auto& it2 : partitionNodeSet)
304 if (std::find_if(nodeSet.begin(), nodeSet.end(), [it2](
const Node<T> *node)
305 { return (*it2 == *node); }) == nodeSet.end())
307 nodeSet.push_back(*it2);
312 return nodeSet.size();
315 template <
typename T>
316 unsigned int getNumberOfReplicatedEdges(
const PartitionMap<T> &partitionMap)
319 unsigned int numberOfEdges = 0;
320 for (
const auto& it : partitionMap)
322 numberOfEdges += it.second->getEdgeSet().size();
324 return numberOfEdges;
327 template <
typename T>
328 unsigned int getNumberOfReplicatedNodes(
const PartitionMap<T> &partitionMap)
330 unsigned int numberOfNodes = 0;
331 for (
const auto& it : partitionMap)
333 numberOfNodes += it.second->getNodeSet().size();
335 return numberOfNodes;
338 template <
typename T>
339 std::ostream &operator<<(std::ostream &os,
const Partition<T> &partition)
341 os <<
"Partition " << partition.getPartitionId() <<
":\n";
342 auto edgeList = partition.getEdgeSet();
343 for (
const auto& it : edgeList)
345 if(!(*it)->isDirected().has_value() && !(*it)->isWeighted().has_value()){
349 else if (((*it)->isDirected().has_value()&& (*it)->isDirected().value()) && ((*it)->isWeighted().has_value() && (*it)->isWeighted().value()))
351 os << dynamic_cast<const DirectedWeightedEdge<T> &>(*it) <<
"\n";
353 else if ((it->isDirected().has_value() && it->isDirected().value()) && !(it->isWeighted().has_value() && it->isWeighted().value()))
355 os << dynamic_cast<const DirectedEdge<T> &>(*it) <<
"\n";
357 else if (!(it->isDirected().has_value() && it->isDirected().value()) && (it->isWeighted().has_value() && it->isWeighted().value()))
359 os << dynamic_cast<const UndirectedWeightedEdge<T> &>(*it) <<
"\n";
361 else if (!(it->isDirected().has_value() && it->isDirected().value()) && !(it->isWeighted().has_value() && it->isWeighted().value()))
363 os << dynamic_cast<const UndirectedEdge<T> &>(*it) <<
"\n";
368 os <<
"Wrong Edge Class" <<
"\n";
Class that implement the Graph. ( This class is not Thread Safe )
Definition: Graph.hpp:91
Definition: Partition.hpp:45
void setPartitionId(unsigned int partitionId)
Set the Partition ID.
Definition: Partition.hpp:190
unsigned int getPartitionId() const
Get the Partition ID.
Definition: Partition.hpp:184
Definition: PartitioningStats.hpp:30