Algon33

Untitled

Aug 1st, 2023 (edited)
126
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. # [1] Simple Made Easy from Rich Hickey
  2. # A Graph class to represent the network flow
  3. class Graph:
  4. def __init__(self, vertices):
  5. # [3] Not composable if components know about each other
  6. # Independent vertices
  7. self.V = vertices
  8. # [4] Let data be the boundary
  9. # Levels to keep track of layers
  10. self.level = [0] * vertices
  11. # [2] (Syntax, Data)
  12. # Graph represented by an adjacency list
  13. self.graph = [[] for _ in range(vertices)]
  14.  
  15. def add_edge(self, u, v, w):
  16. # [2] (Methods, Functions/Namespaces)
  17. # Encapsulating the addition of an edge within a method
  18. self.graph[u].append([v, w, len(self.graph[v])])
  19. # [8] Anatta, Anicca
  20. # Symmetry in bidirectional connection
  21. self.graph[v].append([u, 0, len(self.graph[u]) - 1])
  22.  
  23. def bfs(self, s, t):
  24. # [5] To be true to all the forces in it, to remove yourself
  25. # Initializing levels, independent of previous states
  26. for i in range(self.V):
  27. self.level[i] = -1
  28. self.level[s] = 0
  29. # [6] A great deal of compensatory movement is needed around it.
  30. # Using a queue for BFS traversal
  31. q = deque()
  32. q.append(s)
  33.  
  34. while q:
  35. u = q.popleft()
  36. # [7] Rehabilitación Neurocognitiva, paralysis person imagines moving finger to trace a spiral carved on wood
  37. # Tracing paths in the network
  38. for v, w, _ in self.graph[u]:
  39. if self.level[v] < 0 and w > 0:
  40. self.level[v] = self.level[u] + 1
  41. q.append(v)
  42. # [4] Let data be the boundary
  43. # Return whether a path exists
  44. return self.level[t] >= 0
  45.  
  46. def dfs(self, u, curr_flow, t):
  47. if u == t:
  48. return curr_flow
  49.  
  50. for v, w, rev in self.graph[u]:
  51. # [8] Dukkha
  52. # The unsatisfactoriness of not finding the right path
  53. if self.level[v] == self.level[u] + 1 and w > 0:
  54. min_cap = min(curr_flow, w)
  55. # [7] Engages whole complexity
  56. # Recursive DFS call
  57. flow = self.dfs(v, min_cap, t)
  58. if flow > 0:
  59. self.graph[u][v][1] -= flow
  60. self.graph[v][self.graph[u][v][2]][1] += flow
  61. return flow
  62. # [8] Anicca
  63. # The impermanence of the state; no path found
  64. return 0
  65.  
  66. def max_flow(self, s, t):
  67. max_flow = 0
  68. # [6] To have stuckness in a system, a great deal of compensatory movement is needed around it
  69. # Looping until no augmenting paths are found
  70. while self.bfs(s, t):
  71. while True:
  72. # [5] Quality without a name
  73. # Searching for the flow in each augmenting path
  74. flow = self.dfs(s, float('inf'), t)
  75. # [2] (Imperative Loops/fold, Set functions)
  76. # Aggregating flows into a total
  77. if flow == 0:
  78. break
  79. max_flow += flow
  80. # [4] Let data be the boundary
  81. # The maximum flow is the result
  82. return max_flow
  83.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment