all_shortest_paths

all_shortest_paths#

graph_tool.structure.all_shortest_paths(g, source, target, dist_map=None, pred_map=None, all_preds_map=None, epsilon=1e-08, edges=False, **kwargs)[git][source]#

Return an iterator over all shortest paths from source to target.

Parameters:
gGraph

Graph to be used.

sourceVertex

Source vertex of the search.

targetVertex

Target vertex of the search.

dist_mapVertexPropertyMap (optional, default: None)

Vertex property map with the distances from source to all other vertices.

pred_mapVertexPropertyMap (optional, default: None)

Vertex property map with the predecessors in the search tree. If this is provided, the shortest paths are not computed, and are obtained directly from this map.

all_preds_mapVertexPropertyMap (optional, default: None)

Vector-valued vertex property map with all possible predecessors in the search tree. If this is provided, the shortest paths are obtained directly from this map.

epsilonfloat (optional, default: 1e-8)

Maximum relative difference between distances to be considered “equal”, in case floating-point weights are used.

edgesbool (optional, default: False)

If True, the returned iterator is over edge descriptors.

**kwargsKeyword parameter list

The remaining parameters will be passed to shortest_distance().

Returns:
path_iteratoriterator over a sequence of integers

Iterator over sequences of vertices from source to target in the shortest path. If edges == True, the iterator is over sequences of edge descriptors (Edge).

See also

shortest_path

Shortest path between two vertices

all_predecessors

All shortest-path predecessors of every vertex

count_shortest_paths

Number of shortest paths between two vertices

random_shortest_path

Uniformly random shortest path

Notes

The paths are computed in the same manner as in shortest_distance(), which is used internally.

If both dist_map and pred_map are provided, the shortest_distance() is not called.

Examples

>>> g = gt.collection.data["pgp-strong-2009"]
>>> for path in gt.all_shortest_paths(g, 92, 45):
...     print(path)
[  92  107 2176 7027   26   21   45]
[  92  107 2176 7033   26   21   45]
[  92   82   94 5877 5879   34   45]
[  92   89   94 5877 5879   34   45]

The union of all shortest paths between two vertices can be shown in a drawing of a network with stored positions:

>>> g = gt.collection.data["karate"]
>>> onpath = g.new_ep("bool")
>>> inpath = g.new_vp("bool")
>>> for path in gt.all_shortest_paths(g, 16, 24):
...     inpath.a[path] = True
...     for u, v in zip(path[:-1], path[1:]):
...         onpath[g.edge(u, v)] = True
>>> vcolor = g.new_vp("string", vals=np.where(inpath.a, "#f28c8e",
...                                           "#cccccc"))
>>> ecolor = g.new_ep("string", vals=np.where(onpath.a, "#e41a1c",
...                                           "#999999"))
>>> ewidth = g.new_ep("double", vals=np.where(onpath.a, 2.5, 1))
>>> gt.graph_draw(g, g.vp.pos, vertex_fill_color=vcolor,
...               edge_color=ecolor, edge_pen_width=ewidth,
...               edge_zorder=onpath.copy("double"),
...               vertex_zorder=g.new_vp("double", vals=2 * inpath.a),
...               output="all_shortest_paths-karate.pdf")
<...>
../_images/all_shortest_paths-karate.png

All shortest paths between vertices 16 and 24 of Zachary’s karate club network, with their vertices and edges shown in red.#