Question
what is the total/ OVERALL COMPLEXITY of this code : import time #________________________Dinic's Algorithm---->(adj,graph,queue,array)______________________ # list and a class. The list is used to store
what is the total/ OVERALL COMPLEXITY of this code :
import time #________________________Dinic's Algorithm---->(adj,graph,queue,array)______________________ # list and a class. The list is used to store the adjacency list of the graph, and the class is used to store information about each edge in the graph. # a queue and an array. The queue is used to store the nodes that need to be visited, while the array is used to store the level of each node.
class Edge: def __init__(self, v, flow, C, rev): self.v = v self.flow = flow self.C = C self.rev = rev
class Graph: def __init__(self, V): self.adj = [[] for i in range(V)] self.V = V self.level = [0 for i in range(V)] def addEdge(self, u, v, C): a = Edge(v, 0, C, len(self.adj[v])) b = Edge(u, 0, 0, len(self.adj[u])) self.adj[u].append(a) self.adj[v].append(b)
def BFS(self, s, t): for i in range(self.V): self.level[i] = -1 self.level[s] = 0 q = [] q.append(s) while q: u = q.pop(0) for i in range(len(self.adj[u])): e = self.adj[u][i] if self.level[e.v] < 0 and e.flow < e.C:
self.level[e.v] = self.level[u]+1 q.append(e.v)
return False if self.level[t] < 0 else True def sendFlow(self, u, flow, t, start):
if u == t: return flow while start[u] < len(self.adj[u]): e = self.adj[u][start[u]] if self.level[e.v] == self.level[u]+1 and e.flow < e.C:
curr_flow = min(flow, e.C-e.flow) temp_flow = self.sendFlow(e.v, curr_flow, t, start) if temp_flow and temp_flow > 0:
e.flow += temp_flow self.adj[e.v][e.rev].flow -= temp_flow return temp_flow start[u] += 1 def DinicMaxflow(self, s, t):
if s == t: return -1
total = 0
while self.BFS(s, t) == True:
start = [0 for i in range(self.V+1)] while True: flow = self.sendFlow(s, float('inf'), t, start) if not flow: break total += flow return total # c = [[0 for i in range(5000)] for j in range(5000)] g = Graph(5000)
def loadGraph(): my_file = open("/Users/elaff//Algorithm/project/networkdata.txt", "r") for number in my_file: nums=number.split("\t") row=int(nums[0])-1 col=int(nums[1].strip())-1 g.addEdge(row, col, float(nums[2].strip()))
loadGraph() start3 = time.perf_counter() f = g.DinicMaxflow(0, 90) 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started