javascript - 在BFS cytoscape Javascript中获取nodeID

标签 javascript graph cytoscape.js

我正在使用 cytoscape.js 在定制图表中进行 BFS 搜索。我需要添加在此搜索中访问过的每个节点 ID。我已经尝试过类似的事情

document.getElementById("console").innerHTML += v.id;

在bfs函数中

var bfs = cy.elements().bfs('#1', function(v, e, u, i, depth){}, false);

但是它不起作用,我是 JS 和编程新手,如果有任何建议我将不胜感激。

最佳答案

如果你想获取bfs的每个访问过的节点的id,你可以,因为它也解释对here ,像这样使用访问函数:

var cy = window.cy = cytoscape({
  container: document.getElementById('cy'),

  boxSelectionEnabled: false,
  autounselectify: true,

  style: [
    {
      selector: 'node',
      css: {
        'content': 'data(id)',
        'text-valign': 'center',
        'text-halign': 'center',
        'height': '60px',
        'width': '60px',
        'border-color': 'black',
        'border-opacity': '1',
        'border-width': '10px'
      }
    },
    {
      selector: '$node > node',
      css: {
        'padding-top': '10px',
        'padding-left': '10px',
        'padding-bottom': '10px',
        'padding-right': '10px',
        'text-valign': 'top',
        'text-halign': 'center',
        'background-color': '#bbb'
      }
    },
    {
      selector: 'edge',
      css: {
        'target-arrow-shape': 'triangle'
      }
    },
    {
      selector: ':selected',
      css: {
        'background-color': 'black',
        'line-color': 'black',
        'target-arrow-color': 'black',
        'source-arrow-color': 'black'
      }
    }
  ],

  elements: {
            nodes: [
              { data: { id: 'n0' } },
              { data: { id: 'n1' } },
              { data: { id: 'n2' } },
              { data: { id: 'n3' } },
              { data: { id: 'n4' } },
              { data: { id: 'n5' } },
              { data: { id: 'n6' } },
              { data: { id: 'n7' } },
              { data: { id: 'n8' } },
              { data: { id: 'n9' } },
              { data: { id: 'n10' } },
              { data: { id: 'n11' } },
              { data: { id: 'n12' } },
              { data: { id: 'n13' } },
              { data: { id: 'n14' } },
              { data: { id: 'n15' } },
              { data: { id: 'n16' } }
            ],
            edges: [
              { data: { source: 'n0', target: 'n1' } },
              { data: { source: 'n1', target: 'n2' } },
              { data: { source: 'n1', target: 'n3' } },
              { data: { source: 'n2', target: 'n7' } },
              { data: { source: 'n2', target: 'n11' } },
              { data: { source: 'n2', target: 'n16' } },
              { data: { source: 'n3', target: 'n4' } },
              { data: { source: 'n3', target: 'n16' } },
              { data: { source: 'n4', target: 'n5' } },
              { data: { source: 'n4', target: 'n6' } },
              { data: { source: 'n6', target: 'n8' } },
              { data: { source: 'n8', target: 'n9' } },
              { data: { source: 'n8', target: 'n10' } },
              { data: { source: 'n11', target: 'n12' } },
              { data: { source: 'n12', target: 'n13' } },
              { data: { source: 'n13', target: 'n14' } },
              { data: { source: 'n13', target: 'n15' } },
            ]
          },

  layout: {
    name: 'dagre',
    padding: 5
  }
});


// here is the important part of the code
var idList = [];            // list for id storage
var bfs = cy.elements().bfs({
  roots: '#n0',
  visit: function (v, e, u, i, depth) {
    console.log("The id of the node " + i +  " is " + v.id());   // i is the number of the i'th visited node, v is the node itself
    idList[i] = v.id();
  },
  directed: false
});
body { 
  font: 14px helvetica neue, helvetica, arial, sans-serif;
}

#cy {
  height: 100%;
  width: 75%;
  position: absolute;
  left: 0;
  top: 0;
  float: left;
}
<html>
<head>
<meta charset=utf-8 />
<meta name="viewport" content="user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, minimal-ui">
<script src="https://cdnjs.cloudflare.com/ajax/libs/cytoscape/3.2.17/cytoscape.min.js"></script>
<script src="https://unpkg.com/jquery@3.3.1/dist/jquery.js"></script>
 <script src="https://unpkg.com/dagre@0.7.4/dist/dagre.js"></script>
 <script src="https://cdn.rawgit.com/cytoscape/cytoscape.js-dagre/1.5.0/cytoscape-dagre.js"></script>
</head>
<body>
<div id="cy"></div>
</body>
</html>

关于javascript - 在BFS cytoscape Javascript中获取nodeID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54163626/

相关文章:

javascript - 替换函数与 if 语句组合

javascript - 背景图片有问题?

c# - 创建任务管理器之类的图表

javascript - 删除在 DFS 算法 cytoscape JS 中发现的边缘

javascript - 使用 Cytoscape JS 突出显示边缘不起作用

javascript - 根据滚动位置淡入淡出图像

javascript - 为什么bindCallback不是一个函数?

ios - 如何在 barChart/swift 中将 Double 值更改为 String?

Java 绘图生成器

javascript - cytoscape.js 复合节点忽略布局?