Javascript 实现一个图形类

Javascript 实现一个图形类,javascript,graph,breadth-first-search,Javascript,Graph,Breadth First Search,我对BFS方法有问题。 当我调用它时,如果我尝试在其他队列上使用dequeue方法,它会显示错误“无法读取未定义的'length'”,并且它会正确返回节点。 我还尝试在for循环中使用一个数字而不是u.arr.length(问题在哪里),它显示了与u.arr[I].finalnode相同的错误,该错误表示“无法读取未定义的最终节点”。 我认为问题在于变量u的赋值,但我不知道为什么 class Node { //declaration of graph's nodes constructo

我对BFS方法有问题。
当我调用它时,如果我尝试在其他队列上使用dequeue方法,它会显示错误“无法读取未定义的'length'”,并且它会正确返回节点。
我还尝试在for循环中使用一个数字而不是u.arr.length(问题在哪里),它显示了与u.arr[I].finalnode相同的错误,该错误表示“无法读取未定义的最终节点”。
我认为问题在于变量u的赋值,但我不知道为什么

class Node {
  //declaration of graph's nodes
  constructor(nome) {
    this.nome = nome;
    this.ar = [];
  }
}

class Item {
  //declaration of list's elements
  constructor(data, next) {
    this.data = data;
    this.next = null;
  }
}

class Queue {
  //declaration of list class
  constructor(head) {
    this.head = null;
    this.tail = null;
    this.size = 0;
  }
  enqueue(s) {
    var i = new Item(s);
    if (this.size == 0) {
      this.head = i;
      this.tail = i;
      this.size++;
    } else {
      if (this.size == 1) {
        this.tail = i;
        this.head.next = this.tail;
        this.size++;
      }
      if (this.size > 1) {
        this.tail.next = i;
        this.tail = this.tail.next;
        this.size++;
      }
    }
  }

  dequeue() {
    var s = this.head;
    this.head = this.head.next;
    this.size--;
    return s.data;
  }
}

class Graph {
  //declaration of graph class
  constructor() {
    this.arr = [];
  }

  insertnode(n) {
    this.arr.push(n);
  }

  insertedge(n1, n2, p) {
    n1.ar.push({ finalnode: n2, peso: p });
  }

  BFS(s) {
    var i;
    var u;
    var lista = new Queue();
    for (i = 0; i < this.arr.length; i++) {
      this.arr[i].color = "white";
      this.arr[i].d = Infinity;
      this.arr[i].parent = null;
    }
    s.color = "grey";
    s.d = 0;
    lista.enqueue(s);
    console.log(lista);
    while (lista.head != null) {
      u = lista.dequeue();

      for (i = 0; i < u.ar.length; i++) {
        //here is the problem
        u.ar[i].finalnode.color = "grey";
        u.ar[i].finalnode.parent = u;
        u.ar[i].finalnode.d = u.d + 1;
        lista.enqueue(u.ar[i]);
      }
      u.color = "black";
    }
  }

  DFS() {}
}
类节点{
//图的节点声明
建造师(nome){
this.nome=nome;
this.ar=[];
}
}
类项目{
//清单要素声明
构造函数(数据,下一个){
这个数据=数据;
this.next=null;
}
}
类队列{
//列表类的声明
建造师(主管){
this.head=null;
this.tail=null;
此值为0.size=0;
}
排队等候{
var i=新项目;
如果(this.size==0){
this.head=i;
this.tail=i;
这个.size++;
}否则{
如果(this.size==1){
this.tail=i;
this.head.next=this.tail;
这个.size++;
}
如果(this.size>1){
this.tail.next=i;
this.tail=this.tail.next;
这个.size++;
}
}
}
出列(){
var s=这个头;
this.head=this.head.next;
这个尺寸--;
返回美国数据;
}
}
类图{
//图类的声明
构造函数(){
this.arr=[];
}
插入节点(n){
这个。arr.push(n);
}
插入边缘(n1、n2、p){
n1.ar.push({最终节点:n2,比索:p});
}
BFS(s){
var i;
var u;
var lista=新队列();
对于(i=0;i
您正在做的
lista.enqueue(u.ar[i]),但
u.ar[i]
不是
节点
实例,而是在
insertEdge
中创建的边对象
{finalnode:n2,peso:p}

你会想用

for (i = 0; i < u.ar.length; i++) {
  var finalnode = u.ar[i].finalnode;
  finalnode.color = "grey";
  finalnode.parent = u;
  finalnode.d = u.d + 1;
  lista.enqueue(finalnode);
//              ^^^^^^^^^
}
for(i=0;i
你能给我们一些使用这些失败的类的示例代码,让我们看看程序是如何运行的吗?var G=new Graph()var a=new Node('a')var b=new Node('b')G.insertnode(a)G.insertnode(b)G.insertedge(a,b,5)G.BFS(a)这是我用来测试类的代码