OrientDB:概率加权边和遍历

时间:2015-10-21 21:18:32

标签: orientdb

给出6个节点(A,B,C,D,E,F)的样本图

和有向边[A,B],[B,A],[A,D],[D,A],[B,C],[C,B],[B,E],[E ,B],[C,F],[F,C]。边缘被“加权”,概率值为float,介于0和1之间。

create class Node extends V;
create property Node.value string;

insert into Node (value) values ('A');
insert into Node (value) values ('B');
insert into Node (value) values ('C');
insert into Node (value) values ('D');
insert into Node (value) values ('E');
insert into Node (value) values ('F');

create class PE extends E;
create property PE.probability float;

create edge PE
 from  (select from Node where value = 'A')
 to    (select from Node where value = 'B')
 set   probability = 0.9;
create edge PE
 from  (select from Node where value = 'B')
 to    (select from Node where value = 'A')
 set   probability = 0.4;
create edge PE
 from  (select from Node where value = 'A')
 to    (select from Node where value = 'D')
 set   probability = 0.85;
create edge PE
 from  (select from Node where value = 'D')
 to    (select from Node where value = 'A')
 set   probability = 0.85;
create edge PE
 from  (select from Node where value = 'B')
 to    (select from Node where value = 'E')
 set   probability = 0.9;
create edge PE
 from  (select from Node where value = 'E')
 to    (select from Node where value = 'B')
 set   probability = 0.9;
create edge PE
 from  (select from Node where value = 'B')
 to    (select from Node where value = 'C')
 set   probability = 0.4;
create edge PE
 from  (select from Node where value = 'C')
 to    (select from Node where value = 'B')
 set   probability = 0.9;
create edge PE
 from  (select from Node where value = 'C')
 to    (select from Node where value = 'F')
 set   probability = 0.8;
create edge PE
 from  (select from Node where value = 'F')
 to    (select from Node where value = 'C')
 set   probability = 0.8;

遍历图表非常简单,返回所有六个节点。

-- traverse from D
select from (
  traverse out()
   from (
     select from Node where value = 'D'
  )
);

但我真正想要的只是遍历具有聚合路径概率> = 0.5(50%)的节点。我认为以下是接近的,但它什么都不返回

select from (
  traverse out()[p = $aggp]
  from (
    select from Node where value = 'D'
  )
  while p >= 0.5
)
let $aggp = eval($current.inE().probability * $parent.p);

我错过了一些完全明显的东西吗?我正在寻找仅返回A,B,D,E的图遍历,因为边B-> C已经分配概率0.4,因此路径D-> A-> B->的总概率。 C = 0.85 * 0.9 * 0.4 = 0.3 < 0.5。

1 个答案:

答案 0 :(得分:0)

尝试这个JS函数,它具有Node D的@rid作为参数

var g=orient.getGraph();
var nodes = [];
var previous=[];
var currently=[];
var paths=new Array;
var pathsProbability=[];
var b=g.command("sql","select from Node where @rid = " + rid);
var step=1;
var defaultProbability=1.0;
if(b.length>0){
    var vertex=b[0];
    previous.push(vertex);
    nodes.push(vertex);
    paths[0]=new Array(vertex);
    pathsProbability.push(defaultProbability);
    do{
        for(i=0;i<previous.length;i++){
            var vertexOut=previous[i];
            var edges=g.command("sql","select expand(outE()) from Node where @rid = "+ vertexOut.getId());
            for(j=0;j<edges.length;j++){ 
                var edge=edges[j];
                var vIn=edge.getProperty("in");
                if(!check(vIn)){ 
                    var probability=edge.getProperty("probability");
                    setPaths(vertexOut, vIn,probability);
                }
            }
        }
        removePaths();
        step++;
        change();
    }while(previous.length>0);
    return nodes;
}

function check(vIn) {
  for(y=0;y<nodes.length;y++){ 
    var idNode=nodes[y].getId().toString();
    var idIn=vIn.getId().toString();
    if(idNode==idIn)
      return true;
  }
} 

function setPaths(vOut, vIn,prob){
  for (m = 0; m < paths.length; m++) {
    var length=paths[m].length;
    var list = paths[m];
    var last = list[length - 1];
    var lastId=last.getId().toString();
    var idOut=vOut.getId().toString();
    if (lastId==idOut) {
        if (pathsProbability[m] * prob >= 0.5) {
            var listVertex=[];
            for (k=0;k<list.length;k++) {
                listVertex.push(list[k]);
            }
        listVertex.push(vIn);
        paths[paths.length]=listVertex;
        pathsProbability.push(pathsProbability[m]*prob);
        nodes.push(vIn);
        currently.push(vIn);
        return;
        }
    }
  }
}

function change(){
    previous=[];
    for (indice=0;indice<currently.length;indice++)
        previous.push(currently[indice]);
    currently=[];
}

function removePaths(){
    for(i=0;i<paths.length;i++){
        if(paths[i].length==step){
            paths.splice(i, 1);
            pathsProbability.splice(i, 1);  
            i--;
        }
    }
}