Monday, 3 August 2026

SPARQL Property Paths: Finding All Possible Paths Between Two Nodes

  

One of the most powerful features of SPARQL is its ability to traverse RDF graphs using property paths. Property paths allow us to navigate relationships that span multiple hops, making it possible to answer questions such as:

 

·      How are two people connected?

·      What route exists between two entities?

·      Which relationships participate in a connection between two nodes?

·      What dependencies link one asset to another?

 

As you progress on your journey toward becoming a SPARQL expert, you'll discover that property paths are among the most valuable tools available for graph analysis.

 

In this post, we'll tackle an advanced challenge, given two nodes in an RDF graph, can we identify all the triples that participate in paths connecting them?

 

We'll use a simple social network to demonstrate the technique.

 

socialNetwork.turtle

@prefix : <http://example.org/social/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

#################################################
# People
#################################################

:Ram rdf:type :Person ;
     :name "Ram" .

:Hari rdf:type :Person ;
      :name "Hari" .

:Krishna rdf:type :Person ;
         :name "Krishna" .

:Rahim rdf:type :Person ;
       :name "Rahim" .

:Priya rdf:type :Person ;
       :name "Priya" .

:John rdf:type :Person ;
      :name "John" .

:Anita rdf:type :Person ;
       :name "Anita" .

:Vikram rdf:type :Person ;
        :name "Vikram" .

:Neha rdf:type :Person ;
      :name "Neha" .

:Suresh rdf:type :Person ;
        :name "Suresh" .

:Meera rdf:type :Person ;
       :name "Meera" .

:David rdf:type :Person ;
       :name "David" .

:Alex rdf:type :Person ;
      :name "Alex" .

#################################################
# Friendship Network
#################################################

:Ram :knows :Hari .

:Hari :knows :Krishna .

:Krishna :knows :Rahim .

#################################################
# Branch 1
#################################################

:Priya :knows :Hari .

:John :knows :Priya .

#################################################
# Branch 2
#################################################

:Anita :knows :Krishna .

:Vikram :knows :Anita .

:Neha :knows :Vikram .

#################################################
# Branch 3
#################################################

:Suresh :knows :Hari .

:Meera :knows :Suresh .

#################################################
# Another route to Rahim
#################################################

:David :knows :Rahim .

#################################################
# Small cycle
#################################################

:Alex :knows :John .

:John :knows :Alex .

   

1. The Problem

Suppose we want to answer the following question "What paths exist between Ram and Rahim?".

 

At first glance, the answer seems obvious Ram Hari Krishna Rahim.

 

However, rather than simply checking whether a path exists, we want to identify the individual triples that participate in the connection.

 

This is where SPARQL property paths shine.

 

Query

 

PREFIX : <http://example.org/social/>

SELECT ?subject ?predicate ?object
WHERE {

    :Ram (:|!:)* ?subject .

    ?subject ?predicate ?object .

    ?object (:|!:)* :Rahim .
}

Output

subject

predicate  

object

<http://example.org/social/Ram>

<http://example.org/social/knows>  

<http://example.org/social/Hari>

<http://example.org/social/Hari>   

<http://example.org/social/knows>  

<http://example.org/social/Krishna>

<http://example.org/social/Krishna>

<http://example.org/social/knows>  

<http://example.org/social/Rahim>

 

:Ram (:|!:)* ?subject .

This finds every node reachable from Ram. The expression (:|!:)* means:

 

·      Follow any predicate

·      Zero or more times

·      Only in the forward direction

 

?subject ?predicate ?object .

This captures an individual triple from the graph.

 

?object (:|!:)* :Rahim .

This verifies that the object can eventually reach Rahim. Together, these conditions identify triples that belong to at least one path connecting Ram and Rahim.

 

2. Making the Query Reusable

Hardcoding nodes is useful for demonstrations but not ideal in real applications. Instead, we can parameterize the query using VALUES.

 

PREFIX : <http://example.org/social/>

SELECT ?subject ?predicate ?object
WHERE {
  
    VALUES ?start { :Ram } 
    VALUES ?end { :Rahim }

    ?start (:|!:)* ?subject .

    ?subject ?predicate ?object .

    ?object (:|!:)* ?end .
}

   

Output

subject

predicate  

object

<http://example.org/social/Ram>

<http://example.org/social/knows>  

<http://example.org/social/Hari>

<http://example.org/social/Hari>   

<http://example.org/social/knows>  

<http://example.org/social/Krishna>

<http://example.org/social/Krishna>

<http://example.org/social/knows>  

<http://example.org/social/Rahim>

 

Just update the start variable to :John, to get the paths from John to Rahim.

 

PREFIX : <http://example.org/social/>

SELECT ?subject ?predicate ?object
WHERE {
  
    VALUES ?start { :John } 
    VALUES ?end { :Rahim }

    ?start (:|!:)* ?subject .

    ?subject ?predicate ?object .

    ?object (:|!:)* ?end .
}

   

In summary, Property paths are one of the most powerful capabilities offered by SPARQL. By combining property path expressions with ordinary triple patterns, we can identify the individual triples that participate in routes between two nodes in a graph.

 

In this post, we used a social network to:

 

·      Traverse arbitrary-length paths

·      Restrict traversal to a forward direction

·      Discover connections between people

·      Parameterize queries using VALUES

·      Handle cycles safely

·      Decompose graph routes into their constituent triples

 

This pattern is extremely useful whenever you need to understand how entities are connected inside an RDF graph. Whether you're working with social networks, data lineage platforms, organizational hierarchies, or knowledge graphs, property paths provide a powerful mechanism for exploring relationships that span multiple hops.


  

Previous                                                    Next                                                    Home

No comments:

Post a Comment