如何在JavaScript中构建图形



我在JavaScript中构建图形,我遇到了这个问题。

输出不是我想要的。

我知道的图形是每个节点之间的边缘和此adjacentList的目标是显示边缘并与节点建立连接。

如果我的代码不完整,请给我一个提示以完成它或修复我的代码并满足请求。

输出应为:

Answer:
0-->1 2
1-->3 2 0
2-->4 1 0
3-->1 4
4-->3 2 5
5-->4 6
6-->5

这是我的JS:

 class Graph { 
    constructor() { 
    this.numberOfNodes = 0;
    this.adjacentList = {
    }; 
  } 
  addVertex(node)  { 
    this.adjacentList[node]=[];
    this.numberOfNodes++;
   } 
   addEdge(node1, node2) { 
    this.adjacentList[node1]=[];
    this.adjacentList[node1].push(node2);
   } 
  showConnections() { 
    const allNodes = Object.keys(this.adjacentList); 
    for (let node of allNodes) { 
       let nodeConnections = this.adjacentList[node]; 
       let connections = ""; 
       let vertex;
    for (vertex of nodeConnections) {
        connections += vertex + " ";
      } 
       console.log(node + "-->" + connections); 
      } 
     } 
    } 
    const myGraph = new Graph();
    myGraph.addVertex('0');
    myGraph.addVertex('1');
    myGraph.addVertex('2');
    myGraph.addVertex('3');
    myGraph.addVertex('4');
    myGraph.addVertex('5');
    myGraph.addVertex('6');
    myGraph.addEdge('3', '1'); 
    myGraph.addEdge('3', '4'); 
    myGraph.addEdge('4', '2'); 
    myGraph.addEdge('4', '5'); 
    myGraph.addEdge('1', '2'); 
    myGraph.addEdge('1', '0'); 
    myGraph.addEdge('0', '2'); 
    myGraph.addEdge('6', '5');
    myGraph.showConnections(); 

我想出了MapSet来解决问题。你可以看看。

function Graph() {
  let nodes = new Map()
  
  this.addVertex = node => {
    if(!nodes.has(node)) nodes.set(node, new Set())
  }
  
  this.addEdge = (node, egde) => {
    if(nodes.has(node) && nodes.has(egde)) {
      nodes.get(node).add(egde)
      nodes.get(egde).add(node)
    }
  }
  
  this.showConnections = () => {
    nodes.forEach((node, index) => console.log(`${index} -> ${[...node]}`) )
  }
}
const myGraph  = new Graph()
    myGraph.addVertex('0');
    myGraph.addVertex('1');
    myGraph.addVertex('2');
    myGraph.addVertex('3');
    myGraph.addVertex('4');
    myGraph.addVertex('5');
    myGraph.addVertex('6');
    myGraph.addEdge('3', '1'); 
    myGraph.addEdge('3', '4'); 
    myGraph.addEdge('4', '2'); 
    myGraph.addEdge('4', '5'); 
    myGraph.addEdge('1', '2'); 
    myGraph.addEdge('1', '0'); 
    myGraph.addEdge('0', '2'); 
    myGraph.addEdge('6', '5');
myGraph.showConnections();

addVertex(node)  { 
  this.adjacentList[node]=[]; <---| 
  this.numberOfNodes++;           |
 }                                |  <-- Do the same task
 addEdge(node1, node2) {          |  
  this.adjacentList[node1]=[];----|
  this.adjacentList[node1].push(node2);
} 

然后,您应该从addEdge函数中删除this.adjacentList[node1]=[]。而且因为您的图形从双方获取数据:

[{'1': '3'}, {'3': '2'}] =>  3-->1,2 

然后,您应该以两种方式推出节点

addEdge(node1, node2) {
  this.adjacentList[node1].push(node2);
  this.adjacentList[node2].push(node1);
}

代码应为:

class Graph { 
    constructor() { 
    this.numberOfNodes = 0;
    this.adjacentList = {
    }; 
  } 
  addVertex(node)  { 
    this.adjacentList[node]=[];
    this.numberOfNodes++;
   } 
   addEdge(node1, node2) { 
    // this.adjacentList[node1]=[]; <------------ Remove this line
     this.adjacentList[node1].push(node2);
     this.adjacentList[node2].push(node1); // <-- Add this line
   }
  
  showConnections() { 
    const allNodes = Object.keys(this.adjacentList); 
    for (let node of allNodes) { 
       let nodeConnections = this.adjacentList[node]; 
       let connections = ""; 
       let vertex;
    for (vertex of nodeConnections) {
        connections += vertex + " ";
      } 
       console.log(node + "-->" + connections); 
      } 
     } 
    } 
    const myGraph = new Graph();
    myGraph.addVertex('0');
    myGraph.addVertex('1');
    myGraph.addVertex('2');
    myGraph.addVertex('3');
    myGraph.addVertex('4');
    myGraph.addVertex('5');
    myGraph.addVertex('6');
    myGraph.addEdge('3', '1'); 
    myGraph.addEdge('3', '4'); 
    myGraph.addEdge('4', '2'); 
    myGraph.addEdge('4', '5'); 
    myGraph.addEdge('1', '2'); 
    myGraph.addEdge('1', '0'); 
    myGraph.addEdge('0', '2'); 
    myGraph.addEdge('6', '5');
    myGraph.showConnections(); 

此外,您应该处理错误并查看showConnection以选择它。

对象properties是使用半隆'定义的,而不是等于=。另外,使用COMMA ,而不是半彩;

分开单个属性

所以您的片段:

const newNode={
      this.numberOfNodes = 0;
      this.adjacentList = {
     };

实际上应该是:

const newNode={
      numberOfNodes : 0,
      adjacentList : {
     }; 

最新更新