Algorithmic Solutions > LEDA > LEDA Guide > Graph Algorithms > Shortest Path Algorithms > Generic Algorithms for SSSP

## Generic Algorithms for SSSP

Let `G` be a directed graph, `s` a node of `G,` and `cost` a cost function on the edges of `G`. Edge costs may be positive or negative. A single source shortest path algorithm computes the shortest paths from `s` to all other nodes of `G` with respect to `cost`.

Remark: Finding the shortest path from one node to one other node is not significantly faster than computing the shortest paths from the source to all other nodes.

`SHORTEST_PATH_T()` is the generic LEDA function for computing single source shortest paths in a directed graph. `SHORTEST_PATH_T()` is a template function. The template parameter `NT` can be instantiated with any number type.

Example of How to Use `SHORTEST_PATH_T()`

`SHORTEST_PATH()` is the name of the preinstantiated versions of `SHORTEST_PATH_T()`. There is one function `SHORTEST_PATH()` for edge costs of type `int` and one for `double`.

Example of How to Use `SHORTEST_PATH()`

Important Notice: `SHORTEST_PATH()` only performs correctly if all arithmetic is performed without rounding errors and without overflows. If you are not sure about this, you should use `SHORTEST_PATH_T()` with one of the LEDA number types.

### Running time

The running time of `SHORTEST_PATH_T()` and `SHORTEST_PATH()` for a graph `G=(V,E)` is

• linear in the size of the graph (O(|V|+|E|)) for acyclic graphs
• O(|E|+|V|log|V|) if all edge costs are non-negative
• O(|V||E|) otherwise

### Tips

• `SHORTEST_PATH_T()` provides a convenient interface to the algorithms for solving the SSSP. You do not have to think about cycles and negative edge costs beforehand.
• If you know that there can be no overflows and no rounding errors you can use `SHORTEST_PATH().`
• If your graph is acyclic, you can also call the SSSP Algorithm for Acyclic Graphs directly.
• If your edge costs are non-negative, you can also call Dijkstra's Algorithm for SSSP directly.
• If your graph contains cycles and there are edges with negative costs, you can also call the Bellman-Ford Algorithm for SSSP directly.
• LEDA also provides functions for checking the results of an SSSP Algorithm.

Shortest Path Algorithms

Checking the Results of an SSSP Algorithm

Dijkstra's Algorithm for SSSP

Number Types

Graphs and Related Data Types

Graph Algorithms

Manual Entries: