Calculate length of path betwen nodes (with unknown edges)?

后端 未结 1 414
没有蜡笔的小新
没有蜡笔的小新 2021-01-21 18:12

The problem is how to calculate the distance between two nodes (concepts) in a Triple Store (RDF) using sparql queries without know the type of edges. Essencially, is to use Dij

相关标签:
1条回答
  • 2021-01-21 18:34

    You can use the same technique that's used in Calculate length of path between nodes?, but you'll need to use a wildcard instead of a particular property. The pattern (<>|!<>) is a wildcard, because every property is either <> or it isn't. You could also use (:|!:), but that will only work if you have a : prefix defined. (<>|!<>) will always work. Here's an example:

    @prefix : <urn:ex:>
    
    :a :p :b .
    :b :q :c .
    :c :r :d .
    :d :s :e .
    
    prefix : <urn:ex:>
    
    select ?start ?end (count(?mid) as ?length) {
      ?start (<>|!<>)* ?mid .
      ?mid (<>|!<>)+ ?end .
    }
    group by ?start ?end
    
    ------------------------
    | start | end | length |
    ========================
    | :a    | :b  | 1      |
    | :a    | :c  | 2      |
    | :a    | :d  | 3      |
    | :a    | :e  | 4      |
    | :b    | :c  | 1      |
    | :b    | :d  | 2      |
    | :b    | :e  | 3      |
    | :c    | :d  | 1      |
    | :c    | :e  | 2      |
    | :d    | :e  | 1      |
    ------------------------
    
    0 讨论(0)
提交回复
热议问题