【问题标题】:Removing edge found in DFS algorithm cytoscape JS去除 DFS 算法 cytoscape JS 中发现的边缘
【发布时间】:2019-01-06 18:08:08
【问题描述】:

今天我开始使用 cytoscape 并尝试在准备好的图形上进行 DFS 搜索。我想删除算法会想到的每一条边。

var dfs = cy.elements().dfs('#0', function(v, e, u, i, depth){}, false);
var highlightNextEle = function(){
    if( i < dfs.path.length ){
        dfs.path[i+1].remove();
        i++;
        setTimeout(highlightNextEle, 1000);
    }
};
highlightNextEle();

我尝试了这段代码,他删除了第一条边,但随后他删除了与启动节点相关的所有内容。 如有任何提示,请提前感谢您

【问题讨论】:

    标签: javascript graph tree cytoscape.js


    【解决方案1】:

    如果你想删除在 dfs 中找到的边,你可以像这样使用 dfs 中的处理函数:

    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
      }
    });
    cy.unbind('click');
    cy.bind('click', 'node', function(evt) {
      var edges = cy.collection();
      var dfs = cy.elements().dfs({
        roots: `#${evt.target.id()}`,
        visit: function(v, e, u, i, depth) {
          console.log('visit ' + v.id());
          if (e) edges = edges.add(e);
        },
        directed: false
      });
    
      console.log(dfs);
      cy.remove(edges);
    });
    body {
      font: 14px helvetica neue, helvetica, arial, sans-serif;
    }
    
    #cy {
      height: 100%;
      width: 100%;
      left: 0;
      top: 0;
      float: left;
      position: absolute;
    }
    <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://unpkg.com/cytoscape@3.3.0/dist/cytoscape.min.js">
      </script>
      <script src="https://unpkg.com/jquery@3.3.1/dist/jquery.js"></script>
      <!-- cyposcape dagre -->
      <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>

    【讨论】:

    • 是否可以删除在 DFS 搜索后留下的图中最后一个站立边,但在 DFS 内部不是手动删除?
    • 是的,但您为什么需要该功能? dfs 是一种遍历算法,如果要删除所有边,只需调用 cy.remove(cy.edges())。如果您确实需要在 dfs 中执行此操作,我可以编辑答案,但请先解释您为什么要这样做:)
    猜你喜欢
    • 1970-01-01
    • 2018-07-20
    • 1970-01-01
    • 2016-02-08
    • 2012-07-09
    • 1970-01-01
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多