design and analysis of algorithms -...

82
1 Design and Analysis of Algorithms 演算法設計與分析 Lecture 8 November 16, 2016 洪國寶

Upload: lehuong

Post on 12-Mar-2018

220 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

1

Design and Analysis of

Algorithms

演算法設計與分析

Lecture 8

November 16, 2016

洪國寶

Page 2: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

2

Outline

• Review

– Amortized analysis

• Advanced data structures

– Binary heaps

– Binomial heaps

– Fibonacci heaps

– Data structures for disjoint sets

Page 3: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

3

Huffman code (Implementation)

• Time complexity and data structure:

Let S be the set of n weights (nodes).

Constructing a Huffamn code based on greedy strategy can be described as following

Repeat until |S|=1

Find two min nodes x and y from S and remove them from S

Construct a new node z with weight w(z)=w(x)+w(y) and

insert z into S

Page 4: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

4

Huffman code (Implementation)

Why data structures are important?

• An algorithm for constructing Huffman code (Tree):

Repeat until |S|=1– Find two min nodes x and y from S and remove them from S

– Construct a new node z with weight w(z)=w(x)+w(y) and insert z into S

• The time complexity of the algorithm depends on how S is implemented.

• Data structure for S each iteration total

linked list O(n) O(1) O(n2)

Sorted array O(1) O(n) O(n2)

? O(log n) O(log n) O(n log n)

Page 5: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

Priority Queues

• 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.

• Priority queues are often implemented with heaps.

• Applications.– Dijkstra's shortest path algorithm.

– Prim's MST algorithm.

– Event-driven simulation.

– Huffman encoding. (Lecture 6)

– Heapsort.

– . . .

5

Page 6: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

6

Mergeable Heaps

Support the following operations. • MAKE-HEAP() creates and returns a new heap containing no elements.

• INSERT(H, x) inserts node x, whose key field has already been filled in, into

heap H.

• MINIMUM(H) returns a pointer to the node in heap H whose key is minimum.

• EXTRACT-MIN(H) deletes the node from heap H whose key is minimum,

returning a pointer to the node.

• UNION(Hl, H2) creates and returns a new heap that contains all the nodes of

heaps H1 and H2. Heaps H1 and H2 are "destroyed" by this operation.

• DECREASE-KEY(H, x, k) assigns to node x within heap H the new key value k,

which is assumed to be no greater than its current key value.

• DELETE(H, x) deletes node x from heap H.

Page 7: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

7

Dijkstra/Prim

1 make-heap

|V| insert

|V| extract-min

|E| decrease-key

Priority Queues

make-heap

Operation

insert

minimum

extract-min

union

decrease-key

delete

1

Binary

log N

1

log N

N

log N

log N

1

Binomial

log N

log N

log N

log N

log N

log N

1

Fibonacci *

1

1

log N

1

1

log N

1

Linked List

1

N

N

1

1

N

Mergeable Heaps

O(|E| + |V| log |V|)O(|E| log |V|)O(|V|2)

Page 8: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

8

Binary Heap: Definition• Binary heap.

– Almost complete binary tree.

• filled on all levels, except last, where filled from left to right

– Min-heap ordered.

• every child greater than (or equal to) parent

06

14

78 18

81 7791

45

5347

6484 9983

Page 9: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

9

Binary Heap: Properties

•Properties.

– Min element is in root.

– Heap with N elements has height = log2 N.

06

14

78 18

81 7791

45

5347

6484 9983

N = 14

Height = 3

Page 10: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

10

Binary Heaps: Array Implementation

•Implementing binary heaps.– Use an array: no need for explicit parent or child pointers.

•Parent(i) = i/2

•Left(i) = 2i

•Right(i) = 2i + 1

06

14

78 18

81 7791

45

5347

6484 9983

1

2 3

4 5 6 7

8 9 10 11 12 13 14

Page 11: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

11

Binary heap operations

• Make-heap

make-heap( )

allocate an array H on size N

heap-size[H] 0

• Minimum

minimum(H)

return H[1]

Page 12: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

12

Inserting into a binary heap

• There are two constraints that must be met

– an almost complete tree

– the heap-order property

• We increase the size of the heap to make a

hole in the next spot

• It the item can be legally inserted in the hole,

then do so, otherwise move the parent value

down and try again

Page 13: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

13

Binary Heap: Insertion example

•Insert element x (key = 42) into heap.

– Insert into next available slot.

– Bubble up until it's heap ordered.

06

14

78 18

81 7791

45

5347

6484 9983 42 next free slot

Page 14: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

14

Binary Heap: Insertion example

•Insert element x (key = 42) into heap.

– Insert into next available slot.

– Bubble up until it's heap ordered.

06

14

78 18

81 7791

45

5347

6484 9983 4242

swap with parent

Page 15: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

15

Binary Heap: Insertion example

•Insert element x (key = 42) into heap.

– Insert into next available slot.

– Bubble up until it's heap ordered.

06

14

78 18

81 7791

45

4247

6484 9983 4253

swap with parent

Page 16: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

16

Binary Heap: Insertion example

•Insert element x (key = 42) into heap.

– Insert into next available slot.

– Bubble up until it's heap ordered.

06

14

78 18

81 7791

42

4547

6484 9983 53

stop: heap ordered

Page 17: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

17

Binary Heap: Insertion algorithm

• insert(H,k)

heap-size[H] heap-size[H]+1

i heap-size[H]

while i > 1 and H[parent(i)] > k

do H[i] H[parent(i)]

i parent(i)

H[i] k

• Time complexity = O(tree height) = O(log n)

Page 18: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

18

Binary Heap: Decrease Key

•Decrease key of element x to k.

– Bubble up until it's heap ordered.

06

14

78 18

81 7791

42

4547

6484 9983 53

Page 19: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

19

Binary Heap: Decrease Key

• decrease-key(H,i,k)

while i > 1 and H[parent(i)] > k

do H[i] H[parent(i)]

i parent(i)

H[i] k

• Time complexity = O(tree height) = O(log n)

Page 20: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

20

Binary Heap: Extract-Min

• Delete minimum element from heap.

– Exchange root with rightmost leaf.

– Bubble root down until it's heap ordered.

• power struggle principle: better subordinate is promoted

06

14

78 18

81 7791

42

4547

6484 9983 53

Page 21: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

21

Binary Heap: Extract-Min• Delete minimum element from heap.

– Exchange root with rightmost leaf.

– Bubble root down until it's heap ordered.

• power struggle principle: better subordinate is promoted

53

14

78 18

81 7791

42

4547

6484 9983 06

Page 22: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

22

Binary Heap: Extract-Min• Delete minimum element from heap.

– Exchange root with rightmost leaf.

– Bubble root down until it's heap ordered.

• power struggle principle: better subordinate is promoted

53

14

78 18

81 7791

42

4547

6484 9983

exchange with left child

Page 23: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

23

Binary Heap: Extract-Min

• Delete minimum element from heap.

– Exchange root with rightmost leaf.

– Bubble root down until it's heap ordered.

• power struggle principle: better subordinate is promoted

14

53

78 18

81 7791

42

4547

6484 9983

exchange with right child

Page 24: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

24

Binary Heap: Extract-Min

• Delete minimum element from heap.– Exchange root with rightmost leaf.

– Bubble root down until it's heap ordered.• power struggle principle: better subordinate is promoted

– Time complexity = O(tree height) = O(log n)

14

18

78 53

81 7791

42

4547

6484 9983

stop: heap ordered

Page 25: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

25

Binary Heap: Extract-Min

• extract-min(H)

if heap-size[H] < 1

then error “heap underflow”

min H[1]

H[1] H[heap-size(H)]

heap-size[H] heap-size[H] -1

heapify(H, 1)

return min

Bubble root down until it's heap ordered.

Page 26: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

26

Binary Heap: Heapify

• When heapify(H,i) is called, it is assumed that the

binary trees rooted at left(i) and right(i) are heaps,

but that A[i] may be larger than its children, thus

violating the heap property.

• The function of heapify is to let the value at A[i]

float down in the heap so that the subtree rooted at

index i becomes a heap.

• The running time is O(h) if the height of node i

is h.

Page 27: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

27

Binary Heap: Heapify

• heapify(H,i)

l left(i)

r right(i)

if l ≦ heap-size[H] and H[l] < H[i]

then smallest l

else smallest i

if r ≦ heap-size[H] and H[r] < H[smallest]

then smallest r

if smallest ≠ i

then H[i] H[smallest]

heapify(H,smallest)

Page 28: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

28

Binary Heap: Delete

• delete(H,i)

H[i] H[heap-size[H]]

heap-size[H] heap-size[H] – 1

heapify(H,i)

Page 29: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

29

H1 H2

14

78 18

81 7791

11

6253

6484 9941

Binary Heap: Union• Union.

– Combine two binary heaps H1 and H2 into a single heap.

– No easy solution.

• (N) operations apparently required

– Can support fast union with fancier heaps.

Page 30: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

30

Binary Heap: Union

• Union(H1,H2)

H make-heap()

heap-size[H] heap-size[H1] + heapsize[H2]

for i 1 to heap-size[H1]

do H[i] H1[i]

for i 1 to heap-size[H2]

do H[heap-size[H1]+i] H2[i]

for i heap-size[H]/2 down to 1

do heapify(H,i)

Page 31: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

31

Binary Heap: Union

14

78

41 91

62 8453

18

7781

99 6411

14

78 18

81 779141

11

6253

6484 99

H1

H2

Page 32: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

32

Binary Heap: Union

• Proof of correctness by loop invariant

– At the start of each iteration, each node i+1, i+2, …,n is the root of a heap

• Time complexity

– Simple upper bound:

• each call to HEAPIFY: O(lg n)

• O(n) such calls

• running time at most O(n lg n)

– A tighter bound is O(n).

Page 33: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

33

Binary Heap: Union

• Time complexity

– A tighter bound is O(n). (Use blackboard)

Page 34: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

34

Binary Heap: Heapsort

• To sort N items:

– Insert N items into a binary max-heap.

– Perform N extract-max operations.

• O(N log N) sort.

• No extra storage (in-place sort).

• Which one is better? Merge sort or heap sort?

Page 35: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

35

Build-Max-Heap Operation

• We start by recognizing that if each subtree

of a node is a heap, then when we connect

the two subtrees, we can obtain a new heap

by finding where the root value should go

• All leaf nodes are, by definition, heaps

Page 36: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

36

Page 37: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

37

Page 38: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

38

Example: The action of max-heapify(A,2)

Page 39: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

39

Page 40: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

40

Example: The operation of build-max-heap

Page 41: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

41

Running Time of

BUILD-MAX-HEAP

• Simple upper bound:

– each call to MAX-HEAPIFY: O(lg n)

– O(n) such calls

– running time at most O(n lg n)

• A tighter bound is O(n).

Page 42: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

42

Page 43: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

43

Page 44: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

44

Running Time of HEAPSORT

• Upper bound:

– Build max-heap: O(n)

– each call to MAX-HEAPIFY: O(log n)

• O(n) such calls

– running time O(n log n)

Page 45: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

45

Some sorting algorithms

Insertion

sort

Merge

sort

Heapsort Quicksort

complexity O(n2) O(n log n) O(n log n) O(n log n)

In-place

sort

yes no yes

Data

structure

Linked

list

Linked

list

Array

Remark On-line

sorting

Page 46: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

46

Sorting algorithms

• The ideal sorting algorithm would have the following properties: – Stable: Equal keys aren't reordered.

– Operates in place, requiring O(1) extra space.

– Worst-case O(n·lg(n)) key comparisons.

– Worst-case O(n) swaps.

– Adaptive: Speeds up to O(n) when data is nearly sorted or when there are few unique keys.

• There is no algorithm that has all of these properties, and so the choice of the optimal sorting algorithm depends on the application.

Page 47: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

47

Animated Sorting Algorithms

• http://www.sorting-algorithms.com/

Page 48: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

48

Priority Queues

make-heap

Operation

insert

minimum

extract-min

union

decrease-key

delete

1

Binary

log N

1

log N

N

log N

log N

1

Binomial

log N

log N

log N

log N

log N

log N

1

Fibonacci *

1

1

log N

1

1

log N

1

Linked List

1

N

N

1

1

N

Mergeable Heaps

Coming up next

Page 49: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

49

Binomial Trees

• Binomial tree.

– Recursive definition:

Bk-1

Bk-1

B0 Bk

B0 B1 B2 B3 B4

Page 50: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

50

Binomial Trees• Useful properties of order k binomial tree Bk.– Number of nodes = 2k.

– Height = k.

– Degree of root = k.

– Deleting root yields binomialtrees Bk-1, … , B0.

• Proof.– By induction on k.

B0 B1 B2 B3 B4

B1

Bk

Bk+1

B2

B0

Page 51: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

51

Binomial Trees

• A property useful for naming the data structure.

– Bk has nodes at depth i.

B4

i

k

62

4

depth 2

depth 3

depth 4

depth 0

depth 1

Page 52: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

52

Binomial Heap

• Binomial heap. Vuillemin, 1978.

– Sequence of binomial trees that satisfy binomial heap property.• each tree is min-heap ordered

• 0 or 1 binomial tree of order k

B4 B0B1

55

45 32

30

24

23 22

50

48 31 17

448 29 10

6

37

3 18

Page 53: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

53

Page 54: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

54

Binomial Heap: Properties• Properties of N-node binomial heap.– Min key contained in root of B0, B1, . . . , Bk. (root list)

– Contains binomial tree Bi iff bi = 1 where bn b2b1b0 is binary representation of N.

– At most log2 N + 1 binomial trees.

– Height log2 N.

B4 B0B1

55

45 32

30

24

23 22

50

48 31 17

448 29 10

6

37

3 18

N = 19

# trees = 3

height = 4

binary = 10011

Page 55: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

55

Binomial Heap Operation 1

• Create a binomial heap

make-binomial-heap( )

H allocate-node( )

head[H] nil

return H

• Running time = O(1)

Page 56: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

56

Binomial Heap Operation 2

• Minimum

• Running time = O(log n)

Page 57: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

57

Binomial Heap Operation 3: Union

• Create heap H that is union of heaps H' and H''.

– "Mergeable heaps."

– Easy if H' and H'' are each order k binomial trees.

• connect roots of H' and H''

• choose smaller key to be root of H (Binomial-link)

H''55

45 32

30

24

23 22

50

48 31 17

448 29 10

6

H'

Page 58: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

58

Binomial Heap: Union

Page 59: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

59

Binomial Heap: Union

001 1

100 1+

011 1

11

1

1

0

1

19 + 7 = 26

55

45 32

30

24

23 22

50

48 31 17

448 29 10

6

37

3 18

41

3328

15

25

7 12

+

Page 60: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

60

Binomial Heap: Union

55

45 32

30

24

23 22

50

48 31 17

448 29 10

6

37

3 18

41

3328

15

25

7 12

+

Page 61: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

61

Binomial Heap: Union

55

45 32

30

24

23 22

50

48 31 17

448 29 10

6

37

3

41

3328

15

25

7

+

12

18

18

12

Page 62: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

62

55

45 32

30

24

23 22

50

48 31 17

448 29 10

6

37

3

41

3328

15

25

7

+

12

18

25

377

3

18

12

18

12

Page 63: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

63

55

45 32

30

24

23 22

50

48 31 17

448 29 10

6

37

3

41

3328

15

25

7

12

+

18

25

377

3

41

28 33 25

3715 7

3

18

12

18

12

Page 64: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

64

55

45 32

30

24

23 22

50

48 31 17

448 29 10

6

37

3

41

3328

15

25

7

+

18

12

41

28 33 25

3715 7

3

12

18

25

377

3

41

28 33 25

3715 7

3

18

12

Page 65: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

65

55

45 32

30

24

23 22

50

48 31 17

448 29 10

6

37

3

41

3328

15

25

7

+

18

12

41

28 33 25

3715 7

3

12

18

25

377

3

41

28 33 25

3715 7

3

55

45 32

30

24

23 22

50

48 31 17

448 29 10

6

18

12

Page 66: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

66

Page 67: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

67

Page 68: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

68

Binomial Heap Operation 3: Union

• Create heap H that is union of heaps H' and H''.

– Analogous to binary addition.

• Running time = O(log N)

– Proportional to number of trees in root lists 2( log2 N + 1).

001 1

100 1+

011 1

11

1

1

0

1

19 + 7 = 26

Page 69: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

69

Binomial Heap Operation 3: Union

• Running time = O(log N)

– Binomial-heap-merge: merge two sorted linked lists

• Proportional to number of trees in root lists 2( log2 N + 1).

– while loop (line 9- line 21)

• Each iteration executes one binomial-link O(1)

• Proportional to number of trees in root lists 2( log2 N + 1).

001 1

100 1+

011 1

11

1

1

0

1

19 + 7 = 26

Page 70: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

70

Binomial Heap Operation 3:

Extract-min• Delete node with minimum key in binomial heap H.– Find root x with min key in root list of H, and delete

– H' broken binomial trees

– H Union(H', H)

• Running time. O(log N)

55

45 32

30

24

23 22

50

48 31 17

37

6 18

448 29 10

H

H'

Page 71: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

71

Page 72: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

72

Page 73: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

73

Binomial Heap: Extract-min•Running time. O(log N)– 1. Find the root with minimum key from root list: O(# of trees) = O(log n)

– 2. Make-binomial-heap: O(1)

– 3. Reverse the order of the linked list of x’s children: O(degree(x)) = O(log n)

– 4. Binomial-heap-union: O(log n)

55

45 32

30

24

23 22

50

48 31 17

37

6 18

448 29 10

H

H'

Page 74: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

74

Binomial Heap Operation 5: Insert

• Insert a new node x into binomial heap H.

– H' MakeHeap(x)

– H Union(H', H)

• Running time. O(log N)

3

37

6 18

55

45 32

30

24

23 22

50

48 31 17

448 29 10

H

x

H'

Page 75: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

75

Binomial Heap Operation 5: Insert

Page 76: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

76

Binomial Heap: Sequence of Inserts• Insert a new node x into binomial heap H.

– If N = .......0, then only 1 steps.

– If N = ......01, then only 2 steps.

– If N = .....011, then only 3 steps.

– If N = ....0111, then only 4 steps.

• Inserting 1 item can take (log N) time.– If N = 11...111, then log2 N steps.

• But, inserting sequence of N items takes O(N) time!

– (N/2)(1) + (N/4)(2) + (N/8)(3) + . . . 2N

– Amortized analysis.

– Basis for getting most operationsdown to constant time.

50

48 31 17

4429 10

3

37

6 x

22

1

22

21

1

NN

N

nn

Nn

Page 77: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

77

3

37

6 18

55

x 32

30

24

23 22

50

48 31 17

448 29 10

H

Binomial Heap Operation 6:

Decrease Key• Decrease key of node x in binomial heap H.– Suppose x is in binomial tree Bk.

– Bubble node x up the tree if x is too small.

• Running time. O(log N)

– Proportional to depth of node x log2 N .

depth = 3

Page 78: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

78

Page 79: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

79

Page 80: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

80

Binomial Heap Operation 7: Delete

• Delete node x in binomial heap H.

– Decrease key of x to -.

– Delete min.

• Running time. O(log N)

Page 81: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

81

Binomial Heap Operation 7: Delete

Page 82: Design and Analysis of Algorithms - ailab.cs.nchu.edu.twailab.cs.nchu.edu.tw/course/Algorithms/105/AL08.pdf · Find two min nodes x and y from S and remove ... 83 84 99 64 42 next

82

Questions?