paths

Given an adjacency list of sources, targets, and weights that results in a graph, this directive produces a list of all possible paths in tabular format. It requires the sub-directive path operation.

pebblestream:paths(source, target, weight)
pebblestream:paths-operation(operator, header)

Use the paths directive to transform graph relationships into a tabular format more conducive to spreadsheet processing. Like other transformative directives, the "from" directive is required to work.

Example

Path operations

The paths directive is a powerful feature that facilitates applying powerful graph traversal operations. Here is the complete list of all the path operations you can use.

path

path operationdescription
+The "+" path operation adds all the weights for each discovered path.
-Like the "+" path operation, the "-" path operation subtracts all the weights for each discovered path.
*The "*" path operation multiplies all the weights for each discovered path.
/Like the "*" path operation, the "/" path operation divides all the weights for each discovered path.
firstThe "first" path operation will return the first node on each discovered path.
secondThe "second" path operation will return the second node on each discovered path.
penultimateThe "penultimate" path operation will return the node just before the last node on each discovered path.
lastThe "last" path operation will return the last node on each discovered path.
lengthThe "length" path operation will return a path's total number of hops (edges).
minThe "min" path operator will return the minimum weight on each discovered path.
maxLike the "min", this path operator will return the maximum weight on each discovered path.
avgThe "avg" path operator will compute the average of all weights of each discovered path.

Use Case

Let's work through an example. Consider the worksheet Stations detailing train station connections.

startenddistance
Washington DCBaltimore35
BaltimorePhiladelphia110
PhiladelphiaNewark150
NewarkNew York25

Let's use the paths and paths-operation directives to extract information from the Stations worksheet into the Trips worksheet.

First, let's detail every possible trip from every station going north. And let's include the trip's total distance. Here is the PebbleScript for that.

pebblestream:from("Stations")  
pebblestream:paths("start", "end", "distance")  
pebblestream:paths-operation("+", "trip distance")

The paths directive will scan the Stations worksheet and find all possible paths from one city to another. For each path found, the paths-operation directive will repeatedly apply an operation to each of the values associated with each step in the path. In this case, we are using the "+" operation to add up all the distances between each station on the trip. We label this value "trip distance." The following worksheet shows the result.

starttrip distanceend
Washington DC35Baltimore
Washington DC145Philadelphia
Washington DC295Newark
Washington DC320New York City
Baltimore110Philadelphia
Baltimore260Newark
Baltimore285New York City
Philadelphia150Newark
Philadelphia175New York City
Newark25New York City

This result shows that the longest trip is from Washington, DC, to New York City, and the shortest is from Newark to New York City.

👍

Multiple path operations can be applied at once to a paths directive.

Use the paths operation "length" to count the number of stops on each trip. Add this path operation to show the stops.

pebblestream:from("Stations")  
pebblestream:paths("start", "end", "distance")  
pebblestream:paths-operation("+", "trip distance")
pebblestream:paths-operation("length", "stops")

which results in the new column "stops" added to the worksheet.

starttrip distancestopsend
Washington DC351Baltimore
Washington DC1452Philadelphia
Washington DC2953Newark
Washington DC3204New York City
Baltimore1101Philadelphia
Baltimore2602Newark
Baltimore2853New York City
Philadelphia1501Newark
Philadelphia1752New York City
Newark251New York City

Use the "min" paths operation to find the smallest hop on each path. Once again, add the new paths-operation to achieve this.

pebblestream:from("Stations")  
pebblestream:paths("start", "end", "distance")  
pebblestream:paths-operation("+", "trip distance")
pebblestream:paths-operation("length", "stops")
pebblestream:paths-operation("min", "shortest hop")

This results in the worksheet below

starttrip distanceshortest hopstopsend
Washington DC35351Baltimore
Washington DC145352Philadelphia
Washington DC295353Newark
Washington DC320254New York City
Baltimore1101101Philadelphia
Baltimore2601102Newark
Baltimore285253New York City
Philadelphia1501501Newark
Philadelphia175252New York City
Newark25251New York City

Let's add a column to find the stop just before the final stop in the trip. We will use the "penultimate" paths operation. Let's remove the "shortest hop" column for clarity.

pebblestream:from("Stations")  
pebblestream:paths("start", "end", "distance")  
pebblestream:paths-operation("+", "trip distance")
pebblestream:paths-operation("length", "stops")
pebblestream:paths-operation("penultimate", "get ready!")

This results in the following worksheet, with the "get ready!" column showing the penultimate stop on a trip.

starttrip distancestopsget ready!end
Washington DC351Washington DCBaltimore
Washington DC1452BaltimorePhiladelphia
Washington DC2953PhiladelphiaNewark
Washington DC3204NewarkNew York City
Baltimore1101BaltimorePhiladelphia
Baltimore2602PhiladelphiaNewark
Baltimore2853NewarkNew York City
Philadelphia1501PhiladelphiaNewark
Philadelphia1752NewarkNew York City
Newark251NewarkNew York City

Finally, use the paths operation "id" to label each path with a unique number. We will put this in the "path id" column.

pebblestream:from("Stations")  
pebblestream:paths("start", "end", "distance")  
pebblestream:paths-operation("+", "trip distance")
pebblestream:paths-operation("length", "stops")
pebblestream:paths-operation("penultimate", "get ready!")
pebblestream:paths:operation("id", "path id")

This results in the following worksheet.

path idstarttrip distancestopsget ready!end
0Washington DC351Washington DCBaltimore
1Washington DC1452BaltimorePhiladelphia
2Washington DC2953PhiladelphiaNewark
3Washington DC3204NewarkNew York City
4Baltimore1101BaltimorePhiladelphia
5Baltimore2602PhiladelphiaNewark
6Baltimore2853NewarkNew York City
7Philadelphia1501PhiladelphiaNewark
8Philadelphia1752NewarkNew York City
9Newark251NewarkNew York City

❗️

The paths directives will report an error if the provided sources and targets result in a graph with cycles.