@@ -1431,15 +1431,14 @@ def pnc_k_shortest_simple_paths(self, source, target, weight_function=None,
1431
1431
a path is returned. Otherwise a tuple of path length and path is
1432
1432
returned.
1433
1433
1434
- OUTPUT: iterator
1435
1434
ALGORITHM:
1436
1435
1437
1436
This algorithm is based on the ``feng_k_shortest_simple_paths`` algorithm
1438
1437
in [Feng2014 ]_, but postpones the shortest path tree computation when non-simple
1439
1438
deviations occur. See Postponed Node Classification algorithm in [ACN2023 ]_
1440
1439
for the algorithm description.
1441
1440
1442
- EXAMPLES:
1441
+ EXAMPLES::
1443
1442
1444
1443
sage: from sage. graphs. path_enumeration import pnc_k_shortest_simple_paths
1445
1444
sage: g = DiGraph( [(1, 2, 20), (1, 3, 10), (1, 4, 30), (2, 5, 20), (3, 5, 10), (4, 5, 30) ])
@@ -1448,7 +1447,7 @@ def pnc_k_shortest_simple_paths(self, source, target, weight_function=None,
1448
1447
sage: list( pnc_k_shortest_simple_paths( g, 1, 5, report_weight=True))
1449
1448
[(2.0, [1, 2, 5 ]), ( 2. 0, [1, 4, 5 ]) , ( 2. 0, [1, 3, 5 ]) ]
1450
1449
1451
- TESTS:
1450
+ TESTS::
1452
1451
1453
1452
sage: from sage. graphs. path_enumeration import pnc_k_shortest_simple_paths
1454
1453
sage: g = DiGraph( [(0, 1, 9), (0, 3, 1), (0, 4, 2), (1, 6, 4),
@@ -1515,9 +1514,9 @@ def pnc_k_shortest_simple_paths(self, source, target, weight_function=None,
1515
1514
G.delete_edges(G.incoming_edges(source, labels = False ))
1516
1515
G.delete_edges(G.outgoing_edges(target, labels = False ))
1517
1516
1518
- by_weight, weight_function = self ._get_weight_function(by_weight = by_weight,
1519
- weight_function = weight_function,
1520
- check_weight = check_weight)
1517
+ by_weight, weight_function = G ._get_weight_function(by_weight = by_weight,
1518
+ weight_function = weight_function,
1519
+ check_weight = check_weight)
1521
1520
1522
1521
def reverse_weight_function (e ):
1523
1522
return weight_function((e[1 ], e[0 ], e[2 ]))
@@ -1545,7 +1544,7 @@ def pnc_k_shortest_simple_paths(self, source, target, weight_function=None,
1545
1544
if e[0 ] in dist and e[1 ] in dist}
1546
1545
1547
1546
def sidetrack_length (path ):
1548
- return sum (sidetrack_cost[e] for e in zip (path[: - 1 ] , path[1 :]))
1547
+ return sum (sidetrack_cost[e] for e in zip (path, path[1 :]))
1549
1548
1550
1549
# v-t path in the first shortest path tree T_0
1551
1550
def tree_path (v ):
@@ -1585,22 +1584,22 @@ def pnc_k_shortest_simple_paths(self, source, target, weight_function=None,
1585
1584
# from ``path[t]``.
1586
1585
ancestor_idx_dict = {v: i for i, v in enumerate (path)}
1587
1586
1588
- def ancestor_idx_func (v , t ):
1587
+ def ancestor_idx_func (v , t , len_path ):
1589
1588
if v not in successor:
1590
1589
# target vertex is not reachable from v
1591
1590
return - 1
1592
1591
if v in ancestor_idx_dict:
1593
- if ancestor_idx_dict[v] <= t or ancestor_idx_dict[v] == len (path) - 1 :
1592
+ if ancestor_idx_dict[v] <= t or ancestor_idx_dict[v] == len_path - 1 :
1594
1593
return ancestor_idx_dict[v]
1595
- ancestor_idx_dict[v] = ancestor_idx_func(successor[v], t)
1594
+ ancestor_idx_dict[v] = ancestor_idx_func(successor[v], t, len_path )
1596
1595
return ancestor_idx_dict[v]
1597
1596
1598
1597
if is_simple:
1599
1598
# output
1600
1599
if report_edges and labels:
1601
- P = [edge_labels[e] for e in zip (path[: - 1 ] , path[1 :])]
1600
+ P = [edge_labels[e] for e in zip (path, path[1 :])]
1602
1601
elif report_edges:
1603
- P = list (zip (path[: - 1 ] , path[1 :]))
1602
+ P = list (zip (path, path[1 :]))
1604
1603
else :
1605
1604
P = path
1606
1605
if report_weight:
@@ -1614,7 +1613,7 @@ def pnc_k_shortest_simple_paths(self, source, target, weight_function=None,
1614
1613
for e in G.outgoing_edge_iterator(path[deviation_i]):
1615
1614
if e[1 ] in path[:deviation_i + 2 ]: # e[1] is red or e in path
1616
1615
continue
1617
- ancestor_idx = ancestor_idx_func(e[1 ], deviation_i)
1616
+ ancestor_idx = ancestor_idx_func(e[1 ], deviation_i, len (path) )
1618
1617
if ancestor_idx == - 1 :
1619
1618
continue
1620
1619
new_path = path[:deviation_i + 1 ] + tree_path(e[1 ])
0 commit comments