I was trying to implement Edmonds-Karp in C++ for maximum flow, and I wrote it slightly differently:
- Instead of going through all edges in residual graph, I only went through the edges that are present in the original graph, using the adjacency list.
- I did not update any back-edges when updating the residual graph with min flow.
Interestingly, when I ran my code, it gave me correct results. So I went to Wikipedia's example, where it specifically show how a back-edge is being used. When I fed this graph to my code, I got the correct answer again. I also checked the resultant flow matrix, and it was identical to Wikipedia's.
Can someone explain why we must add and update back-edges, and maybe give an example where they are critical?
Here's the code that I wrote (updated to include back edges):