Would you like to clone this notebook?

When you clone a notebook you are able to make changes without affecting the original notebook.

Cancel

ss-graph

node v10.24.1
version: 1.0.4
endpointsharetweet
// 获取图中两点之间所有路径 const {Graph, GraphEdge, GraphVertex} = require('ss-graph'); const graph = new Graph(); const vertex0 = new GraphVertex('0'); const vertex1 = new GraphVertex('1'); const vertex2 = new GraphVertex('2'); const vertex3 = new GraphVertex('3'); const vertex4 = new GraphVertex('4'); const vertex5 = new GraphVertex('5'); const vertex6 = new GraphVertex('6'); const vertex7 = new GraphVertex('7'); const vertex8 = new GraphVertex('8'); graph .addEdge(new GraphEdge(vertex0, vertex1)) .addEdge(new GraphEdge(vertex0, vertex2)) .addEdge(new GraphEdge(vertex1, vertex3)) .addEdge(new GraphEdge(vertex1, vertex4)) .addEdge(new GraphEdge(vertex3, vertex7)) .addEdge(new GraphEdge(vertex4, vertex7)) .addEdge(new GraphEdge(vertex4, vertex5)) .addEdge(new GraphEdge(vertex2, vertex5)) .addEdge(new GraphEdge(vertex2, vertex6)) .addEdge(new GraphEdge(vertex5, vertex6)) .addEdge(new GraphEdge(vertex6, vertex8)); const pathIterator = graph.findAllPath(vertex3, vertex6); // 获取所有路径 const allPaths = Array.from(pathIterator); let result = `总共有 ${allPaths.length} 条路径:<br/>`; allPaths.forEach((path, index)=>{ result +=`第 ${index+1} 条路径: ${path.map(v=>v.value).join('->')} <br/>`; }); console.log(result);
Loading…

1 comment

  • posted a year ago by wtl6150
    // 获取图中两点之间所有路径 const {Graph, GraphEdge, GraphVertex} = require('ss-graph'); const graph = new Graph(); const vertex0 = new GraphVertex('0'); const vertex1 = new GraphVertex('1'); const vertex2 = new GraphVertex('2'); const vertex3 = new GraphVertex('3'); const vertex4 = new GraphVertex('4'); const vertex5 = new GraphVertex('5'); const vertex6 = new GraphVertex('6'); const vertex7 = new GraphVertex('7'); const vertex8 = new GraphVertex('8'); graph .addEdge(new GraphEdge(vertex0, vertex1)) .addEdge(new GraphEdge(vertex0, vertex2)) .addEdge(new GraphEdge(vertex1, vertex3)) .addEdge(new GraphEdge(vertex1, vertex4)) .addEdge(new GraphEdge(vertex3, vertex7)) .addEdge(new GraphEdge(vertex4, vertex7)) .addEdge(new GraphEdge(vertex4, vertex5)) .addEdge(new GraphEdge(vertex2, vertex5)) .addEdge(new GraphEdge(vertex2, vertex6)) .addEdge(new GraphEdge(vertex5, vertex6)) .addEdge(new GraphEdge(vertex6, vertex8)); const pathIterator = graph.findAllPath(vertex3, vertex6); // 获取所有路径 const allPaths = Array.from(pathIterator); let result = `总共有 ${allPaths.length} 条路径:<br/>`; allPaths.forEach((path, index)=>{ result +=`第 ${index+1} 条路径: ${path.map(v=>v.value).join('->')} <br/>`; }); console.log(result);

sign in to comment