Graph Queries: Why Did My Edge Graph Stop at One Hop?
- Max hops
- 3 hops
- You entered
- Edge limit
- 100 edges
- You entered
If your graph query returned only direct connections (one hop) when you expected deeper paths, the cause is almost always a traversal limit, a filter on edge direction or type, or the way your query language handles path expansion by default. This article explains the common reasons, how to inspect and fix them, and how to design queries that traverse multiple hops reliably.
Formula
The maximum number of distinct paths of length t in a directed graph where each node has out-degree d is d^t (d raised to the power t). This is an upper bound, not the actual count, because real graphs have shared nodes and cycles. The calculator above shows how quickly the number of possible paths explodes as you increase hops and edge limit.
Worked Example
With the defaults (max hops = 3, edge limit = 100), the maximum possible paths of length 3 is 100^3 = 1,000,000. In practice, you will see far fewer because most nodes have fewer than 100 edges, and many paths merge or repeat. If your query returned only 10 results, it is not because the graph has only 10 paths—it is because your query stopped early.
What Does “One Hop” Mean in a Graph Query?
A “hop” is one traversal step along an edge. In a social graph, a one-hop query from user A returns A’s direct friends. A two-hop query returns friends of friends. Most graph query languages (Cypher, Gremlin, SPARQL, GraphQL) support variable-length traversals, but they often have a default maximum depth. For example, in Cypher, MATCH (a)-[:FRIEND*1..3]->(b) explicitly allows 1 to 3 hops. If you write [:FRIEND] without a range, you get exactly one hop. Many beginners forget the * or the range, so the query silently returns only direct neighbors.
Common Reasons Your Query Stopped at One Hop
- Missing variable-length syntax: In Cypher,
[:REL]is one hop;[:REL*]is any length;[:REL*1..3]is 1 to 3. In Gremlin,.out()is one hop;.out('edgeLabel', 1, 3)is 1 to 3 (in older versions) or.repeat(out()).times(3)for exactly 3. - Default depth limit: Some databases (e.g., Neo4j’s older versions) had a default max depth of 1 for certain traversal APIs. Always set an explicit depth.
- Edge direction or type filters: If you only traverse outgoing edges, you won’t see incoming connections. If you filter by edge label, you might exclude the edges that form longer paths.
- Path uniqueness constraints: Some query engines avoid revisiting nodes by default. If a longer path requires revisiting a node, it is pruned. You may need to allow repeated nodes (e.g.,
ALLin Gremlin) if your graph has cycles. - Result limit: A
LIMITclause can cut off results after the first few, which may all be one-hop paths if they are returned first. Remove the limit or order results to see deeper paths. - Index or performance cutoff: Some systems have a “traversal budget” that stops after a certain number of edges visited. If your graph is large, the query may time out before reaching two hops.
How to Diagnose the Exact Cause
- Check the query text: Look for a
*or a range after the edge type. If not, add it. - Run a simple two-hop query: Write a query that explicitly asks for two hops, e.g.,
MATCH (a)-[:FRIEND]->(b)-[:FRIEND]->(c) RETURN c. If this returns results, your graph has two-hop paths; the problem is the query syntax. - Inspect the query plan: Most databases have an
EXPLAINorPROFILEcommand that shows how many edges were traversed. If it stops after one hop, the plan will show a single expansion. - Test with a tiny subgraph: Create a small graph with a known chain of 3 nodes and run your query. If it still stops at one hop, the issue is in the query, not the data.
- Check for global limits: Look at database configuration for traversal depth limits (e.g.,
dbms.cypher.max_lengthin Neo4j).
How to Write Multi-Hop Queries Correctly
In Cypher (Neo4j), use [:REL*1..n] where n is the maximum depth. For example, MATCH (a)-[:FRIEND*1..3]->(b) RETURN DISTINCT b returns all nodes reachable in 1 to 3 hops. In Gremlin, use g.V(a).repeat(out('FRIEND')).times(3).dedup() for exactly 3 hops, or .emit().times(3) for 1 to 3. In SPARQL, use property paths: ?a (foaf:knows)+ ?b for one or more hops, or {1,3} for a range. In GraphQL, you need to explicitly nest connections: friends { friends { friends } }—there is no wildcard, so you must know the depth at design time.
When you write a multi-hop query, remember that the result set can grow exponentially. Use DISTINCT to avoid duplicates, and consider limiting the depth to a reasonable number (e.g., 3–5) to keep performance acceptable.
Performance and Cost Considerations
Multi-hop queries are expensive. Each hop multiplies the number of traversals by the average degree. On a graph with average degree 100, a 3-hop query visits up to 1,000,000 nodes. This can cause timeouts or memory exhaustion. To mitigate:
- Use indexes on the edge properties you filter by.
- Limit the depth to the minimum needed.
- Use
LIMITon the result set, but be aware it may cut off deeper paths. - Consider using a dedicated graph database that optimizes for deep traversals (e.g., Neo4j, Amazon Neptune) rather than a relational database.
- If you only need the count of nodes at each depth, use a BFS algorithm instead of a pure query.
Alternatives When Queries Are Too Slow
If multi-hop queries are too slow, you can precompute the transitive closure (all pairs of reachable nodes) or use a graph embedding to approximate distances. For social networks, a common alternative is to store “friends of friends” as a separate edge type, updated asynchronously. For recommendation systems, you might run a batch job to compute 2-hop neighbors and store them in a table. These approaches trade storage for query speed.
Frequently Asked Questions
Why does my Cypher query return only one hop even with *?
If you use [:REL*] without a range, it means “one or more”, so it should return multiple hops. If you still see only one hop, check that the graph actually has longer paths. Also, if you use MATCH (a)-[:REL*]->(b) and then RETURN b, you might get duplicate b’s, but you should see nodes at distance 2 and 3. If not, your data may not have any paths longer than 1.
Does LIMIT affect the number of hops?
No, LIMIT only limits the number of results returned, not the traversal depth. However, if you limit to 10 results and the first 10 happen to be one-hop neighbors, you won’t see deeper ones. Remove the limit or order by depth to see them.
How do I count nodes at each hop level?
In Cypher, you can use MATCH (a)-[:REL*1..3]->(b) RETURN length(p) as depth, count(DISTINCT b) but you need to capture the path. Alternatively, run separate queries for each depth and union the results.
Can I traverse both incoming and outgoing edges in one query?
Yes. In Cypher, use MATCH (a)-[:REL*1..3]-(b) (without direction) to traverse both ways. In Gremlin, use both() instead of out(). Be aware this can dramatically increase the result set.
Why does my query time out on 2 hops?
If your graph has high-degree nodes (e.g., a celebrity with millions of followers), a 2-hop query can explode. Use a depth limit, filter by edge properties, or use a sampling approach.
Related
Formula used
in t | Max hops | hops | 3 | 1 in d | Edge limit | edges | 100 | 1 out max_paths | Max possible paths | paths | d ^ t | 0
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.