# Use a Cost Function to Calculate Shortest Path

Beginner

20 min.

Learn how to use a more complex cost function to calculate shortest paths and how to wrap this procedure into a table function to compare different paths.

You will learn

- How to calculate shortest path based on a cost function
- How to use insert a condition in the cost function
- How to wrap a GRAPH procedure in a table function

Vijay Krishnan South Madavana RadhakrishnanNovember 1, 2021

## Prerequisites

- You have already created a Graph Workspace using the tutorial First steps with the SAP HANA Graph Engine.
- You have access to the sample graph data used in this tutorial group.
- Make sure your database instance is
**running**before you start.

In the previous tutorial, you used hop distance to calculate a shortest path. Now you will use a more meaningful cost function: you derive the time it takes to traverse a street segment.

The `EDGES`

table contains a `length`

and `maxspeed`

column. `maxspeed`

is a string column with values like ‘30 mph’. For this tutorial you first need to create a new numeric column `SPEED_MPH`

and extract the number part of `maxspeed`

into this column. Your next step will be to re-write the procedure to take the expression “** length/SPEED_MPH**” as cost function.

This tutorial consists of four steps:

- Generate a numeric column that contains the maximum speed allowed information
- Calculate the shortest path to minimize the time spent
- Find Pubs and Bike lanes
- Wrap a GRAPH procedure in a Table Function