code: def prim_mst_edges(
      G,
      minimum,
      weight="weight",
      keys=True,
      data=True,
      ignore_nan=False,
  ):
      """Iterate over edges of Prim's algorithm min/max spanning tree.
  
      Parameters
      ----------
      G : NetworkX Graph
          The graph holding the tree of interest.
  
      minimum : bool (default: True)
          Find the minimum (True) or maximum (False) spanning tree.
  
      weight : string (default: 'weight')
          The name of the edge attribute holding the edge weights.
  
      keys : bool (default: True)
          If `G` is a multigraph, `keys` controls whether edge keys ar yielded.
          Otherwise `keys` is ignored.
  
      data : bool (default: True)
          Flag for whether to yield edge attribute dicts.
          If True, yield edges `(u, v, d)`, where `d` is the attribute dict.
          If False, yield edges `(u, v)`.
  
      ignore_nan : bool (default: False)
          If a NaN is found as an edge weight normally an exception is raised.
          If `ignore_nan is True` then that edge is ignored instead.
  
      """
      is_multigraph = G.is_multigraph()
      push = heappush
      pop = heappop
  
      nodes = set(G)
      c = count()
  
      sign = 1 if minimum else -1
  
      while nodes:
          u = nodes.pop()
          frontier = []
          visited = {u}
          if is_multigraph:
              for v, keydict in G.adj[u].items():
                  for k, d in keydict.items():
                      wt = d.get(weight, 1) * sign
                      if isnan(wt):
                          if ignore_nan:
                              continue
                          msg = f"NaN found as an edge weight. Edge {(u, v, k, d)}"
                          raise ValueError(msg)
                      push(frontier, (wt, next(c), u, v, k, d))
          else:
              for v, d in G.adj[u].items():
                  wt = d.get(weight, 1) * sign
                  if isnan(wt):
                      if ignore_nan:
                          continue
                      msg = f"NaN found as an edge weight. Edge {(u, v, d)}"
                      raise ValueError(msg)
                  push(frontier, (wt, next(c), u, v, d))
          while nodes and frontier:
              if is_multigraph:
                  W, _, u, v, k, d = pop(frontier)
              else:
                  W, _, u, v, d = pop(frontier)
              if v in visited or v not in nodes:
                  continue
              # Multigraphs need to handle edge keys in addition to edge data.
              if is_multigraph and keys:
                  if data:
                      yield u, v, k, d
                  else:
                      yield u, v, k
              else:
                  if data:
                      yield u, v, d
                  else:
                      yield u, v
              # update frontier
              visited.add(v)
              nodes.discard(v)
              if is_multigraph:
                  for w, keydict in G.adj[v].items():
                      if w in visited:
                          continue
                      for k2, d2 in keydict.items():
                          new_weight = d2.get(weight, 1) * sign
                          if isnan(new_weight):
                              if ignore_nan:
                                  continue
                              msg = f"NaN found as an edge weight. Edge {(v, w, k2, d2)}"
                              raise ValueError(msg)
                          push(
                              frontier,
                              (new_weight, next(c), v, w, k2, d2),
                          )
              else:
                  for w, d2 in G.adj[v].items():
                      if w in visited:
                          continue
                      new_weight = d2.get(weight, 1) * sign
                      if isnan(new_weight):
                          if ignore_nan:
                              continue
                          msg = f"NaN found as an edge weight. Edge {(v, w, d2)}"
                          raise ValueError(msg)
                      push(frontier, (new_weight, next(c), v, w, d2))
  
imports: from dataclasses import dataclass, field
  from enum import Enum
  from heapq import heappop, heappush
  from itertools import count
  from math import isnan
  from operator import itemgetter
  from queue import PriorityQueue
  import networkx
  from networkx.utils import UnionFind, not_implemented_for, py_random_state
label_desc: Use image node_id 3 for calling a global function with example usage: prim_mst_edges(G, minimum, weight, keys, data, ignore_nan) without return types