Incorrect Cycle Path Construction in detectUndirectedCycle

Aperta
#2,057 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
55/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Tranquilla
Stack tecnologico
javascript
Ambito
backend

Direzione di ricerca

Start at the detectUndirectedCycle function and inspect how cycle is constructed and how allowTraversal controls DFS. Verify the returned cycle is an ordered vertex array whose first and last vertices match, and assess whether traversal can stop immediately after detection. Run the relevant cycle-detection tests and confirm existing behavior remains covered.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

In the function detectUndirectedCycle, the cycle path is being constructed as an object with vertex keys as properties and their parent vertices as values. This makes the returned cycle not a standard path array, but rather a mapping, which may not be intuitive or useful for consumers expecting an ordered path of vertices forming the cycle. A more conventional approach would be to return an ordered array of vertices representing the cycle, starting and ending at the same vertex.
Additionally, if a cycle is detected, the function continues to traverse the graph until DFS completes, although traversal is blocked by allowTraversal once a cycle is set. However, early exit after detecting a cycle could improve efficiency.

Suggested Fix:

  • Change cycle to be an array that accumulates the ordered path of the cycle.
  • Optionally, add a mechanism to exit the traversal immediately after the cycle is found.
Lingua principale
JavaScript
Stelle
197k
Fork
31k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di trekhleb/javascript-algorithms

Tutte le issue di trekhleb/javascript-algorithms

Issue simili

Altre issue su JavaScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.