Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/475.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript图形删除顶点_Javascript_Graph - Fatal编程技术网

JavaScript图形删除顶点

JavaScript图形删除顶点,javascript,graph,Javascript,Graph,我正在用JavaScript实现我自己的图形类。我有“添加顶点”、“添加边”和“删除边”方法,效果很好。我正在尝试创建移除顶点方法,但它并没有移除顶点的所有边 我的班级: class Graph { constructor() { this.graph = new Map(); } addVertex(name) { if(!this.graph.has(name)) this.graph.set(name, []

我正在用JavaScript实现我自己的图形类。我有“添加顶点”、“添加边”和“删除边”方法,效果很好。我正在尝试创建移除顶点方法,但它并没有移除顶点的所有边

我的班级:

class Graph {
    constructor() {
        this.graph = new Map();
    }

    addVertex(name) {
        if(!this.graph.has(name))
            this.graph.set(name, []);
        return;
    }

    addEdge(node1, node2) {
        this.addVertex(node1);
        this.addVertex(node2);
        const data1 = this.graph.get(node1);
        const data2 = this.graph.get(node2);
        data1.push(node2);
        data2.push(node1);
        this.graph.set(node1, data1);
        this.graph.set(node2, data2);
        return;
    }

    removeEdge(node1, node2) {
        if(this.graph.has(node1)) {
            const data1 = this.graph.get(node1);
            for(let i=0; i<data1.length; i++) {
                if(data1[i] === node2) {
                    data1.splice(i, 1);
                    break;
                }
                this.graph.set(node1, data1);
            } 
        }

        if(this.graph.has(node2)) {
            const data2 = this.graph.get(node2);
            for(let i=0; i<data2.length; i++) {
                if(data2[i] === node1) {
                    data2.splice(i, 1);
                    break;
                }
                this.graph.set(node2, data2);
            } 
        }
    }

    removeVertex(node1) {
        if(!this.graph.has(node1))  return;

        const data = this.graph.get(node1);
        for(let i=0; i<data.length; i++) {
            this.removeEdge(data[i], node1);
        }
        this.graph.delete(node1);
        return;
    }
}
输出为:

Graph {
  graph: Map {
    'A' => [ 'B', 'C', 'D' ],
    'B' => [ 'A', 'C' ],
    'C' => [ 'A', 'B' ]
  }
}
它删除了顶点“D”和边D-C,但没有删除边A-D。这很奇怪,但看起来它只运行下面for循环的第一个循环

for(let i=0; i<data.length; i++) {
   this.removeEdge(data[i], node1);
}

for(设i=0;问题是在同一数组(
data
)上的循环内使用
splice
)。那些
this.graph.set(节点,数据)
没有意义。例如,它们在一个循环中,并且它们不是操作,因为数据已经存储在映射中。请注意数组是可变对象,您的代码中没有任何内容会创建它们的副本。@Bergi您能给我一个如何修复它的示例吗?使用定向边并从
addEdge
removedge
(如果您想要无向边,请添加像
addEdges(a,b){this.addEdge(a,b);this.addEdge(b,a);}
)这样的包装方法。这将允许您在
removeVertex
中仅使用后边调用
removeVertex
,然后使用
this.graph.delete(节点)一次切换前EGDE)
。或者,您可以a)在对数组进行迭代之前克隆该数组,以便不受(效率低下的)拼接的影响,或者b)使用内部
集而不是数组,以便可以按值而不是按索引删除。
for(let i=0; i<data.length; i++) {
   this.removeEdge(data[i], node1);
}