priority queues
TRANSCRIPT
![Page 1: Priority Queues](https://reader033.vdocuments.site/reader033/viewer/2022042607/557210d2497959fc0b8dba3a/html5/thumbnails/1.jpg)
Priority Queues
Ing. Andrea Quan
![Page 2: Priority Queues](https://reader033.vdocuments.site/reader033/viewer/2022042607/557210d2497959fc0b8dba3a/html5/thumbnails/2.jpg)
Priority Queues • Un priority queue es una estructura de datos que sirve
para manejar un conjunto de elementos, cada uno de los cuales esta asociado por un valor llamado key.
• La key asociada se puede reconocer como la prioridad del elemento.
![Page 3: Priority Queues](https://reader033.vdocuments.site/reader033/viewer/2022042607/557210d2497959fc0b8dba3a/html5/thumbnails/3.jpg)
Priority Queues • Un priority queue puede ser max-priority queue o
min-priority-queue. • Un max-priority queue soporta las siguientes
operaciones – INSERT à que inserta un elemento – MAXIMUM à que devuelve el elemento que tiene
la key (prioridad) mas grande – EXTRACT-MAX à saca y devuelve el elemento
con mayor key (prioridad) – INCREASE-KEY à incrementa el valor del key
(prioridad) de un elemento especifico
![Page 4: Priority Queues](https://reader033.vdocuments.site/reader033/viewer/2022042607/557210d2497959fc0b8dba3a/html5/thumbnails/4.jpg)
Priority Queues • Podemos utilizar un heap para implementar un priority
queue – Para implementar un max-priority queue se utiliza un
max-heap – Para implementar un min-priority queue se utiliza un
min-heap
![Page 5: Priority Queues](https://reader033.vdocuments.site/reader033/viewer/2022042607/557210d2497959fc0b8dba3a/html5/thumbnails/5.jpg)
Operaciones
• MAXIMUM HEAP-MAXIMUM(A) { return A[1] }
![Page 6: Priority Queues](https://reader033.vdocuments.site/reader033/viewer/2022042607/557210d2497959fc0b8dba3a/html5/thumbnails/6.jpg)
2
1
4 6 5
3
7
8 9 10 1
16
14 10
8 7 9
2 4
3
HEAP à 16 14 10 8 7 9 3 2 4 1
HEAP-MAXIMUM
![Page 7: Priority Queues](https://reader033.vdocuments.site/reader033/viewer/2022042607/557210d2497959fc0b8dba3a/html5/thumbnails/7.jpg)
Operaciones
• EXTRACT-MAX HEAP-EXTRACT-MAX(A) { if heap-size[A] < 1 { “Error, heap underflow” return null } else { max = A[1] A[1] = A[heap-size[A]] heap-size[A] = heap-size[A] – 1 MAX-HEAPIFY(A,1) return max } }
![Page 8: Priority Queues](https://reader033.vdocuments.site/reader033/viewer/2022042607/557210d2497959fc0b8dba3a/html5/thumbnails/8.jpg)
2
1
4 6 5
3
7
8 9 10 1
16
14 10
8 7 9
2 4
3
HEAP à 16 14 10 8 7 9 3 2 4 1
max = 16
heap-size = 10
![Page 9: Priority Queues](https://reader033.vdocuments.site/reader033/viewer/2022042607/557210d2497959fc0b8dba3a/html5/thumbnails/9.jpg)
2
1
4 6 5
3
7
8 9 10 16
1
14 10
8 7 9
2 4
3
HEAP à 1 14 10 8 7 9 3 2 4 16
max = 16
heap-size = 10
![Page 10: Priority Queues](https://reader033.vdocuments.site/reader033/viewer/2022042607/557210d2497959fc0b8dba3a/html5/thumbnails/10.jpg)
2
1
4 6 5
3
7
8 9 10 16
1
14 10
8 7 9
2 4
3
HEAP à 1 14 10 8 7 9 3 2 4 16
max = 16
heap-size = 9
![Page 11: Priority Queues](https://reader033.vdocuments.site/reader033/viewer/2022042607/557210d2497959fc0b8dba3a/html5/thumbnails/11.jpg)
2
1
4 6 5
3
7
8 9 10 16
14
8 10
4 7 9
2 1
3
HEAP à 14 8 10 4 7 9 3 2 1 16
max = 16
heap-size = 9
![Page 12: Priority Queues](https://reader033.vdocuments.site/reader033/viewer/2022042607/557210d2497959fc0b8dba3a/html5/thumbnails/12.jpg)
Operaciones
• INCREASE-KEY HEAP-INCREASE-KEY(A,i,key){ if key < A[i] { “Error nueva key menor que actual key” } else { A[i] = key while i > 1 and A[PARENT(i)] < A[i] { A[i] A[PARENT(i)] i = PARENT(i) } } }
![Page 13: Priority Queues](https://reader033.vdocuments.site/reader033/viewer/2022042607/557210d2497959fc0b8dba3a/html5/thumbnails/13.jpg)
2
1
4 6 5
3
7
8 9 10 1
16
14 10
8 7 9
2 4
3
HEAP à 16 14 10 8 7 9 3 2 4 1
i = 9
A[9] = 15
![Page 14: Priority Queues](https://reader033.vdocuments.site/reader033/viewer/2022042607/557210d2497959fc0b8dba3a/html5/thumbnails/14.jpg)
2
1
4 6 5
3
7
8 9 10 1
16
14 10
8 7 9
2 15
3
HEAP à 16 14 10 8 7 9 3 2 15 1
i = 9
Parent(i) = 4
![Page 15: Priority Queues](https://reader033.vdocuments.site/reader033/viewer/2022042607/557210d2497959fc0b8dba3a/html5/thumbnails/15.jpg)
2
1
4 6 5
3
7
8 9 10 1
16
14 10
15 7 9
2 8
3
HEAP à 16 14 10 15 7 9 3 2 8 1
i = 9
Parent(i) = 4
![Page 16: Priority Queues](https://reader033.vdocuments.site/reader033/viewer/2022042607/557210d2497959fc0b8dba3a/html5/thumbnails/16.jpg)
2
1
4 6 5
3
7
8 9 10 1
16
14 10
15 7 9
2 8
3
HEAP à 16 14 10 15 7 9 3 2 8 1
i = 4
Parent(i) = 2
![Page 17: Priority Queues](https://reader033.vdocuments.site/reader033/viewer/2022042607/557210d2497959fc0b8dba3a/html5/thumbnails/17.jpg)
2
1
4 6 5
3
7
8 9 10 1
16
15 10
14 7 9
2 8
3
HEAP à 16 15 10 14 7 9 3 2 8 1
i = 4
Parent(i) = 2
![Page 18: Priority Queues](https://reader033.vdocuments.site/reader033/viewer/2022042607/557210d2497959fc0b8dba3a/html5/thumbnails/18.jpg)
2
1
4 6 5
3
7
8 9 10 1
16
15 10
14 7 9
2 8
3
HEAP à 16 15 10 14 7 9 3 2 8 1
i = 2
Parent(i) = 1
![Page 19: Priority Queues](https://reader033.vdocuments.site/reader033/viewer/2022042607/557210d2497959fc0b8dba3a/html5/thumbnails/19.jpg)
2
1
4 6 5
3
7
8 9 10 1
16
15 10
14 7 9
2 8
3
HEAP à 16 15 10 14 7 9 3 2 8 1
![Page 20: Priority Queues](https://reader033.vdocuments.site/reader033/viewer/2022042607/557210d2497959fc0b8dba3a/html5/thumbnails/20.jpg)
Operaciones
• INSERT MAX-HEAP-INSERT(A,key) { heap-size[A] = heap-size[A] + 1 A[heap-size[A]] = -∞ HEAP-INCREASE-KEY(A,heap-size[A],key) }
![Page 21: Priority Queues](https://reader033.vdocuments.site/reader033/viewer/2022042607/557210d2497959fc0b8dba3a/html5/thumbnails/21.jpg)
2
1
4 6 5
3
7
8 9 10 1
16
15 10
14 7 9
2 8
3
- ∞ 11
HEAP à 16 15 10 14 7 9 3 2 8 1 - ∞
![Page 22: Priority Queues](https://reader033.vdocuments.site/reader033/viewer/2022042607/557210d2497959fc0b8dba3a/html5/thumbnails/22.jpg)
2
1
4 6 5
3
7
8 9 10 1
16
15 10
14 7 9
2 8
3
20 11
HEAP à 16 15 10 14 7 9 3 2 8 1 20
![Page 23: Priority Queues](https://reader033.vdocuments.site/reader033/viewer/2022042607/557210d2497959fc0b8dba3a/html5/thumbnails/23.jpg)
2
1
4 6 5
3
7
8 9 10 1
16
15 10
14 20 9
2 8
3
7 11
HEAP à 16 15 10 14 20 9 3 2 8 1 7
![Page 24: Priority Queues](https://reader033.vdocuments.site/reader033/viewer/2022042607/557210d2497959fc0b8dba3a/html5/thumbnails/24.jpg)
2
1
4 6 5
3
7
8 9 10 1
16
20 10
14 15 9
2 8
3
7 11
HEAP à 16 20 10 14 15 9 3 2 8 1 7
![Page 25: Priority Queues](https://reader033.vdocuments.site/reader033/viewer/2022042607/557210d2497959fc0b8dba3a/html5/thumbnails/25.jpg)
2
1
4 6 5
3
7
8 9 10 1
20
16 10
14 15 9
2 8
3
7 11
HEAP à 20 16 10 14 15 9 3 2 8 1 7
![Page 26: Priority Queues](https://reader033.vdocuments.site/reader033/viewer/2022042607/557210d2497959fc0b8dba3a/html5/thumbnails/26.jpg)
2
1
4 6 5
3
7
8 9 10 1
20
16 10
14 15 9
2 8
3
7 11
HEAP à 20 16 10 14 15 9 3 2 8 1 7