13. heap, priority queue - kocwcontents.kocw.net/kocw/document/2015/yeungnam/kimyoungt... · 2016....
TRANSCRIPT
13. Heap, Priority Queue
2015-1 Programming Language
2015년 6월 1일
교수 김 영 탁
영남대학교 공과대학 정보통신공학과(Tel : +82-53-810-2497; Fax : +82-53-810-4742http://antl.yu.ac.kr/; E-mail : [email protected])
Advanced Networking Tech. Lab.Yeungnam University (yuANTL)
Programming LanguageProf. Young-Tak Kimch13 - 2
Outline
Complete Binary Tree Heap, Priority Queue Heap Sorting Heap Priority Queue 응용 1000개의 Task 중에 Priority가 가장 높은 task 10개를 선정하기
Advanced Networking Tech. Lab.Yeungnam University (yuANTL)
Programming LanguageProf. Young-Tak Kimch13 - 3
Heaps
A heap is a binary tree storing keys at its nodes and satisfying the following properties:
Heap-Order: for every internal node v other than the root,key(v) key(parent(v))
Complete Binary Tree: let h be the height of the heap for i 0, … , h 1, there are 2i
nodes of depth i at depth h 1, the internal nodes
are to the left of the external nodes
The last node of a heap is the rightmost node of maximum depth
last node
Advanced Networking Tech. Lab.Yeungnam University (yuANTL)
Programming LanguageProf. Young-Tak Kimch13 - 4
Complete Binary Tree
Complete Binary Tree Property a heap T with height h is a complete binary tree, that is, levels 0,
1, 2, …, h-1 of T have the maximum number of nodes possible (namely, level i has 2i nodes, for 0 ≤ i ≤ h-1 ) and nodes at level h fill this level from left to right
Advanced Networking Tech. Lab.Yeungnam University (yuANTL)
Programming LanguageProf. Young-Tak Kimch13 - 5
Array-based Representation of a Complete Binary Tree
Array-based representation of a complete binary tree if v is the root of T, then index(v) = 1 if v is the left child of node u, then index(v) = 2 index(u) if v is the right child of node u, then index(v) = 2 index(u) + 1
Advanced Networking Tech. Lab.Yeungnam University (yuANTL)
Programming LanguageProf. Young-Tak Kimch13 - 6
Array-based Heap Implementation
We can represent a heap with nkeys by means of a vector of length n 1
For the node at index i the left child is at rank 2i the right child is at rank 2i 1
Links between nodes are not explicitly stored
The cell of at index 0 is not used Operation insert() corresponds
to inserting at index n 1 Operation removeMin()
corresponds to removing at index 1
Yields in-place heap-sort
2
65
79
2 5 6 9 71 2 3 4 50
Advanced Networking Tech. Lab.Yeungnam University (yuANTL)
Programming LanguageProf. Young-Tak Kimch13 - 7
Height of a Heap
Theorem: A heap storing n keys has height O(log n)Proof: (we apply the complete binary tree property) Let h be the height of a heap storing n keys Since there are 2i keys at depth i 0, … , h 1 and at least one key at
depth h, we have n 1 2 4 … 2h1 1 Thus, n 2h , i.e., h log n
1
2
2h1
1
keys0
1
h1
h
depth
Advanced Networking Tech. Lab.Yeungnam University (yuANTL)
Programming LanguageProf. Young-Tak Kimch13 - 8
Heaps and Priority Queues
We can use a heap to implement a priority queueWe store a (key, element) item at each internal
nodeWe keep track of the position of the last node
(2, Sue)
(6, Mark)(5, Pat)
(9, Jeff) (7, Anna)
last node
Advanced Networking Tech. Lab.Yeungnam University (yuANTL)
Programming LanguageProf. Young-Tak Kimch13 - 9
Insertion into a Heap
Method insertItem() of the priority queue ADT corresponds to the insertion of a key k to the heap
The insertion algorithm consists of three steps Find the insertion node z
(the new last node) Store k at z Restore the heap-order
property (upheap(), discussed next)
2
65
79
insertion node
2
65
79 1
z
z
Advanced Networking Tech. Lab.Yeungnam University (yuANTL)
Programming LanguageProf. Young-Tak Kimch13 - 10
Upheap
After the insertion of a new key k, the heap-order property may be violated
Algorithm upheap() restores the heap-order property by swapping k along an upward path from the insertion node
Upheap() terminates when the key k reaches the root or a node whose parent has a key smaller than or equal to k
Since a heap has height O(log n), upheap runs in O(log n) time
2
15
79 6z
1
25
79 6z
Advanced Networking Tech. Lab.Yeungnam University (yuANTL)
Programming LanguageProf. Young-Tak Kimch13 - 11
Insertion with up-heap bubbling (1)
Advanced Networking Tech. Lab.Yeungnam University (yuANTL)
Programming LanguageProf. Young-Tak Kimch13 - 12
Insertion with up-heap bubbling (2)
Advanced Networking Tech. Lab.Yeungnam University (yuANTL)
Programming LanguageProf. Young-Tak Kimch13 - 13
Insertion with up-heap bubbling (3)
Advanced Networking Tech. Lab.Yeungnam University (yuANTL)
Programming LanguageProf. Young-Tak Kimch13 - 14
Removal from a Heap
Method removeMin of the priority queue ADT corresponds to the removal of the root key from the heap
The removal algorithm consists of three steps Replace the root key with the
key of the last node w Remove w Restore the heap-order
property (downheap(), discussed next)
2
65
79
last node
w
7
65
9w
new last node
Advanced Networking Tech. Lab.Yeungnam University (yuANTL)
Programming LanguageProf. Young-Tak Kimch13 - 15
Downheap
After replacing the root key with the key k of the last node, the heap-order property may be violated
Algorithm downheap restores the heap-order property by swapping key k along a downward path from the root
Downheap terminates when key k reaches a leaf or a node whose children have keys greater than or equal to k
Since a heap has height O(log n), downheap runs in O(log n)time
7
65
9w
5
67
9w
Advanced Networking Tech. Lab.Yeungnam University (yuANTL)
Programming LanguageProf. Young-Tak Kimch13 - 16
Down-heap Bubbling after a Removal (1)
Advanced Networking Tech. Lab.Yeungnam University (yuANTL)
Programming LanguageProf. Young-Tak Kimch13 - 17
Down-heap Bubbling after a Removal (2)
Advanced Networking Tech. Lab.Yeungnam University (yuANTL)
Programming LanguageProf. Young-Tak Kimch13 - 18
간단한 Heap의 구현
/* Heap Priority Queue (1) */
#include <stdio.h>#include <stdlib.h>
typedef int Element;#define MAX_HEAP_SIZE 10000typedef struct Heap{
int size;Element *heap;
} Heap;
/* Heap Priority Queue (2) */
void insertHeap(Heap *hp, Task *pTask){
if (hp->size > MAX_HEAP_SIZE){
printf("Exception:: heap overflow !!₩n");exit;
}hp->size++;hp->heap[hp->size] = pTask;
if (hp->size == 1) // if there is only 1 element, return; // adjustment is not necessary
// adjust the correct position of the input elementint curpos = hp->size;while (hp->heap[curpos / 2]->task_pri > pTask->task_pri){
hp->heap[curpos] = hp->heap[curpos / 2];curpos /= 2;if (curpos <= 1)
break;}hp->heap[curpos] = pTask;
}
Advanced Networking Tech. Lab.Yeungnam University (yuANTL)
Programming LanguageProf. Young-Tak Kimch13 - 19
/* Heap Priority Queue (3) */
Element removeMin(Heap *hp){
Element minElm, lastElm;int child, curpos;// hp->heap[1] is the minimum element, currently.
minElm = hp->heap[1];lastElm = hp->heap[hp->size];hp->size--;
// adjust the correct orderfor (curpos = 1; curpos * 2 <= hp->size; curpos = child){
child = curpos * 2;if ((child != hp->size) &&
(hp->heap[child + 1] < hp->heap[child]))child++;
if (lastElm > hp->heap[child])hp->heap[curpos] = hp->heap[child];
elsebreak;
}hp->heap[curpos] = lastElm;return minElm;
}
/* Heap Priority Queue (4) */
void printHeap(Heap *hp){
int i;
printf("Heap (size: %d): ", hp->size);for (i = 1; i <= hp->size; i++){
printf("%d, ", hp->heap[i]);}printf("₩n");
}
Advanced Networking Tech. Lab.Yeungnam University (yuANTL)
Programming LanguageProf. Young-Tak Kimch13 - 20
/* Heap Priority Queue (5) */
#define TEST_HEAP_SIZE 10void main(){
int numElm;int itr;int data[TEST_HEAP_SIZE] = { 3, 7, 9, 4, 2, 8, 6, 1, 5, 0 };int sortedData[TEST_HEAP_SIZE];int d;Heap *hp;
hp = (Heap *)malloc(sizeof(Heap));hp->size = 0;hp->heap = (Element *)malloc(sizeof(Element)* MAX_HEAP_SIZE);printf("₩nData before heap sorting : ");for (int i = 0; i < TEST_HEAP_SIZE; i++){
printf("%2d ", data[i]);}printf("₩n₩n");printf("Initializing heap (size: %d) . . . ₩n", TEST_HEAP_SIZE);for (int i = 0; i < TEST_HEAP_SIZE; i++){
printf("insering data (%d) into heap ...₩n", data[i]);insertHeap(hp, data[i]);printHeap(hp);
}
Advanced Networking Tech. Lab.Yeungnam University (yuANTL)
Programming LanguageProf. Young-Tak Kimch13 - 21
/* Heap Priority Queue (6) */
for (int i = 0; i < TEST_HEAP_SIZE; i++){
d = removeMin(hp);sortedData[i] = d;printf("deleting heap ==> %d₩n", d);printHeap(hp);
}printf("₩nSorted results: ");for (int i = 0; i < TEST_HEAP_SIZE; i++){
printf("%2d ", sortedData[i]);}printf("₩n");free(hp->heap);free(hp);
}
Advanced Networking Tech. Lab.Yeungnam University (yuANTL)
Programming LanguageProf. Young-Tak Kimch13 - 22
구조체 Task 배열의 Heap Sorting
구조체 Task int task_id char task_title[20]; int task_priority;
Heap sorting의 응용: (예) 총 10000개의 Task 중우선순위가 가장 높은 10개를 찾아내기 수시로 새로운 task가 추가될 수 있음 전체 10000개의 task를 모두 sorting 할 필요는 없음 항상 현재 시점에서 가장 우선 순위 (priority)가 가장 높은 task를
신속하게 찾을 수 있어야 함
Advanced Networking Tech. Lab.Yeungnam University (yuANTL)
Programming LanguageProf. Young-Tak Kimch13 - 23
/* Task.h */
#ifndef TASK_H#define TASK_H
#define TASK_TITLE_LEN 20typedef struct Task{
int task_id;int task_pri;char task_title[TASK_TITLE_LEN];Task(int id, int pri, char *title);
} Task;#endif
/* Heap.h */
#ifndef HEAP_H#define HEAP_H
typedef struct Heap{
int size;int max_size;Task **heap;Heap(int maxSize);
} Heap;
void insertHeap(Heap *hp, Task *pTask);Task *removeMin(Heap *hp);void printHeap(Heap *hp);#endif
Advanced Networking Tech. Lab.Yeungnam University (yuANTL)
Programming LanguageProf. Young-Tak Kimch13 - 24
/* Heap.c (1) */
#include <stdio.h>#include <stdlib.h>#include "Task.h"#include "Heap.h"
#define MAX_HEAP_SIZE 10000
Heap::Heap(int heapMaxSize){
max_size = heapMaxSize;heap = (Task **)malloc(sizeof(Task *) * heapMaxSize);for (int i = 0; i < heapMaxSize; i++)
heap[i] = NULL;size = 0;
}
void printHeap(Heap *hp){
int i;
printf(" Heap status (size: %d): ", hp->size);
for (i = 1; i <= hp->size; i++){
printf("(pri:%2d, id:%2d), ", hp->heap[i]->task_pri, hp->heap[i]->task_id);}printf("₩n");
}
Advanced Networking Tech. Lab.Yeungnam University (yuANTL)
Programming LanguageProf. Young-Tak Kimch13 - 25
/* Heap.c (2) */
void insertHeap(Heap *hp, Task *pTask){
if (hp->size > MAX_HEAP_SIZE){
printf("Exception:: heap overflow !!₩n");exit;
}hp->size++;hp->heap[hp->size] = pTask;
if (hp->size == 1) // if there is only 1 element, adjustment is not necessaryreturn;
// adjust the correct position of the input elementint curpos = hp->size;while (hp->heap[curpos / 2]->task_pri > pTask->task_pri){
hp->heap[curpos] = hp->heap[curpos / 2];curpos /= 2;if (curpos <= 1)
break;}hp->heap[curpos] = pTask;
}
Advanced Networking Tech. Lab.Yeungnam University (yuANTL)
Programming LanguageProf. Young-Tak Kimch13 - 26
/* Heap.c (3) */
Task *removeMin(Heap *hp){
Task *minElm, *lastElm;int child, curpos;// hp->heap[1] is the minimum element, currently.
minElm = hp->heap[1];lastElm = hp->heap[hp->size];hp->size--;
// adjust the correct orderfor (curpos = 1; curpos * 2 <= hp->size; curpos = child){
child = curpos * 2;if ((child != hp->size) && (hp->heap[child + 1]->task_pri < hp->heap[child]->task_pri))
child++;if (lastElm->task_pri > hp->heap[child]->task_pri)
hp->heap[curpos] = hp->heap[child];else
break;}hp->heap[curpos] = lastElm;return minElm;
}
Advanced Networking Tech. Lab.Yeungnam University (yuANTL)
Programming LanguageProf. Young-Tak Kimch13 - 27
/* main.c (1) */#include <stdio.h>#include <stdlib.h>#include "Task.h"#include "Heap.h"
void genBigRandArray(int m[], int bigRandMax);#define TEST_HEAP_SIZE 1000#define RESULT_SIZE 10void main(){
int *id, *pri;Heap heap_Task(TEST_HEAP_SIZE);Task **ppTask, *pTask, **ppSortedTask;
id = (int *)malloc(sizeof(int)* TEST_HEAP_SIZE);pri = (int *)malloc(sizeof(int)* TEST_HEAP_SIZE);genBigRandArray(id, TEST_HEAP_SIZE);genBigRandArray(pri, TEST_HEAP_SIZE);ppTask = (Task **)malloc(sizeof(Task *)* TEST_HEAP_SIZE);ppSortedTask = (Task **)malloc(sizeof(Task *)* RESULT_SIZE);for (int i = 0; i < TEST_HEAP_SIZE; i++){
pTask = (Task *)malloc(sizeof(Task));pTask->task_id = id[i];pTask->task_pri = pri[i];ppTask[i] = pTask;
}
Advanced Networking Tech. Lab.Yeungnam University (yuANTL)
Programming LanguageProf. Young-Tak Kimch13 - 28
/* main.c (2) */printf("₩nGenerated tasks before heap sorting : ");for (int i = 0; i < TEST_HEAP_SIZE; i++){
printf("(pri:%2d, id:%2d) ", ppTask[i]->task_pri, ppTask[i]->task_id);}printf("₩n₩n");
printf("Initializing heap (size: %d) . . . ₩n", TEST_HEAP_SIZE);Heap *pH = &heap_Task;for (int i = 0; i < TEST_HEAP_SIZE; i++){
printf("insering task (pri:%3d, id:%3d) into heap ...₩n", ppTask[i]->task_pri, ppTask[i]->task_id);insertHeap(pH, ppTask[i]);//printHeap(pH);
}for (int i = 0; i < RESULT_SIZE; i++){
pTask = removeMin(pH);ppSortedTask[i] = pTask;printf("deleting task (pri:%3d, id:%3d) from heap ==> ₩n", pTask->task_pri, pTask->task_id);//printHeap(pH);
}printf("₩nSorted results: ");for (int i = 0; i < RESULT_SIZE; i++){
printf("task (pri:%3d, id:%3d) ", ppSortedTask[i]->task_pri, ppSortedTask[i]->task_id);}printf("₩n");
Advanced Networking Tech. Lab.Yeungnam University (yuANTL)
Programming LanguageProf. Young-Tak Kimch13 - 29
/* main.c (3) */for (int i = 0; i < TEST_HEAP_SIZE; i++)
free(ppTask[i]);free(ppTask);free(ppSortedTask);
}
void genBigRandArray(int rnd[], int bigRandMax){
char *flag;int count = 0;unsigned int u_int32 = 0;unsigned int bigRand;
flag = (char *)malloc(sizeof(char)* bigRandMax);while (count < bigRandMax){
u_int32 = ((long)rand() << 15) | rand();bigRand = u_int32 % bigRandMax;if (flag[bigRand] == 1) {
continue;} else {
flag[bigRand] = 1;rnd[count++] = bigRand;
}}
}
Advanced Networking Tech. Lab.Yeungnam University (yuANTL)
Programming LanguageProf. Young-Tak Kimch13 - 30
Advanced Networking Tech. Lab.Yeungnam University (yuANTL)
Programming LanguageProf. Young-Tak Kimch13 - 31
Homework 13
13.1 Heap sorting 응용 구현(1) 별 (star)의 정보를 관리하기 위한 구조체 struct Star를 정의하라. struct Star에는
string 타입의 이름 (char name[16]), integer 타입의 id (identifier)와 age가 있고, double 데이터 타입의 distance, luminosity, mass, radius가 있다.
(2) 함수 “Star * genStar()”는 하나의 star를 동적으로 생성하며, star의 데이터 멤버들을 랜덤함수를 사용하여 임의의 값으로 초기화 한 후, 생성된 star의 주소를 반환한다. starID는 중복되지 않는 값으로 지정되어야 하며, starName은 알파벳 ‘a’ ~ ‘z’ 5 ~ 10자로 구성된다. 데이터 멤버 distance, luminosity, mass, radius 및 age 는 10.0 ~ 10000.0 범위의 값을 임의로 가진다.
(3) 함수 printStar(Star *pS)는 구성된 Galaxy에 포함된 star들의 starName, starID를순서대로 (첫번째(head) 노드 부터 마지막(tail) 노드까지) 출력한다.
(4) 10000개의 star를 동적으로 생성하여, 구조체 배열에 저장하라.(5) 생성된 10000개의 star 중 distance가 가장 작은 10개의 star를 찾아 내는 것을
heap sorting을 사용하여 구현하라.