A shortest path connects two nodes using the fewest relationships. In an unweighted network, every relationship has the same cost, so shortest-path length is the network distance between the nodes. section 10.3.1
Use shortest paths to:
- Create network-distance features for link prediction. A distance of 2 indicates that two people may share a friend; larger distances generally suggest weaker future connection likelihood. section 10.3.1
- Analyze centrality. Betweenness centrality counts how often a node lies on shortest paths, while closeness centrality sums distances from a node to others. section 8.1.2 section 8.1.3
- Check data preparation. In link prediction, calculate paths from a feature relationship set separate from training and test relationships to avoid feature leakage. section 10.2
Calculating a network distance
If the shortest route from node A to node E traverses two relationships, their network distance is 2. In Cypher, shortestPath() accepts source and target nodes, relationship constraints, and optional traversal bounds; length(p) returns the distance. section 10.3.1
Listing 10.3 Finding shortest unweighted paths with Cypher
MATCH (source:Person {name:"Surya"}),
(target:Person {name:"Jim"}) #1
MATCH p = shortestPath((source)-[:FRIEND|COWORKER*1..10]->(target)) #2
RETURN p
Shows a Cypher query that finds a shortest unweighted path between Surya and Jim using FRIEND or COWORKER relationships and a one-to-ten traversal range. section 10.3.1
A path is undefined when nodes belong to different components. Such missing network distances can be filled with a dataset maximum, such as 4, when a large distance represents low connection likelihood. section 10.4.1





