code: def is_maximal_matching(G, matching):
      """Return True if ``matching`` is a maximal matching of ``G``
  
      A *maximal matching* in a graph is a matching in which adding any
      edge would cause the set to no longer be a valid matching.
  
      Parameters
      ----------
      G : NetworkX graph
  
      matching : dict or set
          A dictionary or set representing a matching. If a dictionary, it
          must have ``matching[u] == v`` and ``matching[v] == u`` for each
          edge ``(u, v)`` in the matching. If a set, it must have elements
          of the form ``(u, v)``, where ``(u, v)`` is an edge in the
          matching.
  
      Returns
      -------
      bool
          Whether the given set or dictionary represents a valid maximal
          matching in the graph.
  
      Examples
      --------
      >>> G = nx.Graph([(1, 2), (1, 3), (2, 3), (3, 4), (3, 5)])
      >>> nx.is_maximal_matching(G, {(1, 2), (3, 4)})
      True
  
      """
      if isinstance(matching, dict):
          matching = matching_dict_to_set(matching)
      # If the given set is not a matching, then it is not a maximal matching.
      edges = set()
      nodes = set()
      for edge in matching:
          if len(edge) != 2:
              raise nx.NetworkXError(
                  f"matching has non-2-tuple edge {edge}"
              )
          u, v = edge
          if u not in G or v not in G:
              raise nx.NetworkXError(
                  f"matching contains edge {edge} with node not in G"
              )
          if u == v:
              return False
          if not G.has_edge(u, v):
              return False
          if u in nodes or v in nodes:
              return False
          nodes.update(edge)
          edges.add(edge)
          edges.add((v, u))
      # A matching is maximal if adding any new edge from G to it
      # causes the resulting set to match some node twice.
      # Be careful to check for adding selfloops
      for u, v in G.edges:
          if (u, v) not in edges:
              # could add edge (u, v) to edges and have a bigger matching
              if u not in nodes and v not in nodes and u != v:
                  return False
      return True
  
file_path: networkx/networkx/algorithms/matching.py
imports: from collections import Counter
  from itertools import combinations, repeat
  import networkx
  from networkx.utils import not_implemented_for
label_desc: Use image node_id 4 for calling a global function with example usage: is_maximal_matching(G, matching) and returns: True, False, False, False, False