code: def transitive_reduction(G):
      """Returns transitive reduction of a directed graph
  
      The transitive reduction of G = (V,E) is a graph G- = (V,E-) such that
      for all v,w in V there is an edge (v,w) in E- if and only if (v,w) is
      in E and there is no path from v to w in G with length greater than 1.
  
      Parameters
      ----------
      G : NetworkX DiGraph
          A directed acyclic graph (DAG)
  
      Returns
      -------
      NetworkX DiGraph
          The transitive reduction of `G`
  
      Raises
      ------
      NetworkXError
          If `G` is not a directed acyclic graph (DAG) transitive reduction is
          not uniquely defined and a :exc:`NetworkXError` exception is raised.
  
      Examples
      --------
      To perform transitive reduction on a DiGraph:
  
      >>> DG = nx.DiGraph([(1, 2), (2, 3), (1, 3)])
      >>> TR = nx.transitive_reduction(DG)
      >>> list(TR.edges)
      [(1, 2), (2, 3)]
  
      To avoid unnecessary data copies, this implementation does not return a
      DiGraph with node/edge data.
      To perform transitive reduction on a DiGraph and transfer node/edge data:
  
      >>> DG = nx.DiGraph()
      >>> DG.add_edges_from([(1, 2), (2, 3), (1, 3)], color='red')
      >>> TR = nx.transitive_reduction(DG)
      >>> TR.add_nodes_from(DG.nodes(data=True))
      >>> TR.add_edges_from((u, v, DG.edges[u, v]) for u, v in TR.edges)
      >>> list(TR.edges(data=True))
      [(1, 2, {'color': 'red'}), (2, 3, {'color': 'red'})]
  
      References
      ----------
      https://en.wikipedia.org/wiki/Transitive_reduction
  
      """
      if not is_directed_acyclic_graph(G):
          msg = (
              "Directed Acyclic Graph required for transitive_reduction"
          )
          raise nx.NetworkXError(msg)
      TR = nx.DiGraph()
      TR.add_nodes_from(G.nodes())
      descendants = {}
      # count before removing set stored in descendants
      check_count = dict(G.in_degree)
      for u in G:
          u_nbrs = set(G[u])
          for v in G[u]:
              if v in u_nbrs:
                  if v not in descendants:
                      descendants[v] = {
                          y for x, y in nx.dfs_edges(G, v)
                      }
                  u_nbrs -= descendants[v]
              check_count[v] -= 1
              if check_count[v] == 0:
                  del descendants[v]
          TR.add_edges_from((u, v) for v in u_nbrs)
      return TR
  
imports: import heapq
  from collections import deque
  from functools import partial
  from itertools import chain, combinations, product, starmap
  from math import gcd
  import networkx
  from networkx.utils import arbitrary_element, not_implemented_for, pairwise
label_desc: Use image node_id 12 for calling a global function with example usage: transitive_reduction(G) and returns: TR