Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is c + + program is about Ford - Fulkerson Algorithm for Maximum Flow Problem Can someone help to make this with python? Make

This is c++ program is about Ford - Fulkerson Algorithm for Maximum Flow Problem
Can someone help to make this with python?
Make sure the input anformat and output are the same as example
Code:
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define V 15
bool bfs(int rGraph[V][V], int s, int t, int parent[]){
bool visited[V];
memset(visited,0, sizeof(visited));
queue q;
q.push(s);
visited[s]= true;
parent[s]=-1;
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;
}
}
}
return (visited[t]== true);
}
int findDisjointPaths(int graph[V][V], int s, int t){
int u, v;
int rGraph[V][V];
for (u =0; u < V; u++){
for (v =0; v < V; v++){
rGraph[u][v]= graph[u][v];
}
}
int parent[V];
int max_flow =0;
vector> all_path;
while (bfs(rGraph, s, t, parent)){
vector current_path;
int path_flow = INT_MAX;
for (v = t; v != s; v = parent[v]){
u = parent[v];
path_flow = min(path_flow, rGraph[u][v]);
}
for (v = t; v != s; v = parent[v]){
u = parent[v];
current_path.insert(current_path.begin(), v);
rGraph[u][v]-= path_flow;
rGraph[v][u]+= path_flow;
if (v != t && v != s){
for (int temp =0; temp < V; temp++){
rGraph[temp][v]=0;
rGraph[v][temp]=0;
}
}
}
max_flow += path_flow;
all_path.push_back(current_path);
}
cout << endl << max_flow;
for (int temp =0; temp < all_path.size(); temp++){
cout << endl << s <<"";
for (int ttemp =0; ttemp < all_path[temp].size(); ttemp++){
cout << all_path[temp][ttemp]<<"";
}
}
return max_flow;
}
int main(){
//V E
int vertex, edge;
cin >> vertex >> edge;
int s, t;
cin >> s >> t;
int graph[V][V]={0};
for (int temp =0; temp < edge; temp++){
int a, b;
cin >> a >> b;
graph[a][b]=1;
}
findDisjointPaths(graph, s, t);
return 0;
}
Example:
Input Format:
The first line of input consists of two positive integers separated by a space, representing the number of vertices and edges in G respectively.
The second line of input consists of two positive integers separated by a space, representing the starting point and endpoint of the path.
Following that, each line contains two integers (a, b) representing an edge from node a to node b.
Output Format:
Please output the total number of paths and provide the details for each path individually.
INPUT #1
68
14
12
16
26
23
34
35
54
63
OUTPUT #1
1
126354
INPUT #2
1012
27
110
21
23
34
41
45
56
67
69
87
98
105
OUTPUT #2
1
23411056987
INPUT #3
69
41
21
32
35
43
45
56
61
62
63
OUTPUT #3
2
4321
4561
INPUT #4
46
34
14
12
21
24
31
31
OUTPUT #4
2
314
324

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M Kroenke, David J Auer

6th Edition

0132742926, 978-0132742924

More Books

Students also viewed these Databases questions