Route optimization on 25 world ports and 55 weighted shipping lanes: Dijkstra shortest path, BFS depth layers, minimum spanning tree, and PageRank on a real weighted network.
-- Graph with weight and edge-type columns; DIRECTED lanes
CREATE GRAPH external.shipping_network.shipping_network
VERTEX TABLE external.shipping_network.ports
ID COLUMN id NODE TYPE COLUMN region NODE NAME COLUMN name
EDGE TABLE external.shipping_network.routes
SOURCE COLUMN src TARGET COLUMN dst
WEIGHT COLUMN distance_nm
EDGE TYPE COLUMN route_type
DIRECTED;
CREATE GRAPHCSR external.shipping_network.shipping_network;
-- Degree centrality for hub detection
USE external.shipping_network.shipping_network
CALL algo.degree() YIELD node_id, in_degree, out_degree, total_degree
RETURN node_id, in_degree, out_degree, total_degree
ORDER BY total_degree DESC LIMIT 10;
-- PageRank
USE external.shipping_network.shipping_network
CALL algo.pageRank({dampingFactor: 0.85, iterations: 20})
YIELD node_id, score, rank
RETURN node_id, score, rank ORDER BY score DESC LIMIT 10;
-- Weighted Dijkstra shortest path (default weighted=true)
USE external.shipping_network.shipping_network
CALL algo.shortestPath({source: 1, target: 22})
YIELD node_id, step, distance
RETURN node_id, step, distance ORDER BY step;
-- Explicit weighted flag (same result)
USE external.shipping_network.shipping_network
CALL algo.shortestPath({source: 1, target: 22, weighted: true})
YIELD node_id, step, distance
RETURN node_id, step, distance ORDER BY step;
-- BFS depth layers from Shanghai
USE external.shipping_network.shipping_network
CALL algo.bfs({source: 1}) YIELD node_id, depth, parent_id
RETURN depth, count(*) AS ports_at_distance
ORDER BY depth;
-- Minimum spanning tree backbone
USE external.shipping_network.shipping_network
CALL algo.mst() YIELD sourceId, targetId, weight
RETURN sourceId, targetId, weight ORDER BY weight;
-- Plain SQL on the same Delta table for region breakdown
SELECT
CASE WHEN s.region = d.region THEN 'Intra-region' ELSE 'Cross-region' END AS category,
COUNT(*) AS route_count,
ROUND(AVG(r.distance_nm), 0) AS avg_distance_nm,
ROUND(SUM(r.fuel_cost_usd), 2) AS total_fuel_cost
FROM external.shipping_network.routes r
JOIN external.shipping_network.ports s ON r.src = s.id
JOIN external.shipping_network.ports d ON r.dst = d.id
GROUP BY CASE WHEN s.region = d.region THEN 'Intra-region' ELSE 'Cross-region' END
ORDER BY route_count DESC;
## When to Use When your graph has meaningful edge weights, distances, costs, transit times, bandwidths, and you need true weighted path algorithms instead of hop-count shortest path. The shipping dataset models 25 major container ports (Shanghai, Singapore, Rotterdam, Dubai, Felixstowe, ...) connected by 55 directed lanes carrying `distance_nm` (weight), `transit_days`, `route_type`, and `fuel_cost_usd`. You'll run weighted Dijkstra to find the 9,750 nm path Shanghai → Singapore → Colombo → Dubai → Piraeus, then build the minimum-cost backbone of 24 routes that connects all 25 ports. ## What You Will Learn 1. Declare the weight attribute once in `CREATE GRAPH ... WEIGHT COLUMN distance_nm` so every algorithm picks it up by default. 2. `algo.shortestPath({source, target})` runs weighted Dijkstra by default; pass `weighted: true` explicitly for clarity or `weighted: false` to fall back to BFS hop-count. 3. `algo.bfs({source})` produces a depth layer distribution, useful for "how many hops to reach every port from Shanghai." 4. `algo.mst()` returns the n-1 edges of a minimum spanning tree as (sourceId, targetId, weight) triples, the cheapest subset that keeps the graph connected. 5. `algo.degree()` reports in/out/total degree per node for hub detection, complementing PageRank's influence-weighted score. 6. Mix Cypher traversal queries with plain SQL on the underlying Delta tables (region aggregation, route-type breakdown), both access the same `ports` and `routes` tables. ## Prerequisites - Understanding of `CREATE GRAPH` with `WEIGHT COLUMN`. - A weight column that is DOUBLE and non-negative (Dijkstra requires non-negative weights).