def bron_kerbosch(self, P: Set[Node], R: Set[Node], X: Set[Node], Cs: List[Set[Node]]): """! \brief apply bron kerbosch algorithm for finding maximal cliques Code taken from: arxiv.org/1006.5440 \code proc BronKerbosch(P,R,X) 1: if P∪X = empty then 2: report R as a maximal clique 3: end if 4: for each vertex v∈P do 5: BronKerbosch(P ∩ Neigbours(v), R ∪ {v}, X ∩ Neighbours(v)) 6: P←P\{v} 7: X←X∪{v} 8: end for \endcode """ if len(P.union(X)) == 0: Cs.append(R) for v in P: self.bron_kerbosch( P=P.intersection(BaseGraphOps.neighbours_of(self, v)), R=R.union([v]), X=X.intersection(BaseGraphOps.neighbours_of(self, v)), Cs=Cs, ) P = P.difference([v]) X = X.union([v])
def markov_blanket(self, t: NumCatRVariable) -> Set[NumCatRVariable]: """! get markov blanket of a node from K. Murphy, 2012, p. 662 """ if BaseGraphOps.is_in(self, t) is False: raise ValueError("Node not in graph: " + str(t)) ns: Set[NumCatRVariable] = BaseGraphOps.neighbours_of(self, t) return ns
def order_by_greedy_metric( self, nodes: Set[NumCatRVariable], s: Callable[[Graph, Dict[Node, bool]], Optional[Node]] = min_unmarked_neighbours, ) -> Dict[str, int]: """! From Koller and Friedman 2009, p. 314 """ marked = {n.id(): False for n in nodes} cardinality = {n.id(): -1 for n in nodes} for i in range(len(nodes)): X = s(g=self, nodes=nodes, marked=marked) if X is not None: cardinality[X.id()] = i TEMP = BaseGraphOps.neighbours_of(self, X) while TEMP: n_x = TEMP.pop() for n in BaseGraphOps.neighbours_of(self, X): self = BaseGraphAlgOps.added_edge_between_if_none( self, n_x, n, is_directed=False) marked[X.id()] = True return cardinality
def nb_neighbours_of(g: AbstractGraph, n: AbstractNode) -> int: """! \brief obtain number of neighbours of a given node. \param n node whose neighbour set we are interested in. \see Graph.neighbours_of Number of nodes in the neighbour set of n. \code{.py} >>> n1 = Node("n1", {}) >>> n2 = Node("n2", {}) >>> n3 = Node("n3", {}) >>> n4 = Node("n4", {}) >>> e1 = Edge( >>> "e1", start_node=n1, end_node=n2, edge_type=EdgeType.UNDIRECTED >>> ) >>> e2 = Edge( >>> "e2", start_node=n2, end_node=n3, edge_type=EdgeType.UNDIRECTED >>> ) >>> e3 = Edge( >>> "e3", start_node=n3, end_node=n4, edge_type=EdgeType.UNDIRECTED >>> ) >>> graph_2 = Graph( >>> "g2", >>> data={"my": "graph", "data": "is", "very": "awesome"}, >>> nodes=set([n1, n2, n3, n4]), >>> edges=set([e1, e2, e3]), >>> ) >>> graph_2.nb_neighbours_of(n2) >>> 2 \endcode """ return len(BaseGraphOps.neighbours_of(g, n))
def test_neighbours_of(self): ndes = set( [n.id() for n in BaseGraphOps.neighbours_of(self.graph_2, self.n2)] ) self.assertEqual(ndes, set([self.n1.id(), self.n3.id()]))