使用SPARQL查询找到最短路径 [英] Finding shortest path with SPARQL query
问题描述
我想了解SPARQL查询的计算限制,并且我想知道如何编写一个查询来确定两个对象之间是否存在定向路径。
我知道一种方法来处理特定长度的路径:
SELECT?a?b?c ?d
WHERE {?a< http:// graphtheory / hasNeighbor> ?b。
?b< http:// graphtheory / hasNeighbor> ?C 。
?c< http:// graphtheory / hasNeighbor> ?d。
FILTER(?a!=?c&&" b!=?d
&&?a =< http:// graphtheory / node / 1>
& amp ;& ;? d =< http:// graphtheory / node / 2>)
}
LIMIT 10
有没有办法在单个查询中搜索任何长度的路径?这对SPARQL来说是不可能的吗?
AndyS给出了所有要素来回答这个问题,但是有一些错别字可能会使它很难应用它们。正如他所说:
lockquote
SPARQL 1.1具有属性路径,其中包含任意数量的*运算符。
它不会告诉你路径是什么,也不会告诉你最短路径的长度 - 只有是否有这样一条路径。
$ b $
PREFIX:< p> HTTP:// graphtheory />
PREFIX节点:< http:// graphtheory / node />
ASK {node:1:hasNeighbor * node:2}
据我所知,如果不使用属性路径,则无法做到这一点。
I am trying to understand the computational limitations of the SPARQL query, and I would like know how to write a query that will determine if there is a directed path between two objects.
I know a way to do it for a path of a specific length:
SELECT ?a ?b ?c ?d
WHERE { ?a <http://graphtheory/hasNeighbor> ?b .
?b <http://graphtheory/hasNeighbor> ?c .
?c <http://graphtheory/hasNeighbor> ?d .
FILTER (?a != ?c && ?b != ?d
&& ?a = <http://graphtheory/node/1>
&& ?d = <http://graphtheory/node/2>)
}
LIMIT 10
Is there a way to search for any length path in a single query? Is this impossible with SPARQL?
AndyS gave all the elements to answer this question, but there are some typos that might make it hard to apply them. As he says:
SPARQL 1.1 has property path which includes the * operator for any number of.
It does not tell you what the path is nor the length of the shortest path - only whether there is such a path.
The way to do this (based on AndyS, but with two little fixes) is:
PREFIX : <http://graphtheory/>
PREFIX node: <http://graphtheory/node/>
ASK { node:1 :hasNeighbor* node:2 }
As far as I can tell, there is no way to do this without using property paths.
这篇关于使用SPARQL查询找到最短路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!