expo fibonacci
DESCRIPTION
Fibonacci Heaps are efficient data structures that work great for the standard heap operations.TRANSCRIPT
![Page 1: Expo fibonacci](https://reader033.vdocuments.site/reader033/viewer/2022052304/557623dad8b42a4e1c8b4ea3/html5/thumbnails/1.jpg)
Estructuras de Datos Avanzadas
Diego SánchezArthur Oviedo
![Page 2: Expo fibonacci](https://reader033.vdocuments.site/reader033/viewer/2022052304/557623dad8b42a4e1c8b4ea3/html5/thumbnails/2.jpg)
AgendaResumen de Heaps Binarios
Fibonacci Heaps
![Page 3: Expo fibonacci](https://reader033.vdocuments.site/reader033/viewer/2022052304/557623dad8b42a4e1c8b4ea3/html5/thumbnails/3.jpg)
Resumen de Heaps BinariosEs un árbol binario casi completo.
Satisface la propiedad de orden del heap ( MAX-Heap: key[hijo] <= key[padre] )
Se representa con un arreglo. – PARENT(i)
• return ⌊i/2⌋ -LEFT(i)• return 2i
– RIGHT(i)• return 2i + 1
![Page 4: Expo fibonacci](https://reader033.vdocuments.site/reader033/viewer/2022052304/557623dad8b42a4e1c8b4ea3/html5/thumbnails/4.jpg)
Heaps BinariosSon utizados para:
Implementar HeapSort (Algoritmo de ordenamiento ) T(n) = O( n log n) S(n) = O(n)
Implementar colas de prioridadUtilizados en algoritmos importantes: Dijkstra
(Ruta mínima en un grafo), Prim( Árbol de expansión mínimo)
Simulación discreta de eventos(Colas de procesos, Colas de impresión)
![Page 5: Expo fibonacci](https://reader033.vdocuments.site/reader033/viewer/2022052304/557623dad8b42a4e1c8b4ea3/html5/thumbnails/5.jpg)
• Mantener la propiedad del heap O(lg n)• Crear un Heap a partir de un conjunto de
elementos dadosO(n)
• Dar mínimo/máximo O(1)• Extraer mínimo/máximo O(lg n)• Incrementar/Decrementar llave O(lg n)• Insertar nodo O(lg n)• Eliminar nodo O(lg n)• Unión de dos heaps O(n)
Resumen de Heaps Binarios
![Page 6: Expo fibonacci](https://reader033.vdocuments.site/reader033/viewer/2022052304/557623dad8b42a4e1c8b4ea3/html5/thumbnails/6.jpg)
Fibonacci Heaps
![Page 7: Expo fibonacci](https://reader033.vdocuments.site/reader033/viewer/2022052304/557623dad8b42a4e1c8b4ea3/html5/thumbnails/7.jpg)
DefiniciónConjunto de árboles ordenados(min-heaps)
![Page 8: Expo fibonacci](https://reader033.vdocuments.site/reader033/viewer/2022052304/557623dad8b42a4e1c8b4ea3/html5/thumbnails/8.jpg)
RepresentaciónUtilizamos listas circulares doblemente enlazadas para
mantener la lista de las raíces de cada árbol: left[x], right[x]
Hay un puntero min[H] hacia el menor elemento del heap
Cada nodo tiene una referencia a su padre p[x]. p[x] = NIL si x es una de las raíces
Cada nodo tiene una referencia a alguno de sus hijos child[x]. child[x] = NIL si el nodo es una hoja
Utilizamos listas circulares doblemente enlazadas para mantener a la lista de hijos de un nodo
n[H] = # de nodos en el Heapdeg[x] = # hijos del nodo x
![Page 9: Expo fibonacci](https://reader033.vdocuments.site/reader033/viewer/2022052304/557623dad8b42a4e1c8b4ea3/html5/thumbnails/9.jpg)
Función Potencial
t(H) = # de árboles en la lista de raíces del heapm(H) = # de nodos marcados del heap
![Page 10: Expo fibonacci](https://reader033.vdocuments.site/reader033/viewer/2022052304/557623dad8b42a4e1c8b4ea3/html5/thumbnails/10.jpg)
D(n)D(n) = Grado (Número de hijos) máximo de
cualquier nodo en un Fibonacci Heap de n nodos.
Costo Extraer mínimo = O(D(n))
D(n) ≤ log N, donde = (1 + 5) / 2
![Page 11: Expo fibonacci](https://reader033.vdocuments.site/reader033/viewer/2022052304/557623dad8b42a4e1c8b4ea3/html5/thumbnails/11.jpg)
Heap de FibonacciOperaciones
Crear heapInsertar nodoUnión de dos Fibonacci HeapsExtraer mínimoDecrementar llaveEliminar nodo
![Page 12: Expo fibonacci](https://reader033.vdocuments.site/reader033/viewer/2022052304/557623dad8b42a4e1c8b4ea3/html5/thumbnails/12.jpg)
Creación de un nuevo HeapMAKE-FIB-HEAP: Reserva espacio para el
heap. n[H] = 0min[H] = NILΦ(H) = 0O(1)
![Page 13: Expo fibonacci](https://reader033.vdocuments.site/reader033/viewer/2022052304/557623dad8b42a4e1c8b4ea3/html5/thumbnails/13.jpg)
Insertar NodoFIB-HEAP-INSERT(H, x)1 degree[x] ← 02 p[x] ← NIL3 child[x] ← NIL4 left[x] ← x5 right[x] ← x6 mark[x] ← FALSE7 concatenate the root list
containing x with root list H8 if min[H] = NIL or key[x] < key[min[H]]9 then min[H] ← x10 n[H] ← n[H] + 1
![Page 14: Expo fibonacci](https://reader033.vdocuments.site/reader033/viewer/2022052304/557623dad8b42a4e1c8b4ea3/html5/thumbnails/14.jpg)
Costo de InserciónComo, t(H′) = t(H)+1 and m(H′) = m(H) Incremento de la función potencial=
((t(H) + 1) + 2 m(H)) - (t(H) + 2 m(H)) = 1.Costo Real = O(1)Costo Amortizado = O(1) + 1 = O(1)
Estamos pagando 1 unidad de costo amortizado constante por cada inserción
![Page 15: Expo fibonacci](https://reader033.vdocuments.site/reader033/viewer/2022052304/557623dad8b42a4e1c8b4ea3/html5/thumbnails/15.jpg)
Dar mínimo elementoFIB-HEAP-MIN(H)1 return min[h]
•Cambio Función potencial = 0•Costo Amortizado = O(1) + 0 = O(1)
![Page 16: Expo fibonacci](https://reader033.vdocuments.site/reader033/viewer/2022052304/557623dad8b42a4e1c8b4ea3/html5/thumbnails/16.jpg)
Unión de dos Fibonacci HeapsFIB-HEAP-UNION(H1, H2)1 H ← MAKE-FIB-HEAP()2 min[H] ← min[H1]3 concatenate the root list of H2 with the root list of H4 if (min[H1] = NIL) or (min[H2] ≠ NIL and min[H2] < min[H1])5 then min[H] ← min[H2]6 n[H] ← n[H1] + n[H2]7 free the objects H1 and H28 return H
![Page 17: Expo fibonacci](https://reader033.vdocuments.site/reader033/viewer/2022052304/557623dad8b42a4e1c8b4ea3/html5/thumbnails/17.jpg)
Costo de UniónCambio Función Potencial = Φ(H) - (Φ(H1) + Φ(H2)) = (t(H) + 2m(H)) - ((t(H1) + 2 m(H1)) + (t(H2) + 2 m(H2))) = 0Costo Amortizado = O(1) + 0 = O(1)
![Page 18: Expo fibonacci](https://reader033.vdocuments.site/reader033/viewer/2022052304/557623dad8b42a4e1c8b4ea3/html5/thumbnails/18.jpg)
Extrayendo el mínimoFIB-HEAP-EXTRACT-MIN(H)1 z ← min[H]2 if z ≠ NIL3 then for each child x of z4 do add x to the root list of H5 p[x] ← NIL6 remove z from the root list of H7 if z = right[z]8 then min[H] ← NIL9 else
min[H] ← right[z]10 CONSOLIDATE(H)11 n[H] ← n[H] - 112 return z
![Page 19: Expo fibonacci](https://reader033.vdocuments.site/reader033/viewer/2022052304/557623dad8b42a4e1c8b4ea3/html5/thumbnails/19.jpg)
Extrayendo el mínimo (Consolidate)CONSOLIDATE(H)1 for i ← 0 to D(n[H])2 do A[i] ← NIL3 for each node w in the root list of H4 do x ← w5 d ← degree[x]6 while A[d] ≠ NIL7 do y ← A[d] ▹ Another node with the same degree as x.8 if key[x] > key[y]9 then exchange x ↔ y10 FIB-HEAP-LINK(H, y, x)11 A[d] ← NIL12 d ← d + 113 A[d] ← x14 min[H] ← NIL15 for i ← 0 to D(n[H])16 do if A[i] ≠ NIL17 then add A[i] to the root list of H18 if min[H] = NIL or key[A[i]] < key[min[H]]19 then min[H] ← A[i]
FIB-HEAP-LINK(H, y, x)1 remove y from the root list of H2 make y a child of x, incrementing degree[x]3 mark[y] ← FALSE
![Page 20: Expo fibonacci](https://reader033.vdocuments.site/reader033/viewer/2022052304/557623dad8b42a4e1c8b4ea3/html5/thumbnails/20.jpg)
Extrayendo el mínimo
![Page 21: Expo fibonacci](https://reader033.vdocuments.site/reader033/viewer/2022052304/557623dad8b42a4e1c8b4ea3/html5/thumbnails/21.jpg)
Costo de extraer mínimoCosto Real
O(D(n)) ->El nodo mínimo máximo tenía D(n) hijos
O(D(n) + t(H)) ->Las raíces que quedan son: Las que ya habían
más los nuevos hijosO(D(n)+t(H))
Cambio en el costo amortizado#Raíces antes = t(H)Máximo número de raíces después = D(n) +1 Cambio de potencial = D(n)+1-t(H)
Costo amortizadoCosto total + Cambio potencial =
O(D(n)+t(H)) + D(n) + 1 - t(H) = O(D(n))
![Page 22: Expo fibonacci](https://reader033.vdocuments.site/reader033/viewer/2022052304/557623dad8b42a4e1c8b4ea3/html5/thumbnails/22.jpg)
Decrementar LlaveFIB-HEAP-DECREASE-KEY(H, x, k)1 if k > key[x]2 then error "new key is greater than current key"3 key[x] ← k4 y ← p[x]5 if y ≠ NIL and key[x] < key[y]6 then CUT(H, x, y)7 CASCADING-CUT(H, y)8 if key[x] < key[min[H]]9 then min[H] ← x
CUT(H, x, y)1 remove x from the child list of y, decrementing degree[y]2 add x to the root list of H3 p[x] ← NIL4 mark[x] ← FALSE
CASCADING-CUT(H, y)1 z ← p[y]2 if z ≠ NIL3 then if mark[y] = FALSE4 then mark[y] ← TRUE5 else CUT(H, y, z)6 CASCADING-CUT(H, z)
![Page 23: Expo fibonacci](https://reader033.vdocuments.site/reader033/viewer/2022052304/557623dad8b42a4e1c8b4ea3/html5/thumbnails/23.jpg)
Decrementar llave:En pocas palabras:
Cambie el valor del nodoSi no se viola la regla del heap,
TerminamosSi se viola:
Separe al hijo del padre y póngalo como raíz Corte en cascada:
Si el nodo está desmarcado, márquelo Si el nodo está marcado,
Córtelo de su padre (póngalo como raíz) Desmarque al nodo Corte en cascada al padre
![Page 24: Expo fibonacci](https://reader033.vdocuments.site/reader033/viewer/2022052304/557623dad8b42a4e1c8b4ea3/html5/thumbnails/24.jpg)
Decrementar Llave
![Page 25: Expo fibonacci](https://reader033.vdocuments.site/reader033/viewer/2022052304/557623dad8b42a4e1c8b4ea3/html5/thumbnails/25.jpg)
Costo de decrementar llaveCosto Real =
O(1) + # de Llamadas recursivas Cascading Cut *O(1) = O(c)
Cambio en la función potencial= ((t(H) + c) ->c Nuevas raíces+ 2(m(H) – c +2)) ->Se desmarcaron
c nodos– ( t(H) + 2m(H) ) = 4-c ->Estado anteriorCosto amortizado=
O(c) + 4 – c = O(1)
![Page 26: Expo fibonacci](https://reader033.vdocuments.site/reader033/viewer/2022052304/557623dad8b42a4e1c8b4ea3/html5/thumbnails/26.jpg)
Eliminar NodoFIB-HEAP-DELETE(H, x)1 FIB-HEAP-DECREASE-KEY(H, x, -∞)2 FIB-HEAP-EXTRACT-MIN(H)
Costo Amortizado = O(decrease-key +extract-min) = O(D(n))
![Page 27: Expo fibonacci](https://reader033.vdocuments.site/reader033/viewer/2022052304/557623dad8b42a4e1c8b4ea3/html5/thumbnails/27.jpg)
Acotando D(n)Recordemos: D(n) = Grado máximo de un
nodo de un heap de n nodos
A probar: D(N) log N, donde = (1 + 5) / 2.
![Page 28: Expo fibonacci](https://reader033.vdocuments.site/reader033/viewer/2022052304/557623dad8b42a4e1c8b4ea3/html5/thumbnails/28.jpg)
Acotando D(n)Lema 1: Sea x un nodo con grado k, y sean
y1, . . . , yk sus k respectivos hijos en el orden en el que fueron añadidos a x: Entonces:
Prueba:-Cuando yi fue unido a x, y1, . . . , yi-1 ya se habían unido a x-degree(x)=i-1-degree(yi) = i - 1 Porque unimos nodos con igual grado (Extract min)-yi ha perdido máximo 1 hijo (Si hubiera perdido 2 ya no sería hijo de x, sino que sería una raíz)-Luego el grado de yi = i-1 ó i-2
![Page 29: Expo fibonacci](https://reader033.vdocuments.site/reader033/viewer/2022052304/557623dad8b42a4e1c8b4ea3/html5/thumbnails/29.jpg)
Acotando D(n)Lema 2:
Lema 3:
![Page 30: Expo fibonacci](https://reader033.vdocuments.site/reader033/viewer/2022052304/557623dad8b42a4e1c8b4ea3/html5/thumbnails/30.jpg)
Acotando D(n)Lema 4:
Sea x cualquier nodo del heap. Sea k = deg[x]. Entonces size(x) >= Fk+2 >=φk
Sk = Tamaño mínimo de un nodo con grado k (1 + suma del tamaño de los hijos)S0=1 S1=2 S2=3 //Casos base
![Page 31: Expo fibonacci](https://reader033.vdocuments.site/reader033/viewer/2022052304/557623dad8b42a4e1c8b4ea3/html5/thumbnails/31.jpg)
Acotando D(n)El máximo grado D(n) de cualquier nodo de
un Fibonacci Heap de n nodos = O(lg n)
Sea x un nodo con grado k.Entoncesn size(x) sk Fk k
n k
log n k