Find Weighted Shortest Paths with Dijkstra’s Algorithm
ShortestPathDijkstras finds the optimal shortest path in weighted graphs using Dijkstra’s algorithm. It supports sophisticated weight calculations that can reference edge properties, source node properties, and destination node properties, making it incredibly flexible for real-world routing scenarios.When using the SDKs or curling the endpoint, the query name must match what is defined in the
queries.hx file exactly.How It Works
Dijkstra’s algorithm:- Starts at the source node with distance 0
- Explores neighbors, calculating cumulative path weights
- Always processes the node with the smallest known distance next
- Guarantees finding the optimal shortest path for non-negative weights
All weights must be non-negative. Negative weights can produce incorrect results or infinite loops.
When to Use ShortestPathDijkstras
Use Dijkstra when you need:- Weighted paths: Edges have different costs (distance, time, bandwidth)
- Custom calculations: Combine multiple properties into path weights
- Multi-factor routing: Consider traffic, reliability, cost simultaneously
- Property-based weights: Use node or edge properties in calculations
- Flexibility: Maximum control over what defines “shortest”
Weight Expressions
Weight expressions can reference:_::{property}- Edge property_::FromN::{property}- Source node property_::ToN::{property}- Destination node property- Mathematical functions: ADD, MUL, DIV, SUB, POW, SQRT, etc.
Example 1: Simple property-based weight
Example 2: Traffic-aware routing with multi-context weights
Example 3: Complex multi-factor weight calculation
Property Context Reference
| Context | Description | Example |
|---|---|---|
_::{property} | Edge property | _::{distance} |
_::FromN::{property} | Source node property | _::FromN::{traffic_factor} |
_::ToN::{property} | Destination node property | _::ToN::{popularity} |
Available Mathematical Functions
Use these functions in weight expressions:- Arithmetic: ADD, SUB, MUL, DIV, MOD
- Power: POW, SQRT, EXP, LN, LOG
- Rounding: CEIL, FLOOR, ROUND, ABS
- Trigonometric: SIN, COS, TAN
- Constants: PI()
Performance Considerations
- Time Complexity: O((V + E) log V) using binary heap
- Space Complexity: O(V) for distance tracking
- Weight Calculation: Evaluated once per edge during exploration
- Indexing: Index properties used in weight calculations for best performance
Complex weight expressions may impact query performance. Profile your queries and consider caching frequently-accessed property values.