@@ -17116,17 +17116,33 @@ def shortest_path_all_pairs(self, by_weight=False, algorithm=None,
17116
17116
sage: G.plot(edge_labels=True).show() # long time
17117
17117
sage: dist, pred = G.shortest_path_all_pairs(by_weight = True)
17118
17118
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}}
17120
17124
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}}
17122
17130
sage: pred[0]
17123
17131
{0: None, 1: 0, 2: 1, 3: 2, 4: 0}
17124
17132
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)
17125
17133
sage: dist, pred = G.shortest_path_all_pairs(weight_function = lambda e:e[2]['weight'])
17126
17134
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}}
17128
17140
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}}
17130
17146
17131
17147
So for example the shortest weighted path from `0` to `3` is obtained as
17132
17148
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,
17347
17363
dist = {int_to_vertex[i]: {int_to_vertex[j]: dd[i, j] for j in range(n) if dd[i, j] != +Infinity}
17348
17364
for i in range(n)}
17349
17365
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)}
17352
17368
return dist, pred
17353
17369
17354
17370
elif algorithm == "Johnson_Boost":
@@ -17367,9 +17383,9 @@ def shortest_path_all_pairs(self, by_weight=False, algorithm=None,
17367
17383
dist = dict()
17368
17384
pred = dict()
17369
17385
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)
17373
17389
dist[u] = {v: self._path_length(p, by_weight=by_weight,
17374
17390
weight_function=weight_function)
17375
17391
for v, p in paths.items()}
0 commit comments