Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support networkx 3.1 #35584

Merged
merged 2 commits into from May 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 47 additions & 7 deletions src/sage/graphs/graph.py
Expand Up @@ -6786,13 +6786,26 @@ def cliques_number_of(self, vertices=None, cliques=None):
{(0, 0): 2, (0, 1): 3, (0, 2): 2, (1, 0): 2, (1, 1): 3, (1, 2): 2}
sage: F.cliques_number_of(vertices=[(0, 1), (1, 2)])
{(0, 1): 3, (1, 2): 2}
sage: F.cliques_number_of(vertices=(0, 1))
3
sage: G = Graph({0:[1,2,3], 1:[2], 3:[0,1]})
sage: G.show(figsize=[2,2])
sage: G.cliques_number_of()
{0: 2, 1: 2, 2: 1, 3: 1}
"""
import networkx
return networkx.number_of_cliques(self.networkx_graph(), vertices, cliques)
if cliques is None:
cliques = self.cliques_maximal()

if vertices in self: # single vertex
return sum(1 for c in cliques if vertices in c)

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 @@ -7493,17 +7506,32 @@ def cliques_containing_vertex(self, vertices=None, cliques=None):

sage: C = Graph('DJ{')
sage: C.cliques_containing_vertex()
{0: [[4, 0]], 1: [[4, 1, 2, 3]], 2: [[4, 1, 2, 3]], 3: [[4, 1, 2, 3]], 4: [[4, 0], [4, 1, 2, 3]]}
{0: [[0, 4]],
1: [[1, 2, 3, 4]],
2: [[1, 2, 3, 4]],
3: [[1, 2, 3, 4]],
4: [[0, 4], [1, 2, 3, 4]]}
sage: C.cliques_containing_vertex(4)
[[0, 4], [1, 2, 3, 4]]
sage: C.cliques_containing_vertex([0, 1])
{0: [[0, 4]], 1: [[1, 2, 3, 4]]}
sage: E = C.cliques_maximal()
sage: E
[[0, 4], [1, 2, 3, 4]]
sage: C.cliques_containing_vertex(cliques=E)
{0: [[0, 4]], 1: [[1, 2, 3, 4]], 2: [[1, 2, 3, 4]], 3: [[1, 2, 3, 4]], 4: [[0, 4], [1, 2, 3, 4]]}
{0: [[0, 4]],
1: [[1, 2, 3, 4]],
2: [[1, 2, 3, 4]],
3: [[1, 2, 3, 4]],
4: [[0, 4], [1, 2, 3, 4]]}

sage: G = Graph({0:[1,2,3], 1:[2], 3:[0,1]})
sage: G.show(figsize=[2,2])
sage: G.cliques_containing_vertex()
{0: [[0, 1, 2], [0, 1, 3]], 1: [[0, 1, 2], [0, 1, 3]], 2: [[0, 1, 2]], 3: [[0, 1, 3]]}
{0: [[0, 1, 2], [0, 1, 3]],
1: [[0, 1, 2], [0, 1, 3]],
2: [[0, 1, 2]],
3: [[0, 1, 3]]}

Since each clique of a 2 dimensional grid corresponds to an edge, the
number of cliques in which a vertex is involved equals its degree::
Expand All @@ -7518,8 +7546,20 @@ def cliques_containing_vertex(self, vertices=None, cliques=None):
sage: sorted(sorted(x for x in L) for L in d[(0, 1)])
[[(0, 0), (0, 1)], [(0, 1), (0, 2)], [(0, 1), (1, 1)]]
"""
import networkx
return networkx.cliques_containing_node(self.networkx_graph(), vertices, cliques)
if cliques is None:
cliques = self.cliques_maximal()

if vertices in self: # single vertex
return [c for c in cliques if vertices in c]

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