
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Implement Network_Flow Problem
This is a C++ Program to implement Network_Flow problem using Ford Fulkerson algorithm.
Algorithms:
Begin function bfs() returns true if there is path from source s to sink t in the residual graph which indicates additional possible flow in the graph. End Begin function fordfulkarson() return maximum flow in given graph: A) initiate flow as 0. B) If there is an augmenting path from source to sink, add the path to flow. C) Return flow. End
Example Code
#include <iostream> #include <climits> #include <cstring> #include <queue> #define n 7 using namespace std; bool bfs(int g[n][n], int s, int t, int par[]) { bool visit[n]; memset(visit, 0, sizeof(visit)); queue <int> q; q.push(s); visit[s] = true; par[s] = -1; while (!q.empty()) { int u = q.front(); q.pop(); for (int v=0; v<n; v++) { if (visit[v]==false && g[u][v] > 0) { q.push(v); par[v] = u; visit[v] = true; } } } return (visit[t] == true); } int fordFulkerson(int G[n][n], int s, int t) { int u, v; int g[n][n]; for (u = 0; u < n; u++) { for (v = 0; v < n; v++) g[u][v] = G[u][v]; } int par[n]; int max_flow = 0; while (bfs(g, s, t,par)) { int path_flow = INT_MAX; for (v=t; v!=s; v=par[v]) { u = par[v]; path_flow = min(path_flow, g[u][v]); } for (v = t; v != s; v = par[v]) { u = par[v]; g[u][v] -= path_flow; g[v][u] += path_flow; } max_flow += path_flow; } return max_flow; } int main() { int g[n][n] = {{0, 6, 7, 1}, {0, 0, 4, 2}, {0, 5, 0, 0}, {0, 0, 19, 12}, {0, 0, 0, 17}, {0, 0, 0, 0}}; cout << "The maximum possible flow is " << fordFulkerson(g, 0, 3); return 0; }
Output
The maximum possible flow is 3
- Related Articles
- Program to implement the fractional knapsack problem in Python
- C++ Program to Implement Traveling Salesman Problem using Nearest Neighbour Algorithm
- Network Security: Basics, How to Implement
- How to implement coin change problem using topDown approach using C#?
- How to implement reactive streams using Flow API in Java 9?
- How to implement coin change problem using bottom-up approach using C#?
- C++ Program to Solve N-Queen Problem
- Find minimum s-t cut in a flow network in C++
- C++ Program to Solve the Dominating Set Problem
- Write a C# program to solve FizzBuzz problem
- C++ Program to Solve the Fractional Knapsack Problem
- C++ Program to Implement Vector
- C# program to implement FizzBuzz
- C++ Program to Implement Trie
- C++ Program to Implement Stack

Advertisements