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

Graph constructors: Remove gratuitous nondeterminism #37942

Merged
merged 3 commits into from
May 12, 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
2 changes: 1 addition & 1 deletion src/sage/graphs/generic_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -15719,7 +15719,7 @@ def cluster_triangles(self, nbunch=None, implementation=None):

sage: F = graphs.FruchtGraph()
sage: list(F.cluster_triangles().values())
[1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0]
[1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0]
sage: F.cluster_triangles()
{0: 1, 1: 1, 2: 0, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 0, 9: 1, 10: 1, 11: 0}
sage: F.cluster_triangles(nbunch=[0, 1, 2])
Expand Down
20 changes: 10 additions & 10 deletions src/sage/graphs/graph_decompositions/modular_decomposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,17 +371,17 @@ def print_md_tree(root):
sage: from sage.graphs.graph_decompositions.modular_decomposition import *
sage: print_md_tree(modular_decomposition(graphs.IcosahedralGraph()))
PRIME
3
4
7
9
11
1
5
7
8
11
0
2
6
3
9
4
10
"""

Expand Down Expand Up @@ -494,17 +494,17 @@ def habib_maurer_algorithm(graph, g_classes=None):
sage: from sage.graphs.graph_decompositions.modular_decomposition import *
sage: print_md_tree(habib_maurer_algorithm(graphs.IcosahedralGraph()))
PRIME
3
4
7
9
11
1
5
7
8
11
0
2
6
3
9
4
10

The Octahedral graph is not Prime::
Expand Down
33 changes: 27 additions & 6 deletions src/sage/graphs/graph_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,14 @@ def from_dict_of_dicts(G, M, loops=False, multiedges=False, weighted=False, conv
sage: g.is_isomorphic(graphs.PetersenGraph())
True

The resulting order of vertices is unspecified but deterministic::

sage: from sage.graphs.graph_input import from_dict_of_dicts
sage: g = Graph()
sage: from_dict_of_dicts(g, {i: {} for i in range(99, 90, -1)})
sage: g.vertices(sort=False)
[99, 98, 97, 96, 95, 94, 93, 92, 91]

TESTS:

:issue:`32831` is fixed::
Expand Down Expand Up @@ -493,8 +501,11 @@ def from_dict_of_dicts(G, M, loops=False, multiedges=False, weighted=False, conv

G.allow_loops(loops, check=False)
G.allow_multiple_edges(multiedges, check=False)
verts = set().union(M.keys(), *M.values())
G.add_vertices(verts)
# Use keys of a dictionary instead of a set, to preserve insertion order
verts = dict(M)
for d in M.values():
verts.update(d)
G.add_vertices(verts.keys())
if convert_empty_dict_labels_to_None:
def relabel(x):
return x if x != {} else None
Expand All @@ -504,7 +515,7 @@ def relabel(x):

is_directed = G.is_directed()
if not is_directed and multiedges:
v_to_id = {v: i for i, v in enumerate(verts)}
v_to_id = {v: i for i, v in enumerate(verts.keys())}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't enumerate(verts) precisely the same as enumerate(verts.keys())?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's right; I've written it in this verbose form for clarity only.

for u in M:
for v in M[u]:
if v_to_id[u] <= v_to_id[v] or v not in M or u not in M[v] or u == v:
Expand Down Expand Up @@ -543,8 +554,18 @@ def from_dict_of_lists(G, D, loops=False, multiedges=False, weighted=False):
sage: from_dict_of_lists(g, graphs.PetersenGraph().to_dictionary())
sage: g.is_isomorphic(graphs.PetersenGraph())
True

The resulting order of vertices is unspecified but deterministic::

sage: from sage.graphs.graph_input import from_dict_of_lists
sage: g = Graph()
sage: from_dict_of_lists(g, {i: [] for i in range(99, 90, -1)})
sage: g.vertices(sort=False)
[99, 98, 97, 96, 95, 94, 93, 92, 91]
"""
verts = set().union(D.keys(), *D.values())
# Use keys of a dictionary instead of a set, to preserve insertion order
verts = dict(D)
verts.update({v: None for l in D.values() for v in l})
if not loops:
if any(u in neighb for u, neighb in D.items()):
if loops is False:
Expand All @@ -567,11 +588,11 @@ def from_dict_of_lists(G, D, loops=False, multiedges=False, weighted=False):
multiedges = False
G.allow_loops(loops, check=False)
G.allow_multiple_edges(multiedges, check=False)
G.add_vertices(verts)
G.add_vertices(verts.keys())

is_directed = G.is_directed()
if not is_directed and multiedges:
v_to_id = {v: i for i, v in enumerate(verts)}
v_to_id = {v: i for i, v in enumerate(verts.keys())}
for u in D:
for v in D[u]:
if (v_to_id[u] <= v_to_id[v] or
Expand Down
Loading