def cyclomatic_complexity(control_flow_graph):
  """Computes the cyclomatic complexity of a function from its cfg."""
  enter_block = next(control_flow_graph.get_enter_blocks())

  new_blocks = []
  seen_block_ids = set()
  new_blocks.append(enter_block)
  seen_block_ids.add(id(enter_block))
  num_edges = 0

  while new_blocks:
    block = new_blocks.pop()
    for next_block in block.exits_from_end:
      num_edges += 1
      if id(next_block) not in seen_block_ids:
        new_blocks.append(next_block)
        seen_block_ids.add(id(next_block))
  num_nodes = len(seen_block_ids)

  p = 1  # num_connected_components
  e = num_edges
  n = num_nodes
  return e - n + 2 * p