0:26

There are some things that Cypher is capable of doing very well.

Â And there are other things that require a little bit of creativity

Â in order to get the results that you're looking for.

Â And we'll show you some examples of that.

Â It's also important to keep in mind that because we're working with paths,

Â which are an official structure in graph networks,

Â each one of these examples includes a new variable.

Â Which in this case, we're using the letter p to represent for

Â the actual path objects that we're going to be returning.

Â You may also see the complete word path instead of just the single letter

Â p to represent these objects.

Â We're going to continue to use the data set of a simple road network that we've

Â already been using in previous demonstrations that contains 11 nodes and

Â 14 edges.

Â 1:18

So the first query we're going to demonstrate

Â is finding a path between specific nodes.

Â So this would be very much like trying to find a root between two different

Â locations in our road network.

Â In this case, we're going to find a path between the node named H and

Â the node named P.

Â 1:37

To do this, we'll use the match command and

Â we'll say match p which is a variable we're using to represent our path,

Â = node a, going through an edge to node c.

Â There's something slightly different about this edge, and that is that we're using

Â a star to represent an arbitrary number of edges in sequence between a and c, and

Â we'll be returning all of those edges that are necessary to complete the path.

Â And in this case we only want to return a single path.

Â So when we submit this query, we see this path.

Â It consists of eight nodes and seven edges.

Â And it begins with H and ends with P.

Â 2:29

So we'll issue the same two lines of code and then we'll use this new command,

Â length, to return an actual value.

Â We want to be returning an actual path.

Â And we just want a single value.

Â And when we submit this query, we get the result seven.

Â And we can see that by visually inspecting the graph or our seven edges.

Â But because most networks are much more complex than this,

Â we would need to understand the necessary query to return the length.

Â 2:58

And ideally, in the case of our road network,

Â we would want to find the shortest path between those two nodes.

Â So in this case we're introducing yet

Â another new command specific to paths called shortestPath.

Â We will use the same variable key, and

Â the same descriptions in our syntax, in connecting node a with node c.

Â And in this case, were going to look for the shortest path between node a and

Â node p, and we're going to return that path as well as the length of that path.

Â And we're just going to return a single path.

Â 3:31

And when we submit this query, we get a path that's five nodes and

Â four edges long and if we look at the text results that are returned,

Â we'll see a length displayed in the length column.

Â And that value is 4, and we can see that by visually inspecting our graph.

Â 3:52

The next query we are going to demonstrate is intended to illustrate that

Â there may be more than one shortest path.

Â And so, we may want to know all of the possible shortest paths in order to make

Â a choice between which one we prefer.

Â So we'll be using a command that is built into Neo4j called, allShortestPaths.

Â We'll be issuing a similar query to what we issued previously,

Â we're going to try to find all of the shortest paths between node a and node p.

Â And instead of the letters a and c, we're using the terms source and destination.

Â But the results that we're going to return will actually be in the form of an array.

Â We're using a new term, extract, which is based on the following.

Â Assuming we have matched our path p, we want to identify

Â all of the nodes in p and extract their names.

Â And we'll return these names as a listing, which we'll call the variable paths.

Â If there's more than one shortest path, we'll get multiple listings of node names.

Â So when we submit this query, the results are listed in the rows display and

Â we see there are actually two shortest paths.

Â They each have five nodes and four edges.

Â 5:23

So in this case we still want to find the shortest path, but

Â in this case we may want to constrain the path length to be greater

Â than a particular value, in this case 5.

Â And then, we want to return essentially the same results that we returned in

Â the previous query.

Â But we'll also want to return the length of the resulting path just so

Â we have that information conveniently.

Â So when we issue this command

Â 6:02

Now that we are somewhat familiar with the two shortest path commands,

Â the shortest path, or a single path and

Â the all shortest paths command or multiple shortest paths, we're going to use

Â that in a little bit of a creative way to return the diameter of the graph.

Â And if you remember from a previous lecture,

Â the definition of the diameter of the graph

Â is actually the longest continuous path between two nodes in the graph.

Â So by using the shortest path command, but returning all possible shortest paths,

Â we're actually going to get the longest path included in those results returned.

Â Now, if we look carefully at this script,

Â it is a little different than our previous scripts.

Â In this case our match command is matching all nodes of type MyNode.

Â We'll assign those to the variable end.

Â We're also matching the all nodes of type MyNode and assigning that to variable m.

Â So these matches are the same.

Â But we want to place a constraint such that the nodes in n are not

Â the same as the nodes in m, and

Â then we want to find all of the shortest paths between unique nodes in n and m.

Â And return the names of those nodes as well as the length of that resulting path.

Â And the trick is to use the command order by.

Â And so for those of you who are familiar already with SQL query language,

Â you'll recognize order by.

Â You'll also recognize the descend command.

Â So if we order the resulting paths by their length in descending order,

Â and only return 1, that path should actually be the longest path.

Â And that's equal to the diameter of the graph.

Â 7:48

So when we submit this query, here's the results that we get.

Â We get a path between node e and node l with length severn.

Â Or maybe it occurs to you that maybe this is not the only diameter of the graph,

Â the only path with length of seven.

Â