Heaps

(ヒープ)

Data Structures and Algorithms

5th lecture, October 15, 2015

http://www.sw.it.aoyama.ac.jp/2015/DA/lecture5.html

Martin J. Dürst

AGU

© 2009-15 Martin J. Dürst 青山学院大学

Today's Schedule

 

Summary of Last Lecture

 

Last Week's Homework 1

[昨年度資料につき削除]

Last Week's Homework 2

[昨年度資料につき削除]

Last Week's Homework 3

[昨年度資料につき削除] 

Priority Queue

Example from IT:
Queue for process management, ...
Operations:

 

Simple Implementations

Time complexity of each operation
Implementation Array (ordered) Array (unordered) Linked list (ordered) Linked list (unordered)
insert O(n) O(1) O(n) O(1)
getNext O(1) O(n) O(1) O(n)
findMax O(1) O(n) O(1) O(n)

Time complexity for each operation differs for different implementations.

But there is always an operation that needs O(n).

Is it possible to improve this?

 

Ideas for Improving Priority Queue Implementation

 

 

 

 

Tree Structure

 

 

 

Problem: Balance

 

 

 

Complete Binary Tree

Definition based on tree structure:

 

Implementing a Complete Binary Tree with an Array

 

Heap

A heap is a

⇒ The root always has the highest priority

We need the following operations for implementing a heap::

 

Invariant

 

Restoring Heap Invariants

If the priority at a given node is too high: Use heapify_up

If the priority at a given node is too low: Use heapify_down

Implementation: 5heap.rb

 

Implementing a Priority Queue with a Heap

Time complexity of each operation
Implementation Heap (implemented as an Array)
insert O(log n)
findMax O(1)
getNext O(log n)

 

Heap Sort

 

How to use irb

irb: Interactive Ruby, a 'command prompt' for Ruby

Example usage:

C:\Algorithms>irb
irb(main):001:0> load './5heap'
=> true
irb(main):002:0> h = Heap.new
=> #<Heap:0x2833d60 @array=[nil], @size=0>
irb(main):003:0> h.insert 3
=> #<Heap:0x2833d60 @array=[nil, 3], @size=1>
irb(main):004:0> h.insert(5).insert(7)
=> #<Heap:0x2833d60 @array=[nil, 7, 3, 5], @size=3>
 ...

 

Other Kinds of Heaps

 

Ideas to Improve Implementation of Priority Queue

 

Conceptual Layers

 

Summary

 

Report: Manual Sorting

Deadline: November 3rd, 2015 (Wednesday), 19:00.

Where to submit: Box in front of room O-529 (building O, 5th floor)

Format:

Problem: Propose and describe an algorithm/algorithms for manual sorting, for the following two cases:

  1. One person sorts 6000 pages
  2. 20 people together sort 60000 pages

Each page is a sheet of paper of size A4, where a 10-digit number is printed in big letters.

The goal is to sort the pages by increasing number. There is no knowledge about how the numbers are distributed.

You can use the same algorithm for both cases, or a different algorithm.

Details:

  

Homework

(for next week, no need to submit)

  1. Implement joining two (normal) heaps.
  2. Think about the time complexity of creating a heap:
    heapify_all will be called n/2 times and may take up to O(log n) each time.
    Therefore, one guess for the overall time complexity is O(n log n).
    However, this upper bound can be improved by careful analysis.
  3. Find five different applications of sorting.
  4. Bring a small pair of scissors (to cut paper) to the next lecture.

 

Glossary

priority queue
順位キュー、優先順位キュー、優先順位付き待ち行列
complete binary tree
完全二分木
heap
ヒープ
internal node
内部節
restauration
修復
invariant
不変条件
sort
整列、ソート
decreasing (order)
降順
increasing (order)
昇順
join
合併
binomial queue
2項キュー、2 項ヒープ