我正在用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();
答案 0 :(得分:0)
对象properties
使用分号'
而不是等于=
来定义。另外,各个属性使用逗号,
而不是分号;
您的摘要:
const newNode={
this.numberOfNodes = 0;
this.adjacentList = {
};
实际上应该是这样的:
const newNode={
numberOfNodes : 0,
adjacentList : {
};
答案 1 :(得分:0)
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();
答案 2 :(得分:0)
addVertex(node) {
this.adjacentList[node]=[]; <---|
this.numberOfNodes++; |
} | <-- Do the same task
addEdge(node1, node2) { |
this.adjacentList[node1]=[];----|
this.adjacentList[node1].push(node2);
}
然后,您应该从this.adjacentList[node1]=[]
函数中删除addEdge
。
并且因为您的图形从双方获取数据:
[{'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
进行优化。