29 #include "../../graph/core/Edge.h"
30 #include "../../graph/core/Vertex.h"
31 #include "../../graph/iterator/MemoryIterator.h"
62 int firstVertex = memIt->getFirstVertex()->getId();
65 std::vector< std::pair<int, double> > rootSet;
66 rootSet.push_back(std::vector< std::pair<int, double> >::value_type(firstVertex, 0.));
68 std::vector<std::size_t> rootsVertexId;
71 while(!rootSet.empty() && (rootSet.size() + rootsVertexId.size() < nGroups))
74 std::vector< std::pair<int, double> >::iterator rootIt;
75 std::vector< std::pair<int, double> >::iterator rootItMax;
76 double maxDiff = -std::numeric_limits<double>::max();
78 for(rootIt = rootSet.begin(); rootIt != rootSet.end(); ++rootIt)
80 if(rootIt->second > maxDiff)
82 maxDiff = rootIt->second;
87 std::size_t currentId = rootItMax->first;
89 rootSet.erase(rootItMax);
101 bool remove = edgeToRemove(currentId, diffA, diffB, edgeId);
108 int vertexTo = edge->
getIdTo();
111 m_graph->removeEdge(edgeId);
113 rootSet.push_back(std::vector< std::pair<int, double> >::value_type(vertexFrom, diffA));
114 rootSet.push_back(std::vector< std::pair<int, double> >::value_type(vertexTo, diffB));
118 rootsVertexId.push_back(currentId);
123 for(std::size_t t = 0; t < rootSet.size(); ++t)
125 rootsVertexId.push_back(rootSet[t].first);
128 return rootsVertexId;
141 int firstVertex = memIt->getFirstVertex()->getId();
144 std::vector< std::pair<int, double> > rootSet;
145 rootSet.push_back(std::vector< std::pair<int, double> >::value_type(firstVertex, 0.));
147 std::vector<std::size_t> rootsVertexId;
150 while(!rootSet.empty())
153 std::vector< std::pair<int, double> >::iterator rootIt;
154 std::vector< std::pair<int, double> >::iterator rootItMax;
155 double maxDiff = -std::numeric_limits<double>::max();
157 for(rootIt = rootSet.begin(); rootIt != rootSet.end(); ++rootIt)
159 if(rootIt->second > maxDiff)
161 maxDiff = rootIt->second;
166 std::size_t currentId = rootItMax->first;
168 rootSet.erase(rootItMax);
180 bool remove = edgeToRemove(currentId, diffA, diffB, edgeId);
187 int vertexTo = edge->
getIdTo();
190 m_graph->removeEdge(edgeId);
192 rootSet.push_back(std::vector< std::pair<int, double> >::value_type(vertexFrom, diffA));
193 rootSet.push_back(std::vector< std::pair<int, double> >::value_type(vertexTo, diffB));
197 rootsVertexId.push_back(currentId);
202 for(std::size_t t = 0; t < rootSet.size(); ++t)
204 rootsVertexId.push_back(rootSet[t].first);
207 return rootsVertexId;
213 std::vector<EdgeRemovalInfo> edgeRemovalVec;
216 std::size_t totalPop = 0;
217 std::vector<double> meanVecStart = calculateRootMean(startVertex, -1, totalPop);
218 double deviationStart = calculateRootDeviation(startVertex, -1, meanVecStart);
221 std::queue<int> queue;
222 std::set<int> visited;
224 queue.push(startVertex);
225 visited.insert(startVertex);
228 while(!queue.empty())
230 int currentId = queue.front();
240 std::set<int>::iterator itNeighbours = neighbours.begin();
242 while(itNeighbours != neighbours.end())
250 vTo = m_graph->getVertex(e->
getIdTo());
252 vTo = m_graph->getVertex(e->
getIdFrom());
258 if(visited.find(vTo->
getId()) == visited.end())
261 queue.push(vTo->
getId());
262 visited.insert(vTo->
getId());
265 double diffVFrom = 0.;
267 std::size_t popA = 0;
268 std::size_t popB = 0;
270 double ssdi = calculateEdgeDifference(vertex->
getId(), vTo->
getId(), diffVFrom, diffVTo, popA, popB);
272 double l = deviationStart - ssdi;
276 eri.
m_SSDT = deviationStart;
294 edgeRemovalVec.push_back(eri);
306 double maxDiff = -std::numeric_limits<double>::max();
308 for(std::size_t t = 0; t < edgeRemovalVec.size(); ++t)
310 if(edgeRemovalVec[t].m_l > maxDiff && edgeRemovalVec[t].m_popa >= m_popMin && edgeRemovalVec[t].m_popb >= m_popMin)
312 maxDiff = edgeRemovalVec[t].m_l;
314 edgeToRemoveId = edgeRemovalVec[t].m_edgeId;
316 diffA = edgeRemovalVec[t].m_SSDTa;
317 diffB = edgeRemovalVec[t].m_SSDTb;
324 m_SSDiValues.push_back(maxDiff);
326 edgeRemovalVec.clear();
334 std::vector<double> meanVecFrom = calculateRootMean(vertexFrom, vertexTo, popA);
335 diffA = calculateRootDeviation(vertexFrom, vertexTo, meanVecFrom);
338 std::vector<double> meanVecTo = calculateRootMean(vertexTo, vertexFrom, popB);
339 diffB = calculateRootDeviation(vertexTo, vertexFrom, meanVecTo);
342 return diffA + diffB;
347 std::vector<double> meanAttrs(m_attrs.size(), 0.);
349 std::queue<int> queue;
350 std::set<int> visited;
352 queue.push(startVertex);
353 visited.insert(startVertex);
355 std::size_t count = 0;
357 while(!queue.empty())
359 int currentId = queue.front();
366 for(std::size_t t = 0; t < m_attrs.size(); ++t)
368 std::string attrName = m_attrs[t];
387 std::set<int>::iterator itNeighbours = neighbours.begin();
389 while(itNeighbours != neighbours.end())
397 vTo = m_graph->getVertex(e->
getIdTo());
399 vTo = m_graph->getVertex(e->
getIdFrom());
402 if(vTo && vTo->
getId() != vertexToIgnore)
404 if(visited.find(vTo->
getId()) == visited.end())
406 queue.push(vTo->
getId());
407 visited.insert(vTo->
getId());
418 for(std::size_t t = 0; t < m_attrs.size(); ++t)
420 meanAttrs[t] /= count;
428 double deviation = 0.;
430 std::queue<int> queue;
431 std::set<int> visited;
433 queue.push(startVertex);
434 visited.insert(startVertex);
436 while(!queue.empty())
438 int currentId = queue.front();
445 deviation += calculateDistance(vertex, meanVec);
449 std::set<int>::iterator itNeighbours = neighbours.begin();
451 while(itNeighbours != neighbours.end())
459 vTo = m_graph->getVertex(e->
getIdTo());
461 vTo = m_graph->getVertex(e->
getIdFrom());
464 if(vTo && vTo->
getId() != vertexToIgnore)
466 if(visited.find(vTo->
getId()) == visited.end())
468 queue.push(vTo->
getId());
469 visited.insert(vTo->
getId());
483 double distance = 0.;
485 for(std::size_t t = 0; t < m_attrs.size(); ++t)
487 std::string attrName = m_attrs[t];
double m_SSDTa
Sum of Square Difference for Tree A.
SkaterPartition(te::graph::AbstractGraph *graph, std::vector< std::string > attrs)
Default constructor.
A struct that represents the skater partition values for each edge.
int getId()
It returns the edge identification.
std::vector< te::dt::AbstractData * > & getAttributes()
It returns the vector of attributes associated with this element.
double calculateRootDeviation(int startVertex, int vertexToIgnore, std::vector< double > &meanVec)
Utilitary function for spatial analysis module.
From the point of view of graph theory, vertices are treated as featureless and indivisible objects...
Class used to define the edge struct of a graph. Its compose with a identifier, the vertex origin and...
std::vector< double > calculateRootMean(int startVertex, int vertexToIgnore, std::size_t &pop)
This file contains a class that represents the skater partition operation.
~SkaterPartition()
Virtual destructor.
std::vector< std::size_t > execute(std::size_t nGroups, std::string popAttr="", std::size_t minPop=0)
Function to execute the skater partition.
Abstract class used to define the main functions of graph struct. All graph implementations must used...
double m_l
Difference between m_SSDT and m_SSDi;.
std::size_t m_edgeId
Edge identification.
std::size_t m_popa
Sum of population for Tree A.
int getIdFrom()
It returns the vertex origin identification.
std::set< int > & getNeighborhood()
Returns the Neighborhood vector.
bool edgeToRemove(int startVertex, double &diffA, double &diffB, std::size_t &edgeToRemoveId)
double m_SSDi
Sum of m_SSDa and m_SSDb.
double calculateEdgeDifference(int vertexFrom, int vertexTo, double &diffA, double &diffB, std::size_t &popA, std::size_t &popB)
TESAEXPORT bool GetGraphVertexAttrIndex(te::graph::AbstractGraph *graph, std::string attrName, int &index)
Function used to get the vertex attribute index in the graph of the gpm.
std::size_t m_popb
Sum of population for Tree B.
double m_SSDT
Sum of Square Difference for Tree.
int getId()
It returns the vertex id.
TESAEXPORT double GetDataValue(te::dt::AbstractData *ad)
Function used to get the numeric value from a gpm property.
double m_SSDTb
Sum of Square Difference for Tree B.
double calculateDistance(te::graph::Vertex *vertex, std::vector< double > &meanVec)
int getIdTo()
It returns the vertex destiny identification.