Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

What is the maximum flow of the network below from node 1 to node 7 ? Answer must be number and Python algorithm you used

What is the maximum flow of the network below from node 1 to node 7?
Answer must be number and Python algorithm you used to get that number.
Use it as an example to calculate the maximum flow (add all edges)for the network in the photo.
class MaximumFlow:
def __init__(self, nodes):
self.nodes = nodes
self.graph ={}
for i in self.nodes:
for j in self.nodes:
self.graph[(i, j)]=0
def add_edge(self, node_a, node_b, capacity):
self.graph[(node_a, node_b)]+= capacity
def add_flow(self, node, sink, flow):
if node in self.seen:
return 0
self.seen.add(node)
if node == sink:
return flow
for next_node in self.nodes:
if self.flow[(node, next_node)]>0:
new_flow = min(flow, self.flow[(node, next_node)])
inc = self.add_flow(next_node, sink, new_flow)
if inc >0:
self.flow[(node, next_node)]-= inc
self.flow[(next_node, node)]+= inc
return inc
return 0
def construct(self, source, sink):
self.flow = self.graph.copy()
total =0
while True:
self.seen = set()
add = self.add_flow(source, sink, float("inf"))
if add ==0:
break
total += add
return total
The algorithm can be used like this in the example network:
m = MaximumFlow([1,2,3,4,5])
m.add_edge(1,2,4)
m.add_edge(1,3,6)
m.add_edge(2,3,8)
m.add_edge(2,4,3)
m.add_edge(3,5,4)
m.add_edge(4,5,5)
print(m.construct(1,5)) # 7
image text in transcribed

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

SQL For Data Science Data Cleaning Wrangling And Analytics With Relational Databases

Authors: Antonio Badia

1st Edition

3030575918, 978-3030575915

More Books

Students also viewed these Databases questions

Question

8. Explain the relationship between communication and context.

Answered: 1 week ago