All Pairs Shortest Path Algorithm (WIP)
The All Pairs Shortest Path (APSP) algorithm is used to find the shortest paths between all pairs of nodes in a graph. One of the most common algorithms to solve this problem is the Floyd-Warshall algorithm.
Floyd-Warshall Algorithm
The Floyd-Warshall algorithm works by iteratively improving the shortest path estimates between all pairs of nodes. It uses a dynamic programming approach to build up the shortest paths by considering each node as an intermediate point.
Steps of the Algorithm
-
Initialization: Create a distance matrix
dist
wheredist[i][j]
represents the shortest distance from nodei
to nodej
. Initializedist[i][j]
to the weight of the edge fromi
toj
if it exists, otherwise set it to infinity. Setdist[i][i]
to 0 for all nodesi
. -
Iterative Update: For each node
k
, update the distance matrix by consideringk
as an intermediate node. For each pair of nodes(i, j)
, updatedist[i][j]
to be the minimum ofdist[i][j]
anddist[i][k] + dist[k][j]
. -
Result: After considering all nodes as intermediate points, the distance matrix
dist
will contain the shortest distances between all pairs of nodes.
Below is a simple implementation of the Floyd-Warshall algorithm in Rust:
const INF: i32 = i32::MAX / 2; // Use a large value to represent infinity fn floyd_warshall(graph: &Vec<Vec<i32>>) -> Vec<Vec<i32>> { let n = graph.len(); let mut dist = graph.clone(); for k in 0..n { for i in 0..n { for j in 0..n { if dist[i][j] > dist[i][k] + dist[k][j] { dist[i][j] = dist[i][k] + dist[k][j]; } } } } dist } fn main() { let graph = vec![ vec![0, 3, INF, 5], vec![2, 0, INF, 4], vec![INF, 1, 0, INF], vec![INF, INF, 2, 0], ]; let shortest_paths = floyd_warshall(&graph); println!("Shortest distances between every pair of vertices:"); for row in shortest_paths { for &dist in &row { if dist == INF { print!("INF "); } else { print!("{} ", dist); } } println!(); } }
- Initialization: The
graph
is represented as an adjacency matrix wheregraph[i][j]
is the weight of the edge from nodei
to nodej
. If there is no edge, it is set toINF
. - Iterative Update: The nested loops update the distance matrix by considering each node as an intermediate point.
- Result: The final distance matrix
shortest_paths
contains the shortest distances between all pairs of nodes.
This implementation demonstrates the basic concept of the Floyd-Warshall algorithm in Rust. You can modify the graph and test with different inputs to see how the algorithm computes the shortest paths.