Custom Weight Calculations ย
Helixโs shortest path algorithms support sophisticated weight calculations that can reference properties from edges, source nodes, and destination nodes. This enables real-world routing scenarios where path costs depend on multiple factors and contexts.Custom weights are available in both ShortestPathDijkstras and ShortestPathAStar.
Property Contexts
Property contexts allow you to reference different parts of the graph structure in your weight expressions:| Context Syntax | Description | Example Use Case |
|---|---|---|
_::{property} | Edge property | Road distance, bandwidth, cost |
_::FromN::{property} | Source node property | Origin traffic, elevation, population |
_::ToN::{property} | Destination node property | Target popularity, capacity, demand |
_::FromV::{property} | Source node vector property | Embedding similarity |
_::ToV::{property} | Destination node vector property | Feature matching |
All weight expressions must evaluate to non-negative values. Negative weights can cause incorrect results or infinite loops.
Context Reference Table
Edge Context: _::{property}
Access properties directly on the edge being evaluated:
- Distance-based routing
- Bandwidth optimization
- Cost minimization
- Time-based routing
Source Node Context: _::FromN::{property}
Access properties from the node where the edge originates:
- Traffic-aware routing (avoid congested sources)
- Elevation-based path planning
- Load balancing (distribute from busy sources)
- Source capacity constraints
Destination Node Context: _::ToN::{property}
Access properties from the node where the edge terminates:
- Popularity-weighted routing
- Destination capacity management
- Cost-aware pathfinding
- Attraction-based navigation
Example 1: Time-decay routing with source context
Example 2: Destination-aware routing with popularity weighting
Example 3: Combining source and destination contexts
Real-World Use Cases
Traffic-Aware Navigation
Cost Optimization
Load Balancing
Capacity Planning
Best Practices
1. Property Normalization
Ensure properties are on similar scales:2. Avoid Division by Zero
Protect against zero denominators:3. Index Key Properties
For best performance, index properties used in weight calculations:4. Test Weight Distributions
Verify weights produce expected behavior:- Log sample weights during development
- Ensure non-negative values
- Check for reasonable ranges