<commit_before><?php

class AlgorithmConnectedComponents extends Algorithm{
    
    /**
     * 
     * @var Graph
     */
    private $graph;
    
    /**
     * 
     * @param Graph $graph
     */
    public function __construct(Graph $graph){
        $this->graph = $graph;
    }
    
    /**
     * check whether this graph consists of only a single component
     * 
     * could be improved by not checking for actual number of components but stopping when there's more than one
     * 
     * @return boolean
     * @uses AlgorithmConnectedComponents::getNumberOfComponents()
     */
    public function isSingle(){
        return ($this->getNumberOfComponents() === 1);
    }
    
    /**
     * @return int number of components
     * @uses Graph::getVertices()
     * @uses AlgorithmSearchDepthFirst::getVertices()
     */
    public function getNumberOfComponents(){
        $visitedVertices = array();
        $components = 0;
        
        foreach ($this->graph->getVertices() as $vertex){                        //for each vertices
            if ( ! isset( $visitedVertices[$vertex->getId()] ) ){                    //did I visit this vertex before?
                
                $alg = new AlgorithmSearchDepthFirst($vertex);
                $newVertices = $alg->getVertices();                                    //get all vertices of this component
                
                $components++;
                
                foreach ($newVertices as $v){                                            //mark the vertices of this component as visited<commit_msg>Improve check for single by only issuing search once<commit_after><?php

class AlgorithmConnectedComponents extends Algorithm{
    
    /**
     * 
     * @var Graph
     */
    private $graph;
    
    /**
     * 
     * @param Graph $graph
     */
    public function __construct(Graph $graph){
        $this->graph = $graph;
    }
    
    /**
     * check whether this graph consists of only a single component
     * 
     * could be improved by not checking for actual number of components but stopping when there's more than one
     * 
     * @return boolean
     * @uses AlgorithmSearchBreadthFirst::getNumberOfVertices()
     */
    public function isSingle(){
        $alg = new AlgorithmSearchBreadthFirst($this->graph->getVertexFirst());
        return ($this->graph->getNumberOfVertices() === $alg->getNumberOfVertices());
    }
    
    /**
     * @return int number of components
     * @uses Graph::getVertices()
     * @uses AlgorithmSearchDepthFirst::getVertices()
     */
    public function getNumberOfComponents(){
        $visitedVertices = array();
        $components = 0;
        
        foreach ($this->graph->getVertices() as $vertex){                        //for each vertices
            if ( ! isset( $visitedVertices[$vertex->getId()] ) ){                    //did I visit this vertex before?
                
                $alg = new AlgorithmSearchDepthFirst($vertex);
                $newVertices = $alg->getVertices();                                    //get all vertices of this component
                
                $components++;
                
                foreach ($newVertices as $v){                                            //mark the vertices of this component as visited
{'fraction_non_alphanumeric': 0.09972883398614041, 'fraction_numerical': 0.0009038867128653209, 'mean_word_length': 1.6581265012009607, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5208547', 'n_tokens_mistral': 814, 'n_tokens_neox': 781, 'n_words': 234}