Get Shortest Path

Finds the shortest path in the graph between two specified nodes.

The Get Shortest Path API finds the shortest path in the graph between two specified nodes.

Quick Start

You must specify a start and end node for the path you want to find. For example, the following query finds the shortest path from the "Renaissance" wikipedia page to the "Internet" page.

POST /1/api/[async|sync]/getshortestpath/v1?source_name=Renaissance&target_name=Internet

The API returns all nodes and edges on the path. Each edge has a length, which was used when finding the shortest path.

{
  "nodes": [
    {
      "id": 29992,
      "attributes": {
        "name": "Renaissance"
      }
    },
    {
      "id": 133784,
      "attributes": {
        "name": "Western culture"
      }
    },
    {
      "id": 717,
      "attributes": {
        "name": "Internet"
      }
    }
  ],
  "edges": [
    {
      "source": 29992,
      "target": 133784,
      "length": 1,
      "attributes": {
        "weight": 0.0156612899
      }
    },
    {
      "source": 133784,
      "target": 717,
      "length": 1,
      "attributes": {
        "weight": 0.158500001
      }
    }
  ]
}

In the above example, the length of each edge is 1, because we have used the default value of the measure parameter, uniform, which assigns every edge in the graph a length of 1. We can obtain different paths by changing the measure parameter. For example, by setting measure equal to 'weighted', we use the 'weight' attribute of each edge as its length.

Synchronous
https://api.havenondemand.com/1/api/sync/getshortestpath/v1
Asynchronous
https://api.havenondemand.com/1/api/async/getshortestpath/v1
Authentication

This API requires an authentication token to be supplied in the following parameter:

Parameter Description
apikey The API key to use to authenticate the API request.
Parameters

This API accepts the following parameters:

Required
Name Type Description
source_id
number The ID of the source node.
target_id
number The ID of the target node.
source_name
string The name of the source node. You must percent-encode the node name.
target_name
string The name of the target node. You must percent-encode the node name.
Optional
Name Type Description
graph
enum The graph you want to query. Default value: wiki_eng.
measure
enum The method to use to determine the distance between nodes. Default value: uniform.
Enumeration Types

This API's parameters use the enumerations described below:

graph
The graph you want to query.
wiki_eng English Wikipedia
Graph where the nodes come from English wikipedia pages.
measure
The method to use to determine the distance between nodes.
uniform Uniform
All edges cost 1.
weighted Weight
Edges cost their weight.
inverseweighted Inverse Weight
Edges cost the reciprocal of their weight.

This API returns a JSON response that is described by the model below. This single model is presented both as an easy to read abstract definition and as the formal JSON schema.

Asynchronous Use

Additional requests are required to get the result if this API is invoked asynchronously.

You can use /1/job/status/<job-id> to get the status of the job, including results if the job is finished.

You can also use /1/job/result/<job-id>, which waits until the job has finished and then returns the result.

Model
This is an abstract definition of the response that describes each of the properties that might be returned.
Get Shortest Path Response {
edges ( array[Edges] , optional)
nodes ( array[Nodes] , optional)
}
Get Shortest Path Response:Edges {
attributes ( Attributes )
length ( number ) Length/weight/cost of edge.
source ( integer ) Source node ID.
target ( integer ) Target node ID.
}
Get Shortest Path Response:Edges:Attributes {
weight ( number , optional) The weight of the edge.
}
Get Shortest Path Response:Nodes {
attributes ( Attributes )
id ( integer ) Node ID.
}
Get Shortest Path Response:Nodes:Attributes {
name ( string , optional) The name of the node.
}
Model Schema
This is a JSON schema that describes the syntax of the response. See json-schema.org for a complete reference.
{
    "properties": {
        "edges": {
            "items": {
                "properties": {
                    "attributes": {
                        "properties": {
                            "weight": {
                                "type": "number"
                            }
                        },
                        "type": "object"
                    },
                    "length": {
                        "type": "number"
                    },
                    "source": {
                        "type": "integer"
                    },
                    "target": {
                        "type": "integer"
                    }
                },
                "required": [
                    "source",
                    "target",
                    "length",
                    "attributes"
                ],
                "type": "object"
            },
            "type": "array"
        },
        "nodes": {
            "items": {
                "properties": {
                    "attributes": {
                        "properties": {
                            "name": {
                                "type": "string"
                            }
                        },
                        "type": "object"
                    },
                    "id": {
                        "type": "integer"
                    }
                },
                "required": [
                    "id",
                    "attributes"
                ],
                "type": "object"
            },
            "type": "array"
        }
    },
    "type": "object"
}
https://api.havenondemand.com/1/api/sync/getshortestpath/v1
/api/api-example/1/api/sync/getshortestpath/v1
Examples
See this API for yourself - select one of our examples below.
Get Shortest Path
Find shortest path linking 'Renaissance' to 'Internet'
Get Shortest Path
Find out how Miley Cyrus is linked to Kim Jong-un on Wikipedia
Parameters
Required
Name Type Value
source_id
number
target_id
number
source_name
string
target_name
string
Optional
Name Type Value
graph
enum
measure
enum


ASync – Response An error occurred making the API request
Response Code:
Response Body

	
Making API Request…
Checking result of job

To try this API with your own data and use it in your own applications, you need an API Key. You can create an API Key from your account page - API Keys.

Output Refresh An error occurred making the API request View Input
Rendered RawHtml Response
Result Display
Response Code:
Response Body:

			
Make this call with curl


If you would like to provide us with more information then please use the box below:

We will use your submission to help improve our product.