Dijkstra: shortest path algorithm for weighted graphs#

These methods are used for the computation of shortest paths on weighted graphs and were adapted from the Networkx (networkx.algorithms.shortest_paths.weighted) implementation.

distanceclosure.dijkstra.all_pairs_dijkstra_path_length(G, weight='weight', disjunction=<built-in function sum>)#

Compute shortest path lengths between all nodes in a weighted graph.

Parameters
  • G (NetworkX graph) –

  • weight (string or function) –

    If this is a string, then edge weights will be accessed via the edge attribute with this key (that is, the weight of the edge joining u to v will be G.edges[u, v][weight]). If no such edge attribute exists, the weight of the edge is assumed to be one.

    If this is a function, the weight of an edge is the value returned by the function. The function must accept exactly three positional arguments: the two endpoints of an edge and the dictionary of edge attributes for that edge. The function must return a number.

  • disjunction (function (default=sum)) – Whether to sum paths or use the max value. Use sum for metric and max for ultrametric.

Returns

distance – (source, dictionary) iterator with dictionary keyed by target and shortest path length as the key value.

Return type

iterator

Examples

>>> G = nx.path_graph(5)
>>> length = dict(all_pairs_dijkstra_path_length(G))
>>> for node in [0, 1, 2, 3, 4]:
...     print(f"1 - {node}: {length[1][node]}")
1 - 0: 1
1 - 1: 0
1 - 2: 1
1 - 3: 2
1 - 4: 3
>>> length[3][2]
1
>>> length[2][2]
0

Note

Edge weight attributes must be numerical. Distances are calculated as sums of weighted edges traversed.

The dictionary returned only has keys for reachable node pairs.

distanceclosure.dijkstra.single_source_dijkstra_path_length(G, source, weight_function, paths=None, disjunction=<built-in function sum>)#

Uses (a custom) Dijkstra’s algorithm to find shortest weighted paths

Parameters
  • G (NetworkX graph) –

  • source (node) – Starting node for path.

  • weight_function (function) – Function with (u, v, data) input that returns that edges weight

  • paths (dict, optional (default=None)) – dict to store the path list from source to each node, keyed by node. If None, paths are not stored.

  • disjunction (function (default=sum)) – Whether to sum paths or use the max value. Use sum for metric and max for ultrametric.

Returns

distance – A mapping from node to shortest distance to that node from one of the source nodes.

Return type

dictionary

Raises

NodeNotFound – If source is not in G.

Note

The optional predecessor and path dictionaries can be accessed by the caller through the original paths objects passed as arguments. No need to explicitly return paths.