From 59abeccc3ecac80123ea8d081c6e37022f5c522c Mon Sep 17 00:00:00 2001 From: Dennis Luxen Date: Thu, 12 Aug 2010 15:42:22 +0000 Subject: [PATCH] Show the number of connected components and the size of the largest one --- Contractor/Contractor.h | 65 ++++++++++++++++++++++++++++++++++++++++- createHierarchy.cpp | 3 ++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/Contractor/Contractor.h b/Contractor/Contractor.h index f616bacfc..6ea7bed60 100644 --- a/Contractor/Contractor.h +++ b/Contractor/Contractor.h @@ -16,7 +16,7 @@ You should have received a copy of the GNU Affero General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA or see http://www.gnu.org/licenses/agpl.txt. -*/ + */ #ifndef CONTRACTOR_H_INCLUDED #define CONTRACTOR_H_INCLUDED @@ -460,8 +460,69 @@ public: } } } + + NodeID GetNumberOfComponents() + { + NodeID unique = 1; + NodeID largestRun = 0; + NodeID tmp = 1; + _components = new std::vector(_graph->GetNumberOfNodes(), 0); + for(NodeID i = 0; i < _graph->GetNumberOfNodes(); i++) + { + BFSAtNode(i); + } +#ifdef _GLIBCXX_PARALLEL + __gnu_parallel::sort( _components->begin(), _components->end() ); +#else + std::sort(_components->begin(), _components->end()); +#endif + + for(NodeID i = 0; i < _graph->GetNumberOfNodes()-1; i++) + { + if( _components->at(i) != _components->at(i+1)){ + if(tmp > largestRun) + { + largestRun = tmp; + } else { + cout << tmp << " " << flush; + } + tmp = 1; + unique++; + } + else + tmp++; + } + if(tmp > largestRun) + largestRun = tmp; + _components->clear(); + cout << "Largest component has size: " << largestRun << endl; + return unique; + } + private: + void BFSAtNode(const NodeID n){ + std::stack * bfsStack = new std::stack(); + if(_components->at(n) != 0) + return; + _components->at(n) = n+1; + bfsStack->push(n); + while(!bfsStack->empty()) + { + NodeID node = bfsStack->top(); + bfsStack->pop(); + for(_DynamicGraph::EdgeIterator e = _graph->BeginEdges(node); e < _graph->EndEdges(node); e++) + { + if(_components->at(_graph->GetTarget(e))!=0) + continue; + _components->at(_graph->GetTarget(e)) = n+1; + bfsStack->push(_graph->GetTarget(e)); + } + } + delete bfsStack; + } + + double _Timestamp() { return time(NULL); } @@ -721,6 +782,8 @@ private: } _DynamicGraph* _graph; + std::vector * _components; + }; #endif // CONTRACTOR_H_INCLUDED diff --git a/createHierarchy.cpp b/createHierarchy.cpp index 540b73812..9941d626a 100644 --- a/createHierarchy.cpp +++ b/createHierarchy.cpp @@ -85,6 +85,9 @@ int main (int argc, char *argv[]) int2ExtNodeMap->clear(); Contractor* contractor = new Contractor( n, edgeList ); + + cout << "Number of connected components: " << contractor->GetNumberOfComponents() << endl; + contractor->Run(); contractor->checkForAllOrigEdges(edgeList);