The relationship chain that you are talking about is not the same as a "path". A node does not have more than one relationship that is the first in the chain.
The chain of relationships is a doubly-linked list that contains that Node's relationships. Given that Neo4J already has found the first user in the pattern, it will perform the following steps (or something similar):
- Follow the pointer from the node record to the first element of the linked list that contains all of that node's relationships (this first element is the "first relationship in the chain").
- For each element of the linked list:
- Check if matches the criteria for the searched-for relationship (here, it would be that it has the type
HAS_SKILL
).
- If it does match the criteria, the relationship is kept for future following; if it does not match, it is discarded.
- Follow the pointer to the next element in the relationship linked-list (in the "chain"); if at the last element already, exit the loop.
- For each of the relationships retrieved by scanning the linked list, follow them to the node they point to and continue evaluating the pattern.
The actual algorithm may differ slightly; e.g. it may use depth-first traversal instead of breadth-first, or it may be optimized in a different way, but the end result is the same.
From Graph Databases, 2nd Edition by Ian Robinson, Jim Webber and Emil Eifrem, page 154:
To find a relationship for a node, we follow that node’s relationship pointer to its first relationship (the LIKES relation‐ ship in this example). From here, we then follow the doubly linked list of relation‐ ships for that particular node (that is, either the start node doubly linked list, or the end node doubly linked list) until we find the relationship we’re interested in.
Finally, @InverseFalcon points out that this will be implemented differently for densely-related nodes, by their estimate at around 50+ relationships. At this point, a slightly different structure is used which groups by types and direction so the cost to search through is reduced.