code: def _filter(graph, targets, full):
      import networkx as nx
  
      if not targets:
          return graph
  
      new_graph = graph.copy()
      if not full:
          descendants = set()
          for target in targets:
              descendants.update(nx.descendants(graph, target))
              descendants.add(target)
          new_graph.remove_nodes_from(set(graph.nodes()) - descendants)
  
      undirected = new_graph.to_undirected()
      connected = set()
      for target in targets:
          connected.update(
              nx.node_connected_component(undirected, target)
          )
  
      new_graph.remove_nodes_from(set(new_graph.nodes()) - connected)
      return new_graph
  
file_path: dvc/dvc/commands/dag.py
imports: from typing import TYPE_CHECKING
  from dvc.cli import formatter
  from dvc.cli.command import CmdBase
  from dvc.cli.utils import append_doc_link
  from dvc.ui import ui
label_desc: Use image node_id 7 for calling a global function with example usage: _filter(graph, targets, full) and returns: new_graph, graph