diff --git a/Trajectories/trajectories.py b/Trajectories/trajectories.py
index d93a137d8133bbe34f998f7799d4121a0a5b7847..ae0b3a76c24ba26ce832a57c6cedb9839e50acfd 100755
--- a/Trajectories/trajectories.py
+++ b/Trajectories/trajectories.py
@@ -61,28 +61,29 @@ traj_vert_ind = []
 # of vertex indices.
 
 for n in topology.topological_sort(g):
-    if g.vertex(n).in_degree() >= 1:
-        # Find the index of the closest direct predecessor of a node:
-        # head of the in-edge with smallest cost.
+    if traj_prop[n] == - 1:
+        if g.vertex(n).in_degree() >= 1:
+            # Find the index of the closest direct predecessor of a node:
+            # head of the in-edge with smallest cost.
 
-        all_cost = g.get_in_edges(n, eprops = [g.edge_properties.cost_function])
-        # numpy array with dtype float64
+            all_cost = g.get_in_edges(n, eprops = [g.edge_properties.cost_function])
+            # numpy array with dtype float64
 
-        i_min = all_cost[:, 2].argmin()
-        closest_pred = all_cost[i_min, 0]
-
-        if closest_succ[closest_pred] == n:
-            # Assign to n the trajectory of closest_pred. This means
-            # updating ind_traj, traj_prop and
-            # traj_vert_ind. closest_pred is already in a trajectory,
-            # traj_prop[closest_pred]] != - 1. We extend the
-            # trajectory of closest_pred forward.
-            traj_prop[n] = traj_prop[closest_pred]
-            traj_vert_ind[traj_prop[n]].append(n)
+            i_min = all_cost[:, 2].argmin()
+            closest_pred = all_cost[i_min, 0]
+
+            if closest_succ[closest_pred] == n:
+                # Assign to n the trajectory of closest_pred. This means
+                # updating ind_traj, traj_prop and
+                # traj_vert_ind. closest_pred is already in a trajectory,
+                # traj_prop[closest_pred]] != - 1. We extend the
+                # trajectory of closest_pred forward.
+                traj_prop[n] = traj_prop[closest_pred]
+                traj_vert_ind[traj_prop[n]].append(n)
+            else:
+                ind_traj = new_traj(ind_traj, traj_prop, n, traj_vert_ind)
         else:
             ind_traj = new_traj(ind_traj, traj_prop, n, traj_vert_ind)
-    else:
-        ind_traj = new_traj(ind_traj, traj_prop, n, traj_vert_ind)
 
     # traj_prop[n] != - 1