public function getNumberOfComponents()
    {
        $visitedVertices = array();
        $components = 0;

        // for each vertices
        foreach ($this->graph->getVertices()->getMap() as $vid => $vertex) {
            // did I visit this vertex before?
            if (!isset($visitedVertices[$vid])) {

                // get all vertices of this component
                $newVertices = $this->createSearch($vertex)->getVertices()->getIds();

                ++$components;

                // mark the vertices of this component as visited
                foreach ($newVertices as $vid) {
                    $visitedVertices[$vid] = true;
                }
            }
        }

        // return number of components
        return $components;
    }
public function getNumberOfComponents()
    {
        $visitedVertices = array();
        $components = 0;

        // for each vertices
        foreach ($this->graph->getVertices()->getMap() as $vid => $vertex) {
            // did I visit this vertex before?
            if (!isset($visitedVertices[$vid])) {

                // get all vertices of this component
                $newVertices = $this->createSearch($vertex)->getVertices()->getIds();

                ++$components;

                // mark the vertices of this component as visited
                foreach ($newVertices as $vid) {
                    $visitedVertices[$vid] = true;
                }
            }
        }

        // return number of components
        return $components;
    }
public function getNumberOfComponents ( ) { $ visitedVertices = array ( ) ; $ components = 0 ; // for each vertices foreach ( $ this -> graph -> getVertices ( ) -> getMap ( ) as $ vid => $ vertex ) { // did I visit this vertex before? if ( ! isset ( $ visitedVertices [ $ vid ] ) ) { // get all vertices of this component $ newVertices = $ this -> createSearch ( $ vertex ) -> getVertices ( ) -> getIds ( ) ; ++ $ components ; // mark the vertices of this component as visited foreach ( $ newVertices as $ vid ) { $ visitedVertices [ $ vid ] = true ; } } } // return number of components return $ components ; }
count number of connected components

A null Graph (a Graph with no vertices) will return 0 components.

@return int number of components
@uses Graph::getVertices()
@uses AlgorithmSearchBreadthFirst::getVertices()
count number of connected components