Prerequisites
The prerequisites for running the shortest path solve graph example are listed below:- Graph server enabled
- Python API
- Solve graph script
- Seattle road network CSV file
Python API Installation
Depending on the target operating system, a Python virtual environment may need to be installed first: The native Kinetica Python API is accessible through the following means:Python Virtual Environment
A Python virtual environment is necessary to install in an operating environment where Python is externally managed.-
Install a Python virtual environment:
-
Activate the Python virtual environment:
PyPI
-
Install the API:
-
Test the installation:
If Import Successful is displayed, the API has been installed as is ready for use.
Git
-
In the desired directory, run the following, but be sure to replace
<kinetica-version>with the name of the installed Kinetica version, e.g.,v7.2: -
Change directory into the newly downloaded repository:
-
In the root directory of the unzipped repository, install the Kinetica API:
-
Test the installation (Python3 is necessary for running the API example):
Data File
The example script references the road_weights.csv data file, mentioned in the Prerequisites, in the current local directory, by default. This directory can specified as a parameter when running the example script.Script Detail
This example is going to demonstrate solving (both individual and batch solve) for the shortest path between source points and several destination points located within a road network in Seattle.Constants
Several constants are defined at the beginning of the script:-
SCHEMA— the name of the schema in which the tables supporting the graph creation and match operations will be createdThe schema is created during the table setup portion of the script because the schema must exist prior to creating the tables that will later support the graph creation and match operations. -
TABLE_SRN— the name of the table into which the Seattle road network dataset is loaded -
GRAPH_S— the Seattle road network graph -
TABLE_GRAPH_S_SPSOLVED/TABLE_GRAPH_S_SPSOLVED2/TABLE_GRAPH_S_SPSOLVED3— the Seattle road network graph shortest path solution tables
Constant Definitions
Graph Creation
One graph is used for the shortest path solve graph example utilized in the script:seattle_road_network_graph, a graph based on the road_weights
dataset (the CSV file mentioned in Prerequisites).
The GRAPH_S graph is created with the following characteristics:
- It is directed because the roads in the graph have directionality (one-way and two-way roads)
- It has no explicitly defined nodes because the example relies on implicit nodes attached to the defined edges
- The edges are identified by
WKTLINE, using WKT LINESTRINGs from theWKTLINEcolumn of theseattle_road_networktable. The road segments’ directionality (DIRECTION) is derived from theTwoWaycolumn of theseattle_road_networktable. - The weights (
VALUESPECIFIED) are represented using the time taken to travel the segment found in thetimecolumn of theseattle_road_networktable. The weights are matched to the edges using the sameWKTLINEcolumn as edges (EDGE_WKTLINE) and the sameTwoWaycolumn as the edge direction (EDGE_DIRECTION). - It has no inherent restrictions for any of the nodes or edges in the graph
- It will be replaced with this instance of the graph if a graph of the same
name exists (
recreate)
Create Seattle Road Network Graph
Shortest Path
Single Source to Single Destination
The first example illustrates a simple shortest path solve from a single source node to a single destination node. First, the source node and destination node are defined.Define Beginning & Ending Points
seattle_road_network_graph graph is solved with the solve results
being exported to the response:
Solve Graph for Shortest Path
Solve Graph for Shortest Path Solution

Single Source to Many Destinations
The second example illustrates a shortest path solve from a single source node to many destination nodes. First, the source node and destination nodes are defined. When one source node and many destination nodes are provided, the graph solver will calculate a shortest path solve for each destination node.Define Beginning & Multiple Ending Points
seattle_road_network_graph graph is solved with the solve results
being exported to the response
Solve Graph for Shortest Paths with Multiple Destinations
Solve Graph for Shortest Paths with Multiple Destinations Solution

Many Sources to Many Destinations
The third example illustrates a shortest path solve from a many source nodes to many destination nodes. First, the source node and destination nodes are defined. If many source node and many destination nodes are provided, the graph solver will pair the source and destination node by list index and calculate a shortest path solve for each pair. For this example, there are two starting points (POINT(-122.1792501 47.2113606) and
POINT(-122.375180125237 47.8122103214264)) and paths will be calculated from
the first source to two different destinations and from the second source to two
other destinations.
Calculations from multiple unique sources are faster and more
efficient than calculations with one unique source, but results
may differ slightly between multiple unique source calculations
and single unique source calculations (less than ~1% variance).
Define Multiple Beginning & Multiple Ending Points
seattle_road_network_graph graph is solved with the solve results
being exported to the response:
Solve Graph for Shortest Paths with Multiple Sources & Multiple Destinations
Solve Graph for Shortest Paths with Multiple Sources & Multiple Destinations Solution

Download & Run
Included below is a complete example containing all the above requests, the data files, and output. To run the complete sample, ensure that:- the solve_graph_seattle_shortest_path.py script is in the current directory
- the road_weights.csv file is in the current directory or use
the
data_dirparameter to specify the local directory containing it
Run Example