t will give you a maximum flow. This version use BFS to search path. Assume the first is the source and the last is the sink. Time complexity - O(Ef) example graph = [[0, 16, 13, 0, 0, 0], [0, 0, 10, 12, 0, 0], [0, 4, 0, 0, 14, 0],
t will give you a maximum flow. This version use BFS to search path. Assume the first is the source and the last is the sink. Time complexity - O(Ef) example graph = [[0, 16, 13, 0, 0, 0], [0, 0, 10, 12, 0, 0], [0, 4, 0, 0, 14, 0],
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
Given a n*n adjacency array. | |
it will give you a maximum flow. | |
This version use BFS to search path. | |
Assume the first is the source and the last is the sink. | |
Time complexity - O(Ef) | |
example | |
graph = [[0, 16, 13, 0, 0, 0], | |
[0, 0, 10, 12, 0, 0], | |
[0, 4, 0, 0, 14, 0], | |
[0, 0, 9, 0, 0, 20], | |
[0, 0, 0, 7, 0, 4], | |
[0, 0, 0, 0, 0, 0]] | |
answer should be | |
23 | |
""" | |
import copy | |
import queue | |
import math | |
def maximum_flow_bfs(adjacency_matrix): | |
""" | |
Get the maximum flow through a graph using a breadth first search | |
""" | |
#initial setting | |
new_array=copy.deepcopy(adjacency_matrix) | |
total=0 | |
whileTrue: | |
#setting min to max_value | |
min_flow=math.inf | |
#save visited nodes | |
visited= [0]*len(new_array) | |
#save parent nodes | |
path= [0]*len(new_array) | |
#initialize queue for BFS | |
bfs=queue.Queue() | |
#initial setting | |
visited[0] =1 | |
bfs.put(0) | |
#BFS to find path | |
whilebfs.qsize() >0: | |
#pop from queue | |
src=bfs.get() | |
forkinrange(len(new_array)): | |
#checking capacity and visit | |
if(new_array[src][k] >0andvisited[k] ==0 ): | |
#if not, put into queue and chage to visit and save path | |
visited[k] =1 | |
bfs.put(k) | |
path[k] =src | |
#if there is no path from src to sink | |
ifvisited[len(new_array) -1] ==0: | |
break | |
#initial setting | |
tmp=len(new_array) -1.
|
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 3 steps with 1 images
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education