Search
 
SCRIPT & CODE EXAMPLE
 

CPP

Edmonds-Karp algorithm C++

#include <iostream>
// Part of Cosmos by  OpenGenus Foundation //
#include <limits.h>
#include <string.h>
#include <queue>
using namespace std;
#define V 6
/* Returns true if there is a path from source 's' to sink 't' in
 * residual graph. Also fills parent[] to store the path */
bool bfs(int rGraph[V][V], int s, int t, int parent[])
{
    // Create a visited array and mark all vertices as not visited
    bool visited[V];
    memset(visited, 0, sizeof(visited));
    // Create a queue, enqueue source vertex and mark source vertex
    // as visited
    queue <int> q;
    q.push(s);
    visited[s] = true;
    parent[s] = -1;
    // Standard BFS Loop
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        for (int v = 0; v < V; v++)
            if (visited[v] == false && rGraph[u][v] > 0)
            {
                q.push(v);
                parent[v] = u;
                visited[v] = true;
            }
    }
    // If we reached sink in BFS starting from source, then return
    // true, else false
    return visited[t] == true;
}
// Returns tne maximum flow from s to t in the given graph
int fordFulkerson(int graph[V][V], int s, int t)
{
    int u, v;
    // Create a residual graph and fill the residual graph with
    // given capacities in the original graph as residual capacities
    // in residual graph
    int rGraph[V][V]; // Residual graph where rGraph[i][j] indicates
                      // residual capacity of edge from i to j (if there
                      // is an edge. If rGraph[i][j] is 0, then there is not)
    for (u = 0; u < V; u++)
        for (v = 0; v < V; v++)
            rGraph[u][v] = graph[u][v];
    int parent[V];  // This array is filled by BFS and to store path
    int max_flow = 0;  // There is no flow initially
    // Augment the flow while tere is path from source to sink
    while (bfs(rGraph, s, t, parent))
    {
        // Find minimum residual capacity of the edges along the
        // path filled by BFS. Or we can say find the maximum flow
        // through the path found.
        int path_flow = INT_MAX;
        for (v = t; v != s; v = parent[v])
        {
            u = parent[v];
            path_flow = min(path_flow, rGraph[u][v]);
        }
        // update residual capacities of the edges and reverse edges
        // along the path
        for (v = t; v != s; v = parent[v])
        {
            u = parent[v];
            rGraph[u][v] -= path_flow;
            rGraph[v][u] += path_flow;
        }
        // Add path flow to overall flow
        max_flow += path_flow;
    }
    // Return the overall flow
    return max_flow;
}
C++Copy
Comment

PREVIOUS NEXT
Code Example
Cpp :: sleep function i nc++ 
Cpp :: Your age doubled is: xx where x is the users age doubled. (print answer with no decimal places) 
Cpp :: go to particular place in vector using iterator 
Cpp :: c++ hide credentials 
Cpp :: c++ 2 dim array initialize 
Cpp :: c++ bind what are placeholders 
Cpp :: c++ trim string 
Cpp :: default order in set in c++ 
Cpp :: time function in c++ 
Cpp :: C++ Vector Initialization method 01 
Cpp :: initialize object as null in c++ 
Cpp :: shrek c++ 
Cpp :: ue4 execute delegate from blueprint 
Cpp :: accepting multiple values from a function in cpp 
Cpp :: cpp cout more than 1 value 
Cpp :: c++ map access 
Cpp :: result += a +b in c++ meaning 
Cpp :: How to make an array dynamically using pointers 
Cpp :: deal with bad input cpp 
Cpp :: ala vida 
Cpp :: how to traverse string like array in cpp 
Cpp :: left recursion program in c++ 
Cpp :: How to assign two dimensional initializer list in c++ 
Cpp :: The iostream is the head er file which contains all the functions of program like cout, cin and etc. 
Cpp :: MPI_File_seek 
Cpp :: user inptu in cpp 
Cpp :: c++ compile to msi 
Cpp :: online c++ compiler 
Cpp :: cpp map contains 
Cpp :: stream in c++ 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =