liveBook · Manning

Shortest path

As explained in Graph Algorithms for Data Science

Network distance is the minimum number of relationships along a path connecting two nodes, particularly when relationships are unweighted.

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

How Graph Algorithms for Data Science covers it

How deep it goes
Defines unweighted traversal, demonstrates Cypher syntax, and addresses leakage and disconnected graphs without covering weighted-path algorithms.
Examples
Uses nodes A and E, a Surya-to-Jim Cypher query, social-network friends, and Marvel-character centrality visualizations.
What it assumes
Assumes basic graph nodes and relationships while defining unweighted paths and distance through concrete examples.
What you can do after
Apply it via Cypher to find paths, measure relationship distance, and construct graph features for link prediction.
How it connects
Connects shortest path with network distance, link prediction, feature leakage, betweenness centrality, and closeness centrality.

Also explained in

Compare how 6 books explain shortest path →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Graph Algorithms for Data Science. The section links point to the parts of the book it draws on.