Find the lowest path cost
The language tabs use earlier client interfaces. Check SDK versions and connection setup before running them.
Dijkstra’s algorithm finds the total cost of the cheapest route. This endpoint returns the cost, not the route’s sequence of records.
Before you run it: Unspecified edges cost 1. Use a path-returning endpoint when the application needs the route itself.
The weights below assign cost 1 to u1 → u2 and cost 2 to u2 → u3. That route costs 3 in total. A weight is an application-defined cost such as distance or delay; it is not the number of records returned.
- Start at u1Choose u3 as the destination.
- Add edge weightsThe sample path costs one plus two.
- Read the minimum costCheck that the expected result is three.
Follow the example
- Prepare the relationship and endpoints.
- Assign nonnegative weights with the documented keys.
- URL-encode weights_json in the URL.
- Read the returned cost.
Set up the relationship
Complete the graph quickstart first. This example uses graph_demo.people and its follows relation.
The relation must be declared in the schema's [[relations]] block. See schemas/reference#relations.
Run the request
weights_json is a URL-encoded JSON object keyed by "src|dst" with a numeric weight. An empty {} means "every edge weighs 1" and Dijkstra degenerates to shortest hop-count.
WEIGHTS='{"u1|u2":1.0,"u2|u3":2.0}'
curl -G "https://$OC_HOST/v1/tenants/$OC_TENANT/graph/graph_demo.people/dijkstra" \
--data-urlencode "rel=follows" \
--data-urlencode "src=u1" \
--data-urlencode "dst=u3" \
--data-urlencode "weights_json=$WEIGHTS" \
-H "Authorization: Bearer $OC_TOKEN"Check the result
A reachable target returns a cost; an unreachable one returns null.
{ "cost": 3.0 }One field: cost: number | null. null means no path exists. The endpoint returns the cost only - if you need the path itself, use all_simple_paths.
Understand the behavior
- Classic Dijkstra over a min-heap, expanding the cheapest frontier node.
- Edges missing from
weights_jsonfall back to a weight of 1. - For the top-K cheapest paths instead of a single shortest, use the k-shortest endpoint - loop-free paths in increasing weight.
Important limits and checks
- Negative weights. Dijkstra assumes non-negative edges. Negative weights produce undefined results - reformulate as a positive cost, or pick a different algorithm.
- Forgetting to URL-encode
weights_json. The JSON contains{}, quotes, and the|separator - use--data-urlencodeor your SDK's helper rather than concatenating into the URL.