binomial & fibonaccci heap by priya

52
WELCOME

Transcript of binomial & fibonaccci heap by priya

WELCOME

BINOMIAL HEAP & FIBONACCI HEAP

Presented By :

NAME IDShohani Amrin

12133103025

Badhon Kumer Bhodra

12133103027

Israt Rashid trisha

12133103029

INDEXBinomial heap :

1.Find Minimum Key 2.Insert New Node

3.Delete a Node4.Unite Two Binomial Heaps

5.Binomial Heap Union

Fibonacci heap:1.insert2.union

3.Delete min4.Decrease key

5.Delete

BINOMIAL HEAP

Binomial Heap

The binary heap data structure is fine for the simple operations of inserting, deleting and extracting elements, but other operations aren't so well supported.

One such operation is the Union operation, which joins two heaps together.

If the heaps are binary heaps then this requires building up a new heap from scratch, using the elements of the old heaps, which is expensive for large heaps.

Binomial heap presents the data structure, which supports Union operations more efficiently.

Binomial Trees

The binomial tree is the building block for the binomial heap. A binomial tree is an ordered tree that is, a tree where the children of each node are ordered.

Binomial trees are defined recursively, building up from single nodes. A single tree of degree k is constructed from two trees of degree k - 1 by making the root of one tree the leftmost child of the root of the other tree.

The Binomial Heap Properties

A binomial heap is a collection of binomial trees that satisfies the following binomial-heap properties:

1. No two binomial trees in the collection have the same size.

2. Each node in each tree has a key.3. Each binomial tree in the collection is heap-

ordered in the sense that each non-root has a key strictly less than the key of its parent.

The number of trees in a binomial heap is O(log n).

Operations on Binomial Heaps

Creation of a new heap. Search for the minimum key. Uniting two binomial heaps. Insertion of a node. Removal of the root of a tree. Decreasing a key. Removal of a node.

Search for the Minimum Key

To do this we find the smallest key among those stored at the roots connected to the head of H.

What's the cost of minimum-search?The cost is O(log n) because there are O(log n) heaps,

in each tree the minimum is located at the root, and the roots are linked.

Find Minimum Key

Insert New Node

Delete a Node

Unite Two Binomial Heaps

Binomial Heap 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

Running Time O(log N)

FIBONACCI HEAP

19

Fibonacci Heap Operations:InsertUnionDelete minDecrease keyDelete

20

Fibonacci HeapsFibonacci heap history. Fredman and Tarjan (1986)

Ingenious data structure and analysis. Original motivation: O(m + n log n) shortest path

algorithm. also led to faster algorithms for MST, weighted bipartite

matching Still ahead of its time.

Fibonacci heap intuition. Similar to binomial heaps, but less structured. Decrease-key and union run in O(1) time. "Lazy" unions.

21

Fibonacci Heaps: Structure

Fibonacci heap. Set of min-heap ordered trees.

22

723

30

17

35

26 46

24

H 39

4118 52

3

44

min

marked

Fibonacci Heaps: Insert

Insert. Create a new singleton tree. Add to left of min pointer. Update min pointer.

23

723

30

17

35

26 46

24

H 39

4118 52

3

44

min21

Insert 21

Fibonacci Heaps: Insert

Insert. Create a new singleton tree. Add to left of min pointer. Update min pointer.

2439

41

723

18 52

3

30

17

35

26 46

24

44

min

H

21

Insert 21

Fibonacci Heaps: Insert

Insert. Create a new singleton tree. Add to left of min pointer. Update min pointer.

Running time. O(1) amortized Actual cost = O(1). Change in potential = +1. Amortized cost = O(1).

2539

41

7

18 52

3

30

17

35

26 46

24

44

min

H

2123

Insert 21

Fibonacci Heaps: Union

Union. Concatenate two Fibonacci heaps. Root lists are circular, doubly linked lists.

2639

41

717

18 52

3

30

23

35

26 46

24

44

min

H' H''

21

min

Fibonacci Heaps: Union

Union. Concatenate two Fibonacci heaps. Root lists are circular, doubly linked lists.

Running time. O(1) amortized Actual cost = O(1). Change in potential = 0. Amortized cost = O(1).

2739

41

717

18 52

3

30

23

35

26 46

24

44

min

H' H''

21

Fibonacci Heaps: Delete Min

Delete min. Delete min and concatenate its children into root list. Consolidate trees so that no two roots have same degree.

28

39

4118 52

3

44

min

1723

30

7

35

26 46

24

Fibonacci Heaps: Delete Min

Delete min. Delete min and concatenate its children into root list. Consolidate trees so that no two roots have same degree.

29

39

411723 18 52

30

7

35

26 46

24

44

currentmin

Fibonacci Heaps: Delete Min

Delete min. Delete min and concatenate its children into root list. Consolidate trees so that no two roots have same degree.

30

39

411723 18 52

30

7

35

26 46

24

44

current

0 1 2 3

min

Fibonacci Heaps: Delete Min

Delete min. Delete min and concatenate its children into root list. Consolidate trees so that no two roots have same degree.

31

39

411723 18 52

30

7

35

26 46

24

44

current

0 1 2 3

min

Fibonacci Heaps: Delete Min

Delete min. Delete min and concatenate its children into root list. Consolidate trees so that no two roots have same degree.

32

39

411723 18 52

30

7

35

26 46

24

44current

0 1 2 3

min

Fibonacci Heaps: Delete Min

Delete min. Delete min and concatenate its children into root list. Consolidate trees so that no two roots have same degree.

33

39

411723 18 52

30

7

35

26 46

24

44current

0 1 2 3

Merge 17 and 23 trees.

min

Fibonacci Heaps: Delete Min

Delete min. Delete min and concatenate its children into root list. Consolidate trees so that no two roots have same degree.

34

39

4117

23

18 52

30

7

35

26 46

24

44

current

0 1 2 3

Merge 7 and 17 trees.

min

Fibonacci Heaps: Delete Min

Delete min. Delete min and concatenate its children into root list. Consolidate trees so that no two roots have same degree.

35

39

417

30

18 52

17

35

26 46

24

44

current

0 1 2 3

23

Merge 7 and 24 trees.

min

Fibonacci Heaps: Delete Min

Delete min. Delete min and concatenate its children into root list. Consolidate trees so that no two roots have same degree.

36

39

417

30

18 52

23

17

35

26 46

24 44

current

0 1 2 3

min

Fibonacci Heaps: Delete Min

Delete min. Delete min and concatenate its children into root list. Consolidate trees so that no two roots have same degree.

37

39

417

30

18 52

23

17

35

26 46

24 44

current

0 1 2 3

min

Fibonacci Heaps: Delete Min

Delete min. Delete min and concatenate its children into root list. Consolidate trees so that no two roots have same degree.

38

39

417

30

18 52

23

17

35

26 46

24 44

current

0 1 2 3

min

Fibonacci Heaps: Delete Min

Delete min. Delete min and concatenate its children into root list. Consolidate trees so that no two roots have same degree.

39

39

417

30

18 52

23

17

35

26 46

24 44

current

0 1 2 3

Merge 41 and 18 trees.

min

Fibonacci Heaps: Delete Min

Delete min. Delete min and concatenate its children into root list. Consolidate trees so that no two roots have same degree.

40

3941

7

30

1852

23

17

35

26 46

24

44

current

0 1 2 3

min

Fibonacci Heaps: Delete Min

Delete min. Delete min and concatenate its children into root list. Consolidate trees so that no two roots have same degree.

41

3941

7

30

1852

23

17

35

26 46

24

44

current

0 1 2 3

min

Fibonacci Heaps: Delete Min

Delete min. Delete min and concatenate its children into root list. Consolidate trees so that no two roots have same degree.

42

3941

7

30

1852

23

17

35

26 46

24

44

min

Stop.

Fibonacci Heaps: Decrease Key

Decrease key of element x to k. Case 0: min-heap property not violated.

decrease key of x to k change heap min pointer if necessary

43

24

46

17

30

23

7

88

26

21

52

39

18

41

38

Decrease 46 to 45.72

45

35

min

Fibonacci Heaps: Decrease Key

Decrease key of element x to k. Case 1: parent of x is unmarked.

decrease key of x to k cut off link between x and its parent mark parent add tree rooted at x to root list, updating heap min pointer

44

24

45

17

30

23

7

88

26

21

52

39

18

41

38

Decrease 45 to 15.72

15

35

min

Fibonacci Heaps: Decrease Key

Decrease key of element x to k. Case 1: parent of x is unmarked.

decrease key of x to k cut off link between x and its parent mark parent add tree rooted at x to root list, updating heap min pointer

45

24

15

17

30

23

7

88

26

21

52

39

18

41

38

Decrease 45 to 15.72

24

35

min

Fibonacci Heaps: Decrease Key

Decrease key of element x to k. Case 1: parent of x is unmarked.

decrease key of x to k cut off link between x and its parent mark parent add tree rooted at x to root list, updating heap min pointer

46

24 17

30

23

7

88

26

21

52

39

18

41

38

Decrease 45 to 15.

24

35

min

15

72

Fibonacci Heaps: Decrease KeyDecrease key of element x to k.

Case 2: parent of x is marked. decrease key of x to k cut off link between x and its parent p[x], and add x to root

list cut off link between p[x] and p[p[x]], add p[x] to root list

If p[p[x]] unmarked, then mark it. If p[p[x]] marked, cut off p[p[x]], unmark, and repeat.

4735

24

15

17

30

23

7

88

26

21

52

39

18

41

38

Decrease 35 to 5.

72 24

5

min

Fibonacci Heaps: Decrease KeyDecrease key of element x to k.

Case 2: parent of x is marked. decrease key of x to k cut off link between x and its parent p[x], and add x to root

list cut off link between p[x] and p[p[x]], add p[x] to root list

If p[p[x]] unmarked, then mark it. If p[p[x]] marked, cut off p[p[x]], unmark, and repeat.

48

24 17

30

23

7

26

21

52

39

18

41

38

Decrease 35 to 5.

24

5

88

parent marked

15

72

min

Fibonacci Heaps: Decrease KeyDecrease key of element x to k.

Case 2: parent of x is marked. decrease key of x to k cut off link between x and its parent p[x], and add x to root

list cut off link between p[x] and p[p[x]], add p[x] to root list

If p[p[x]] unmarked, then mark it. If p[p[x]] marked, cut off p[p[x]], unmark, and repeat.

49

24

26

17

30

23

7

21

52

39

18

41

38

Decrease 35 to 5.

88 24

515

72

parent marked

min

Fibonacci Heaps: Decrease Key

Decrease key of element x to k. Case 2: parent of x is marked.

decrease key of x to k cut off link between x and its parent p[x], and add x to root

list cut off link between p[x] and p[p[x]], add p[x] to root list

If p[p[x]] unmarked, then mark it. If p[p[x]] marked, cut off p[p[x]], unmark, and repeat.

50

26

17

30

23

7

21

52

39

18

41

38

Decrease 35 to 5.

88

515 24

72

min

Fibonacci Heaps: DeleteDelete node x.

Decrease key of x to -. Delete min element in heap.

Amortized cost. O(D(n)) O(1) for decrease-key. O(D(n)) for delete-min. D(n) = max degree of any node in Fibonacci heap.

51

THANK

YOU!