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

221
Visualizações
Determine whether certain nodes on a graph are directly connected

How can I determine whether selected nodes on a graph are directly connected?

I think, I need to calculate paths for each selected node to all of the other selected nodes. Then, when I have the path, I have to check whether path contains only selected nodes.

In the code, all I have is a notion of nodes and edges, like this:

const nodes = [
  { id: "A" },
  { id: "B" },
  { id: "C" },
  { id: "D" },
  { id: "E" },
  { id: "F" },
];

const edges = [
  { target: "A", source: "B" },
  { target: "B", source: "C" },
  { target: "C", source: "D" },
  { target: "D", source: "E" },
  { target: "E", source: "F" },
];

Examples:

Good selection:

enter image description here

Bad selection:

enter image description here

Is there some algorithm for checking that? Are you aware of some npm packages in case of JavaScript?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Possible solution

  1. Transform the current directed graph into an undirected graph (so for each pair (source, target), (target, source)should be added to edges too), as we are not interested in whether A reaches B or B reaches A, but instead we are trying to see whether they are connected
  2. Run a graph traversal algorithm (Depth First Search (DFS) or Breadth First Search (BFS) from any selected node on the undirected graph you've just created. If all selected nodes can be reached, return true, otherwise return false

Notes

  • It does not matter from which selected node you run the DFS/BFS from as long as the graph is undirected. Therefore, it is important to run DFS/BFS only once to keep O(1) time complexity. Running DFS/BFS from all selected nodes will result in O(N) time complexity, where N = # of nodes, and would also provide a correct result, but would introduce redundant graph traversals.
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