簡體   English   中英

如何檢查節點是否相關? 如果有兩個鄰居,我該如何選擇呢?

[英]How to check if nodes are related? How do I chose only one neighbour if it has two?

我在D3中有一個部隊布局。

我有很多帶有鏈接的節點。 我的問題是,如果節點滿足特定條件,我想刪除鏈接。

假設我有節點A,B,C。

說這個tilda字符-“〜”表示已連接。

If (A~B && A~C && B~C){

DELETE THE A~C link. //which would leave A~B~C
}

我試圖通過每個鏈接:

link.forEach(function{d){ ....

但我似乎無法理解如何執行邏輯。

我將遍歷每個節點3次,檢查A〜B,A〜C,B〜C,但是如果我有100個節點,那將會非常慢。

任何幫助,將不勝感激 :)

這是我當前的edges / links數組的外觀:

edges = [
{
    "source": "A",
    "target": "B",
    "other information" : "randomstring",
    "other information" : "randomstring"
},
{
    "source": "B",
    "target": "C",
    "other information" : "randomstring",
    "other information" : "randomstring"
} // and so on ....
]

這是一個圖論問題,我假設您想打破一個循環,這就是我要做的

給定大小為n且階為m的圖g

1)從links構建一個哈希表,該哈希表將兩個帶有鏈接的節點映射O(m)如果哈希是在恆定時間內完成的,則為O(m) ),例如

// a reference to the link itself (which can be an object or a dom node)
var hash = {}
links.each(function (d) {
  var u = d.source
  var v = d.target
  hash[u] = hash[u] || {}
  // again replace this with the dom node if you want
  hash[u][v] = d
})

2)運行dfs查找后邊緣(在我寫的文章中或通過谷歌快速搜索找到更多關於后邊緣的信息),每當找到后邊緣時,您將獲得有關源/目標節點和周期長度O(n + m)

3)如果周期的長度是3或您的標准是什么,則刪除鏈接,從鏈接中擦除將花費O(km) ,其中k是找到的周期數

現在使用d3,您可以簡單地重新綁定新數據(刪除一些鏈接)並重新呈現圖形

假設你的邏輯可以簡化為檢查當前entry.source等於以下entry.target,你可以使用array.reduce

    edges = [
    {
        "source": "A",
        "target": "B",
        "other information" : "randomstring",
        "other information" : "randomstring"
    },{
        "source": "A",
        "target": "C",
        "other information" : "randomstring",
        "other information" : "randomstring"
    },{
        "source": "B",
        "target": "C",
        "other information" : "randomstring",
        "other information" : "randomstring"
    },{
        "source": "D",
        "target": "C",
        "other information" : "randomstring",
        "other information" : "randomstring"
    },{
        "source": "C",
        "target": "E",
        "other information" : "randomstring",
        "other information" : "randomstring"
    }
]




var res = edges.reduce(function (acc, cur, i, array) {

    if (array[i - 1] == undefined || acc[acc.length - 1].target == cur.source) {
        acc.push(cur)
    }

    return acc

}, [])
console.log(res)

小提琴

這可能不是最有效的解決方案,但是檢查100個對象應該沒問題; 此外,它是完全合成的,並且還很好地使用了reduce

似乎我們遇到了類似的問題,即我必須創建一個函數才能到達特定節點。 您可以在創建和使用的地方看到相同基本功能的不同版本: http : //bl.ocks.org/CrandellWS/79be3b8c9e8b74549af5

最初的嘗試是使用forEach循環,但我發現使用常規的for循環更簡單。 盡管我希望這可以解決您的問題,但是您應該閱讀以下答案 :為什么在數組迭代中使用“ for…in”是一個壞主意?

  function getObjByValue(myRoot, myType, myType2, myVal, myVal2){
    var d;
    console.log(typeof myRoot)
    if(typeof myRoot == "object"){
      for(d in myRoot){

        //checking for requirements.
        if(myRoot[d][myType] == myVal && myRoot[d][myType2] == myVal2 ){
          console.log(d);

          //accessing the specific one desired...
          d3.select('#specificOne').text(myRoot[d]["other information"]);

          //deleteing it from the copied array
          delete myRoot[d];

          //do something with the rest
          printRemaining(myRoot);

            //done
            return;
        }
      }
    }
  } 

getObjByValue(edges, 'source', 'target', 'A', 'C');

 edges = [ { "source": "A", "target": "B", "other information" : "randomstringAB", "other information2" : "randomstringAB" },{ "source": "A", "target": "C", "other information" : "randomstringAC", "other information2" : "randomstringAC" },{ "source": "B", "target": "C", "other information" : "randomstringBC", "other information2" : "randomstringBC" },{ "source": "D", "target": "C", "other information" : "randomstringDC", "other information2" : "randomstringDC" },{ "source": "C", "target": "E", "other information2" : "randomstringCE", "other information" : "randomstringCE" } ] function getObjByValue(myRoot, myType, myType2, myVal, myVal2){ var d; console.log(typeof myRoot) if(typeof myRoot == "object"){ for(d in myRoot){ if(myRoot[d][myType] == myVal && myRoot[d][myType2] == myVal2 ){ console.log(d); //accessing the specific one desired... d3.select('#specificOne').text(myRoot[d]["other information"]); //deleteing it from the copied array delete myRoot[d]; printRemaining(myRoot); console.log(d); console.log('done'); //done return; } } } } function printRemaining(myArray){ for(d in myArray){ d3.select('#edges').append('p').text(myArray[d]["other information"]+'\\r\\n'); } } getObjByValue(edges, 'source', 'target', 'A', 'C'); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> <p>First the specific one</p> <div id="specificOne"></div> <br /><br /><br /><br /><br /> <p>It's time to eat, Here is the leftovers.</p> <div id="edges"></div> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM