Loading TOC...

MarkLogic 12 Product Documentation
ModifyPlan.prototype.shortestPath

ModifyPlan.prototype.shortestPath(
   start as ColumnIdentifier,
   end as ColumnIdentifier,
   [path as columnIdentifier?],
   [length as columnIdentifier?]
) as ModifyPlan

Summary

This method can be used to find the shortest path between two nodes in a given graph.

Parameters
start The column representing the input starting subject of the shortest path search. The column can be named with a string or a column function such as op.col, op.viewCol, or op.schemaCol, or constructed from an expression with the op.as function.
end The column representing the input ending object of the shortest path search. The column can be named with a string or a column function such as op.col, op.viewCol, or op.schemaCol, or constructed from an expression with the op.as function.
path The column is the output column representing the actual shortest path(s) taken from start to end. Values are not returned for this column if it is null. The columns can be named with a string or a column function such as op.col, op.viewCol, or op.schemaCol.
length The column is the output column representing the length of the shortest path. Value is not returned for this column if it is null. The columns can be named with a string or a column function such as op.col, op.viewCol, or op.schemaCol.

Usage Notes

shortestPath is a method of the following classes:

Example

// Insert data
declareUpdate();
const sem = require("/MarkLogic/semantics.xqy");
const turtleDocument = '\n\
@prefix ken: <http://example.org/kennedy/>.  \n\
@prefix foaf: <http://xmlns.com/foaf/0.1/>. \n\
ken:person1 foaf:knows ken:person2 . \n\
ken:person1 foaf:knows ken:person3 . \n\
ken:person1 foaf:knows ken:person5 . \n\
ken:person1 foaf:knows ken:person7 . \n\
ken:person2 foaf:knows ken:person11 . \n\
ken:person2 foaf:knows ken:person3 . \n\
ken:person3 foaf:knows ken:person13 . \n\
ken:person13 foaf:knows ken:person17 . \n\
ken:person13 foaf:knows ken:person19 . \n\
ken:person4 foaf:knows ken:person6 . \n\
ken:person6 foaf:knows ken:person8 . \n\
ken:person8 foaf:knows ken:person10 . \n\
ken:person10 foaf:knows ken:person12 . \n\
ken:person12 foaf:knows ken:person14 . \n\
ken:person14 foaf:knows ken:person16 . \n\
ken:person16 foaf:knows ken:person18 . \n\
ken:person18 foaf:knows ken:person20 . \n\
ken:person20 foaf:knows ken:person20 . \n\
ken:person9 foaf:knows ken:person12 . \n\
ken:person12 foaf:knows ken:person15 . \n\
ken:person15 foaf:knows ken:person18 . \n\
ken:person18 foaf:knows ken:person9 .'

sem.rdfInsert(sem.rdfParse(turtleDocument,["turtle","repair"]), "rdfxml");

//Calculate the shortest path between person1 and person19
const op = require('/MarkLogic/optic');
const ken = op.prefixer('http://example.org/kennedy');
const start = op.col('start');
const predicate = op.col('predicate');
const end = op.col('end');
const path = op.col('path');
const length = op.col('length');

op.fromTriples([op.pattern(start, predicate, end)])
  .shortestPath(start, end, path, length)
  .where(op.and(op.eq(start,ken('person1')),op.eq(end,ken('person19'))))
  .result();
  

Example

// Insert the data as in previous example
// Calculate the paths between two people which length is 5
const op = require('/MarkLogic/optic');
const ken = op.prefixer('http://example.org/kennedy');
const start = op.col('start');
const predicate = op.col('predicate');
const end = op.col('end');
const path = op.col('path');
const length = op.col('length');

op.fromSPARQL("select * where {?start ?predicate ?end.} ")
  .shortestPath('start','end','path','length')
  .where(op.eq(length,5))
  .result();
  

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.