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

122
Views
¿Qué hace el bucle con los vértices del gráfico en el algoritmo Union Find?

Intento aprender el algoritmo Union Find . Hay una función que acepta el número de vértice del gráfico:

 // Find which component/set 'p' belongs to, takes amortized constant time. find(p) { // Find the root of the component/set let root = p; while (root != this.id[root]) root = this.id[root]; // Compress the path leading back to the root. // Doing this operation is called "path compression" // and is what gives us amortized time complexity. while (p != root) { let next = this.id[p]; this.id[p] = root; p = next; } return root; }

El código completo se proporciona por enlace

No puedo entender lo que está sucediendo aquí:

 let root = p; while (root != this.id[root]) root = this.id[root];

El vértice de ingresos p se asigna a root . Entonces algo sucede en bucle.

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

0

this.id es un mapeo entre índices de actual a principal. El ciclo intenta seguir el mapeo comenzando en p , un paso de mapeo a la vez y se detiene si encuentra un mapeo que se mapea a sí mismo.

Para [0,2,0] a partir de p=1 , vemos un 2, vamos al índice 2, vemos un 0, vamos al índice 0, vemos un 0 de nuevo, hemos terminado.

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!