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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A simplicial complex has dimension equal to the size of its maximal s… #336

Merged
merged 6 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions test/transform/test_graph_to_simplicial_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_graph_to_clique_complex(self):
assert sc[(0,)]["label"] == 5
assert sc[(0, 1)]["weight"] == 10

sc = graph_to_clique_complex(G, max_dim=2)
sc = graph_to_clique_complex(G, max_rank=1)

assert sc.dim == 1
assert (0, 2, 3) not in sc
Expand Down Expand Up @@ -93,7 +93,7 @@ def test_graph_2_clique_complex(self):
assert (0, 1, 2) in sc

with pytest.deprecated_call():
sc = graph_2_clique_complex(G, max_dim=2)
sc = graph_2_clique_complex(G, max_rank=1)

assert sc.dim == 1
assert (0, 2, 3) not in sc
Expand Down
26 changes: 14 additions & 12 deletions toponetx/transform/graph_to_simplicial_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,37 +37,39 @@ def graph_to_neighbor_complex(G: nx.Graph) -> SimplicialComplex:


def graph_to_clique_complex(
G: nx.Graph, max_dim: int | None = None
G: nx.Graph, max_rank: int | None = None
) -> SimplicialComplex:
"""Get the clique complex of a graph.

Parameters
----------
G : networks.Graph
Input graph.
max_dim : int, optional
The max dimension of the cliques in the output clique complex.
max_rank : int, optional
The maximum rank of the simplices in the output clique complex.

Returns
-------
SimplicialComplex
The clique simplicial complex of dimension dim of the graph G.
The clique simplicial complex of rank `max_rank` of the graph G.
"""
cliques = nx.enumerate_all_cliques(G)

# `nx.enumerate_all_cliques` returns cliques in ascending order of size. Abort calling the generator once we reach
# cliques larger than the requested max dimension.
cliques = takewhile(
lambda clique: max_dim is None or len(clique) <= max_dim, cliques
lambda clique: max_rank is None or len(clique) <= max_rank + 1, cliques
)

SC = SimplicialComplex(cliques)

# copy attributes of the input graph
for node in G.nodes:
SC[[node]].update(G.nodes[node])
for edge in G.edges:
SC[edge].update(G.edges[edge])
# if the resulting complex has dimension 1 or higher, copy the attributes of the edges
if SC.dim >= 1:
for edge in G.edges:
SC[edge].update(G.edges[edge])
SC.complex.update(G.graph)

return SC
Expand Down Expand Up @@ -101,23 +103,23 @@ def graph_2_neighbor_complex(G) -> SimplicialComplex:
"`graph_2_clique_complex` is deprecated and will be removed in a future version, use `graph_to_clique_complex` instead."
)
def graph_2_clique_complex(
G: nx.Graph, max_dim: int | None = None
G: nx.Graph, max_rank: int | None = None
) -> SimplicialComplex:
"""Get the clique complex of a graph.

Parameters
----------
G : networks.Graph
Input graph.
max_dim : int, optional
The max dimension of the cliques in the output clique complex.
max_rank : int, optional
The maximum rank of the simplices in the output clique complex.

Returns
-------
SimplicialComplex
The clique simplicial complex of dimension dim of the graph G.
The clique simplicial complex of rank `max_rank` of the graph `G`.
"""
return graph_to_clique_complex(G, max_dim)
return graph_to_clique_complex(G, max_rank)


def weighted_graph_to_vietoris_rips_complex(
Expand Down
Loading