code: def boykov_kolmogorov_impl(G, s, t, capacity, residual, cutoff):
      if s not in G:
          raise nx.NetworkXError(f"node {str(s)} not in graph")
      if t not in G:
          raise nx.NetworkXError(f"node {str(t)} not in graph")
      if s == t:
          raise nx.NetworkXError("source and sink are the same node")
  
      if residual is None:
          R = build_residual_network(G, capacity)
      else:
          R = residual
  
      # Initialize/reset the residual network.
      # This is way too slow
      # nx.set_edge_attributes(R, 0, 'flow')
      for u in R:
          for e in R[u].values():
              e["flow"] = 0
  
      # Use an arbitrary high value as infinite. It is computed
      # when building the residual network.
      INF = R.graph["inf"]
  
      if cutoff is None:
          cutoff = INF
  
      R_succ = R.succ
      R_pred = R.pred
  
      def grow():
          """Bidirectional breadth-first search for the growth stage.
  
          Returns a connecting edge, that is and edge that connects
          a node from the source search tree with a node from the
          target search tree.
          The first node in the connecting edge is always from the
          source tree and the last node from the target tree.
          """
          while active:
              u = active[0]
              if u in source_tree:
                  this_tree = source_tree
                  other_tree = target_tree
                  neighbors = R_succ
              else:
                  this_tree = target_tree
                  other_tree = source_tree
                  neighbors = R_pred
              for v, attr in neighbors[u].items():
                  if attr["capacity"] - attr["flow"] > 0:
                      if v not in this_tree:
                          if v in other_tree:
                              return (
                                  (u, v)
                                  if this_tree is source_tree
                                  else (v, u)
                              )
                          this_tree[v] = u
                          dist[v] = dist[u] + 1
                          timestamp[v] = timestamp[u]
                          active.append(v)
                      elif v in this_tree and _is_closer(u, v):
                          this_tree[v] = u
                          dist[v] = dist[u] + 1
                          timestamp[v] = timestamp[u]
              _ = active.popleft()
          return None, None
  
      def augment(u, v):
          """Augmentation stage.
  
          Reconstruct path and determine its residual capacity.
          We start from a connecting edge, which links a node
          from the source tree to a node from the target tree.
          The connecting edge is the output of the grow function
          and the input of this function.
          """
          attr = R_succ[u][v]
          flow = min(INF, attr["capacity"] - attr["flow"])
          path = [u]
          # Trace a path from u to s in source_tree.
          w = u
          while w != s:
              n = w
              w = source_tree[n]
              attr = R_pred[n][w]
              flow = min(flow, attr["capacity"] - attr["flow"])
              path.append(w)
          path.reverse()
          # Trace a path from v to t in target_tree.
          path.append(v)
          w = v
          while w != t:
              n = w
              w = target_tree[n]
              attr = R_succ[n][w]
              flow = min(flow, attr["capacity"] - attr["flow"])
              path.append(w)
          # Augment flow along the path and check for saturated edges.
          it = iter(path)
          u = next(it)
          these_orphans = []
          for v in it:
              R_succ[u][v]["flow"] += flow
              R_succ[v][u]["flow"] -= flow
              if R_succ[u][v]["flow"] == R_succ[u][v]["capacity"]:
                  if v in source_tree:
                      source_tree[v] = None
                      these_orphans.append(v)
                  if u in target_tree:
                      target_tree[u] = None
                      these_orphans.append(u)
              u = v
          orphans.extend(sorted(these_orphans, key=dist.get))
          return flow
  
      def adopt():
          """Adoption stage.
  
          Reconstruct search trees by adopting or discarding orphans.
          During augmentation stage some edges got saturated and thus
          the source and target search trees broke down to forests, with
          orphans as roots of some of its trees. We have to reconstruct
          the search trees rooted to source and target before we can grow
          them again.
          """
          while orphans:
              u = orphans.popleft()
              if u in source_tree:
                  tree = source_tree
                  neighbors = R_pred
              else:
                  tree = target_tree
                  neighbors = R_succ
              nbrs = (
                  (n, attr, dist[n])
                  for n, attr in neighbors[u].items()
                  if n in tree
              )
              for v, attr, d in sorted(nbrs, key=itemgetter(2)):
                  if attr["capacity"] - attr["flow"] > 0:
                      if _has_valid_root(v, tree):
                          tree[u] = v
                          dist[u] = dist[v] + 1
                          timestamp[u] = time
                          break
              else:
                  nbrs = (
                      (n, attr, dist[n])
                      for n, attr in neighbors[u].items()
                      if n in tree
                  )
                  for v, attr, d in sorted(nbrs, key=itemgetter(2)):
                      if attr["capacity"] - attr["flow"] > 0:
                          if v not in active:
                              active.append(v)
                      if tree[v] == u:
                          tree[v] = None
                          orphans.appendleft(v)
                  if u in active:
                      active.remove(u)
                  del tree[u]
  
      def _has_valid_root(n, tree):
          path = []
          v = n
          while v is not None:
              path.append(v)
              if v in (s, t):
                  base_dist = 0
                  break
              elif timestamp[v] == time:
                  base_dist = dist[v]
                  break
              v = tree[v]
          else:
              return False
          length = len(path)
          for i, u in enumerate(path, 1):
              dist[u] = base_dist + length - i
              timestamp[u] = time
          return True
  
      def _is_closer(u, v):
          return timestamp[v] <= timestamp[u] and dist[v] > dist[u] + 1
  
      source_tree = {s: None}
      target_tree = {t: None}
      active = deque([s, t])
      orphans = deque()
      flow_value = 0
      # data structures for the marking heuristic
      time = 1
      timestamp = {s: time, t: time}
      dist = {s: 0, t: 0}
      while flow_value < cutoff:
          # Growth stage
          u, v = grow()
          if u is None:
              break
          time += 1
          # Augmentation stage
          flow_value += augment(u, v)
          # Adoption stage
          adopt()
  
      if flow_value * 2 > INF:
          raise nx.NetworkXUnbounded(
              "Infinite capacity path, flow unbounded above."
          )
  
      # Add source and target tree in a graph attribute.
      # A partition that defines a minimum cut can be directly
      # computed from the search trees as explained in the docstrings.
      R.graph["trees"] = (source_tree, target_tree)
      # Add the standard flow_value graph attribute.
      R.graph["flow_value"] = flow_value
      return R
  
imports: from collections import deque
  from operator import itemgetter
  import networkx
  from networkx.algorithms.flow.utils import build_residual_network
label_desc: Use image node_id 2 for calling a global function with example usage: boykov_kolmogorov_impl(G, s, t, capacity, residual, cutoff) and returns: R, None,  None, flow, True, unknown, False, unknown