Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

what is the total/ OVERALL COMPLEXITY of this code: import copy # to make residual graph import decimal import time #___________________________ Edmonds-Karp Algorithm---->(graph(adjacency array),queue,array) _______________________

what is the total/ OVERALL COMPLEXITY of this code:

import copy # to make residual graph import decimal import time #___________________________ Edmonds-Karp Algorithm---->(graph(adjacency array),queue,array) _______________________ # list: The list is used to store the adjacency list of the graph #Queue: A queue is used to store the vertices that are to be visited in a breadth-first search. # Array: An array is used to store the visited status of each vertex, as well as the parent of each vertex.

def bfs(graph, s, t, parent): # O(V^2) # Mark all the vertices as not visited visited = [False] * len(graph) # O(1) queue = [] # Create a queue for BFS # O(1) # Mark the source node as visited and enqueue it queue.append(s) # O(1) visited[s] = True # O(1) # Standard BFS Loop while queue: # queue not empty # O(V^2) u = queue.pop(0) # Dequeue a vertex from queue and print it # O(1) for ind in range(len(graph[u])): # O(V) if not visited[ind] and graph[u][ind] > 0: # O(1) queue.append(ind) # O(1) visited[ind] = True # O(1) parent[ind] = u # O(1) return visited[t] # O(1)

# Returns the maximum flow from s to t in the given graph def Edmonds_Karp(graph, source, sink): # This array is filled by BFS and to store path parent = [-1] * (len(graph)) # make parent of all vertix -1 # O(1) residualGraph = copy.deepcopy(graph) # O(V^2) max_flow = 0 # There is no flow initially # O(1) # Augment the flow while there is path from source to sink while bfs(residualGraph, source, sink, parent): # O(V^2) path_flow = float("Inf") # O(1) s = sink # O(1) path = [] # to color it # O(1) while s != source: # O(P) # stort path to color it path.append((parent[s], s)) # O(1) # Find the minimum value in select path path_flow = min(path_flow, residualGraph[parent[s]][s]) # O(1) s = parent[s] # O(1) # Add path flow to overall flow max_flow += path_flow # O(1) # update residual capacities of the edges and reverse edges v = sink while v != source: # O(P) u = parent[v] # O(1) residualGraph[u][v] -= path_flow # O(1) residualGraph[v][u] += path_flow # O(1) v = parent[v] # O(1) return max_flow # O(1)

c = [[0 for i in range(5000)] for j in range(5000)]

def loadGraph(): my_file = open("/Users/elaff//Algorithm/project/networkdata.txt", "r") for number in my_file: number=number.strip() nums=number.split("\t") row=int(nums[0].strip())-1 col=int(nums[1].strip())-1 c[row][col]=float(nums[2].strip()) loadGraph() s = 0 t = 90

start3 = time.perf_counter() f=Edmonds_Karp(c,s,t) print("Maximum flow: ", f) end3 = time.perf_counter() #o(1) print("time: ",end3 - start3) # o(1)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

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

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions