tf_graph_shortest_path
tf_graph_shortest_path
Given a distance-weighted directed graph, consisting of a queryCURSOR input consisting of the starting and ending node for each edge and a distance, and a specified origin and destination node, tf_graph_shortest_path computes the shortest distance-weighted path through the graph between origin_node and destination_node, returning a row for each node along the computed shortest path, with the traversal-ordered index of that node and the cumulative distance from the origin_node to that node. If either origin_node or destination_node do not exist, an error is returned.
SELECT * FROM TABLE(tf_graph_shortest_path(edge_list => CURSOR(SELECT node1, node2, distance FROM table),origin_node => <origin node>,destination_node => <destination node>)
Input Arguments
| Parameter | Description | Data Types |
|---|---|---|
node1 | Origin node column in directed edge list CURSOR | Column< INT | BIGINT | TEXT ENCODED DICT> |
node2 | Destination node column in directed edge list CURSOR | Column< INT | BIGINT | TEXT ENCODED DICT> (must be the same type as node1) |
distance | Distance between origin and destination node in directed edge list CURSOR | Column< INT | BIGINT | FLOAT | DOUBLE > |
origin_node | The origin node to start graph traversal from. If not a value present in edge_list.node1, will cause empty result set to be returned. | BIGINT | TEXT ENCODED DICT |
destination_node | The destination node to finish graph traversal at. If not a value present in edge_list.node1, will cause empty result set to be returned. | BIGINT | TEXT ENCODED DICT |
Output Columns
| Name | Description | Data Types |
|---|---|---|
path_step | The index of this node along the path traversal from origin_node to destination_node, with the first node (the origin_node) indexed as 1. | Column< INT > |
node | The current node along the path traversal from origin_node to destination_node. The first node (as denoted by path_step = 1) will always be the input origin_node, and the final node (as denoted by MAX(path_step)) will always be the input destination_node. | Column < INT | BIGINT | TEXT ENCODED DICT> (same type as the node1 and node2 input columns) |
cume_distance | The cumulative distance adding all input distance values from the origin_node to the current node. | Column < INT | BIGINT | FLOAT | DOUBLE> (same type as the distance input column) |
Example A
/* Compute the shortest flight route on United Airlines for the year 2008 as measuredby flight time between origin airport 'RDU' (Raleigh-Durham, NC) and destinationairport 'SAT' (San Antonio, TX), adding 60 minutes for each leg to account forboarding/plane change time costs, and only counting routes that were flown at least300 times during the year. */SELECT*FROMTABLE(tf_graph_shortest_path(edge_list => CURSOR(SELECTorigin,dest,/* Add 60 minutes to each leg to accountfor boarding/plane change costs */AVG(airtime) + 60 as avg_airtimeFROMflights_2008WHEREcarrier_name = 'United Air Lines'GROUP byorigin,destHAVINGCOUNT(*) > 300),origin_node => 'RDU',destination_node => 'SAT'))ORDER BYpath_steppath_step|node|cume_distance1|RDU|02|ORD|1673|DEN|3544|SAT|519
Example B
/* Compute the shortest path between along a time-traversal weightededge graph of roads in the Eastern United States between a location in North Carolina anda location in Maine, joining to a node locations table to output the lon/lat pairsof each node. */selectpath_step,node,lon,lat,cume_distancefromtable(tf_graph_shortest_path(cursor(selectnode1,node2,traversal_timefromusa_roads_east_time),1561955,1591319)),USA_roads_east_coordswherenode = node_idorder bycume_distance desclimit 20;path_step|node|lon|lat|cume_distance4380|1591319|-71.55136299999999|43.75256|134420174379|1591989|-71.55174099999999|43.75245|134411994378|1589348|-71.554147|43.752464|134363714377|2315795|-71.554867|43.752489|134349244376|1589286|-71.55497099999999|43.752113|134342144375|1589285|-71.555049|43.751833|134336854374|2315785|-71.555999|43.750704|134312384373|2315973|-71.55798799999999|43.748622|134265534372|2315950|-71.56366299999999|43.746268|134177984371|1589788|-71.56476599999999|43.745765|134160534370|1591997|-71.56484|43.745691|134158844369|1589787|-71.564886|43.745645|134157794368|2315951|-71.56517599999999|43.745353|134151134367|2315952|-71.56659499999999|43.744599|134127564366|1591999|-71.56685899999999|43.744565|134123974365|543394|-71.567357|43.744335|134116064364|543393|-71.567832|43.744116|134108524363|543392|-71.571827|43.743673|134054444362|541181|-71.57268499999999|43.743802|134042714361|1589786|-71.572964|43.743844|13403890
