// C++ program for finding minimum cut using Ford-Fulkerson
#include <iostream>
#include <limits.h>
#include <string.h>
#include <queue>
using namespace std;
// Shows number of vertices in given graph
#define V1 6
/* Now returns true if there is a path from source 's1' to sink
't1' in
residual graph. Also fills parent1[] to store the path */
int bfs(int rGraph1[V1][V1], int s1, int t1, int parent1[]){
// Build a visited array and mark all vertices as not visited
bool visited1[V1];
memset(visited1, 0, sizeof(visited1));
// Build a queue, enqueue source vertex and mark source vertex
// as visited
queue <int> q1;
q1.push(s1);
visited1[s1] = true;
parent1[s1] = -1;
// Shows standard BFS Loop
while (!q1.empty()){
int u1 = q1.front();
q1.pop();
for (int v1=0; v1<V1; v1++){
if (visited1[v1]==false && rGraph1[u1][v1] > 0){
q1.push(v1);
parent1[v1] = u1;
visited1[v1] = true;
}
}
}
// It has been seen that if we reached sink in BFS starting
//from source,
//then return true, else false
return (visited1[t1] == true);
}
/*Shows a DFS(Depth First Search) based function to find all
reachable
vertices from s. The function marks visited[i] as true if i is
reachable from s. The initial values in visited[] must be false.
We can also use BFS to determine reachable vertices */
void dfs(int rGraph1[V1][V1], int s1, bool visited1[]){
visited1[s1] = true;
for (int i = 0; i < V1; i++)
if (rGraph1[s1][i] && !visited1[i])
dfs(rGraph1, i, visited1);
}
// Now prints the minimum s-t cut
void minCut(int graph1[V1][V1], int s1, int t1){
int u1, v1;
// Build a residual graph and fill the residual graph with
// given capacities in the original graph as residual capacities
// in residual graph
int rGraph1[V1][V1]; // rGraph1[i][j] indicates residual
// capacity of edge i-j
for (u1 = 0; u1 < V1; u1++)
for (v1 = 0; v1 < V1; v1++)
rGraph1[u1][v1] = graph1[u1][v1];
int parent1[V1]; // This array is filled by BFS and to store
//path
// Now augment the flow while there is a path from source to
//sink
while (bfs(rGraph1, s1, t1, parent1)){
// Determine minimum residual capacity of the edges along
//the
// path filled by BFS(Breadth First Search) or we can say determine the maximum flow through the path found.
int path_flow1 = INT_MAX;
for (v1=t1; v1!=s1; v1=parent1[v1]){
u1 = parent1[v1];
path_flow1 = min(path_flow1, rGraph1[u1][v1]);
}
// Perform update residual capacities of the edges and
//reverse edges along the path
for (v1=t1; v1 != s1; v1=parent1[v1]){
u1 = parent1[v1];
rGraph1[u1][v1] -= path_flow1;
rGraph1[v1][u1] += path_flow1;
}
}
// Here, flow is maximum now, determine vertices reachable from
//s
bool visited1[V1];
memset(visited1, false, sizeof(visited1));
dfs(rGraph1, s1, visited1);
// Now print all edges that are from a reachable vertex to
// non-reachable vertex in the original graph
for (int i = 0; i < V1; i++)
for (int j = 0; j < V1; j++)
if (visited1[i] && !visited1[j] && graph1[i][j])
cout << i << " - " << j << endl;
return;
}
// Driver program to test above functions
int main(){
// Assuming create a graph shown in the above example
int graph1[V1][V1] = { {0, 17, 14, 0, 0, 0},{0, 0, 11, 13, 0, 0},
{0, 5, 0, 0, 15, 0},{0, 0, 9, 0, 0, 21},{0, 0, 0, 8, 0, 5},
{0, 0, 0, 0, 0, 0}};
minCut(graph1, 0, 5);
return 0;
}
About Online C++ Compiler
Try our Online C++ Compiler (Version GNU GCC v11.3.0) to Edit, Run, and Share your C++ Code directly from your browser. This online development environment provides you the latest version GNU GCC v11.3.0.
How to use Online C++ Compiler?
Write and Execute Code
- Write your program (or, paste it) directly under the "Source Code" tab.
- If you want to save your program, go to the "Project" menu and save it.
- You can directly execute your program without saving it by clicking on on "Execute" button.
User Input
The latest version of Coding Ground allows to provide program input at run time from the termnial window exactly the same way as you run your program at your own computer. So simply run a program and provide your program input (if any) from the terminal window available in the right side.
Online C++ Compiler: Keyboard Shortcuts
The following are the keyword shortcut of this Online C++ Compiler:
Shortcut | Description |
⌘ + Enter | Run the program |
⌘ + S | Save Project (Login Required) |
⇧ + ⌘ + S | Save As Project |
⌘ + P | New Project |
⌘ + G | Share Project |
⌘ + Z | Undo Editing |
⌘ + Y | Redo Editing |
⌘ + A | Select All Text |
⌘ + X | Cut Selected Text |
⌘ + C | Copy Selected Text |
⌘ + V | Paste Copied Text |
⌘ + F | Search Text |
⌘ + ⌥ + F | Replace Text |
Shortcut | Description |
Ctrl + Enter | Run the program |
Ctrl + S | Save Project |
Shift + Ctrl + S | Save As Project |
Ctrl + G | Share Project |
Ctrl + Z | Undo Editing |
Ctrl + Y | Redo Editing |
Ctrl + A | Select All Text |
Ctrl + X | Cut Selected Text |
Ctrl + C | Copy Selected Text |
Ctrl + V | Paste Copied Text |
Ctrl + F | Search Text |
Ctrl + H | Replace Text |
Online C++ Compiler: Save and Share C++ Code (Project)
Save C++ Project Online
You can save your C++ Project with us so that you can access this project later on. To save a project you will need to create a login Id with us. So before you save a project, please create a login Id using a link given at the top right corner of this page.
Share C++ Project Online
You can use this feature to share your C++ Code with your teachers, classmates and colleagues. Just click Share Button and it will create a short link, which can be shared through Email, WhatsApp or even through Social Media. A shared link will be deleted if it has been passive for almost 3 months.
More Features of Online C++ Compiler
- Theme – You can change the current editor's theme from the "Editor Theme" option under "Settings" menu.
- Font Size – You can change the font size of the editor /compiler from from the "Font Size" option under "Settings" menu.
- Tab Size – You can change the tab size from the "Tab Size" option under "Settings" Menu.
- Show/Hide Line Numbers – You can show/hide the line number with the code from the "Show Line Numbers" or "Hide Line Numbers" option under "Settings" Menu.
- And, many more.
Benefits of Using Online C++ Compiler
There are several benefits of using the Online C++ Compiler to run your C++ code:
- Platform independence: You can run your code from any device without taking care of operating systems.
- Convenience: You don't need to install anything for using this.
- No setup required: There is no need for additional setup to run your code.
- Updated version: Our online compiler/editors/terminals are the latest up-to-date.