Showing posts with label turtle. Show all posts
Showing posts with label turtle. Show all posts

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

SPARQL Recursive Paths: Finding Friends of Friends with Property Paths

  

One of the most powerful features of SPARQL is its support for property paths, which allow us to traverse relationships across multiple hops in a graph.

 

In traditional SQL databases, finding indirect relationships often requires multiple self-joins. In SPARQL, however, a single property path expression can traverse a graph recursively, making it easy to answer questions such as:

 

·      Who knows Krishna directly or indirectly?

·      Who can eventually reach Krishna through a chain of friendships?

·      Which people are connected through friends of friends?

·      How can we navigate hierarchical or network structures without knowing the path length in advance?

 

In this post, we'll explore recursive paths using a simple social network example.

 

Consider the following RDF graph:

 

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. Finding Direct Connections

Let's start with a simple query. Suppose we want to find people who directly know Rahim.

 

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

SELECT ?personName
WHERE {
   ?person :knows :Rahim ;
           :name ?personName.
}

   

Output

 

personName
David
Krishna

   

Only Krishna and David have direct :knows relationships to Rahim. However, this does not tell the whole story.

 

Ram can reach Rahim through Hari and Krishna. Similarly, John can reach Rahim through Priya, Hari, and Krishna. To discover those indirect relationships, we need recursive paths.

 

2. Property Paths

A property path allows SPARQL to repeatedly follow a predicate. The two most commonly used recursive path operators are summarized in following table.

 

Operator

Meaning

*

Zero or more occurrences

+

One or more occurrences

 

2.1 Zero or More Occurrences (*)

The following query asks "Who can reach Rahim through zero or more :knows relationships?"

 

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

SELECT ?personName
WHERE {
   ?person :knows* :Rahim ;
           :name ?personName.
}
ORDER BY ?personName

   

Output

 

personName
Alex
Anita
David
Hari
John
Krishna
Meera
Neha
Priya
Rahim
Ram
Suresh
Vikram

   

At first glance, one result may seem surprising.

 

Why is Rahim included?

The reason is that the * operator means zero or more occurrences. A path of length zero is valid.

 

Therefore, SPARQL considers: Rahim → Rahim without traversing any edge. This behavior is mathematically correct, but it is not always what users expect.

 

2.2 One or More Occurrences (+)

Most of the time, we want people who actually know Rahim either directly or indirectly. In that case, we should use the + operator.

 

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

SELECT ?personName
WHERE {
   ?person :knows+ :Rahim ;
           :name ?personName.
}
ORDER BY ?personName

   

Output

 

personName
Alex
Anita
David
Hari
John
Krishna
Meera
Neha
Priya
Ram
Suresh
Vikram

   

Notice that Rahim no longer appears in the result set. The + operator requires at least one traversal of the :knows relationship. This is usually the preferred option when searching for friends, connections, dependencies, or lineage relationships.

 

2.3 Select all the persons that Hari knows

 

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

SELECT DISTINCT ?personName
WHERE {
   ?Hari :knows+ ?person .
  
   ?person :name ?personName.
}
ORDER BY ?personName

   

Output

 

personName
Alex
Anita
Hari
John
Krishna
Priya
Rahim
Suresh
Vikram

   

2.4 Finding Everyone Connected to Hari

 

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

SELECT DISTINCT ?personName
WHERE {
   ?person :knows+ ?Hari .
  
   ?person :name ?personName.
}
ORDER BY ?personName

   

Output

 

personName
Alex
Anita
David
Hari
John
Krishna
Meera
Neha
Priya
Ram
Suresh
Vikram

   

In summary, Property paths are one of the most powerful features of SPARQL. They allow us to navigate graph relationships without knowing the path length in advance.

 

Remember these two operators:

 

·      *: Zero or more occurrences

·      +: One or more occurrences

 

Use * when zero-length paths should be included. Use + when at least one relationship traversal is required.

 

Whenever you need to answer questions involving friends-of-friends, organizational hierarchies, dependencies, network connectivity, or data lineage, recursive property paths provide a concise and elegant solution.


Previous                                                    Next                                                    Home