From fd8a9b54d092ea689a5f2863ac7739af662cf828 Mon Sep 17 00:00:00 2001 From: Ross Keenan Date: Sun, 5 Dec 2021 12:01:57 +0200 Subject: [PATCH] fix: :bug: dfsAllPaths works in more cases --- main.js | 10 +++++----- src/graphUtils.ts | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/main.js b/main.js index 3b8122d9..08677f73 100644 --- a/main.js +++ b/main.js @@ -20920,7 +20920,7 @@ function dfsAllPaths(g, startNode) { const queue = [ { node: startNode, path: [] }, ]; - const visited = []; + const visited = {}; const allPaths = []; let i = 0; while (queue.length > 0 && i < 1000) { @@ -20928,12 +20928,12 @@ function dfsAllPaths(g, startNode) { const { node, path } = queue.shift(); const extPath = [node, ...path]; const succsNotVisited = g.hasNode(node) - ? g.filterOutNeighbors(node, (n, a) => !visited.includes(n)) + ? g.filterOutNeighbors(node, (n) => !visited[n] || visited[n] < 5) : []; - const newItems = succsNotVisited.map((n) => { - return { node: n, path: extPath }; + const newItems = succsNotVisited.map((node) => { + visited[node] = visited[node] ? visited[node] + 1 : 1; + return { node, path: extPath }; }); - visited.push(...succsNotVisited); queue.unshift(...newItems); if (!g.hasNode(node) || !g.outDegree(node)) allPaths.push(extPath); diff --git a/src/graphUtils.ts b/src/graphUtils.ts index 5a741451..e6f903fb 100644 --- a/src/graphUtils.ts +++ b/src/graphUtils.ts @@ -181,7 +181,7 @@ export function dfsAllPaths(g: MultiGraph, startNode: string): string[][] { const queue: { node: string; path: string[] }[] = [ { node: startNode, path: [] }, ]; - const visited = []; + const visited: { [note: string]: number } = {}; const allPaths: string[][] = []; let i = 0; @@ -191,13 +191,13 @@ export function dfsAllPaths(g: MultiGraph, startNode: string): string[][] { const extPath = [node, ...path]; const succsNotVisited = g.hasNode(node) - ? g.filterOutNeighbors(node, (n, a) => !visited.includes(n)) + ? g.filterOutNeighbors(node, (n) => !visited[n] || visited[n] < 5) : []; - const newItems = succsNotVisited.map((n) => { - return { node: n, path: extPath }; + const newItems = succsNotVisited.map((node) => { + visited[node] = visited[node] ? visited[node] + 1 : 1; + return { node, path: extPath }; }); - visited.push(...succsNotVisited); queue.unshift(...newItems); if (!g.hasNode(node) || !g.outDegree(node)) allPaths.push(extPath);