Skip to main content

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:
  1. Starts at the source node with distance 0
  2. Explores neighbors, calculating cumulative path weights
  3. Always processes the node with the smallest known distance next
  4. 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

Here’s how to run the query using the SDKs or curl

Example 2: Traffic-aware routing with multi-context weights

Here’s how to run the query using the SDKs or curl

Example 3: Complex multi-factor weight calculation

Here’s how to run the query using the SDKs or curl

Property Context Reference

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()
See Advanced Weight Expressions for more details.

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.

Custom Weights

Deep dive into property contexts and weight patterns

Weight Expressions

Advanced mathematical weight calculations

ShortestPathAStar

Faster pathfinding with heuristics

Mathematical Functions

All available math functions