# Shortest k is fast, but I don't understand how it works\[Revised!\]

**URL:** <https://community.neo4j.com/t/shortest-k-is-fast-but-i-dont-understand-how-it-works-revised/73797>\
**Category:** Neo4j Graph Platform\
**Created:** [May 9, 2025, 8:58am UTC](https://community.neo4j.com/t/shortest-k-is-fast-but-i-dont-understand-how-it-works-revised/73797 "2025-05-09T08:58:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dongho](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dongho/32/3279_2.png) [@Dongho](https://community.neo4j.com/u/Dongho)\
**Post date:** [May 9, 2025, 8:58am UTC](https://community.neo4j.com/t/shortest-k-is-fast-but-i-dont-understand-how-it-works-revised/73797/1 "2025-05-09T08:58:59Z")

</div>

I used the graph from the following link:

> **[network-management/data at main · neo4j-graph-examples/network-management](https://github.com/neo4j-graph-examples/network-management/tree/main/data)**
>
> Dependency and root cause analysis and more for network and IT management graph example - neo4j-graph-examples/network-management

Two queries returned different results and had quite different times!

\< query 1 \>  
match (m:Machine{name:'DC1-RCK-1-2-M-3'}), (m2:Machine{name:'DC1-RCK-1-4-M-45'})  
match path = shortest 300 (m)-[:!TYPE&!HOLDS]-+(m2)  
where not any(node in nodes(path) where 'DataCenter' in labels(node) or 'Version' in labels(node))  
return count(_)  
count(_)  
7  
15624 ms ==\> time was changed. see my reply below.

\< query 2 \>  
match (m:Machine{name:'DC1-RCK-1-2-M-3'}), (m2:Machine{name:'DC1-RCK-1-4-M-45'})  
match path = shortest 300 (m)--+(m2)  
where not any(node in nodes(path) where 'DataCenter' in labels(node) or 'Version' in labels(node))  
and not any(rel in relationships(path) where type(rel) in ['TYPE', 'HOLDS'])  
return count(_)  
count(_)  
3  
800 ms

---

<div class="post-metadata">

**Author:** ![valerio.malenchino](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/valerio.malenchino/32/5358_2.png) [@valerio.malenchino](https://community.neo4j.com/u/valerio.malenchino)\
**Post date:** [May 9, 2025, 12:44pm UTC](https://community.neo4j.com/t/shortest-k-is-fast-but-i-dont-understand-how-it-works-revised/73797/2 "2025-05-09T12:44:34Z")

</div>

The first query finds the 300 shortest paths that do not have TYPE and HOLDS. Path are filtered on expansion, before shortest is applied.  
The second query finds 300 shortest paths between the 2 end nodes, then throws out the paths that have TYPE and HOLDS. Paths are post-filtered.

That is not the same. The second query could return 300 shortest paths, all of them including TYPE and/or HOLDS, and then throw them all away in the following filter. It might ignore other paths that might be a bit longer, but not include TYPE/HOLDS.  
The first query will only consider for shortest the paths that obey the filter.

You should not expect the same results, they are different queries.

---

<div class="post-metadata">

**Author:** ![Dongho](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dongho/32/3279_2.png) [@Dongho](https://community.neo4j.com/u/Dongho)\
**Post date:** [May 10, 2025, 5:19am UTC](https://community.neo4j.com/t/shortest-k-is-fast-but-i-dont-understand-how-it-works-revised/73797/3 "2025-05-10T05:19:57Z")

</div>

Thank you for your reply and kind explanations.  
I also found that the first query executes at once when I index for (m:Machine) on m.name.
