Skip to content

Commit 3982a61

Browse files
committed
black. remove bispy/api, move content to bispy/__init__
1 parent 3ae73cc commit 3982a61

11 files changed

Lines changed: 78 additions & 139 deletions

File tree

bispy/__init__.py

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,41 @@
1414
to_tuple_list,
1515
)
1616

17-
from .api.api import (
18-
MaxBisimAlgorithm,
19-
find_max_bisimulation,
20-
saha_iter,
21-
total_saha,
22-
)
17+
18+
class Algorithms(Enum):
19+
PaigeTarjan = auto()
20+
DovierPiazzaPolicriti = auto()
21+
22+
23+
def compute_maximum_bisimulation(
24+
graph: nx.DiGraph,
25+
initial_partition,
26+
algorithm=Algorithm.PaigeTarjan,
27+
):
28+
"""Compute the maximum bisimulation of the given graph, possibly using
29+
an initial partition (or labeling set). The preferred algorithm may be
30+
chosen as well (*Paige-Tarjan* or *Dovier-Piazza-Policriti*).
31+
32+
Example:
33+
>>> import networkx as nx
34+
>>> from bispy import compute_maximum_bisimulation, Algorithms
35+
>>> graph = nx.balanced_tree(2,3, create_using=nx.DiGraph)
36+
>>> compute_maximum_bisimulation(graph)
37+
[(3, 4, 5, 6), (7, 8, 9, 10, 11, 12, 13, 14), (1, 2), (0,)]
38+
>>> compute_maximum_bisimulation(graph),
39+
algorithm=Algorithms.DovierPiazzaPolicriti)
40+
[(3, 5, 6), (7, 8, 9, 10, 11, 12, 13, 14), (0,), (2,), (1,), (4,)]
41+
42+
:param graph: The input graph.
43+
:param initial_partition: A partition of the set of nodes of the graph,
44+
two nodes in different blocks of this partition cannot be bisimilar.
45+
Defaults to the trivial initial partition.
46+
:param algorithm: The algorithm used to compute the maximum bisimulation.
47+
:returns: The maximum bisimulation of the given graph, with the given
48+
initial partition.
49+
"""
50+
51+
if algorithm == Algorithms.PaigeTarjan:
52+
return paige_tarjan(graph, initial_partition)
53+
elif algorithm == Algorithms.DovierPiazzaPolicriti:
54+
return dovier_piazza_policriti(graph, initial_partition)

bispy/api/__init__.py

Whitespace-only changes.

bispy/api/api.py

Lines changed: 0 additions & 125 deletions
This file was deleted.

bispy/dovier_piazza_policriti/dovier_piazza_policriti.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ def split_upper_ranks(partition: RankedPartition, block: _Block):
9393
# if this is an upper-rank node with respect to the collapsed block, we
9494
# need to split. the split is not needed if the block is a singoletto.
9595
if vertex.rank > block.rank and not (
96-
vertex.qblock.split_helper_block is None and vertex.qblock.size <= 1
96+
vertex.qblock.split_helper_block is None
97+
and vertex.qblock.size <= 1
9798
):
9899
# if needed, create the aux block to help during the splitting
99100
# phase

bispy/dovier_piazza_policriti/ranked_partition.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ def append_at_rank(self, block: _Block, rank: Union[int, float]):
8585
:param rank: The input rank.
8686
"""
8787

88-
self.append_at_index(block, RankedPartition.rank_to_partition_idx(rank))
88+
self.append_at_index(
89+
block, RankedPartition.rank_to_partition_idx(rank)
90+
)
8991

9092
def append_at_index(self, block, index: int):
9193
"""Append a new block in the `index`-th class.

bispy/paige_tarjan/paige_tarjan.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,8 @@ def paige_tarjan_qblocks(q_partition: List[_QBlock]) -> List[_QBlock]:
324324
q_partition.extend(new_qblocks)
325325

326326
return [
327-
qblock for qblock in filter(lambda qblock: qblock.size > 0, q_partition)
327+
qblock
328+
for qblock in filter(lambda qblock: qblock.size > 0, q_partition)
328329
]
329330

330331

bispy/saha/ranked_compound_xblocks_container.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ def __init__(self, compound_xblocks, max_rank):
1212
first_nonempty_index = -1
1313

1414
for compound_xblock in compound_xblocks:
15-
rank = compound_xblock.qblocks.first.value.vertexes.first.value.rank
15+
rank = (
16+
compound_xblock.qblocks.first.value.vertexes.first.value.rank
17+
)
1618
if rank == float("-inf"):
1719
self._xblocks[0].append(compound_xblock)
1820
first_nonempty_index = 0
@@ -39,7 +41,9 @@ def update_first_nonempty_index(self):
3941
def append_at_rank(self, xblock, rank):
4042
rank_index = 0 if rank == float("-inf") else rank + 1
4143
self._xblocks[rank_index].append(xblock)
42-
self._first_nonempty_index = min(self._first_nonempty_index, rank_index)
44+
self._first_nonempty_index = min(
45+
self._first_nonempty_index, rank_index
46+
)
4347

4448
def append(self, xblock):
4549
self.append_at_rank(xblock, xblock.qblocks.first.value.rank)

bispy/saha/saha.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,9 @@ def saha(
806806
if source_vertex.rank < destination_vertex.rank:
807807
source_vertex.rank = destination_vertex.rank
808808

809-
propagate_nwf(source_vertex.scc, scc_finishing_time)
809+
propagate_nwf(
810+
source_vertex.scc, scc_finishing_time
811+
)
810812
else:
811813
if source_vertex.rank < destination_vertex.rank:
812814
source_vertex.rank = destination_vertex.rank

bispy/saha/saha_partition.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,26 @@ def add_edge(
6262
else:
6363
return back_to_original(max_bisi, self.node_to_idx)
6464

65+
def add_edges(
66+
self, edges: List[Tuple[Any, Any]], verbose: bool = True
67+
) -> Union[None, List[Tuple[Any]]]:
68+
"""Add multiple edges and compute the maximum bisimulation
69+
incrementally.
70+
71+
:param edges: A list of edges, represented by 2-tuples.
72+
:param verbose: If `True`, the maximum bisimulation will be returned
73+
after the last iteration. Otherwise the method returns `None`.
74+
Defaults to `True`.
75+
:return: The maximim bisimulation after the addition of the last edge
76+
if `verbose` is `True`.
77+
"""
78+
79+
for idx, edge in enumerate(edges):
80+
if idx == len(edges) - 1 and verbose:
81+
return self.add_edge(edge)
82+
else:
83+
self.add_edge(edge, verbose=False)
84+
6585

6686
def saha(
6787
graph, initial_partition=None, is_integer_graph=False

bispy/utilities/graph_decorator.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,9 @@ def decorate_bispy_graph(
374374
edge.count.value += 1
375375

376376
if topological_sorted_images:
377-
finishing_time_list = compute_counterimage_finishing_time_list(vertexes)
377+
finishing_time_list = compute_counterimage_finishing_time_list(
378+
vertexes
379+
)
378380
build_vertexes_image(finishing_time_list)
379381

380382
if compute_rank:

0 commit comments

Comments
 (0)