Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Making Databases Work The Pragmatic Wisdom Of Michael Stonebraker

Authors: Michael L. Brodie

1st Edition

1947487167, 978-1947487161

More Books

Students also viewed these Databases questions

Question

3. What is the standard error of the mean for a sample size of 100?

Answered: 1 week ago

Question

What are the attributes of a technical decision?

Answered: 1 week ago

Question

How do the two components of this theory work together?

Answered: 1 week ago

Question

Write a letter asking them to refund your $1,500 down payment.

Answered: 1 week ago