k_shortest_paths

k_shortest_paths#

graph_tool.structure.k_shortest_paths(g, source, target, k, weights=None, edges=False)[git][source]#

Return the \(k\) shortest simple paths between two vertices.

Parameters:
gGraph

Graph to be used.

sourceVertex

Source vertex.

targetVertex

Target vertex.

kint

Number of paths.

weightsEdgePropertyMap (optional, default: None)

Non-negative edge weights. If not supplied, every edge has unit length.

edgesbool (optional, default: False)

If True, the paths are given by the indices of their edges, otherwise by their vertices.

Returns:
pathslist of numpy.ndarray

The paths, in non-decreasing order of length. Fewer than k paths are returned if there are fewer simple paths between the two vertices.

See also

shortest_path

A single shortest path

all_shortest_paths

All paths of minimum length

all_paths

All simple paths between two vertices

resource_constrained_path

Shortest path with constraints on resources

Notes

The paths are simple (they do not repeat vertices), and are distinct as sequences of edges, so that paths that differ only by parallel edges are counted separately. They are obtained with the algorithm of [yen-finding-1971], which derives each new candidate path from the previous one by deviating from it at each of its vertices (the “spur” vertices), avoiding the edges already used by the paths found with the same prefix. Each of the \(k\) steps requires a shortest-path search from every vertex of the last path, which are done in parallel, giving \(O(kV(E + V\log V))\) time in the worst case.

Parallel implementation.

If enabled during compilation, this algorithm will run in parallel using OpenMP. See the parallel algorithms section for information about how to control several aspects of parallelization.

References

[yen-finding-1971]

Jin Y. Yen, “Finding the k shortest loopless paths in a network”, Management Science 17, 712-716 (1971), DOI: 10.1287/mnsc.17.11.712 [sci-hub, @tor]

Examples

>>> g = gt.collection.data["karate"]
>>> for p in gt.k_shortest_paths(g, g.vertex(16), g.vertex(26), 4):
...     print(p)
[16  5  0  8 33 26]
[16  5  0 13 33 26]
[16  5  0 19 33 26]
[16  5  0 31 33 26]

The paths can be highlighted in a drawing of the network:

>>> onpath = g.new_ep("bool")
>>> for p in gt.k_shortest_paths(g, g.vertex(16), g.vertex(26), 4):
...     for u, v in zip(p[:-1], p[1:]):
...         onpath[g.edge(u, v)] = True
>>> u = gt.GraphView(g, efilt=onpath)
>>> inpath = u.get_total_degrees(g.get_vertices()) > 0
>>> vcolor = g.new_vp("string", vals=np.where(inpath, "#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),
...               output="k_shortest_paths-karate.pdf")
<...>
../_images/k_shortest_paths-karate.png

Four shortest paths between vertices 16 and 26 of Zachary’s karate club network. The vertices and edges of the paths are shown in red.#