Skip to content

Commit 42a2bd7

Browse files
committed
fix bugs of pnc algorithm
1 parent 94fe540 commit 42a2bd7

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/sage/graphs/path_enumeration.pyx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,7 +1581,7 @@ def pnc_k_shortest_simple_paths(self, source, target, weight_function=None,
15811581

15821582
# ancestor_idx_vec[v] := the first vertex of ``path[:t+1]`` or ``id_target`` reachable by
15831583
# edges of first shortest path tree from v.
1584-
cdef vector[int] ancestor_idx_vec = [-1 for _ in range(len(G))]
1584+
cdef vector[int] ancestor_idx_vec = [-1 for _ in range(len(G) + len(unnecessary_vertices))]
15851585

15861586
def ancestor_idx_func(v, t, target_idx):
15871587
if ancestor_idx_vec[v] != -1:
@@ -1596,9 +1596,8 @@ def pnc_k_shortest_simple_paths(self, source, target, weight_function=None,
15961596
cdef dict pred = {}
15971597

15981598
# calculate shortest path from dev to one of green vertices
1599-
def shortest_path_to_green(dev, exclude_vertices):
1599+
def shortest_path_to_green(dev, exclude_vertices, target_idx):
16001600
t = len(exclude_vertices)
1601-
ancestor_idx_vec[id_target] = t + 1
16021601
# clear
16031602
while not pq.empty():
16041603
pq.pop()
@@ -1612,7 +1611,7 @@ def pnc_k_shortest_simple_paths(self, source, target, weight_function=None,
16121611
v, d = pq.top()
16131612
pq.pop()
16141613

1615-
if ancestor_idx_func(v, t, t + 1) == t + 1: # green
1614+
if ancestor_idx_func(v, t, target_idx) == target_idx: # green
16161615
path = []
16171616
while v in pred:
16181617
path.append(v)
@@ -1667,7 +1666,7 @@ def pnc_k_shortest_simple_paths(self, source, target, weight_function=None,
16671666
yield P
16681667

16691668
# GET DEVIATION PATHS
1670-
original_cost = cost
1669+
original_cost = cost - sidetrack_cost[(path[-2], path[-1])]
16711670
former_part = set(path)
16721671
for deviation_i in range(len(path) - 2, dev_idx - 1, -1):
16731672
for e in G.outgoing_edge_iterator(path[deviation_i]):
@@ -1687,7 +1686,8 @@ def pnc_k_shortest_simple_paths(self, source, target, weight_function=None,
16871686
original_cost -= sidetrack_cost[(path[deviation_i - 1], path[deviation_i])]
16881687
former_part.remove(path[deviation_i + 1])
16891688
else:
1690-
deviations = shortest_path_to_green(path[dev_idx], set(path[:dev_idx]))
1689+
ancestor_idx_vec[id_target] = len(path)
1690+
deviations = shortest_path_to_green(path[dev_idx], set(path[:dev_idx]), len(path))
16911691
if not deviations:
16921692
continue # no path to target in G \ path[:dev_idx]
16931693
deviation_weight, deviation = deviations

0 commit comments

Comments
 (0)