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

Commit c69daa7

Browse files
committed
trac #34358: a few more
1 parent dcbbba2 commit c69daa7

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

src/sage/graphs/generic_graph.py

+25-9
Original file line numberDiff line numberDiff line change
@@ -17116,17 +17116,33 @@ def shortest_path_all_pairs(self, by_weight=False, algorithm=None,
1711617116
sage: G.plot(edge_labels=True).show() # long time
1711717117
sage: dist, pred = G.shortest_path_all_pairs(by_weight = True)
1711817118
sage: dist
17119-
{0: {0: 0, 1: 1, 2: 2, 3: 3, 4: 2}, 1: {0: 1, 1: 0, 2: 1, 3: 2, 4: 3}, 2: {0: 2, 1: 1, 2: 0, 3: 1, 4: 3}, 3: {0: 3, 1: 2, 2: 1, 3: 0, 4: 2}, 4: {0: 2, 1: 3, 2: 3, 3: 2, 4: 0}}
17119+
{0: {0: 0, 1: 1, 2: 2, 3: 3, 4: 2},
17120+
1: {0: 1, 1: 0, 2: 1, 3: 2, 4: 3},
17121+
2: {0: 2, 1: 1, 2: 0, 3: 1, 4: 3},
17122+
3: {0: 3, 1: 2, 2: 1, 3: 0, 4: 2},
17123+
4: {0: 2, 1: 3, 2: 3, 3: 2, 4: 0}}
1712017124
sage: pred
17121-
{0: {0: None, 1: 0, 2: 1, 3: 2, 4: 0}, 1: {0: 1, 1: None, 2: 1, 3: 2, 4: 0}, 2: {0: 1, 1: 2, 2: None, 3: 2, 4: 3}, 3: {0: 1, 1: 2, 2: 3, 3: None, 4: 3}, 4: {0: 4, 1: 0, 2: 3, 3: 4, 4: None}}
17125+
{0: {0: None, 1: 0, 2: 1, 3: 2, 4: 0},
17126+
1: {0: 1, 1: None, 2: 1, 3: 2, 4: 0},
17127+
2: {0: 1, 1: 2, 2: None, 3: 2, 4: 3},
17128+
3: {0: 1, 1: 2, 2: 3, 3: None, 4: 3},
17129+
4: {0: 4, 1: 0, 2: 3, 3: 4, 4: None}}
1712217130
sage: pred[0]
1712317131
{0: None, 1: 0, 2: 1, 3: 2, 4: 0}
1712417132
sage: G = Graph( { 0: {1: {'weight':1}}, 1: {2: {'weight':1}}, 2: {3: {'weight':1}}, 3: {4: {'weight':2}}, 4: {0: {'weight':2}} }, sparse=True)
1712517133
sage: dist, pred = G.shortest_path_all_pairs(weight_function = lambda e:e[2]['weight'])
1712617134
sage: dist
17127-
{0: {0: 0, 1: 1, 2: 2, 3: 3, 4: 2}, 1: {0: 1, 1: 0, 2: 1, 3: 2, 4: 3}, 2: {0: 2, 1: 1, 2: 0, 3: 1, 4: 3}, 3: {0: 3, 1: 2, 2: 1, 3: 0, 4: 2}, 4: {0: 2, 1: 3, 2: 3, 3: 2, 4: 0}}
17135+
{0: {0: 0, 1: 1, 2: 2, 3: 3, 4: 2},
17136+
1: {0: 1, 1: 0, 2: 1, 3: 2, 4: 3},
17137+
2: {0: 2, 1: 1, 2: 0, 3: 1, 4: 3},
17138+
3: {0: 3, 1: 2, 2: 1, 3: 0, 4: 2},
17139+
4: {0: 2, 1: 3, 2: 3, 3: 2, 4: 0}}
1712817140
sage: pred
17129-
{0: {0: None, 1: 0, 2: 1, 3: 2, 4: 0}, 1: {0: 1, 1: None, 2: 1, 3: 2, 4: 0}, 2: {0: 1, 1: 2, 2: None, 3: 2, 4: 3}, 3: {0: 1, 1: 2, 2: 3, 3: None, 4: 3}, 4: {0: 4, 1: 0, 2: 3, 3: 4, 4: None}}
17141+
{0: {0: None, 1: 0, 2: 1, 3: 2, 4: 0},
17142+
1: {0: 1, 1: None, 2: 1, 3: 2, 4: 0},
17143+
2: {0: 1, 1: 2, 2: None, 3: 2, 4: 3},
17144+
3: {0: 1, 1: 2, 2: 3, 3: None, 4: 3},
17145+
4: {0: 4, 1: 0, 2: 3, 3: 4, 4: None}}
1713017146

1713117147
So for example the shortest weighted path from `0` to `3` is obtained as
1713217148
follows. The predecessor of `3` is ``pred[0][3] == 2``, the predecessor
@@ -17347,8 +17363,8 @@ def shortest_path_all_pairs(self, by_weight=False, algorithm=None,
1734717363
dist = {int_to_vertex[i]: {int_to_vertex[j]: dd[i, j] for j in range(n) if dd[i, j] != +Infinity}
1734817364
for i in range(n)}
1734917365
pred = {int_to_vertex[i]: {int_to_vertex[j]: (int_to_vertex[pp[i, j]] if i != j else None)
17350-
for j in range(n) if (i == j or pp[i, j] != -9999)}
17351-
for i in range(n)}
17366+
for j in range(n) if (i == j or pp[i, j] != -9999)}
17367+
for i in range(n)}
1735217368
return dist, pred
1735317369

1735417370
elif algorithm == "Johnson_Boost":
@@ -17367,9 +17383,9 @@ def shortest_path_all_pairs(self, by_weight=False, algorithm=None,
1736717383
dist = dict()
1736817384
pred = dict()
1736917385
for u in self:
17370-
paths=self.shortest_paths(u, by_weight=by_weight,
17371-
algorithm=algorithm,
17372-
weight_function=weight_function)
17386+
paths = self.shortest_paths(u, by_weight=by_weight,
17387+
algorithm=algorithm,
17388+
weight_function=weight_function)
1737317389
dist[u] = {v: self._path_length(p, by_weight=by_weight,
1737417390
weight_function=weight_function)
1737517391
for v, p in paths.items()}

0 commit comments

Comments
 (0)