(smollerGraph)
  • Episode 12
  • 34 min

Shortest paths in a graph database

Shortest paths in smollerGraph, with a breadth-first search that keeps each node's parent, then two of them that meet in the middle, shortestPath in Cypher as Neo4j writes it, and a path drawn on the page.

1 November 2026 · Watch on YouTube

We teach smollerGraph to find the shortest path between two nodes. A breadth-first search keeps the relationship that first reached each node, so the path can be read back from the end, and sg path prints it, with what the search cost. On a social graph, a search from one end reaches nearly everyone, so we search from both ends, until the two meet in the middle. Cypher gets shortestPath, as Neo4j writes it, with relationships that go either way, a plan that finds both ends and then the path, and the page draws the path. It's a vanilla version of what Neo4j and Kùzu offer, written to see how it works.

Topics: Cypher, Graph algorithms, Visualization

The code

Each step on screen is a tag in the database's repository. The code at the end of the episode is ep012/end.

  1. 4:43ep012/step-1HopsBrowseChanges
  2. 14:58ep012/step-2Both endsBrowseChanges
  3. 19:10ep012/step-3shortestPath in CypherBrowseChanges
  4. 30:05ep012/step-4The path on the pageBrowseChanges

Transcript

Intro

0:00 Hi, and welcome to episode twelve, where we're going to teach smollerGraph to find the shortest path between two nodes, the way Neo4j's shortest path does it, with a search from both ends. First, a quick look at what we'll have by the end.

0:17 By the end, smollerGraph is going to answer this question, how the red fox is related to the lion, with the shortest path between them. We run it, and the page draws the path, from the fox up to the order of carnivores, and down to the lion. And the table has the path as Cypher writes it, six hops long.

0:38 The series has a website, where you can find more about the project. The code is on GitHub, open source, with a tag for each step of each episode, so you can check it out at any point. And each episode gets a page of its own, with its chapters and the code it shows.

Last time

0:55 Last time, we taught smollerGraph to follow a relationship backwards. Labels and relationship types became tokens, kept once each in a store of their own, which made room in the records. So each relationship went into a second chain, of the relationships that end at a node, as well as the chain of those that start at it. The planner learned to start from the node it can find most quickly, and to expand both ways from it. And we measured all three databases in Cypher, from either end of a pattern. Today, those two chains are what lets us walk a path in either direction.

Today

1:31 Here's how today is going to go, from the search up to the page. We're going to write a breadth-first search, which keeps track of how it reached each node, so it can read the path back once it gets to the end. Then we make it search from both ends at once, and you're going to see why that matters so much on a social graph. Cypher gets shortest path, written the way Neo4j writes it, and the page draws the path it returns, as Neo4j Browser does. So, let's get going, with why we'd want a shortest path at all.

Why paths

2:02 A shortest path answers a question many graphs get asked: how do I get from here to there, in as few steps as possible? On a metro map, it's the route with the fewest stops between two stations. On a social network, it's how many people stand between you and someone else, the degrees of separation a professional network shows next to a name. In security, BloodHound draws a company's users, machines and permissions as a graph, and the shortest path from a user to an administrator is a way an attacker could go. And in a knowledge graph, a path is how two things are related. For our animals, it's how closely two species are related, through the genera and families they share, which mvGPT could ask about at some point.

Neo4j and Kùzu

2:47 Neo4j and Kùzu both answer this question, and they write it in two different ways. In Neo4j, you wrap a pattern in shortest path, and name the path with a variable, as in p equals shortest path. Recent versions of Neo4j have SHORTEST too, from GQL, the ISO standard for graph queries, which Cypher follows more and more closely. Behind the scenes, Neo4j's manual says it runs a fast bidirectional breadth-first search, a search from both ends at once. And Neo4j Browser draws a path that a query returns as its nodes and the relationships between them, which is what our page is going to do. Kùzu writes it inside the relationship, as a star, then SHORTEST, and the range of hops to try. Keep in mind that both of these count hops. A path whose relationships have a cost, such as a distance, is a different question, which Neo4j leaves to its libraries, the Graph Data Science library and APOC, and which we'll leave for later. What we're going to write is a vanilla version of Neo4j's shortest path, to see how it works, and it isn't going to scale the way theirs does.

Breadth-first

3:55 The search we're going to write is a breadth-first search, which goes out from the start a hop at a time. From the red fox, following PARENT_TAXON either way, the first hop reaches its genus, Vulpes. The second reaches the other foxes in Vulpes, and the dog family, Canidae. The third reaches the dog family's other genera, and the order of carnivores, and the fourth, more of their species, the other families of carnivores, and the mammals. Each hop goes on from the nodes the last one reached, and no further, so when the search reaches the lion, at six hops, there's no shorter path. The search keeps, for each node, the relationship that first reached it, and the node it came from, its parent. So the path is read back from the lion, parent by parent, to the fox.

Hops

4:44 A path is a list of nodes and the relationships between them, and Neighbours gives us the nodes alone. So in the graph's header file, we start with the direction, which gets a third value, k both, as the comment says, to follow those that start at a node, those that end at it, or both. A path between two species goes up from one and down to the other, so it has to follow PARENT_TAXON either way. Then Hop, a struct with two fields, the relationship's id, and the node at its other end, which its comment calls a step from a node along one of its relationships. In the class, Hops returns the hops from a node along relationships of one type. Its comment says which: those that start at it, those that end at it with k in, or with k both, the ones out and then the ones in. Neighbours stays, for the walks and the planner, and its comment says it returns the nodes those hops lead to. In the private part, Follow is going to walk one chain, out or in, and add its hops, in the order they were added, so both directions share it. In graph, the function that was Neighbours becomes Hops, with the signature from the header, and in place of a vector of node ids, it fills a vector of hops. The type's token is looked up first, as before, and without one, there are no hops. Then it reads the node's record once, and follows its chain out unless the direction is in, and its chain in unless it's out. So with k both, it follows both, one after the other. Follow walks a chain from its first id, reading each relationship, and keeps a hop for each one of the type, with its id and the node at its other end. Going out, that's the node it goes to, and going in, the node it comes from. Then the next id is the next out, or the next in. The chain is newest first, so its hops go on the end of the list backwards, with reverse iterators, from the last to the first. Neighbours is a loop over Hops now, taking the node from each hop, so the walks from earlier episodes give the answers they gave.

The search

6:44 The search goes next to the walk from episode one, in query's header file. We include cstddef, for size t, optional, for a path that may not be there, and string, for a path written out as text. Path is a struct that holds a path's nodes, from its start to its end, and the relationships between them, one fewer, as its comment says. That's what a path is in Neo4j too, nodes and relationships, one after the other. Path search is what a search found: the path, as an optional, as there may be none, and how many nodes the search reached, which is most of what it cost. That number is how we're going to compare one search with another. Shortest path takes the graph, the two nodes, the relationship type and the direction to follow it in, and its comment says it returns a path with as few hops as any other. There can be several of those, and it returns the first one the search finds. Path text writes a path as Cypher does, with the nodes' names, and each relationship's arrow pointing the way it goes. In query, we include optional and string, and unordered map, the standard library's hash table, as the search keeps a parent for each node it reaches, and looks them up by the node's id. Then Abseil's str cat, for the path's text. In an unnamed namespace, Parents is a using alias, a short name for an unordered map from a node to a hop. Its comment says what that hop holds: the relationship that reached the node, and the node the search came from, so it points back. Path to builds the path from the search's start to a node, out of the parents. It starts with the node, and goes from parent to parent, adding each relationship and the node before it, until it gets to a parent with no relationship, which is the start. It's built from the end, so the nodes and the relationships are reversed once it's done, and its comment says so. Then comes the search, in Shortest path, where a path from a node to that node is the node on its own, with no hops. Otherwise, the start goes into the parents, with no relationship as its parent. The frontier is the nodes the search reached last, which is the start to begin with, and the comment over it says why the search works. It's a breadth-first search, the nodes one hop away, then one hop further, and so on, so the first time it reaches the end, it's by a path with as few hops as any. While there's a frontier, it expands each of its nodes along each of its hops. Try emplace adds the node with its parent, and its second value is false when the node was in the map already. That node was reached by a path as short as this one, or shorter, as the comment says, so the hop is skipped. When the hop reaches the end, the path is read back from it, and returned with how many nodes were reached. Otherwise, the node goes on the next frontier, which takes over once the hop is done. When the frontier runs out, there's no path, and it returns how far the search got. Path text starts with the first node's name, in brackets. For each relationship, it reads the record, and when the relationship starts at the node before it, it gets its arrow on the right, and otherwise on the left, then its type, and the next node.

sg path

9:49 In main, the usage gets a line for sg path, which takes a graph's folder, the two nodes' names, and the relationship type. Find path is the function that runs it. It finds both nodes by their names in the index, and when either isn't there, it says which one, and returns one. Then it reads the clock and the count of pages read, runs the search, following the type either way, and reads the clock again. When there's a path, it prints it, then how many hops it has, which is its number of relationships. Otherwise, it prints no path, and either way, it prints how many nodes the search reached, the time it took, and how many pages it read. Its comment says what it prints: a shortest path along relationships of a type followed either way, and what finding it cost. In main, path is a command when it comes with the folder, the two names and the type, and it calls Find path with the three after the folder. The CMake file gets a new test file, path test, in the list of the test program's sources. The tests go in that file of their own, as the search is a part of the database with its own job. It includes sstream, for facts written in a string, then Abseil's status, facts, the graph's header, GoogleTest, and query's header, as the test is about the search. The path test fixture loads part of our food chain: the fox eats the rabbit and the field mouse, the owl eats the field mouse too, the rabbit eats clover, and the mouse eats wheat. The rabbit lives in the meadow, with LIVES IN, which is a type the searches don't follow. Shortest is a helper that runs a search along EATS, between two names, and returns the path as text, or none, and its comment says so, the path as Cypher writes it. Finds a shortest path either way is the test that matters most, as it's the reason we wrote Hops with both directions. From the owl to the rabbit, there's a path through the field mouse and the fox, and the relationship in the middle, from the fox to the mouse, is followed against its arrow, as its comment says. Following the arrows alone, there's no path from the owl to the rabbit, and from the fox to the clover, there's one through the rabbit. Says how far it searched starts with the simplest case, a search from a node to that node. From the fox to the fox, the path is the fox on its own, with no hops. Then from the fox to the meadow, which no EATS reaches, so the search goes through the six animals and plants that EATS joins to the fox before it gives up, as the comment says.

12:14 Let's build it, with the search and sg path, and run the tests. All fifty-three of them pass, the old ones and the two new ones, so the walks still find what they found, through Hops. With the Wikidata graph loaded, let's ask for the path from the red fox to the lion. It goes up from the fox to its genus and the dog family, to the order of carnivores, and down through the cat family and Panthera, to the lion. Six hops, and the search reached four hundred and twenty-three nodes on the way, with a hundred and thirty-four pages read. From the fox to the grey wolf, the path goes up to the dog family, and down through Canis, the genus of wolves. That's four hops, and the search reached fifty-six nodes.

One end

12:59 The Wikidata graph is a tree, and a search in a tree doesn't have far to go, while a social graph is a different story. With the friends graph from episode three loaded, a hundred thousand people who each know ten others, let's ask for the path between the first two of our thousand people. There's a path of five hops, through four other people. But the search reached about ninety-eight thousand of the hundred thousand people to find it. It read all seven thousand, four hundred and forty-six pages of nodes and relationships, which took about forty milliseconds.

13:31 Here's why it reaches so many: each person knows ten others, and about ten others know them, so following KNOWS either way, a person has about twenty neighbours. The first hop reaches nineteen people, the second three hundred and forty-eight, and the third six and a half thousand, about twenty times more at each hop. The fourth hop reaches about sixty-eight thousand, and the fifth the rest of them. The path is five hops long, so the search has to finish the fourth hop, about seventy-five thousand people, before it can reach the end. In a social graph, most people are a few hops away from anyone, so a search from one end meets most of them before it gets there.

Both ends

14:09 So we're going to do what Neo4j does, and search from both ends at once. One search starts from the first person, and the other from the second. Each round, one of them takes a hop, the one with fewer nodes to go on from, so the two grow at about the pace of each other. After two hops or so each, each side has reached a few hundred people, and as soon as one of them reaches a node the other has reached, they meet. The path is the first search's half, up to that node, then the second's half, backwards. Two searches of half the length reach far fewer people than one search of the whole length, as each hop multiplies what a side reaches by about twenty. The first meeting gives a shortest path, as each side takes a whole hop at a time. A shorter path would have to pass through a node that both sides had already reached.

14:58 The search from the end walks the path backwards, so it follows each relationship the other way. In the graph's header file, Reversed moves in from the planner, as the search needs it too. Its comment says what it gives: the direction to follow a relationship in from its other end, where out becomes in, and in becomes out. It's inline, as each file that includes the header gets a copy of it, and inline lets the linker keep one. The first two ifs swap out and in, and anything else is both. In plan, the planner's copy of Reversed goes, as it uses the header's now. In query, Side is a struct for one end of a search from both ends. It holds the direction it follows relationships in, each node it has reached, with the hop it came by, and the nodes it reached last, which it goes on from. Those are its direction, its parents, and its frontier. Expand takes a side one step further, and its loop is the one from our first search, with a single change. When a hop reaches a node for the first time, it checks whether the other side has reached that node, and if it has, it returns the node, which is where the two searches meet. Otherwise, the node goes on the next frontier, and once the hop is done, the side's frontier is the next one, and there's no meeting. Its comment says that: it takes the side a hop further, and returns the first node it reaches that the other side has reached too, if there's one. In Shortest path, the old loop goes, and two sides take its place, each with its own parents and its own frontier, so the two searches don't share a map. The start's side follows the relationships in the direction we were given, from the start, and the end's side follows them reversed, from the end, and the comment says why. The loop goes on while both sides have a frontier, as a side with none has run out of nodes to reach. Each time round, the side with fewer nodes to go on from takes the next hop. That's likely to read fewer relationships, as its comment says, and it keeps the two sides about as big as each other. When they don't meet, the loop goes round again. When they meet, Path to reads the start's half back from the meeting node, through the start's parents. The comment over it says what comes next: the start's half of the path runs to where they met, and the end's half runs from the end to there, so it's added backwards. Path to reads that half from the end's parents, and it's added backwards, its nodes from the one after the meeting node, and its relationships, all of them, with reverse iterators. The nodes reached are both sides' parents, less one, as the meeting node is in both. When either side runs out of nodes, there's no path, as everything it can reach has been reached. In the path tests, the search from the fox to the meadow reaches fewer nodes now. The comment says why: the side that starts from the meadow runs out after a hop, with the fox's side at its two meals, four nodes in all.

17:47 With the friends and the Wikidata graph loaded, let's run the tests. All fifty-three of them pass, the paths from before among them. Then the path between our two people again. It's five hops, and it's the path we found before, but the search reached about fifteen hundred people, not ninety-eight thousand, and read about eight hundred pages, in about a millisecond, which is about forty times less time. A disclaimer, though: from both ends, a search for a path that isn't there costs more. The red fox and the tawny owl have no path, as our graph stops at the classes, mammals and birds. So the two sides go through all of the mammals and all of the birds, about twenty-two thousand taxa, before one of them runs out. From one end, it would have stopped at the mammals.

shortestPath in Cypher

18:33 Now for Cypher, where our subset is going to take Neo4j's way of writing it. The pattern goes inside shortest path, with the path's variable before it, as in p equals. The relationship has a star with no number after it, which means any number of hops, and no arrow at either end, which means it can be followed either way. RETURN can return the path, as p, and its length, with length of p, the number of relationships it has. The plan finds both ends of the path, each in the way that's cheapest for it, and then runs the search between them, which is what Neo4j's plan does too.

19:11 In the parser's header file, k any hops is the number of hops for a star with no number after it, zero, which no relationship of a fixed length can have. It's inline constexpr, like the token store's k no token. The relationship pattern's comment adds one written with no arrow, which goes either way, and its direction's comment adds both, for a relationship with no arrow. Then Return item, for what a query returns in a column, which was a property lookup until now. It has a variable, and a key for a property, such as b dot name. With no key, it's a path's variable, which returns the path, and length is true for length of p, as the comment says. The query gets path, the path's variable, for a pattern written as p equals shortest path, which asks for a shortest path between its two nodes, and its returns become return items. In the parser, the grammar's comment says that MATCH takes a pattern on its own, or a variable, an equals sign, and shortest path around the pattern. A pattern is a node, then a relationship and a node, as often as you like. Parse query looks ahead: a word followed by an equals sign is a path's variable, and the comment says they name the path that follows. So it reads the variable, the equals sign, the keyword shortest path and an opening bracket, and reads the pattern as before. After the pattern, a shortest path reads its closing bracket. Further down, in the returns, in place of lookups, RETURN reads return items now. In parse relationship, the grammar's comment changes, with square brackets around the parts that can be left out. After the star, it reads a number with from chars, as before, which fails when there's no number. The comment over the next line says that a star with no number after it stands for any number of hops, so when what follows isn't a number, the hops are k any hops. Otherwise, the number has to be from one to ten, as before. Then, after the closing bracket and the minus, a relationship that has no arrow on the left, and none on the right, goes either way, so its direction becomes both, as the comment over it says. Parse return item reads a return item, the new part of RETURN. When a word is followed by an opening bracket, it's a function, which has to be length, then the variable, and a closing bracket. Otherwise, it reads the variable, and a dot and a key, when there's a dot. The comment says a variable on its own returns all of it, which our subset allows for a path, as a path has no properties. After the dot, it reads the key, and returns true. Is symbol calls a new helper now, is symbol at, with zero, for the next token. Its comment says it's whether the token that many ahead of the next one is this symbol. The last token is the end, so with min, a look past it finds the end, and doesn't read past the tokens. In the parser's tests, Reads a shortest path parses the query that the plan's tests are going to run too, written as Neo4j would write it, from the owl to the rabbit, returning the path and its length. It checks that it parses, then the path's variable, the relationship's hops, which are any, its direction, which is both, and the two return items, the path, with no key, and its length. In Says what it expected, a relationship with no arrow used to be an error, and it isn't any more. So that line becomes a path's variable without shortest path, which fails where the keyword should be.

22:34 In the plan's header file, we include query's header, for the path. A step gets a new kind, shortest path, which for each row finds a shortest path from the node in one slot to the node in another, along a relationship type, and drops the row when there's no path. The comment on a step's direction adds that a path follows it too. Then Column, a struct for a column of what a plan returns. Its comment says it has a heading, such as b dot name, p or length of p, and the property of the node in a slot that it returns, or with no slot, the row's path, or its length. So it holds the heading, the slot, which is minus one when there's none, the key, and whether it's the length. The plan's three lists, of slots, keys and headings, become a single list of columns. Plan query's comment adds that for a shortest path, it finds both of its nodes, and then the path, and that it refuses a pattern it can't plan. The comment over the cell adds that a path, or its length, comes with the path, and its first node, so the cell gets a path. In plan, Add find is a new function. Its comment says it adds a step that finds a node of the pattern, in a slot of its own. That's a seek in the index for a node with a name, a scan of a label for a node with a label, and otherwise a scan of all the nodes. Then come the filters on it, and it returns its slot. That's the code that found the start until now, moved into a function, as a plan with a path finds two nodes. The node's variable goes on the list of slots, and its slot is the last one, the size of the list less one. With a name, it pushes a node index seek into that slot, with a label, a node by label scan into it, and otherwise a scan of all the nodes. Add filters adds the filters on the node, after whichever step found it, and it returns the slot. Arrow is for EXPLAIN, and its comment says it writes a relationship as Cypher does, with its arrow pointing the way it's followed, or with no arrow when it's followed both ways. It returns a left arrow and a bracket when the direction is in, and a minus and a bracket otherwise, then the type. Then a bracket and a right arrow going out, or a bracket and a minus otherwise. Row is a struct, and its comment says a row is a node in each of the plan's slots, and the path the plan found, for a plan that finds one. So it holds a vector of nodes, which was the whole row until now, and a path. Add returns takes the returns out of plan query, as both kinds of plan are going to need them. Its comment says it adds a column to the plan for each item the query returns, and returns the plan, or an error for an item it can't return. It takes the plan by value, and the callers move theirs in, so nothing is copied. It sets distinct, and goes through the items. The path's variable gets a column with no slot. When it has a key, the comment over that check says why it can't: a path has no properties, so it's returned whole, or as its length. So a key is an error, whose message says the path has no properties. The column's heading is length of p, or p, and the column says whether it's the length. Then it goes on to the next item. Any other variable is looked for among the slots, and when it isn't there, that's an error, as before. A node's variable needs a key, as a node is returned by its properties, such as its name. Then its column gets a heading, the variable, a dot and the key, then the slot, the variable's place in the list of slots, and last, the key. Once each item has its column, it returns the plan. In plan query, a relationship with any number of hops is an error, unless the pattern is a path, as we've not written a search for each path of any length. For a path, the planner checks the pattern first, as there's a single shape it can plan. The pattern has to be two nodes, with a relationship of any number of hops between them, or it's an error that says so, with an example of the pattern. Then Add find finds the first node, and the comment over it says both ends are found, each in the way that's cheapest for it, and then the path between them. Add find finds the second node, and a shortest path step goes after them, from the first one's slot to the second's, with the relationship's type and direction. Then it returns the plan through Add returns, so the code that comes next is for a pattern with no path. There, Add find takes the place of the code that found the start, and the plan goes through Add returns at the end too. In Run, the rows become rows of the struct, so each slot is in the row's nodes. The first row is made with its nodes and an empty path, and each pass builds a vector of rows. That's a small edit in each case of the switch, the seek, the two scans, the expand, the filter and the label, which each write a node into a slot, or read one from it. The expand's loop is laid out again by clang format, as its line got longer, and the label's check gets braces, as it no longer fits on a single line. The shortest path step runs the search for each row, between the nodes in its two slots, with the step's type and direction. When there's a path, the row is kept, with the path moved into it, and otherwise it's dropped. When the steps are done, the result gets the headings, and then, for each row, each column gives a cell. A column with a slot gives the node's property, as before. A column for the length gives the number of the path's relationships, and a column for the path gives its text, each with the path, and its first node as the cell's node. In Explain, an expand writes its relationship with Arrow, so one that goes either way has no arrow. The shortest path step is written with Arrow too, and the comment over it says why its type gets a star after it: the path has any number of hops. For the return line, the headings go into a vector, with a reserve first, which clang tidy asks for in CI, and they're joined with commas. In the plan's tests, Finds both ends of a shortest path checks the plan from the owl to any animal: a seek for the owl, a label scan for the animals, and then the shortest path. And a relationship with a star and no number, outside shortest path, is an error. Follows a relationship either way asks for what the field mouse eats, and what eats it, with no arrow. The answer is the wheat, then the fox and the owl, and the comment gives the order: what the field mouse eats, then what eats it. Finds a shortest path is the test of the whole chain, from the parser, through the plan, to the search and the rows it returns, which is why it's a run test and not a plan test. It runs our shortest path from the owl to the rabbit in Cypher, on the fixture's animals, which returns the path, as Cypher writes it, and its length, three, in a single row. And following the arrows, there's no path from the owl to the rabbit, so there's no row, as the comment says.

29:25 Let's build it, with the parser and the planner, and run the tests. All fifty-seven of them pass, with the four new ones. On the Wikidata graph, let's ask for the plan of a path between the giant panda and the polar bear, with EXPLAIN. It finds the panda in the index, then the polar bear, and then runs the shortest path step between them, with no arrow, as it follows PARENT_TAXON either way. Then we run it, without EXPLAIN, and it returns the path and its length, in a single row. The path goes up from the panda to its genus and the bear family, and down through Ursus, the genus of the brown and the polar bear, four hops in all.

The path on the page

30:05 The page draws the node of each cell a query returns, and the relationships between the nodes it draws. A path is a single cell, with a single node, so it would draw the path's first node and nothing else. In the API's header file, the comment on query JSON says that the nodes it returns have a path's nodes among them, up to the most it can, with the relationships between them, for the page to draw, as before. In the API, the comment adds that a path draws each of its nodes, so the relationships between them draw the path. The loop over the rows takes each cell's path's nodes, and the cell's node when there's no path. Each of them is drawn once, up to the most the page draws, as before. The relationships come from the code that was there, as it draws the relationships from one drawn node to another. In the API's tests, Draws each node of a path builds the food chain in a graph, with Add node and Add relationship, the fox eating the rabbit and the field mouse, and the owl eating the field mouse. That way, the ids are known: the fox is zero, the field mouse one, the rabbit two and the owl three. Then it asks query JSON, as the page would, for the length of the shortest path from the owl to the rabbit, in Cypher. The row has the length, three, with the owl as its node. Then come all four nodes, in the path's order: the owl, the field mouse, the fox and the rabbit. And it gets the three relationships between them, from the owl's chain and the fox's, which draws the path. Each one names the two nodes it joins, by number, so the page can draw it between them.

31:42 Back in the browser, the page at the end of this step opens on the dog family, as it did. Let's ask for a path between two mammals that are about as far apart as mammals get, the platypus and the blue whale. The query takes a few seconds to type, and its text is our Cypher, with no change on the page. We run it, and the page draws the path, from the platypus up through the monotremes, the mammals that lay eggs, to the mammals. Then it goes down through the even-toed hoofed animals, Artiodactyla, where Wikidata puts the whales, to the blue whale, eight hops in all. And the plan tab shows the two seeks, and the shortest path step between them.

What's missing

32:24 Our shortest path leaves a few things out. It counts hops, while on a map or a network, each relationship has a cost, such as a distance or a time, and the path that costs least can have more hops. Neo4j has all shortest paths, which returns each path of the shortest length, not just the first one found. A pattern can give the most hops to try, with a star, two dots and a number, which keeps a search from running through the whole graph. And outside shortest path, a star with no number would match each path of any length, which our planner refuses for now. And this is a vanilla version of what Neo4j and Kùzu offer. Their searches are built for graphs far bigger than ours, with years of engineering behind them, while ours is there to show how a shortest path works.

Wrap-up

33:11 So, today smollerGraph learned to find a shortest path. A breadth-first search keeps the relationship that first reached each node, so the path can be read back from the end, and sg path prints it with what it cost. On a social graph, a search from one end reaches nearly everyone, so the search goes from both ends, and the two meet in the middle, having reached about fifteen hundred people. Cypher has shortest path, with relationships that go either way, and a plan that finds both ends and then the path, which the page draws. It's a vanilla version of what Neo4j offers, small enough to read from one end to the other.

33:50 This is going to be an evolving project, so I'd love to hear from you. Let me know in the comments which two animals you'd like to see the path between, and if you'd like to follow along, subscribe to the channel. I hope you find this useful, and thanks for watching.