# 数据结构图有那些应用场景呢？

``````
class Graph {
constructor() {
this.isDirected = false;
this.vertices = []; // 顶点
this.adjList = new Dictionary(); // 邻边
}

if (!this.vertices.includes(v)) {
this.vertices.push(v);
}
}

if (!this.vertices.includes(v)) {
this.vertices.push(v);
}
if (!this.vertices.includes(w)) {
this.vertices.push(w);
}
if (!this.isDirected) {
}
}

getVertices() {
return this.vertices;
}
}

toString() {
let result = "";

for (let i = 0; i < this.vertices.length; i++) {
const vertex = this.vertices[i];
result += `\${vertex} => \${adjList} \n`;
}
return result;
}
}

function initMark(vertices) {
const map = {};
for (let i = 0; i < vertices.length; i++) {
map[vertices[i]] = false;
}
return map;
}

const vertices = graph.getVertices();

const eachMap = initMark(vertices);
const queue = new Queue();
queue.enqueue(startVertex);

while (!queue.isEmpty()) {
const u = queue.dequeue();
eachMap[u] = true;
for (let i = 0; i < neighbors.length; i++) {
const neighbor = neighbors[i];
if (!eachMap[neighbor]) {
eachMap[neighbor] = true;
queue.enqueue(neighbor);
}
}
if (typeof callback === "function") {
callback(u);
}
}
}
``````

1 个回答

###### 你尚未登录，登录后可以
• 和开发者交流问题的细节
• 关注并接收问题和回答的更新提醒
• 参与内容的编辑和改进，让解决方法与时俱进