# The Shortest Path on Graphs: Dijkstra, Bellman-Ford and Floyd-Warshall algorithms

The Shortest Path in a graph is the shortest path between two specific nodes. In other words, it involves finding the sequence of edges (or nodes) that connects two nodes in a graph, so that the sum of the weights of the edges is minimal compared to all possible sequences of paths between those two nodes.

[wpda_org_chart tree_id=43 theme_id=50]

## The Shortest Path

The concept of Shortest Path is particularly important in various contexts, including:

• Transportation Networks: In transportation systems, such as roads or flight networks, the Shortest Path can help determine the shortest route from one place to another, reducing travel time or associated cost.
• Communication Networks: In communication networks, such as computer networks or telephone networks, finding the Shortest Path can be critical to optimizing data transmission or minimizing latency.
• Resource Management: In resource management problems, such as vehicle planning or supply chain management, finding the Shortest Path can be useful for optimizing resource utilization.
• Workflow Graphs: In workflow graphs, the Minimum Path can help identify the path that requires the least time or resources to complete a process.

There are various algorithms for solving the shortest path problem in a weighted graph, and two of the best-known algorithms are the Dijkstra algorithm and the Bellman-Ford algorithm.

• Dijkstra’s Algorithm: This algorithm finds the Shortest Path from a starting node to all other nodes in the graph. It only works with non-negative weights on the edges and is known for its efficiency when a priority queue is used to select the next node.
• Bellman-Ford algorithm: This algorithm is more flexible and can handle graphs with negative weights (although with some limitations). Returns a Minimum Path from a starting node to all other nodes, detecting any negative cycles in the graph.

The use of the algorithm depends on the specific characteristics of the problem and the graph restrictions. In general, the Shortest Path is a key concept in network optimization and analysis.

## Dijkstra’s algorithm

Dijkstra’s algorithm is a greedy algorithm used to find the shortest path from a starting node to all other nodes in a weighted graph with non-negative weights on the edges. The algorithm maintains a list of known minimum distances from the starting nodes and updates these distances as it explores the graph.

Here’s how Dijkstra’s algorithm works:

• Initialization: Initialize a list of known distances from a starting node to all other nodes. Initialize a priority queue (min-heap) containing all nodes with initial distances.
• Exploration: Until the priority queue is empty, extract the node with the minimum distance. For each adjacent node not yet explored, calculate the total distance from the starting node and, if it is less than the current distance, update the distance.
• Termination: When all nodes have been explored or the minimum distance is infinite for all remaining nodes, the algorithm terminates.

Here is an example Python implementation of Dijkstra’s algorithm:

import heapq

def dijkstra(graph, start_node):
distances = {node: float('infinity') for node in graph}
distances[start_node] = 0

queue = [(0, start_node)]

while queue:
current_distance, current_node = heapq.heappop(queue)

if current_distance > distances[current_node]:
continue

for neighbor, weight in graph[current_node].items():
distance = current_distance + weight
if distance < distances[neighbor]:
distances[neighbor] = distance
heapq.heappush(queue, (distance, neighbor))

return distances

# Example of usage
graph = {
'A': {'B': 1, 'C': 4},
'B': {'A': 1, 'C': 2, 'D': 5},
'C': {'A': 4, 'B': 2, 'D': 1},
'D': {'B': 5, 'C': 1}
}

start_node = 'A'
result = dijkstra(graph, start_node)
print("Minimum Distances from", start_node, ":")
for node, distance in result.items():
print(node, ":", distance)

In this example, graph represents a weighted graph, and the dijkstra function returns a dictionary containing the minimum distances from the starting_node to all other nodes in the graph. The algorithm uses a priority queue to keep track of nodes with minimum distances while exploring the graph.

Executing you get the following result:

Minimum Distances from A :
A : 0
B : 1
C : 3
D : 4

## The Bellman-Ford Algorithm

The Bellman-Ford algorithm is an algorithm used to find the shortest path from a starting node to all other nodes in a weighted graph, even when there are edges with negative weights. The algorithm tracks the minimum distances through an iterative process, relaxing the edges repeatedly until the final distances are obtained.

Here’s how the Bellman-Ford algorithm works:

• Initialization: Initialize a list of known distances from the starting node to all other nodes. Initialize all distances to infinity, except that of the starting node, which is set to 0.
• Iterations: Repeat the following step V-1 times (V is the number of nodes in the graph). For each edge (u, v) in the graph, relax the edge by updating the distance to v through u if the distance to u plus the weight of the edge is smaller than the current distance to v.
• Detection of Negative Cycles: After V-1 iterations, it is possible to detect negative cycles in the graph. If an edge can still be relaxed, then the graph contains a negative cycle reachable from the starting node.

Here is an example Python implementation of the Bellman-Ford algorithm

def bellman_ford(graph, start_node):
distances = {node: float('infinity') for node in graph}
distances[start_node] = 0

for _ in range(len(graph) - 1):
for u in graph:
for v, weight in graph[u].items():
if distances[u] + weight < distances[v]:
distances[v] = distances[u] + weight

# Detecting negative cycles
for u in graph:
for v, weight in graph[u].items():
if distances[u] + weight < distances[v]:
raise ValueError("The graph contains a negative cycle reachable from the start node.")

return distances

# Example of usage
graph = {
'A': {'B': 1, 'C': 4},
'B': {'A': 1, 'C': 2, 'D': 5},
'C': {'A': 4, 'B': 2, 'D': 1},
'D': {'B': 5, 'C': 1}
}

start_node = 'A'
result = bellman_ford(graph, start_node)
print("Minimum Distances from", start_node, ":")
for node, distance in result.items():
print(node, ":", distance)

In this example, graph represents a weighted graph, and the bellman_ford function returns a dictionary containing the minimum distances from the starting_node to all other nodes in the graph. The algorithm can detect negative cycles if present in the graph.

Executing you get the following result:

Minimum Distances from A :
A : 0
B : 1
C : 3
D : 4

## The Floyd-Warshall algorithm

The Floyd-Warshall algorithm is a dynamic programming algorithm that is used to find all shortest paths between all nodes in a weighted graph, even when edges with negative weights are present. This algorithm is efficient for relatively small graphs, since its time complexity is O(V^3), where V is the number of nodes in the graph.

The main goal of the Floyd-Warshall algorithm is to construct a distance matrix that represents the length of the shortest paths between each pair of nodes in the graph.

Here’s how the algorithm works:

• Initialization: Create a distance matrix initialized with the arc lengths between nodes. If there is no edge between two nodes, the corresponding value in the matrix will be set to “infinity”.
• Iterations: For each intermediate node k (1 to V), update the distance matrix. For each pair of nodes i and j, if the path from i to j via node k is shorter than currently known, update the matrix with the new distance.
• Final Distance Matrix: At the end of the algorithm, the distance matrix will contain the shortest paths between each pair of nodes.

The Floyd-Warshall algorithm is especially useful when you need to find all shortest paths in a weighted graph, even if there are edges with negative weights. However, due to its cubic complexity, it may not be efficient for very large graphs.

Here is an example implementation in Python:

def floyd_warshall(graph):
V = len(graph)
distances = [[float('inf') for _ in range(V)] for _ in range(V)]

for i in range(V):
for j in range(V):
if i == j:
distances[i][j] = 0
elif graph[i][j] != 0:
distances[i][j] = graph[i][j]

for k in range(V):
for i in range(V):
for j in range(V):
if distances[i][k] != float('inf') and distances[k][j] != float('inf'):
distances[i][j] = min(distances[i][j], distances[i][k] + distances[k][j])

return distances

# Example of usage
graph = [
[0, 3, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 0, 2],
[4, 0, 0, 0, 0],
[0, 0, 0, 1, 0]
]

result = floyd_warshall(graph)
for row in result:
print(row)

In this example, graph represents the weighted graph and the floyd_warshall function returns the distance matrix with the shortest paths between each pair of nodes.

Executing you get the following result:

[0, 3, 4, 7, 6]
[8, 0, 1, 4, 3]
[7, 10, 0, 3, 2]
[4, 7, 8, 0, 10]
[5, 8, 9, 1, 0]

## Considerations on these three algorithms

Here are some comparative considerations between the Dijkstra, Bellman-Ford and Floyd-Warshall algorithms for calculating the Minimum Path:

Negative Weights:

• Dijkstra: Does not handle arcs with negative weights.
• Bellman-Ford: Can handle bows with negative weights, albeit with some restrictions. Detect negative cycles.
• Floyd-Warshall: Can handle edges with negative weights and negative cycles in the graph.

Graphs with Negative Cycles:

• Dijkstra: Cannot be used on graphs with negative cycles.
• Bellman-Ford: Detects negative cycles and can be used on graphs with this characteristic.
• Floyd-Warshall: Can be used on graphs with negative cycles and automatically detects this condition.

Time Complexity:

• Dijkstra: O((V + E) log V) using min-heap. More efficient on dense graphs.
• Bellman-Ford: O(VE), where V is the number of nodes and E is the number of edges. Less efficient on dense graphs.
• Floyd-Warshall: O(V^3), where V is the number of nodes. Suitable for relatively small graphs.

• Dijkstra: Requires a priority queue (min-heap).
• Floyd-Warshall: Does not require additional data structures beyond the adjacency matrix representation.

Optimal Use:

• Dijkstra: Optimal for graphs with non-negative weights. Perfect for finding shortest paths from a starting node to all other nodes.
• Bellman-Ford: Used when there may be negative weights or negative cycles. Useful when it is necessary to detect the presence of negative cycles.
• Floyd-Warshall: Suitable for small graphs with negative weights and negative cycles.

Choice Based on Problem Requirements:

• Dijkstra: Preferred when it is known a priori that the graph contains no negative weights.
• Bellman-Ford: Used when the presence of negative weights is a possibility, or when it is important to detect negative cycles.
• Floyd-Warshall: Used when it is necessary to find all shortest paths in a graph, regardless of the presence of negative weights or negative cycles.

In summary, the choice between Dijkstra, Bellman-Ford and Floyd-Warshall depends on the specific characteristics of the graph and the requirements of the problem. Dijkstra is preferable for graphs with non-negative weights, Bellman-Ford is useful when there are negative weights and/or negative cycles, while Floyd-Warshall is suitable for small graphs that require all shortest paths.