Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

264
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda