code: def transitive_closure(graph, reflexive=False):
      """
      Calculate the transitive closure of a directed graph,
      optionally the reflexive transitive closure.
  
      The algorithm is a slight modification of the "Marking Algorithm" of
      Ioannidis & Ramakrishnan (1998) "Efficient Transitive Closure Algorithms".
  
      :param graph: the initial graph, represented as a dictionary of sets
      :type graph: dict(set)
      :param reflexive: if set, also make the closure reflexive
      :type reflexive: bool
      :rtype: dict(set)
      """
      if reflexive:
          base_set = lambda k: {k}
      else:
          base_set = lambda k: set()
      # The graph U_i in the article:
      agenda_graph = {k: graph[k].copy() for k in graph}
      # The graph M_i in the article:
      closure_graph = {k: base_set(k) for k in graph}
      for i in graph:
          agenda = agenda_graph[i]
          closure = closure_graph[i]
          while agenda:
              j = agenda.pop()
              closure.add(j)
              closure |= closure_graph.setdefault(j, base_set(j))
              agenda |= agenda_graph.get(j, base_set(j))
              agenda -= closure
      return closure_graph
  
imports: import inspect
  import locale
  import os
  import pydoc
  import re
  import textwrap
  import unicodedata
  import warnings
  from collections import defaultdict, deque
  from itertools import chain, combinations, islice, tee
  from pprint import pprint
  from urllib.request import HTTPPasswordMgrWithDefaultRealm, ProxyBasicAuthHandler, ProxyDigestAuthHandler, ProxyHandler, build_opener, getproxies, install_opener
  from nltk.collections import *
  from nltk.internals import deprecated, raise_unorderable_types, slice_bounds
label_desc: Use image node_id 22 for calling a global function with example usage: transitive_closure(graph, reflexive) and returns: closure_graph