int dfsCount(int node) { int count; int i; // Start with a count of 1 (this counts this node) count = 1; // Mark this node as used, so we don't count it again used[node] = true; // Iterate over and search our neighbors for (i = 0; i < numPeople; i++) { // If this node is connected to node i, and we haven't already searched // node i, search it now if ((connectMatrix[node][i]) && (!used[i])) { // Continue the depth-first search on node i and add the node count // from that part of the search to our total count count += dfsCount(i); } } // Return the total count return count; }
int componentCount(int node) { int count; // Clear the used array memset(used, 0, sizeof(used)); // Do a depth-first search starting at this node, and count the number // of nodes visited by the search return dfsCount(node); }
KDPropertyInterface* KDPropertyModel::Private::dfsCount( KDPropertyGroupInterface* root, int& count ) const { //qDebug() << "dfsCount( " << root << ", " << count << " )"; for( int i = 0; i < root->propertyCount(); ++i ) { KDPropertyInterface* prop = root->propertyAt(i); if( prop->isCategory() ) { prop = dfsCount( static_cast<KDPropertyGroupInterface*>(prop),count); if( count == 0 && prop ) return prop; } else { if( count == 0 ) return prop; else --count; } } return 0; }