Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

can someone rewrite this c to python? #include #include #include #include #include #include #include using namespace std; #define V 1 5 bool bfs ( int

can someone rewrite this c to python?
#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;
}

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

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions