给定一个数组,数组描述是两个点之间的连接关系,给定两个点,需要找出两个点之间的所有“路径”,如何写这个算法?
数据结构如下,描绘图形如下:
//原始数据结构
let arr = [
//第一条路径
{source: 'node-1', target: 'node-2'},
{source: 'node-2', target: 'node-5'},
{source: 'node-5', target: 'node-6'},
{source: 'node-6', target: 'node-8'},
//第二条路径
{source: 'node-5', target: 'node-7'},
{source: 'node-7', target: 'node-8'},
//第三条路径
{source: 'node-1', target: 'node-3'},
{source: 'node-3', target: 'node-4'},
{source: 'node-4', target: 'node-7'},
]
现在假设给定两个节点:node1、node8,需要求出他们之间的所有路径,用二维数组表示:
返回结果如下:
let result = [
//第一条路径
[
{source: 'node-1', target: 'node-2'},
{source: 'node-2', target: 'node-5'},
{source: 'node-5', target: 'node-6'},
{source: 'node-6', target: 'node-8'},
],
//第二条路径
[
{source: 'node-1', target: 'node-2'},
{source: 'node-2', target: 'node-5'},
{source: 'node-5', target: 'node-7'},
{source: 'node-7', target: 'node-8'},
],
//第三条路径
[
{source: 'node-1', target: 'node-3'},
{source: 'node-3', target: 'node-4'},
{source: 'node-4', target: 'node-7'},
{source: 'node-7', target: 'node-8'},
]
]