[[TableOfContents]] = ê³„íš = * Priority_Queue * Heap * Heap 구현 * STLì—서 Priority_Queue 사용 * Heap Sort = ì°¸ì—¬ìž = ||강사 || [ê¶Œì˜ê¸°] || ||<|10> 참여ìž|| [권준í˜] || || [ì´ì§€ìˆ˜] || || [ê¹€ì •ë¯¼] || || [ìœ ìž¬ë²”] || || [성훈] || || [] || || [] || = ë‚´ìš© = == Priority Queue == * In computer science/data structures, a priority queue is an abstract data type which is like a regular queue or stack data structure, but where additionally each element has a "priority" associated with it. In a priority queue, an element with high priority is served before an element with low priority. If two elements have the same priority, they are served according to their order in the queue. * While priority queues are often implemented with heaps, they are conceptually distinct from heaps. A priority queue is an abstract concept like "a list" or "a map"; just as a list can be implemented with a linked list or an array, a priority queue can be implemented with a heap or a variety of other methods such as an unordered array. * 출처 : [http://en.wikipedia.org/wiki/Priority_queue] == Priority Queue in STL == {{{ #include<iostream> #include<cstdlib> #include<ctime> #include<queue> using namespace std; priority_queue <int> pq; int v[100]; int main(void) { int n = 10; srand(time(NULL)); printf("Vector \n"); for(int i = 0; i<n; i++){ v[i] = rand() % 100; printf("%d ", v[i]); } printf("\n\n"); for(int i = 0; i<n; i++){ pq.push(v[i]); } printf("Priority Queue \n"); while(!pq.empty()){ printf("%d ", pq.top()); pq.pop(); } printf("\n\n"); } }}} * priority_queue : [http://www.cplusplus.com/reference/queue/priority_queue/?kw=priority_queue] == Heap == * íž™(Heap)ì€ íŠ¹ë³„í•œ 트리를 기본으로 한 ìžë£Œêµ¬ì¡°(tree-based structure)로서 다ìŒê³¼ ê°™ì€ heap ì†ì„±(property)ì„ ë§Œì¡±í•œë‹¤. * Aê°€ Bì˜ ë¶€ëª¨ë…¸ë“œ(parent node) ì´ë©´, íž™(heap) ì— ì ìš©ëœ ê²ƒê³¼ ê°™ì€ ìˆœì„œë¡œ key(B)ì— ê´€í•´ì„œ key(A)는 순서가 ì •í•´ì§„ë‹¤. íž™ì—는 ë‚´ë¦¼ì°¨ìˆœì¸ '최대 íž™'ê³¼ ì˜¤ë¦„ì°¨ìˆœì¸ '최소 íž™'ì´ ìžˆë‹¤. * ê° ë…¸ë“œì˜ children ì˜ ìµœëŒ€ê°œìˆ˜ëŠ” heap ì˜ ì¢…ë¥˜ì— ë”°ë¼ ë‹¤ë¥´ì§€ë§Œ, ë§Žì€ íƒ€ìž…ì—서 children ì˜ ê°œìˆ˜ëŠ” 2ê°œ ì´í•˜ì´ë‹¤. ê·¸ë¦¬ê³ ì´ê²ƒì€ ì´ì§„ íž™(binary heap)으로 ì•Œë ¤ì ¸ 있다. * 출처 : [http://ko.wikipedia.org/wiki/%ED%9E%99_(%EC%9E%90%EB%A3%8C_%EA%B5%AC%EC%A1%B0)] * Heap Visualization - [http://www.cs.usfca.edu/~galles/visualization/Heap.html] * A heap data structure should not be confused with the heap which is a common name for the pool of memory from which dynamically allocated memory is allocated. The term was originally used only for the data structure. == Binary Heap == * 출처 : [http://en.wikipedia.org/wiki/Binary_heap] === Property === * Shape property The tree is a complete binary tree; that is, all levels of the tree, except possibly the last one (deepest) are fully filled, and, if the last level of the tree is not complete, the nodes of that level are filled from left to right. Heap property * Heap property All nodes are either greater than or equal to or less than or equal to each of its children, according to a comparison predicate defined for the heap. === Operation === * Max Heap ì´ë¼ê³ ê°€ì •í•˜ìž. * 삽입 1. 가장 마지막 ìœ„ì¹˜ì— ì›ì†Œë¥¼ 삽입한다. 2. 삽입 한 ì›ì†Œë¥¼ ì›ì†Œì˜ 부모와 비êµí•œë‹¤. 만약 부모가 ë” í¬ë‹¤ë©´ 멈춘다. 3. 부모가 ë” ìž‘ë‹¤ë©´, 부모와 위치를 바꾼다. ê·¸ë¦¬ê³ ë‹¤ì‹œ ê·¸ 위치ì—서 부모와 비êµ... * ì‚ì œ 1. Rootì˜ ì›ì†Œë¥¼ ì œê±°í•˜ê³ , Rootì— Heapì— ê°€ìž¥ 마지막 ìœ„ì¹˜ì— ìžˆëŠ” ì›ì†Œë¥¼ 놓는다. 2. 새로운 Root와 Rootì˜ ìžì‹ê³¼ 비êµí•œë‹¤. 만약 Rootê°€ ë” í¬ë‹¤ë©´ 멈춘다. 3. Rootê°€ ë” ìž‘ë‹¤ë©´, ìžì‹ê³¼ 위치를 êµì²´í•œë‹¤. ê·¸ë¦¬ê³ ë‹¤ì‹œ ê·¸ 위치ì—서 ìžì‹ê³¼ 비êµ... === Implementation === == Heap Sort == attachment:heapsort_animation.gif * 간단한 설명 : [http://ko.wikipedia.org/wiki/%ED%9E%99_%EC%A0%95%EB%A0%AC] * Heap Sort Visualization - [http://www.cs.usfca.edu/~galles/visualization/HeapSort.html] == Application of Priority Queue == * Dijkstra's algorithm : 최단 경로를 구하는 ì•Œê³ ë¦¬ì¦˜ì¸ë° 그래프 ì‹œê°„ì— ë°°ì›ë‹ˆë‹¤. * [http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm#Using_a_priority_queue] * Best-first search algorithms * Prim's algorithms * ê·¸ 외ì—ë„ ì—¬ëŸ¬ê°€ì§€ 등등등.. = 후기 = * ì£¼ì œë¥¼ 하나를 ìž¡ê³ ì„¤ëª…í•˜ë ¤ë‹ˆê¹Œ 편하네요. í”¼ë“œë°±ì„ ë°›ì•„ì„œ ê°œì„ ì„ í•´ë³´ë ¤ê³ í•˜ëŠ”ë° ê°œì„ ì´ ë˜ëŠ”ì§€ëŠ” 잘 못ëŠë¼ê² 네요. 좀 ë” í”¼ë“œë°±ì„ ì£¼ì—ˆìœ¼ë©´ 합니다. ë” ì¢‹ì€ ìƒˆì‹¹ êµì‹¤ì´ ë 수 있ë„ë¡ í•˜ê³ ì‹¶ë„¤ìš”. 강사나 í•™ìƒì—게나 시간 ë‚비가 ë˜ì§€ 않는 ìƒˆì‹¹ì´ ë˜ì—ˆìœ¼ë©´ 합니다. ë‹¤ìŒ ì£¼ëŠ” Mapê³¼ Binary Search Tree를 ê°€ë¥´ì¹ ê³„íšìž…니다. Floyd Algorithm까지는 ê°ˆ ê¸¸ì´ ë¨¸ë„¤ìš”. 올 í•´ê°€ 가기 ì „ì—는 마무리 해야 ë í…ë°... - [ê¶Œì˜ê¸°] * ë”ê°€ 늘어납니다! 재귀만 ë³´ë©´ 머리가 아파오는 ì €ê°€ ì´ê±¸ ì œëŒ€ë¡œ ë”°ë¼ê°ˆìˆ˜ 있ì„ì§€ ê±±ì •ìž…ë‹ˆë‹¤. 넘어아 í• ì‚¬ì´ë‹ˆ ì´ë²ˆê¸°íšŒì— 극복해보는걸로,,, 쿨ëŸ,, 수업내용 ìžì²´ëŠ” 알차서 좋습니다. 너무 알차서 ë˜ìƒˆê¹€ì§ˆì´ 필요하긴 하지만요. ã… ã… - [권준í˜] * ì´ë²ˆì— ì°¸ê³ ìžë£Œê°€ ë” ë§Žì•„ì ¸ì„œ ì„¤ëª…ë„ ì•Œì•„ë“£ê¸° íŽ¸í–ˆê³ , ì£¼ì œë„ í•œê°€ì§€ë¼ ìƒê°ì´ 집중ë˜ì„œ 괜찮다는 ìƒê°ì´ 들었습니다. (특히 ëˆˆì— ë³´ì´ëŠ” 예시를 ë³´ë©´ 확실히 ì´í•´ê°€ ë¹ ë¥´ë„¤ìš”) ì˜ê¸°í˜•ì´ ì‹œê°„ì„ ë” íˆ¬ìží•´ì£¼ì‹œëŠ” ë§Œí¼ ë” ì¢‹ì€ ìƒˆì‹¹ì´ ë˜ê°€ëŠ” 것 같습니다. ~~ê°ì‚¬ížˆ 잘 ë°›ì•„ë¨¹ê² ìŠµë‹ˆë‹¤.~~ 지금까지 새싹êµì‹¤ 하면서 ì •ë§ ìž¬ë°Œê²Œ 들었는ë°, ì´ë²ˆ ì‹œê°„ì€ ë” ìž¬ë°Œê²Œ 들었네요. ~~ì €ë¥¼ ê³¼ì œë¥¼ 하게 만드시다니!~~ ë”ê°€ ê°œì„ í–ˆìœ¼ë©´ ì¢‹ì„ ê±° ê°™ì€ê²Œ ìƒê°ë‚˜ë©´ 남길í…ë°, 딱히 ìƒê°ì´ 안나네요. ìƒê°ë‚˜ë©´ 위키나 카톡으로 ì „í•´ë“œë¦¬ê² ìŠµë‹ˆë‹¤. - [ê¹€ì •ë¯¼] = ìˆ™ì œ = * Binary Heap 구현 하기. ---- [새싹êµì‹¤/2014], [새싹êµì‹¤/2014/다빈치ì¸ìž¬ë°˜]