<commit_before><?php
class AlgorithmMaxFlowEdmondsKarp{

    /**
     *
     * @var Graph
     */
    private $graph;

    /**
     * @var Vertex
     */
    private $startVertex;

    /**
     * @var Vertex
     */
    private $destinationVertex;

    /**
     *
     * @param Vertex $startVertex the vertex where the flow search starts
     * @param Vertex $destinationVertex the vertex where the flow search ends the destination
     */
    public function __construct(Vertex $startVertex, Vertex $destinationVertex){
        $this->startVertex = $startVertex;
        $this->destinationVertex = $destinationVertex;
        $this->graph = $startVertex->getGraph();
        $this->workingGraph = $this->graph->createGraphClone();
    }

    private function start(){
         
        
        $currentGraph = $this->mergeParallelEdges($this->workingGraph);         // remove parallel edges
        do{
            $pathFlow = $this->getGraphShortestPathFlow($currentGraph);         // Get Shortes path if NULL-> Done
             
            if($pathFlow){
                 $currentGraph = $this->getResidualGraph($currentGraph, $pathFlow);
            }
        } while($pathFlow);

        //return flow sum of outgoing flows
        return $this->getFlowGraphFromResidualGraph($currentGraph);
        //return $currentGraph;
    }


    private function getFlowGraphFromResidualGraph($residualGraph){
        //TODO generate flow $returnGraph from $residualGraph and $this->graph
        //run over original graph and create a new graph with the flow 
        echo "\nMissing function to remove not used edges and rotate the used edges.\n\n";
        
        $input=$this->graph;
        measure(function() use ($input){
            echo "bla";
            return $input;
        	
        },'bla');
    
        
        $resultGraph=$this->graph->createGraphCloneEdgeless();
        
        $originalGraphEdgesArray=$this->graph->getEdges();
        foreach ($originalGraphEdgesArray as $edge){
            //For each edge of the residual graph, which goes in the opposite way
            //than the original edge, insert the edge reverted residual edge in the new graph
            
            //get the Original vertices to find the propper edge
            $originalStartVertexArray = $edge->getStartVertices();
            $originalStartVertex = array_shift($originalStartVertexArray);
         
            $originalTargetVertexArray = $edge->getTargetVertices();
            $originalTargetVertex = array_shift($originalTargetVertexArray);
            
            //if vertices in resultgrpah not existing create them
           
            $residualGraphEdgeTargetVertex = $residualGraph->getVertex($originalStartVertex->getId());
            $residualGraphEdgeStartVertex = $residualGraph->getVertex($originalTargetVertex->getId());
           
            //vertices are existing now create the edge
            
            //check if residual graph has backward edge
            $residualEdgeArray= $residualGraphEdgeStartVertex->getEdgesTo($residualGraphEdgeTargetVertex);
                     
            $residualEdge = array_shift($residualEdgeArray);
            
            $newFlowEdge = $resultGraph->createEdgeClone($edge);
            if($residualEdge){
                
                $newFlowEdge->setWeight($residualEdge->getWeight());
            }
            else{
                $newFlowEdge->setWeight(0);
            }
            
            
            //if not existing remove the edge
            
        }

        return $resultGraph;
    }




    private function mergeParallelEdges($currentGraph){
        //TODO 1. find and merge parallel edges
        // alg works only without prarallel edges
        return $currentGraph;
    }

    /**
     * get the shortest path flow (by count of edges)
     *
     * @param Graph $currentGraph
     * @return Graph if path exists OR NULL
     */
    private function getGraphShortestPathFlow($currentGraph)
    {
        
        $startVertex = $currentGraph->getVertex($this->startVertex->getId());
        
        // 1. Search shortest path from s -> t
       
//         measure(function() use ($currentGraph){<commit_msg>added cli output of maxFlow from graph<commit_after><?php
class AlgorithmMaxFlowEdmondsKarp{

    /**
     *
     * @var Graph
     */
    private $graph;

    /**
     * @var Vertex
     */
    private $startVertex;

    /**
     * @var Vertex
     */
    private $destinationVertex;

    /**
     *
     * @param Vertex $startVertex the vertex where the flow search starts
     * @param Vertex $destinationVertex the vertex where the flow search ends the destination
     */
    public function __construct(Vertex $startVertex, Vertex $destinationVertex){
        $this->startVertex = $startVertex;
        $this->destinationVertex = $destinationVertex;
        $this->graph = $startVertex->getGraph();
        $this->workingGraph = $this->graph->createGraphClone();
    }

    private function start(){
         
        
        $currentGraph = $this->mergeParallelEdges($this->workingGraph);         // remove parallel edges
        do{
            $pathFlow = $this->getGraphShortestPathFlow($currentGraph);         // Get Shortes path if NULL-> Done
             
            if($pathFlow){
                 $currentGraph = $this->getResidualGraph($currentGraph, $pathFlow);
            }
        } while($pathFlow);

        //return flow sum of outgoing flows
        return $this->getFlowGraphFromResidualGraph($currentGraph);
        //return $currentGraph;
    }


    private function getFlowGraphFromResidualGraph($residualGraph){
        //TODO generate flow $returnGraph from $residualGraph and $this->graph
        //run over original graph and create a new graph with the flow 
        echo "\nMissing function to remove not used edges and rotate the used edges.\n\n";
        
        $input=$this->graph;
        measure(function() use ($input){
            echo "bla";
            return $input;
        	
        },'bla');
    
        
        $resultGraph=$this->graph->createGraphCloneEdgeless();
        
        $originalGraphEdgesArray=$this->graph->getEdges();
        foreach ($originalGraphEdgesArray as $edge){
            //For each edge of the residual graph, which goes in the opposite way
            //than the original edge, insert the edge reverted residual edge in the new graph
            
            //get the Original vertices to find the propper edge
            $originalStartVertexArray = $edge->getStartVertices();
            $originalStartVertex = array_shift($originalStartVertexArray);
         
            $originalTargetVertexArray = $edge->getTargetVertices();
            $originalTargetVertex = array_shift($originalTargetVertexArray);
            
            //if vertices in resultgrpah not existing create them
           
            $residualGraphEdgeTargetVertex = $residualGraph->getVertex($originalStartVertex->getId());
            $residualGraphEdgeStartVertex = $residualGraph->getVertex($originalTargetVertex->getId());
           
            //vertices are existing now create the edge
            
            //check if residual graph has backward edge
            $residualEdgeArray= $residualGraphEdgeStartVertex->getEdgesTo($residualGraphEdgeTargetVertex);
                     
            $residualEdge = array_shift($residualEdgeArray);
            
            $newFlowEdge = $resultGraph->createEdgeClone($edge);
            if($residualEdge){
                
                $newFlowEdge->setWeight($residualEdge->getWeight());
            }
            else{
                $newFlowEdge->setWeight(0);
            }
            
           
            
            //if not existing remove the edge
            
        }
        
       $resultGraphStartVertex = $resultGraph->getVertex($this->startVertex->getId());
       $resultGraphOutgoingEdgesArray = $resultGraphStartVertex->getOutgoingEdges();
       $maxFlow=0;
       foreach ($resultGraphOutgoingEdgesArray as $edge){
           $maxFlow=$maxFlow+$edge->getWeight();
       }
        echo "Max Flow is: ".$maxFlow."\n";
        return $resultGraph;
    }




    private function mergeParallelEdges($currentGraph){
        //TODO 1. find and merge parallel edges
        // alg works only without prarallel edges
        return $currentGraph;
    }

    /**
     * get the shortest path flow (by count of edges)
     *
     * @param Graph $currentGraph
     * @return Graph if path exists OR NULL
     */
    private function getGraphShortestPathFlow($currentGraph)
    {
        
        $startVertex = $currentGraph->getVertex($this->startVertex->getId());
        
        // 1. Search shortest path from s -> t
       
//         measure(function() use ($currentGraph){
{'fraction_non_alphanumeric': 0.09670131043831902, 'fraction_numerical': 0.0007907817442385902, 'mean_word_length': 1.9979681679647816, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 76, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6797296', 'n_tokens_mistral': 2246, 'n_tokens_neox': 2178, 'n_words': 568}