def all_shortest_paths(G, source, target, weight=None):
 if (weight is not None):
  (pred, dist) = nx.dijkstra_predecessor_and_distance(G, source, weight=weight)
 else:
  pred = nx.predecessor(G, source)
 if (source not in G):
  raise nx.NodeNotFound('Source {} is not in G'.format(source))
 if (target not in pred):
  raise nx.NetworkXNoPath()
 stack = [[target, 0]]
 top = 0
 while (top >= 0):
  (node, i) = stack[top]
  if (node == source):
   (yield [p for (p, n) in reversed(stack[:(top + 1)])])
  if (len(pred[node]) > i):
   top += 1
   if (top == len(stack)):
    stack.append([pred[node][i], 0])
   else:
    stack[top] = [pred[node][i], 0]
  else:
   stack[(top - 1)][1] += 1
   top -= 1

def all_shortest_paths ( G , source , target , weight = None ) : if ( weight is not None ) : ( pred , dist ) = nx . dijkstra_predecessor_and_distance ( G , source , weight = weight ) else : pred = nx . predecessor ( G , source ) if ( source not in G ) : raise nx . NodeNotFound ( 'Source {} is not in G' . format ( source ) ) if ( target not in pred ) : raise nx . NetworkXNoPath ( ) stack = [ [ target , 0 ] ] top = 0 while ( top >= 0 ) : ( node , i ) = stack [ top ] if ( node == source ) : ( yield [ p for ( p , n ) in reversed ( stack [ : ( top + 1 ) ] ) ] ) if ( len ( pred [ node ] ) > i ) : top += 1 if ( top == len ( stack ) ) : stack . append ( [ pred [ node ] [ i ] , 0 ] ) else : stack [ top ] = [ pred [ node ] [ i ] , 0 ] else : stack [ ( top - 1 ) ] [ 1 ] += 1 top -= 1
compute all shortest paths in the graph .