Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

208
Vistas
Cómo obtener console.log('¡Lo encontré!') para Breadth First Search Traversal usando Graph Node 'BKKK'

Confíe en que lo está haciendo muy bien, estoy usando el gráfico transversal a continuación usando Node.js para encontrar todos los edges or routes para el aeropuerto llamado BKKK

Pero de alguna manera el código no BreadthFirst la console.log('found it!') 'PHX'

¿Puede alguien decirme qué está mal en el siguiente código? ¿Por qué no se muestra console.log('found it!') al compilar el código?

 const airports = 'PHX BKK OKC JFK LAX MEX EZE HEL LOS LAP LIM'.split(' '); const routes = [ ['PHX','LAX'], ['PHX','JFK'], ['JFK','OKC'], ['JFK','HEL'], ['JFK','LOS'], ['MEX','LAX'], ['MEX','BKK'], ['MEX','LIM'], ['MEX','EZE'], ['LIM','BKK'], ]; //The Graph const adjacencyList = new Map(); //add-node function addnode(airport){ adjacencyList.set(airport, []); } //Add edge,undirected function addEdge(origin,destination){ adjacencyList.get(origin).push(destination); adjacencyList.get(destination).push(origin); } //Create the Graph airports.forEach(addnode); routes.forEach(route => addEdge(...route)); console.log(adjacencyList); //BFS Breadth First Search function bfs(start){ const visited = new Set(); const queue = [start] while (queue.length > 0) { const airport = queue.shift(); const destinations = adjacencyList.get(airport); for(const destination of destinations){ if(destination === 'BKKK'){ console.log('found it!'); if(!visited.has(destination)){ visited.add(destination); queue.push(destination); } } } } } bfs('PHX');

Tu ayuda es altamente apreciada

Saludos

Carolina

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Aquí está el código actualizado que funcionará para usted:

 const airports = 'PHX BKK OKC JFK LAX MEX EZE HEL LOS LAP LIM'.split(' '); const routes = [ ['PHX', 'LAX'], ['PHX', 'JFK'], ['JFK', 'OKC'], ['JFK', 'HEL'], ['JFK', 'LOS'], ['MEX', 'LAX'], ['MEX', 'BKK'], ['MEX', 'LIM'], ['MEX', 'EZE'], ['LIM', 'BKK'], ]; //The Graph const adjacencyList = new Map(); //add-node function addnode(airport) { adjacencyList.set(airport, []); } //Add edge,undirected function addEdge(origin, destination) { adjacencyList.get(origin).push(destination); adjacencyList.get(destination).push(origin); } //Create the Graph airports.forEach(addnode); routes.forEach(route => addEdge(...route)); console.log(adjacencyList); //BFS Breadth First Search function bfs(start) { const visited = new Set(); const queue = [start]; while (queue.length > 0) { const airport = queue.shift(); const destinations = adjacencyList.get(airport); for (const destination of destinations) { if (destination === 'BKK') { console.log('found it!'); break; } if (!visited.has(destination)) { visited.add(destination); queue.push(...adjacencyList.get(destination)); } } } } bfs('PHX');

problemas:

  • tienes un error de ortografía en la línea #49
  • Dado que su adición a la cola de destino está dentro de un bloque if que verifica si el destino es el que está buscando, nunca llegará debido al error de ortografía e incluso si se solucionó no resolverá el problema si los aeropuertos están no conectado directamente
  • No estás agregando todos los destinos posibles a la cola

Sugerencias:

Para hacer este código un poco más funcional, se puede simplificar a esto:

 const routes = [ ['PHX', 'LAX'], ['PHX', 'JFK'], ['JFK', 'OKC'], ['JFK', 'HEL'], ['JFK', 'LOS'], ['MEX', 'LAX'], ['MEX', 'BKK'], ['MEX', 'LIM'], ['MEX', 'EZE'], ['LIM', 'BKK'], ]; const makeGraph = routes => { return routes.reduce((list, [origin, destination]) => { console.log(origin, destination); if (!list.get(origin)) { list.set(origin, []); } if (!list.get(destination)) { list.set(destination, []); } list.get(origin).push(destination); list.get(destination).push(origin); return list; }, new Map()); }; //find if there is a route between two airports const findRoute = (graph, origin, destination) => { const queue = [origin]; const visited = new Set(); while (queue.length) { const airport = queue.shift(); if (airport === destination) { console.log('found it!'); break; } if (!visited.has(airport)) { visited.add(airport); queue.push(...graph.get(airport)); } } return false; }; const graph = makeGraph(routes); findRoute(graph, 'PHX', 'BKK');
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda