Skip to content

Commit

Permalink
Improve cliques_number_of() and cliques_containing_vertices()
Browse files Browse the repository at this point in the history
The previous implementation was taken from the ones that are deprecated
in networkx 3.1.

We replace it by a better implementation suggested by David Coudert.
  • Loading branch information
tornaria committed May 5, 2023
1 parent aa4dd4b commit f6778f4
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/sage/graphs/graph.py
Expand Up @@ -6796,11 +6796,16 @@ def cliques_number_of(self, vertices=None, cliques=None):
if cliques is None:
cliques = self.cliques_maximal()

if vertices in self: # single vertex
if vertices in self: # single vertex
return sum(1 for c in cliques if vertices in c)
else:
return { v : sum(1 for c in cliques if v in c)
for v in vertices or self }

from collections import Counter
count = Counter()

for c in cliques:
count.update(c)

return { v : count[v] for v in vertices or self }

@doc_index("Clique-related methods")
def cliques_get_max_clique_graph(self):
Expand Down Expand Up @@ -7544,11 +7549,17 @@ def cliques_containing_vertex(self, vertices=None, cliques=None):
if cliques is None:
cliques = self.cliques_maximal()

if vertices in self: # single vertex
if vertices in self: # single vertex
return [c for c in cliques if vertices in c]
else:
return { v : [c for c in cliques if v in c]
for v in vertices or self }

from collections import defaultdict
d = defaultdict(list)

for c in cliques:
for v in c:
d[v].append(c)

return { v : d[v] for v in vertices or self }

@doc_index("Clique-related methods")
def clique_complex(self):
Expand Down

0 comments on commit f6778f4

Please sign in to comment.