Skip to content

Commit 4e20e93

Browse files
committed
improve the use of graphs in sage/combinat/posets/posets.py
1 parent 6ea1fe9 commit 4e20e93

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

src/sage/combinat/posets/posets.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,7 +2065,7 @@ def plot(self, label_elements=True, element_labels=None,
20652065

20662066
if cover_labels is not None:
20672067
if callable(cover_labels):
2068-
for v, w in graph.edges(sort=True, labels=False):
2068+
for v, w in graph.edges(sort=False, labels=False):
20692069
graph.set_edge_label(v, w, cover_labels(v, w))
20702070
elif isinstance(cover_labels, dict):
20712071
for v, w in cover_labels:
@@ -2453,7 +2453,7 @@ def is_d_complete(self) -> bool:
24532453
min_elmt = d[0]
24542454
max_elmt = d[3]
24552455

2456-
if len(H.neighbors_in(max_elmt)) != 2:
2456+
if H.in_degree(max_elmt) != 2:
24572457
# Top of a diamond cannot cover anything but the two side elements
24582458
return False
24592459

@@ -2469,7 +2469,7 @@ def is_d_complete(self) -> bool:
24692469
continue
24702470
for mx in potential_max:
24712471
if len(H.all_paths(mn, mx)) == 2:
2472-
if len(H.neighbors_in(mx)) != 1:
2472+
if H.in_degree(mx) != 1:
24732473
# Max element covers something outside of double tailed diamond
24742474
return False
24752475
# Success
@@ -3632,7 +3632,7 @@ def dimension(self, certificate=False, *, solver=None, integrality_tolerance=1e-
36323632
P = Poset(self._hasse_diagram) # work on an int-labelled poset
36333633
hasse_diagram = P.hasse_diagram()
36343634
inc_graph = P.incomparability_graph()
3635-
inc_P = inc_graph.edges(sort=True, labels=False)
3635+
inc_P = inc_graph.edges(sort=False, labels=False)
36363636

36373637
# cycles is the list of all cycles found during the execution of the
36383638
# algorithm
@@ -5323,9 +5323,9 @@ def edge_color(va, vb):
53235323

53245324
for i0, i1 in Subsets(factors_range, 2):
53255325
for x in prod_dg:
5326-
neigh0 = [y for y in prod_dg.neighbors(x)
5326+
neigh0 = [y for y in prod_dg.neighbor_iterator(x)
53275327
if edge_color(x, y) == i0]
5328-
neigh1 = [z for z in prod_dg.neighbors(x)
5328+
neigh1 = [z for z in prod_dg.neighbor_iterator(x)
53295329
if edge_color(x, z) == i1]
53305330
for x0, x1 in cartesian_product_iterator([neigh0, neigh1]):
53315331
x2 = list(x0)
@@ -7175,7 +7175,7 @@ def order_polytope(self):
71757175
"""
71767176
from sage.geometry.polyhedron.constructor import Polyhedron
71777177
ineqs = [[0] + [ZZ(j == v) - ZZ(j == u) for j in self]
7178-
for u, v, w in self.hasse_diagram().edges(sort=True)]
7178+
for u, v in self.hasse_diagram().edges(sort=False, labels=False)]
71797179
for i in self.maximal_elements():
71807180
ineqs += [[1] + [-ZZ(j == i) for j in self]]
71817181
for i in self.minimal_elements():
@@ -8369,7 +8369,7 @@ def frank_network(self):
83698369
pdict[(0, i)] = [(1, j) for j in self if self.ge(i, j)]
83708370
pdict[(1, i)] = [(2, 0)]
83718371
G = DiGraph(pdict, format="dict_of_lists")
8372-
a = {(u, v): 0 for (u, v, l) in G.edge_iterator()}
8372+
a = {e: 0 for e in G.edge_iterator(labels=False)}
83738373
for i in self:
83748374
a[((0, i), (1, i))] = 1
83758375
return (G, a)
@@ -9108,50 +9108,49 @@ def _ford_fulkerson_chronicle(G, s, t, a):
91089108
(11, 2)
91099109
"""
91109110
# pi: potential function as a dictionary.
9111-
pi = {v: 0 for v in G.vertex_iterator()}
9111+
pi = {v: 0 for v in G}
91129112
# p: value of the potential pi.
91139113
p = 0
91149114

91159115
# f: flow function as a dictionary.
9116-
f = {(u, v): 0 for (u, v, l) in G.edge_iterator()}
9116+
f = {edge: 0 for edge in G.edge_iterator(labels=False)}
91179117
# val: value of the flow f. (Cannot call it v due to Python's asinine
91189118
# handling of for loops.)
91199119
val = 0
91209120

91219121
# capacity: capacity function as a dictionary. Here, just the
91229122
# indicator function of the set of arcs of G.
9123-
capacity = {(u, v): 1 for (u, v, l) in G.edge_iterator()}
9123+
capacity = {edge: 1 for edge in G.edge_iterator(labels=False)}
91249124

91259125
while True:
91269126

91279127
# Step MC1 in Britz-Fomin, Algorithm 7.2.
91289128

91299129
# Gprime: directed graph G' from Britz-Fomin, Section 7.
91309130
Gprime = DiGraph()
9131-
Gprime.add_vertices(G.vertices(sort=False))
9132-
for (u, v, l) in G.edge_iterator():
9131+
Gprime.add_vertices(G)
9132+
for u, v in G.edge_iterator(labels=False):
91339133
if pi[v] - pi[u] == a[(u, v)]:
91349134
if f[(u, v)] < capacity[(u, v)]:
91359135
Gprime.add_edge(u, v)
91369136
elif f[(u, v)] > 0:
91379137
Gprime.add_edge(v, u)
91389138

9139-
# X: list of vertices of G' reachable from s, along with
9140-
# the shortest paths from s to them.
9141-
X = Gprime.shortest_paths(s)
9139+
# X: list of vertices of G' reachable from s
9140+
X = set(Gprime.depth_first_search(s))
91429141
if t in X:
91439142
# Step MC2a in Britz-Fomin, Algorithm 7.2.
9144-
shortest_path = X[t]
9143+
shortest_path = Gprime.shortest_path(s, t, by_weight=False)
91459144
shortest_path_in_edges = zip(shortest_path[:-1], shortest_path[1:])
9146-
for (u, v) in shortest_path_in_edges:
9147-
if v in G.neighbors_out(u):
9145+
for u, v in shortest_path_in_edges:
9146+
if v in G.neighbor_out_iterator(u):
91489147
f[(u, v)] += 1
91499148
else:
91509149
f[(v, u)] -= 1
91519150
val += 1
91529151
else:
91539152
# Step MC2b in Britz-Fomin, Algorithm 7.2.
9154-
for v in G.vertex_iterator():
9153+
for v in G:
91559154
if v not in X:
91569155
pi[v] += 1
91579156
p += 1

0 commit comments

Comments
 (0)