Datalens.ai
← All algorithms

GRAPH-PATHS

floyd_warshall

Find all-pairs shortest path lengths using Floyd's algorithm.

Example of usage

Find and assess regional delivery routes through named logistics hubs.

Usage / limitations

+Use when: Find routes through networks.

−Limitations: Check input requirements, mathematical assumptions and version-specific behaviour before using.

Mathematical formula

See method documentation: implementation and assumptions vary.
Sample dataShow
{
  "variable": "network_data",
  "domain": "Regional delivery routing",
  "feature_names": [
    "origin_hub",
    "destination_hub",
    "travel_minutes"
  ],
  "hubs": [
    "Depot",
    "Market",
    "Port",
    "Clinic",
    "Campus"
  ],
  "roads": [
    {
      "from": "Depot",
      "to": "Market",
      "minutes": 2
    },
    {
      "from": "Depot",
      "to": "Port",
      "minutes": 5
    },
    {
      "from": "Market",
      "to": "Port",
      "minutes": 1
    },
    {
      "from": "Market",
      "to": "Clinic",
      "minutes": 4
    },
    {
      "from": "Port",
      "to": "Clinic",
      "minutes": 1
    },
    {
      "from": "Clinic",
      "to": "Campus",
      "minutes": 3
    }
  ],
  "description": "Estimated inter-hub delivery times, in minutes."
}

Code example (Python)

# DataLens.ai example — regional delivery route analysis.
# Sample network_data is supplied by the Copy input + code + test action.
import networkx as nx
import numpy as np
# Build the named, weighted regional-delivery graph.
network = nx.Graph()
network.add_nodes_from(network_data["hubs"])
network.add_weighted_edges_from((road["from"], road["to"], road["minutes"]) for road in network_data["roads"])
# Directed version models one-way transfers without directed cycles.
directed = nx.DiGraph()
directed.add_nodes_from(network_data["hubs"])
directed.add_weighted_edges_from((road["from"], road["to"], road["minutes"]) for road in network_data["roads"])

# floyd warshall applied to named delivery hubs.
all_distances = nx.floyd_warshall(network, weight="weight")
assert all_distances["Depot"]["Campus"] == 7

# Verified expected output: all assertions completed.
print("Expected output verified")