Guest User

Untitled

a guest
Nov 28th, 2024
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.18 KB | None | 0 0
  1. import random
  2. import time
  3. from collections import defaultdict, deque
  4.  
  5.  
  6. def solve_dfs(n, m, edges):
  7.     graph: list[dict[int, int]] = [defaultdict(int) for _ in range(n)]
  8.     capacities: list[dict[int, int]] = [defaultdict(int) for _ in range(n)]
  9.     for _ in range(m):
  10.         u, v, w = edges[_]
  11.         graph[u][v] = 0
  12.         capacities[u][v] += w
  13.     source, sink = 0, n-1
  14.  
  15.     ans: int = 0
  16.     while True:
  17.         # Run BFS to find shortest s-t path
  18.         parent: list[int] = [-1] * n
  19.         parent[0] = -2
  20.         queue: deque[tuple[int, int]] = deque([(source, 10**18)])
  21.         while queue:
  22.             u, flow = queue.popleft()
  23.             for v in graph[u]:
  24.                 if parent[v] == -1 and capacities[u][v] > graph[u][v]:
  25.                     parent[v] = u
  26.                     if v == sink:
  27.                         max_capacity = min(flow, capacities[u][v] - graph[u][v])
  28.                         break
  29.                     queue.append((v, min(flow, capacities[u][v] - graph[u][v])))
  30.         if parent[sink] == -1:
  31.             # We didn't find au augmenting path
  32.             break
  33.         v = sink
  34.         while v != source:
  35.             graph[parent[v]][v] += max_capacity  # noqa
  36.             graph[v][parent[v]] -= max_capacity  # noqa
  37.             v = parent[v]
  38.         ans += max_capacity
  39.  
  40.     return ans
  41.  
  42. def solve_bfs(n, m, edges):
  43.     graph: list[dict[int, int]] = [defaultdict(int) for _ in range(n)]
  44.     capacities: list[dict[int, int]] = [defaultdict(int) for _ in range(n)]
  45.     for _ in range(m):
  46.         u, v, w = edges[_]
  47.         graph[u][v] = 0
  48.         capacities[u][v] += w
  49.     source, sink = 0, n-1
  50.  
  51.     ans: int = 0
  52.     while True:
  53.         # Run DFS to find an augmenting s-t path (hopefully will be bad)
  54.         parent: list[int] = [-1] * n
  55.         parent[0] = -2
  56.         stack: list[tuple[int, int]] = [(source, 10**18)]
  57.         while stack:
  58.             u, flow = stack.pop()
  59.             for v in graph[u]:
  60.                 if parent[v] == -1 and capacities[u][v] > graph[u][v]:
  61.                     parent[v] = u
  62.                     if v == sink:
  63.                         max_capacity = min(flow, capacities[u][v] - graph[u][v])
  64.                         break
  65.                     stack.append((v, min(flow, capacities[u][v] - graph[u][v])))
  66.         if parent[sink] == -1:
  67.             # We didn't find au augmenting path
  68.             break
  69.         v = sink
  70.         while v != source:
  71.             graph[parent[v]][v] += max_capacity  # noqa
  72.             graph[v][parent[v]] -= max_capacity  # noqa
  73.             v = parent[v]
  74.         ans += max_capacity
  75.  
  76.     return ans
  77.  
  78.  
  79. for _ in range(100000000):
  80.     # Create random inputs
  81.     n = random.randint(2, 10)
  82.     m = random.randint(1, 20)
  83.     edges = [(random.randint(0, n-1), random.randint(0, n-1), random.randint(0, 10**9)) for _ in range(m)]
  84.  
  85.     start_time = time.time()
  86.     solve_bfs(n, m, edges)
  87.     bfs_time = time.time() - start_time
  88.  
  89.     start_time = time.time()
  90.     solve_dfs(n, m, edges)
  91.     dfs_time = time.time() - start_time
  92.  
  93.     if dfs_time > 0.8 and bfs_time < 0.5:
  94.         print(n, m, edges)
  95.         print("DFS:", dfs_time)
  96.         print("BFS:", bfs_time)
  97.         print()
  98.  
Advertisement
Add Comment
Please, Sign In to add comment