Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

263
Views
Algorithm to find all path between two nodes in an undirected weighed graph

I have an undirected graph like this:

let list = new Map([
    ["A", [["B", "rdWeight"], ["C", "rdWeight"]]],
    ["B", [["A", "rdWeight"], ["E", "rdWeight"]]],
    ["C", [["D", "rdWeight"], ["A", "rdWeight"]]],
    ["D", [["C", "rdWeight"], ["E", "rdWeight"]]],
    ["E", [["B", "rdWeight"], ["D", "rdWeight"]]]
]);

rdWeight is just a random string as weight. The function to find all path between two nodes(vertices) is a hit a hit or a miss and I don't understand how. Here's the function I'm using:

function findPath(list, start, end) {
    let paths = [];
    let visited = new Set();
    let queue = [];
    queue.push([start, [start]]);
    while (queue.length > 0) {
        let [current, path] = queue.shift();
        visited.add(current);
        if (current === end) {
            paths.push(path);
        }
        for (let [neighbor] of list.get(current)) {
            if (!visited.has(neighbor)) {
                queue.push([neighbor, [...path, neighbor]]);
            }
        }
    }
    return paths;
}

It works when I give findPath(list, "A", "D"), giving 2 paths [ [ 'A', 'C', 'D' ], [ 'A', 'B', 'E', 'D' ] ] but not in the case of findPath(list, "A", "E"), giving only one path. Please tell me where my code malfunctions.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

When your algorithm finds the target node, and marks it as visited, it will be impossible for it to push that target another time (for an alternative path) on the queue, and so it can only find paths of the same (shortest) length.

Instead of using one visited set, you need to only check that a path is not making a cycle, which means you should only check weather a node is in the path you are working with.

A simple correction is to change this:

        if (!visited.has(neighbor)) {

to

        if (!path.includes(neighbor)) {

...but this kind of task can better be done with a depth-first algorithm, which needs less memory. For instance, with a recursive generator:

function* findPath(list, start, end, visited=new Set) {
    if (start === end) return yield [...visited, end];
    visited.add(start);
    for (let [neighbor] of list.get(start)) {
        if (!visited.has(neighbor)) {
            yield* findPath(list, neighbor, end, visited);
        }
    }
    visited.delete(start);
}

let list = new Map([
    ["A", [["B", "rdWeight"], ["C", "rdWeight"]]],
    ["B", [["A", "rdWeight"], ["E", "rdWeight"]]],
    ["C", [["D", "rdWeight"], ["A", "rdWeight"]]],
    ["D", [["C", "rdWeight"], ["E", "rdWeight"]]],
    ["E", [["B", "rdWeight"], ["D", "rdWeight"]]]
]);

console.log([...findPath(list, "A", "E")]);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!