From f6778f49460e7edef954e6fe25923c7adc3ba30a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gonzalo=20Tornar=C3=ADa?= Date: Thu, 4 May 2023 22:24:33 -0300 Subject: [PATCH] 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. --- src/sage/graphs/graph.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/sage/graphs/graph.py b/src/sage/graphs/graph.py index b6a10efbf48..177d9a43df9 100644 --- a/src/sage/graphs/graph.py +++ b/src/sage/graphs/graph.py @@ -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): @@ -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):