<commit_before><?php

namespace Fhaculty\Graph\Algorithm\MaximumMatching;

use Fhaculty\Graph\Algorithm\Directed;

use Fhaculty\Graph\Exception\LogicException;

use Fhaculty\Graph\Exception\UnexpectedValueException;

use Fhaculty\Graph\Algorithm\MaxFlow\EdmondsKarp as MaxFlowEdmondsKarp;
use Fhaculty\Graph\Algorithm\Groups;
use Fhaculty\Graph\Exception;

class Flow extends Base
{
    public function getEdges()
    {
        $alg = new Directed($this->graph);
        if ($alg->isDirected()) {
            throw new UnexpectedValueException('Input graph contains directed edges');
        }

        $alg = new Groups($this->graph);
        if (!$alg->isBipartit()) {
            throw new UnexpectedValueException('Input graph does not have bipartit groups assigned to each vertex. Consider Using "AlgorithmBipartit::createGraph()" first');
        }

        // create temporary flow graph with supersource and supersink
        $graphFlow = $this->graph->createGraphCloneEdgeless();

        // get all vertices
        $vertices = $graphFlow->getVertices();
        // above $vertices does NOT contain supersource and supersink, because
        // we want to skip over them as they do not have a partition assigned

        $superSource = $graphFlow->createVertex()->setLayoutAttribute('label', 's*');
        $superSink   = $graphFlow->createVertex()->setLayoutAttribute('label', 't*');

        $groups = $alg->getGroups();
        $groupA = $groups[0];
        $groupB = $groups[1];

        // connect supersource s* to set A and supersink t* to set B
        foreach ($vertices as $vertex) {
            $group = $vertex->getGroup();

            // source
            if ($group === $groupA) {
                $superSource->createEdgeTo($vertex)->setCapacity(1)->setFlow(0);

                // temporarily create edges from A->B for flow graph
                $originalVertex = $this->graph->getVertex($vertex->getId());
                foreach ($originalVertex->getVerticesEdgeTo() as $vertexTarget) {
                    $vertex->createEdgeTo($graphFlow->getVertex($vertexTarget->getId()))->setCapacity(1)->setFlow(0);
                }
            // sink
            } elseif ($group === $groupB) {
                $vertex->createEdgeTo($superSink)->setCapacity(1)->setFlow(0);
            } else {
                throw new LogicException('Should not happen. Unknown set: ' + $belongingSet);
            }
        }

        // visualize($resultGraph);

        // calculate (s*, t*)-flow
        $algMaxFlow = new MaxFlowEdmondsKarp($superSource, $superSink);<commit_msg>Ignore coverage for unreachable sanity checks<commit_after><?php

namespace Fhaculty\Graph\Algorithm\MaximumMatching;

use Fhaculty\Graph\Algorithm\Directed;

use Fhaculty\Graph\Exception\LogicException;

use Fhaculty\Graph\Exception\UnexpectedValueException;

use Fhaculty\Graph\Algorithm\MaxFlow\EdmondsKarp as MaxFlowEdmondsKarp;
use Fhaculty\Graph\Algorithm\Groups;
use Fhaculty\Graph\Exception;

class Flow extends Base
{
    public function getEdges()
    {
        $alg = new Directed($this->graph);
        if ($alg->isDirected()) {
            throw new UnexpectedValueException('Input graph contains directed edges');
        }

        $alg = new Groups($this->graph);
        if (!$alg->isBipartit()) {
            throw new UnexpectedValueException('Input graph does not have bipartit groups assigned to each vertex. Consider Using "AlgorithmBipartit::createGraph()" first');
        }

        // create temporary flow graph with supersource and supersink
        $graphFlow = $this->graph->createGraphCloneEdgeless();

        // get all vertices
        $vertices = $graphFlow->getVertices();
        // above $vertices does NOT contain supersource and supersink, because
        // we want to skip over them as they do not have a partition assigned

        $superSource = $graphFlow->createVertex()->setLayoutAttribute('label', 's*');
        $superSink   = $graphFlow->createVertex()->setLayoutAttribute('label', 't*');

        $groups = $alg->getGroups();
        $groupA = $groups[0];
        $groupB = $groups[1];

        // connect supersource s* to set A and supersink t* to set B
        foreach ($vertices as $vertex) {
            $group = $vertex->getGroup();

            // source
            if ($group === $groupA) {
                $superSource->createEdgeTo($vertex)->setCapacity(1)->setFlow(0);

                // temporarily create edges from A->B for flow graph
                $originalVertex = $this->graph->getVertex($vertex->getId());
                foreach ($originalVertex->getVerticesEdgeTo() as $vertexTarget) {
                    $vertex->createEdgeTo($graphFlow->getVertex($vertexTarget->getId()))->setCapacity(1)->setFlow(0);
                }
            // sink
            } elseif ($group === $groupB) {
                $vertex->createEdgeTo($superSink)->setCapacity(1)->setFlow(0);
            } else {
                // @codeCoverageIgnoreStart
                throw new LogicException('Should not happen. Unknown set: ' + $belongingSet);
                // @codeCoverageIgnoreEnd
            }
        }

        // visualize($resultGraph);

        // calculate (s*, t*)-flow
        $algMaxFlow = new MaxFlowEdmondsKarp($superSource, $superSink);
{'fraction_non_alphanumeric': 0.12528301886792453, 'fraction_numerical': 0.0030188679245283017, 'mean_word_length': 2.510596026490066, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 61, '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': '9023', 'n_tokens_mistral': 1457, 'n_tokens_neox': 1419, 'n_words': 342}