• Tidak ada hasil yang ditemukan

File Organization & Processing

N/A
N/A
Protected

Academic year: 2024

Membagikan "File Organization & Processing"

Copied!
54
0
0

Teks penuh

(1)

File Organization & Processing

Instructor

Maher Hadiji [email protected]

2016-2017 Computer Science Department

College of Computer at Al-Lith

Chapter 2:Advanced Heap Structures (Fibonacci Heaps)

(2)

Binomial Trees

• A binomial heap is a collection of binomial trees.

• The binomial tree Bk is an ordered tree defined recursively

Bo Consists of a single node ..

.

Bk Consists of two binominal trees Bk-1 linked together. Root of one is the leftmost child of the root of the other.

(3)

Binomial Trees

B

k-1

B

k-1

B

k
(4)

Binomial Trees

B0 B1 B2 B3

B1 B1 B2

B1

B4

B0

B3 B2

B1

B0

(5)

Binomial Trees

B

k-2

B

k-1

B2 B1 Bo

B

k
(6)

Properties of Binomial Trees

1. There are 2k nodes, 2. The height of tree is k,

3. There are exactly nodes at depth i for i = 0,1,..,k

4. The root has degree k > degree of any other node if the children of the root are numbered from left to right as k-1, k-2,...,0; child i is the root of a subtree Bi.

Only node with greater degree in Bk than those in Bk-1 is the root, The root of Bk has one more child than the root of Bk-1,

Degree of root Bk=Degree of Bk-1+1=(k-1)+1=k

k

i

(7)

Binomial Heaps

A BINOMIAL HEAP H is a set of BINOMIAL TREES that satisfies the following

“Binomial Heap Properties”

1. Each binomial tree in H is HEAP-ORDERED

• the key of a node is ≥ the key of the parent

• Root of each binomial tree in H contains the smallest key in that tree.

(8)

Fibonacci Heaps

• A Fibonacci heap is a collection of heap-ordered trees.

• Each tree is rooted but unordered.

• Each node x has pointed p[x] to its parent & child [x] to one of its children

• Children are linked together in a doubly-linked circular list.

• Doubly-linked circular list: Removing a node is O(1) and splicing two lists is O(1)

• Each node also has degree [x] indicating the number of children of x

• Each node also has mark [x], a boolean field indicating whether x has lost a child since the last time x was made the child of another node

• The entire heap is accessed by a pointer min [H] which points to the minimum-

key root

(9)

Fibonacci Heaps:Example

(10)

Nodes in Fibonacci Heaps

parent

child degree

key

mark

right left

(11)

7 23

30 17

35

26 46

24

39

18 52 41 3

44

Fibonacci Heaps: Structure

• Fibonacci heap.

• Set of heap-ordered trees.

• Maintain pointer to minimum element.

• Set of marked nodes.

roots heap-ordered tree

each parent greater than its children

(12)

7 23

30 17

35

26 46

24

18 52 41 3

44

Fibonacci Heaps: Structure

• Fibonacci heap.

• Set of heap-ordered trees.

• Maintain pointer to minimum element.

• Set of marked nodes.

min find-min takes O(1) time

(13)

7 23

30 17

35

26 46

24

Heap H

39

18 52 41 3

44

Fibonacci Heaps: Structure

• Fibonacci heap.

• Set of heap-ordered trees.

• Maintain pointer to minimum element.

• Set of marked nodes.

use to keep heaps flat (stay tuned) min

marked

(14)

Fibonacci Heaps: Notation

• Notation.

• n = number of nodes in heap.

• rank(x) = number of children of node x.

• rank(H) = max rank of any node in heap H.

• trees(H) = number of trees in heap H.

• marks(H) = number of marked nodes in heap H.

7 23

30 17

35

26 46

24

18 52 41 3

44 rank = 3

min

Heap H

trees(H) = 5 marks(H) = 3

marked

n = 14

(15)
(16)

Fibonacci Heaps: Insert

• Insert.

• Create a new singleton tree.

• Add to root list; update min pointer (if necessary).

7 23

30 17

35

26 46

24

39

41

18 52

3

44 21

insert 21

min

(17)

Fibonacci Heaps: Insert

• Insert.

• Create a new singleton tree.

• Add to root list; update min pointer (if necessary).

39

41 7

23

18 52

3

30 17

35

26 46

24

44 21

min

Heap H

insert 21

(18)

Linking Operation

• Linking operation. Make larger root be a child of smaller root.

39

41

18 52

3

77 44

56 24

15

tree T1 tree T2

39

41

18 52

3

44

77

56 24

15

tree T'

smaller root

larger root still heap-ordered

(19)

19

(20)
(21)

Fibonacci Heaps: Delete Min

• Delete min.

• Delete min; meld its children into root list; update min.

• Consolidate trees so that no two roots have same rank.

39

41

18 52

3

44 17

23

30 7

35

26 46

24

min

18

39

(22)

Fibonacci Heaps: Delete Min

• Delete min.

• Delete min; meld its children into root list; update min.

• Consolidate trees so that no two roots have same rank.

39

41 17

23 18 52

30 7

35

26 46

24

44

min

18

39

(23)

Fibonacci Heaps: Delete Min

• Delete min.

• Delete min; meld its children into root list; update min.

• Consolidate trees so that no two roots have same rank.

39

41 17

23 18 52

30 7

35

26 46

24

44

min current

(24)

Fibonacci Heaps: Delete Min

• Delete min.

• Delete min; meld its children into root list; update min.

• Consolidate trees so that no two roots have same rank.

39

41 17

23 18 52

30 7

26 46

24

44

0 1 2 3

current

min

rank

(25)

Fibonacci Heaps: Delete Min

• Delete min.

• Delete min; meld its children into root list; update min.

• Consolidate trees so that no two roots have same rank.

39

41 17

23 18 52

30 7

35

26 46

24

44

0 1 2 3

min current

rank

(26)

Fibonacci Heaps: Delete Min

• Delete min.

• Delete min; meld its children into root list; update min.

• Consolidate trees so that no two roots have same rank.

39

41 17

23 18 52

30 7

26 46

24

44

0 1 2 3

min

current

rank

(27)

Fibonacci Heaps: Delete Min

• Delete min.

• Delete min; meld its children into root list; update min.

• Consolidate trees so that no two roots have same rank.

39

41 17

23 18 52

30 7

35

26 46

24

44

0 1 2 3

min

current

rank

link 23 into 17

(28)

Fibonacci Heaps: Delete Min

• Delete min.

• Delete min; meld its children into root list; update min.

• Consolidate trees so that no two roots have same rank.

39

41 17

23

18 52

30 7

26 46

24

44

0 1 2 3

min

current

rank

link 17 into 7

(29)

Fibonacci Heaps: Delete Min

• Delete min.

• Delete min; meld its children into root list; update min.

• Consolidate trees so that no two roots have same rank.

39

41 7

30

18 52

17

35

26 46

24

44

0 1 2 3

23

current

min rank

link 24 into 7

(30)

Fibonacci Heaps: Delete Min

• Delete min.

• Delete min; meld its children into root list; update min.

• Consolidate trees so that no two roots have same rank.

39

41 7

30

18 52

23 17

26 46

24 44

0 1 2 3

min

current

rank

(31)

Fibonacci Heaps: Delete Min

• Delete min.

• Delete min; meld its children into root list; update min.

• Consolidate trees so that no two roots have same rank.

39

41 7

30

18 52

23 17

35

26 46

24 44

0 1 2 3

min

current

rank

(32)

Fibonacci Heaps: Delete Min

• Delete min.

• Delete min; meld its children into root list; update min.

• Consolidate trees so that no two roots have same rank.

39

41 7

30

18 52

23 17

26 46

24 44

0 1 2 3

min

current

rank

(33)

Fibonacci Heaps: Delete Min

• Delete min.

• Delete min; meld its children into root list; update min.

• Consolidate trees so that no two roots have same rank.

39

41 7

30

18 52

23 17

35

26 46

24 44

0 1 2 3

min

current

rank

link 41 into 18

(34)

Fibonacci Heaps: Delete Min

• Delete min.

• Delete min; meld its children into root list; update min.

• Consolidate trees so that no two roots have same rank.

41 39 7

30

18 52

23 17

26 46

24

44

0 1 2 3

min

current

rank

(35)

Fibonacci Heaps: Delete Min

• Delete min.

• Delete min; meld its children into root list; update min.

• Consolidate trees so that no two roots have same rank.

7

30

52

23 17

35

26 46

24

0 1 2 3

min rank

41 39

18

44 current

(36)

Fibonacci Heaps: Delete Min

• Delete min.

• Delete min; meld its children into root list; update min.

• Consolidate trees so that no two roots have same rank.

7

30

52

23 17

26 46

24

min

41 39

18

44

stop

(37)

Fibonacci Heaps: Decrease Key

• Intuition for deceasing the key of node x .

• If heap-order is not violated, just decrease the key of x.

• Otherwise, cut tree rooted at x and meld into root list.

• To keep trees flat: as soon as a node has its second child cut, cut it off and meld into root list (and unmark it).

24

46

17

30

23 7

88 26

21

52

39 18

41 38

72 35

min

marked node:

one child already cut

(38)

Fibonacci Heaps: Decrease Key

• Case 1. [heap order not violated]

• Decrease key of x.

• Change heap min pointer (if necessary).

24

46

17

30

23 7

88 26

21

52

39 18

41 38

72 29

35

min

x

decrease-key of x from 46 to 29

(39)

39

(40)

Fibonacci Heaps: Decrease Key

• Case 1. [heap order not violated]

• Decrease key of x.

• Change heap min pointer (if necessary).

24

29

17

30

23 7

88 26

21

52

39 18

41 38

72 35

min

x

decrease-key of x from 46 to 29

(41)

Fibonacci Heaps: Decrease Key

• Case 2a. [heap order violated]

• Decrease key of x.

• Cut tree rooted at x, meld into root list, and unmark.

• If parent p of xis unmarked (hasn't yet lost a child), mark it;

Otherwise, cut p, meld into root list, and unmark

(and do so recursively for all ancestors that lose a second child).

24

29

17

30

23 7

88 26

21

52

39 18

41 38

72 15

35

min

decrease-key of x from 29 to 15

p

x

(42)

Fibonacci Heaps: Decrease Key

• Case 2a. [heap order violated]

• Decrease key of x.

• Cut tree rooted at x, meld into root list, and unmark.

• If parent p of x is unmarked (hasn't yet lost a child), mark it;

Otherwise, cut p, meld into root list, and unmark

(and do so recursively for all ancestors that lose a second child).

24

15

17

30

23 7

88 26

21

52

39 18

41 38

72 35

min

decrease-key of x from 29 to 15

p

x

(43)

Fibonacci Heaps: Decrease Key

• Case 2a. [heap order violated]

• Decrease key of x.

• Cut tree rooted at x, meld into root list, and unmark.

• If parent p of x is unmarked (hasn't yet lost a child), mark it;

Otherwise, cut p,meld into root list, and unmark

(and do so recursively for all ancestors that lose a second child).

24 17

30

23 7

88 26

21

52

39 18

41 38

35

min

decrease-key of x from 29 to 15

p

15

72

x

(44)

Fibonacci Heaps: Decrease Key

• Case 2a. [heap order violated]

• Decrease key of x.

• Cut tree rooted at x, meld into root list, and unmark.

• If parent p of xis unmarked (hasn't yet lost a child), mark it;

Otherwise, cut p, meld into root list, and unmark

(and do so recursively for all ancestors that lose a second child).

24 17

30

23 7

88 26

21

52

39 18

41 38

35

min

decrease-key of x from 29 to 15

p

15

72

x

mark parent

24

(45)

35

Fibonacci Heaps: Decrease Key

• Case 2b. [heap order violated]

• Decrease key of x.

• Cut tree rooted at x, meld into root list, and unmark.

• If parent p of xis unmarked (hasn't yet lost a child), mark it;

Otherwise, cut p, meld into root list, and unmark

(and do so recursively for all ancestors that lose a second child).

24 15

17

30

23 7

88 26

21

52

39 18

41 38

72 24

5

min

x

p

decrease-key of x from 35 to 5

(46)

5

Fibonacci Heaps: Decrease Key

• Case 2b. [heap order violated]

• Decrease key of x.

• Cut tree rooted at x, meld into root list, and unmark.

• If parent p of x is unmarked (hasn't yet lost a child), mark it;

Otherwise, cut p, meld into root list, and unmark

(and do so recursively for all ancestors that lose a second child).

24 15

17

30

23 7

88 26

21

52

39 18

41 38

72 24

min

x

p

decrease-key of x from 35 to 5

(47)

Fibonacci Heaps: Decrease Key

• Case 2b. [heap order violated]

• Decrease key of x.

• Cut tree rooted at x, meld into root list, and unmark.

• If parent p of x is unmarked (hasn't yet lost a child), mark it;

Otherwise, cut p,meld into root list, and unmark (and do so recursively for all ancestors that lose a second child).

24 17

30

23 7

26

21

52

39 18

41 38

24 5

88 15

72

decrease-key of x from 35 to 5

x

p min

(48)

Fibonacci Heaps: Decrease Key

• Case 2b. [heap order violated]

• Decrease key of x.

• Cut tree rooted at x, meld into root list, and unmark.

• If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child).

24 17

30

23 7

26

21

52

39 18

41 38

24 5

88 15

72

decrease-key of x from 35 to 5

x

p

second child cut min

(49)

Fibonacci Heaps: Decrease Key

• Case 2b. [heap order violated]

• Decrease key of x.

• Cut tree rooted at x, meld into root list, and unmark.

• If parent p of xis unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child).

24 26

17

30

23 7

21

52

39 18

41 38

88 24

5 15

72

decrease-key of x from 35 to 5

x p

min

(50)

Fibonacci Heaps: Decrease Key

• Case 2b. [heap order violated]

• Decrease key of x.

• Cut tree rooted at x, meld into root list, and unmark.

• If parent p of xis unmarked (hasn't yet lost a child), mark it Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child).

24 26

17

30

23 7

21

52

39 18

41 38

88 24

5 15

72

decrease-key of x from 35 to 5

x p

p'

second child cut min

(51)

Fibonacci Heaps: Decrease Key

• Case 2b. [heap order violated]

• Decrease key of x.

• Cut tree rooted at x, meld into root list, and unmark.

• If parent p of xis unmarked (hasn't yet lost a child), mark it;

Otherwise, cut p, meld into root list, and unmark

(and do so recursively for all ancestors that lose a second child).

26

17

30

23 7

21

52

39 18

41 38

88 5

15 24

72

decrease-key of x from 35 to 5

x p p'

min

don't mark parent if it's a root

p''

(52)

Fibonacci Heaps: Union

• Union. Combine two Fibonacci heaps.

• Representation. Root lists are circular, doubly linked lists.

41 7

17

18 52

3

30 23

35

26 46

24

44

21

min min

Heap H' Heap H''

(53)
(54)

Fibonacci Heaps: Union

• Union. Combine two Fibonacci heaps.

• Representation. Root lists are circular, doubly linked lists.

39

41 7

17

18 52

3

30 23

35

26 46

24

44

21

min

Heap H

Referensi

Dokumen terkait