Skip to content

Commit f6778f4

Browse files
committed
Improve cliques_number_of() and cliques_containing_vertices()
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.
1 parent aa4dd4b commit f6778f4

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

src/sage/graphs/graph.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6796,11 +6796,16 @@ def cliques_number_of(self, vertices=None, cliques=None):
67966796
if cliques is None:
67976797
cliques = self.cliques_maximal()
67986798

6799-
if vertices in self: # single vertex
6799+
if vertices in self: # single vertex
68006800
return sum(1 for c in cliques if vertices in c)
6801-
else:
6802-
return { v : sum(1 for c in cliques if v in c)
6803-
for v in vertices or self }
6801+
6802+
from collections import Counter
6803+
count = Counter()
6804+
6805+
for c in cliques:
6806+
count.update(c)
6807+
6808+
return { v : count[v] for v in vertices or self }
68046809

68056810
@doc_index("Clique-related methods")
68066811
def cliques_get_max_clique_graph(self):
@@ -7544,11 +7549,17 @@ def cliques_containing_vertex(self, vertices=None, cliques=None):
75447549
if cliques is None:
75457550
cliques = self.cliques_maximal()
75467551

7547-
if vertices in self: # single vertex
7552+
if vertices in self: # single vertex
75487553
return [c for c in cliques if vertices in c]
7549-
else:
7550-
return { v : [c for c in cliques if v in c]
7551-
for v in vertices or self }
7554+
7555+
from collections import defaultdict
7556+
d = defaultdict(list)
7557+
7558+
for c in cliques:
7559+
for v in c:
7560+
d[v].append(c)
7561+
7562+
return { v : d[v] for v in vertices or self }
75527563

75537564
@doc_index("Clique-related methods")
75547565
def clique_complex(self):

0 commit comments

Comments
 (0)