Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Implemented suggested changes
Browse files Browse the repository at this point in the history
Moved lists to iterators.
Avoided recomputing the complement of the graph.
  • Loading branch information
deinst committed Oct 23, 2018
1 parent 65ed276 commit 8ba0fc6
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions src/sage/graphs/graph_decompositions/modular_decomposition.py
Expand Up @@ -2517,8 +2517,8 @@ def gamma_classes(graph):
from itertools import chain
from sage.sets.disjoint_set import DisjointSet

pieces = DisjointSet([frozenset(e) for e in graph.edges(labels=False, sort=False)])
for v in graph.vertices():
pieces = DisjointSet(frozenset(e) for e in graph.edges(labels=False, sort=False))
for v in graph:
neighborhood = graph.subgraph(vertices=graph.neighbors(v))
for component in neighborhood.complement().connected_components():
v1 = component[0]
Expand Down Expand Up @@ -2677,7 +2677,7 @@ def habib_maurer_algorithm(graph, g_classes=None):
if graph.is_directed():
raise ValueError("Graph must be undirected")

if graph.order() == 0:
if not graph.order():
return create_prime_node()

if graph.order() == 1:
Expand All @@ -2690,24 +2690,18 @@ def habib_maurer_algorithm(graph, g_classes=None):
for sg in graph.connected_components()]
return root

elif not graph.complement().is_connected():
root = create_series_node()
root.children = [habib_maurer_algorithm(graph.subgraph(vertices=sg), g_classes)
for sg in graph.complement().connected_components()]
return root

else:
g_comp = graph.complement()
if g_comp.is_connected():
from collections import defaultdict
root = create_prime_node()
if g_classes == None:
if g_classes is None:
g_classes = gamma_classes(graph)
vertex_set = frozenset(graph.vertices())
assert(vertex_set in g_classes)
vertex_set = frozenset(graph.vertex_iterator())
edges = g_classes[vertex_set]
sub = graph.subgraph(edges=edges)
d = defaultdict(list)
for v in sub:
for v1 in sub.neighbors(v):
for v1 in sub.neighbor_iterator(v):
d[v1].append(v)
d1 = defaultdict(list)
for k,v in d.items():
Expand All @@ -2716,6 +2710,12 @@ def habib_maurer_algorithm(graph, g_classes=None):
for sg in d1.values()]
return root

root = create_series_node()
root.children = [habib_maurer_algorithm(graph.subgraph(vertices=sg), g_classes)
for sg in g_comp.connected_components()]
return root


#=============================================================================

# Below functions are implemented to test the modular decomposition tree
Expand Down Expand Up @@ -3289,9 +3289,10 @@ def test_gamma_modules(trials, vertices, prob, verbose=False):
print(g.graph6_string())
g_classes = gamma_classes(g)
for module in g_classes.keys():
m_list = list(module)
for v in g.vertices():
if v not in module:
assert(either_connected_or_not_connected(v, list(module), g))
assert(either_connected_or_not_connected(v, m_list, g))
if verbose:
print("Passes!")

Expand Down Expand Up @@ -3355,6 +3356,11 @@ def random_md_tree(max_depth, max_fan_out, leaf_probability):
#internal function
def rand_md_tree(max_depth, parent_type):
r"""
Create the subtrees of a node.
A child of a node cannot have the same node type as its parent if its
parent's node type is either PARALLEL or SERIES. Also its ``max_depth``
is one less than its parent's.
"""
if random() < leaf_probability or max_depth == 1:
root = create_normal_node(current_leaf[0])
Expand Down

0 comments on commit 8ba0fc6

Please sign in to comment.