Skip to content

Commit

Permalink
fix: 🐛 dfsAllPaths works in more cases
Browse files Browse the repository at this point in the history
  • Loading branch information
SkepticMystic committed Dec 5, 2021
1 parent c876954 commit fd8a9b5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
10 changes: 5 additions & 5 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20920,20 +20920,20 @@ function dfsAllPaths(g, startNode) {
const queue = [
{ node: startNode, path: [] },
];
const visited = [];
const visited = {};
const allPaths = [];
let i = 0;
while (queue.length > 0 && i < 1000) {
i++;
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);
Expand Down
10 changes: 5 additions & 5 deletions src/graphUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit fd8a9b5

Please sign in to comment.