Show the number of connected components and the size of the largest one

This commit is contained in:
Dennis Luxen 2010-08-12 15:42:22 +00:00
parent 4f883aaeda
commit 59abeccc3e
2 changed files with 67 additions and 1 deletions

View File

@ -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<NodeID>(_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<NodeID> * bfsStack = new std::stack<NodeID>();
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<NodeID> * _components;
};
#endif // CONTRACTOR_H_INCLUDED

View File

@ -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);