[英]How can I create a directed graph of strings in JavaScript?
I am trying to create a directed graph data structure in JavaScript. 我正在尝试在JavaScript中创建有向图数据结构。 I want each node in the graph to be doubly-linked, so that I can obtain a list of links that each node points to, as well as the links that point to that node.
我希望图中的每个节点都进行双向链接,这样我就可以获得每个节点指向的链接列表以及指向该节点的链接。 Each node is an array with 3 elements, like so:
[["node that links to node a"], "node a", ["node that node a links to"]]
每个节点都是具有3个元素的数组,例如:
[["node that links to node a"], "node a", ["node that node a links to"]]
//The functions linkNodes and unlinkNodes should add and remove nodes links from the directed graph, respectively.
var nodes = new Object();
function linkNodes(a, b){
//a should be a string, and b should be a string as well
//create the node a, if it doesn't exist yet
//create the node b, if it doesn't exist yet
//link the node a to the node b
}
function unlinkNodes(){
//unlink the nodes a and b, if a and b exist
//create the node a, if it doesn't exist yet
}
function getNode(string){
//get the node that corresponds to the string
//if the string doesn't exist, then return false
}
jsfiddle: http://jsfiddle.net/NTKZ9/1/ jsfiddle: http : //jsfiddle.net/NTKZ9/1/
Is there a more concise way to implement a directed graph in JavaScript, besides the one that I've proposed here? 除了我在这里提出的方法以外,还有没有更简洁的方法可以在JavaScript中实现有向图?
一种简洁的方法是将邻接矩阵存储在二维数组中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.