class_docstr: Base class for representing Cluster Graph.
  
  Cluster graph is an undirected graph which is associated with a subset of variables. The graph contains undirected
  edges that connects clusters whose scopes have a non-empty intersection.
  
  Formally, a cluster graph is  :math:`\mathcal{U}` for a set of factors :math:`\Phi` over :math:`\mathcal{X}` is an
  undirected graph, each of whose nodes :math:`i` is associated with a subset :math:`C_i \subseteq X`. A cluster
  graph must be family-preserving - each factor :math:`\phi \in \Phi` must be associated with a cluster C, denoted
  :math:`\alpha(\phi)`, such that :math:`Scope[\phi] \subseteq C_i`. Each edge between a pair of clusters :math:`C_i`
  and :math:`C_j` is associated with a sepset :math:`S_{i,j} \subseteq C_i \cap C_j`.
  
  Parameters
  ----------
  data: input graph
      Data to initialize graph. If data=None (default) an empty graph is created. The data is an edge list
  
  Examples
  --------
  Create an empty ClusterGraph with no nodes and no edges
  
  >>> from pgmpy.models import ClusterGraph
  >>> G = ClusterGraph()
  
  G can be grown by adding clique nodes.
  
  **Nodes:**
  
  Add a tuple (or list or set) of nodes as single clique node.
  
  >>> G.add_node(('a', 'b', 'c'))
  >>> G.add_nodes_from([('a', 'b'), ('a', 'b', 'c')])
  
  **Edges:**
  
  G can also be grown by adding edges.
  
  >>> G.add_edge(('a', 'b', 'c'), ('a', 'b'))
  
  or a list of edges
  
  >>> G.add_edges_from([(('a', 'b', 'c'), ('a', 'b')),
  ...                   (('a', 'b', 'c'), ('a', 'c'))])
class_docstr_tok: ["Base","class","for","representing","Cluster","Graph",".","Cluster","graph","is","an","undirected","graph","which","is","associated","with","a","subset","of","variables",".","The","graph","contains","undirected","edges","that","connects","clusters","whose","scopes","have","a","non-empty","intersection",".","Formally",",","a","cluster","graph","is",":","math",":","`","\\mathcal","{","U","}","`","for","a","set","of","factors",":","math",":","`","\\Phi","`","over",":","math",":","`","\\mathcal","{","X","}","`","is","an","undirected","graph",",","each","of","whose","nodes",":","math",":","`","i","`","is","associated","with","a","subset",":","math",":","`","C_i","\\subseteq","X","`",".","A","cluster","graph","must","be","family-preserving","-","each","factor",":","math",":","`","\\phi","\\in","\\Phi","`","must","be","associated","with","a","cluster","C",",","denoted",":","math",":","`","\\alpha","(","\\phi",")","`",",","such","that",":","math",":","`","Scope","[","\\phi","]","\\subseteq","C_i","`",".","Each","edge","between","a","pair","of","clusters",":","math",":","`","C_i","`","and",":","math",":","`","C_j","`","is","associated","with","a","sepset",":","math",":","`","S_","{","i",",","j","}","\\subseteq","C_i","\\cap","C_j","`",".","Parameters","--","--","--","--","--","data",":","input","graph","Data","to","initialize","graph",".","If","data=None","(","default",")","an","empty","graph","is","created",".","The","data","is","an","edge","list","Examples","--","--","--","--","Create","an","empty","ClusterGraph","with","no","nodes","and","no","edges",">",">",">","from","pgmpy.models","import","ClusterGraph",">",">",">","G","=","ClusterGraph","(",")","G","can","be","grown","by","adding","clique","nodes",".","*","*","Nodes",":","*","*","Add","a","tuple","(","or","list","or","set",")","of","nodes","as","single","clique","node",".",">",">",">","G.add_node","(","(","'","a","'",",","'","b","'",",","'","c","'",")",")",">",">",">","G.add_nodes_from","(","[","(","'","a","'",",","'","b","'",")",",","(","'","a","'",",","'","b","'",",","'","c","'",")","]",")","*","*","Edges",":","*","*","G","can","also","be","grown","by","adding","edges",".",">",">",">","G.add_edge","(","(","'","a","'",",","'","b","'",",","'","c","'",")",",","(","'","a","'",",","'","b","'",")",")","or","a","list","of","edges",">",">",">","G.add_edges_from","(","[","(","(","'","a","'",",","'","b","'",",","'","c","'",")",",","(","'","a","'",",","'","b","'",")",")",",","...","(","(","'","a","'",",","'","b","'",",","'","c","'",")",",","(","'","a","'",",","'","c","'",")",")","]",")"]
docstr: Returns the cardinality of the node
  
  Parameters
  ----------
  node: any hashable python object (optional)
      The node whose cardinality we want. If node is not specified returns a
      dictionary with the given variable as keys and their respective cardinality
      as values.
  
  Returns
  -------
  int or dict : If node is specified returns the cardinality of the node.
                If node is not specified returns a dictionary with the given
                variable as keys and their respective cardinality as values.
  
  
  Examples
  --------
  >>> from pgmpy.models import ClusterGraph
  >>> from pgmpy.factors.discrete import DiscreteFactor
  >>> student = ClusterGraph()
  >>> factor = DiscreteFactor(['Alice', 'Bob'], cardinality=[2, 2],
  ...                 values=np.random.rand(4))
  >>> student.add_node(('Alice', 'Bob'))
  >>> student.add_factors(factor)
  >>> student.get_cardinality()
  defaultdict(<class 'int'>, {'Alice': 2, 'Bob': 2})
  
  >>> student.get_cardinality(node='Alice')
  2
docstr_tok: ["Returns","the","cardinality","of","the","node","Parameters","--","--","--","--","--","node",":","any","hashable","python","object","(","optional",")","The","node","whose","cardinality","we","want",".","If","node","is","not","specified","returns","a","dictionary","with","the","given","variable","as","keys","and","their","respective","cardinality","as","values",".","Returns","--","--","--","-","int","or","dict",":","If","node","is","specified","returns","the","cardinality","of","the","node",".","If","node","is","not","specified","returns","a","dictionary","with","the","given","variable","as","keys","and","their","respective","cardinality","as","values",".","Examples","--","--","--","--",">",">",">","from","pgmpy.models","import","ClusterGraph",">",">",">","from","pgmpy.factors.discrete","import","DiscreteFactor",">",">",">","student","=","ClusterGraph","(",")",">",">",">","factor","=","DiscreteFactor","(","[","'Alice","'",",","'Bob","'","]",",","cardinality=","[","2",",","2","]",",","...","values=np.random.rand","(","4",")",")",">",">",">","student.add_node","(","(","'Alice","'",",","'Bob","'",")",")",">",">",">","student.add_factors","(","factor",")",">",">",">","student.get_cardinality","(",")","defaultdict","(","<","class","'int","'",">",",","{","'Alice","'",":","2",",","'Bob","'",":","2","}",")",">",">",">","student.get_cardinality","(","node='Alice","'",")","2"]
code: def get_cardinality(self, node=None):
      """
      Returns the cardinality of the node
  
      Parameters
      ----------
      node: any hashable python object (optional)
          The node whose cardinality we want. If node is not specified returns a
          dictionary with the given variable as keys and their respective cardinality
          as values.
  
      Returns
      -------
      int or dict : If node is specified returns the cardinality of the node.
                    If node is not specified returns a dictionary with the given
                    variable as keys and their respective cardinality as values.
  
  
      Examples
      --------
      >>> from pgmpy.models import ClusterGraph
      >>> from pgmpy.factors.discrete import DiscreteFactor
      >>> student = ClusterGraph()
      >>> factor = DiscreteFactor(['Alice', 'Bob'], cardinality=[2, 2],
      ...                 values=np.random.rand(4))
      >>> student.add_node(('Alice', 'Bob'))
      >>> student.add_factors(factor)
      >>> student.get_cardinality()
      defaultdict(<class 'int'>, {'Alice': 2, 'Bob': 2})
  
      >>> student.get_cardinality(node='Alice')
      2
      """
      if node:
          for factor in self.factors:
              for variable, cardinality in zip(
                  factor.scope(), factor.cardinality
              ):
                  if node == variable:
                      return cardinality
  
      else:
          cardinalities = defaultdict(int)
          for factor in self.factors:
              for variable, cardinality in zip(
                  factor.scope(), factor.cardinality
              ):
                  cardinalities[variable] = cardinality
          return cardinalities
  
file_path: pgmpy/pgmpy/models/ClusterGraph.py
imports: from collections import defaultdict
  import numpy
  from pgmpy.base import UndirectedGraph
  from pgmpy.factors import factor_product
label_desc: Use image node_id 8 for calling the ClusterGraph obj's underlying member method code with example usage: obj.get_cardinality(node) and returns: cardinalities, cardinality