/* * This file is part of the GreasePad distribution (https://github.com/FraunhoferIOSB/GreasePad). * Copyright (c) 2022 Jochen Meidow, Fraunhofer IOSB * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "conncomp.h" #include "global.h" namespace Graph { ConnComp::ConnComp( const SparseMatrix &BB) { // qDebug() << Q_FUNC_INFO; assert( BB.rows()==BB.cols() ); // Q_ASSERT( BB.rows()==BB.cols() ); const Index V = BB.rows(); // number of vertices m_visited.setConstant( V,false); m_comp.setConstant( V,-1); int num_connected_components = 0; for ( Index v=0; v=0 ); if ( n==0) { return VectorXi(0); } return m_comp.head(n); } VectorXi ConnComp::tail( const int n) const { assert( n>=0 ); if ( n==0) { return VectorXi(0); } return m_comp.tail(n); } /*! Label of i-th element */ int ConnComp::label( const Index i) const { assert( i>=0 && i &CC, const int c, const Index v) { m_visited(v) = true; m_comp(v) = c; for ( SparseMatrix::InnerIterator it(CC,v); it; ++it) { if ( !m_visited(it.index()) ) { dfs( CC, c, it.row() ); } } } } // namespace Graph