Implementation using Edmond-Karp's algorithm not outputing the maximum flow value
<p>I am implementing a program to solve a matching problem by using the reduction from a matching problem to a maximum flow problem, which I am determined to solve using Edmond-Karp's algorithm.</p>

<p>I have looked through and taken impressions from different pseudo codes that are telling how to carry out the procedure to solve a maximum flow problem using the Edmond-Karp's algorithm and by mixing these impressions, I have produced an implementation that works for many small basic graphs. But by some reason, the implementation gives wrong answers for bigger graphs such as 50 nodes with 100 edges or even bigger than such graphs. So giving a input-output scenario example wouldn't help that much with highlighting the whole problem (if you insist although, I can post such a input-output scenario as you wish). When I say "wrong answers", the implementation does not actually give the maximal matching (or maximal flow value) given a graph to solve.</p>

<p>Down below you can have all the relevant classes that are being used in my implementation, that is in the MaxFlowSolver class (the method solveFlowProblem(...) is the method that uses the whole Edmond-Karp's algorithm):</p>

<p><strong><em>MaxFlowSolver</em></strong></p>

<pre><code>import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;

public class MaxFlowSolver {
    private Graph flowProblemSolution;
    private int flowValue;

    public MaxFlowSolver(Graph toBeSolved, int source, int sink) {
        flowProblemSolution = solveFlowProblem(toBeSolved, source, sink);
    }

    private Graph solveFlowProblem(Graph toSolve, int source, int sink) {
        Graph rGraph = toSolve;
        Edge[] path;

        while((path=BFS(rGraph, source, sink))!=null) {                     
            int r = Integer.MAX_VALUE;
            for(Edge e = path[sink]; e!=null; e = path[e.getStartVertex()])
                r = Math.min(r, e.getCapacity()-e.getFlow());

            for(Edge e = path[sink]; e!=null; e = path[e.getStartVertex()]) {
                e.changeFlow(e.getFlow()+r);
                Edge erev;
                if((erev=getReversedEdge(e, rGraph))==null) {
                    erev = new Edge(e.getEndVertex(), e.getStartVertex(), 0);
                    rGraph.addEdge(erev);
                }
                erev.changeFlow(-e.getFlow());
                e.changeResCap(e.getCapacity()-e.getFlow());
                erev.changeResCap(erev.getCapacity()-erev.getFlow());
            }
            flowValue += r;
        }

        return rGraph;
    }

    private Edge getReversedEdge(Edge e, Graph graph) {
        LinkedList&lt;Edge&gt; neighbours = graph.getEdges().get(e.getEndVertex());
        if(neighbours==null)
            return null;

        for(Edge i : neighbours)
            if(i.getEndVertex()==e.getStartVertex())
                return i;

        return null;
    }

    private Edge[] BFS(Graph resGraph, Integer source, Integer sink) {
        Queue&lt;Integer&gt; q = new LinkedList&lt;&gt;();
        q.add(source);
        Edge[] pred = new Edge[resGraph.numVertices()+1];

        while(!q.isEmpty()) {
            Integer curr = q.poll();
            LinkedList&lt;Edge&gt; neighbours1 = resGraph.getEdges().get(curr);

            if(neighbours1!=null)
                for(Edge someEdge : neighbours1) {
                    if(pred[someEdge.getEndVertex()]==null &amp;&amp; someEdge.getEndVertex()!=source &amp;&amp; 
                            someEdge.getCapacity()&gt;someEdge.getFlow()) {
                        pred[someEdge.getEndVertex()] = someEdge;
                        q.add(someEdge.getEndVertex());
                    }
                }

            if(curr==sink) return pred;
        }

        return null;
    }

    public Graph getSolution() {
        return flowProblemSolution;
    }

    public ArrayList&lt;Edge&gt; retrievePositiveEdges(Graph arg0, int source, int sink) {
        ArrayList&lt;Edge&gt; res = new ArrayList&lt;&gt;();
        Set&lt;Integer&gt; goThroughInts = flowProblemSolution.getEdges().keySet();

        for(Integer i : goThroughInts)
            for(Edge j : flowProblemSolution.getEdges().get(i)) {
                int start = j.getStartVertex();
                int end = j.getEndVertex();
                if(!res.contains(j) &amp;&amp; j.getFlow()&gt;0 &amp;&amp; 
                        start!=source &amp;&amp; start!=sink &amp;&amp; end!=source &amp;&amp; end!=sink &amp;&amp; 
                        arg0.getEdges().get(i).contains(j))
                    res.add(j);
            }

        return res;
    }

    public int getFlowValue() {
        return flowValue;
    }
}
</code></pre>

<p><strong><em>Graph</em></strong></p>

<pre><code>import java.util.ArrayList;
import java.util.LinkedList;
import java.util.HashMap;

    public class Graph {    
        private HashMap&lt;Integer, LinkedList&lt;Edge&gt;&gt; edges;
        private int firstVertices;
        private int secondVertices;
        private int size;

        public Graph(int x, int y) {
            firstVertices = x;
            secondVertices = y;
            edges = new HashMap&lt;&gt;();
            size = 0;
        }

        public Graph(int size) {
            firstVertices = size;
            secondVertices = 0;
            edges = new HashMap&lt;&gt;();
        }

        public boolean addEdge(int x, int y, boolean regularV) {
            if(!(0&lt;=x &amp;&amp; x&lt;=firstVertices &amp;&amp; firstVertices&lt;y &amp;&amp; y&lt;=firstVertices+secondVertices) &amp;&amp; regularV) {
                System.err.println("The graph is no longer bipartite. The execution is aborted.");
                System.exit(1);
            }

            if(!edges.containsKey(x))
                edges.put(Integer.valueOf(x), new LinkedList&lt;Edge&gt;());

            Edge temp = new Edge(x,y);
            if(!edges.get(x).add(temp))
                return false;

            size++;

            return true;
        }

        public boolean addEdge(Edge newEdge) {
            if(!edges.containsKey(newEdge.getStartVertex()))
                edges.put(Integer.valueOf(newEdge.getStartVertex()), new LinkedList&lt;Edge&gt;());

            if(!edges.get(newEdge.getStartVertex()).add(newEdge))
                return false;

            size++;

            return true;
        }

        public void changeNumOfXs(int newVal) {
            firstVertices = newVal;
        }

        public void changeNumOfYs(int newVal) {
            secondVertices = newVal;
        }

        public int getNumOfXs() {
            return firstVertices;
        }

        public int getNumOfYs() {
            return secondVertices;
        }

        public int numOfEdges() {
            return size;
        }

        public HashMap&lt;Integer, LinkedList&lt;Edge&gt;&gt; getEdges() {
            return edges;
        }

        public void printGraph(Kattio io) {
            printGraph(io, size-1, size);
        }

        public void printGraph(Kattio io, int source, int sink) {
            io.println(firstVertices+secondVertices);
            io.println(source + " " + sink);
            io.println(size);
            ArrayList&lt;Edge&gt; printed = new ArrayList&lt;&gt;();
            for(LinkedList&lt;Edge&gt; i : edges.values())
                for(Edge j : i)
                    if(!printed.contains(j)) {
                        printed.add(j);
                        io.println(j.getStartVertex() + " " + j.getEndVertex() + " " + j.getCapacity());
                    }
            io.flush();
        }

        public int numVertices() {
            return firstVertices+secondVertices;
        }

        public int numEdges() {
            return size;
        }
    }
</code></pre>

<p><strong><em>Edge</em></strong></p>

<pre><code>public class Edge {
    private final int startVertex;
    private final int endVertex;
    private int flow;
    private int rCapacity;
    private final int capacity;

    public Edge(int start, int end) {
        startVertex = start;
        endVertex = end;
        flow = 0;
        rCapacity = 1;
        capacity = 1;
    }

    public Edge(int start, int end, int cap) {
        startVertex = start;
        endVertex = end;
        flow = 0;
        rCapacity = cap;
        capacity = cap;
    }

    public void changeFlow(int newFlowVal) {
        flow = newFlowVal;
    }

    public void changeResCap(int newResCapVal) {
        rCapacity = newResCapVal;
    }

    public int getStartVertex() {
        return startVertex;
    }

    public int getEndVertex() {
        return endVertex;
    }

    public int getFlow() {
        return flow;
    }

    public int getCapacity() {
        return capacity;
    }

    public int getResCapacity() {
        return rCapacity;
    }
}
</code></pre>

<p>I have mainly followed the Wikipedia's description of the Edmond-Karp's algorith using adjacency nodes, but also a little from other sources but not that much. The link to the algorithm on wikipedia is down below:</p>

<p><a href="https://en.wikipedia.org/wiki/Edmonds" rel="nofollow noreferrer">https://en.wikipedia.org/wiki/Edmonds</a>–Karp_algorithm#Pseudocode</p>

<p>Is there somewhere in my implementation where I have interpreted the pseudo code on the wikipedia wrong or is it something more specifically with my Java codes? I have been working with this program almost all the day without figuring out what causes the problem that the program for certain graphs does not output the maximal matching (or maximum flow) for some graph.</p>

<p>All help I can get from you will be highly appreciated.</p>

<p>Thank you all in advance!</p>