SAP Home Learn Build Integrate Model Operate Extend with AI ConnectTutorial navigator Knowledge Graph API Devtoberfest Developer Advocates App Space

Manage my Account SAP Devs YouTube ↗ Learnings ↗ Community ↗ Provide Feedback ↗
Logout
โคข Open full site

Use a Cost Function to Calculate Shortest Path

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.

Overview

🎓 beginner 20 min. SAP HANA CloudBeginnerSAP HANA GraphSAP HANA CloudSAP HANA DatabaseSAP HANA Multi Model Processing

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
Unknown U Unknown November 1, 2022
Created by July 14, 2021
Contributors

Prerequisites

Prerequisites

Steps

Intro

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

Step 1 Generate a column for maximum speed information
โ€”

First, you need to add an integer column to the LONDON_BIKE_EDGES table. Extract the number part of maxspeed by executing this statement:

SQL
ALTER TABLE "LONDON_EDGES" ADD("SPEED_MPH" INT);
UPDATE "LONDON_EDGES"
	SET "SPEED_MPH" = TO_INT(REPLACE("maxspeed", ' mph', ''))
	WHERE REPLACE("maxspeed", ' mph', '') <> "maxspeed" ;
SELECT "SPEED_MPH", COUNT(*) AS C FROM "LONDON_EDGES" GROUP BY "SPEED_MPH" ORDER BY C DESC;
-- let's add a default value on the segments that do not have a speed information
UPDATE "LONDON_EDGES" SET "SPEED_MPH" = 30 WHERE "SPEED_MPH" IS NULL;

In the Result panel you can see the distribution of the SPEED_MPH column after updating with default values.

SPEED
SPEED

Step 2 Calculate shortest path to minimize the time spent
+
Step 3 Find pubs and bike lanes
+
Step 4 Wrap a GRAPH procedure in a table function
+
Step 5 Test yourself
+

Resources

Discussion

Share feedback on this tutorial or join the conversation in SAP Community.

Submit detailed feedback Discuss in Community
Steps
Step 1 of 5
1. Generate a column for maximum speed information 2. Calculate shortest path to minimize the time spent 3. Find pubs and bike lanes 4. Wrap a GRAPH procedure in a table function 5. Test yourself

Learn more →