获取子图内的边

时间:2015-05-26 22:45:23

标签: cytoscape.js

我看不到一种简单的方法来获取子图的边缘,即连接节点集合的边。

我能做的最好的事情是计算连接的边缘减去外边缘:

function getInsideEdges(nodes) {
    var connectedEdges = nodes.connectedEdges();
    var complement = nodes.absoluteComplement();
    var outgoingEdges = nodes.edgesWith(complement);
    var insideEdges = connectedEdges.difference(outgoingEdges);
    return insideEdges;
}

考虑到API简化了很多其他操作,这似乎很尴尬。我错过了什么?

编辑:我尝试的第一个解决方案不起作用:nodes.edgesWith(nodes)返回与nodes.connectedEdges()相同的设置。

var cy = cytoscape({headless:true,elements:{
    nodes:[
        {data:{id:'n1'}},
        {data:{id:'n2'}},
        {data:{id:'n3'}}
    ],
    edges:[
        {data:{id:'e1-2',source:'n1',target:'n2'}},
        {data:{id:'e1-3',source:'n1',target:'n3'}},
        {data:{id:'e2-3',source:'n2',target:'n3'}}
    ]
}});
var nodes = cy.nodes();
var subgraphNodes = cy.$('#n1,#n2');
// we want to obtain the edges inside that subgraph: the collection ['e1-2']

subgraphNodes.connectedEdges().size();
// -> 3

subgraphNodes.edgesWith(subgraphNodes).size();
// -> 3

subgraphNodes.connectedEdges().difference(subgraphNodes.edgesWith(subgraphNodes.absoluteComplement())).size();
// -> 1

// another solution
subgraphNodes.outgoers('edge').intersection(subgraphNodes.incomers('edge')).size();
// -> 1

编辑:@maxkfranz修复了cytoscape.js,以便subgraphNodes.edgesWith( subgraphNodes )正确返回解决方案。

1 个答案:

答案 0 :(得分:0)

subgraphNodes.edgesWith( subgraphNodes ) - 因为你想要内部边缘(即连接到节点)子图