Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundamental Structures of Computer Science II

Similar presentations


Presentation on theme: "Fundamental Structures of Computer Science II"— Presentation transcript:

1 Fundamental Structures of Computer Science II
Priority Queues - 2 CS 202 – Fundamental Structures of Computer Science II Bilkent University Computer Engineering Department CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

2 Fundamental Structures of Computer Science II
Binomial Queues Binamial Queue A collection of heap-ordered trees (a forest). A tree in the forest is a binomial tree. There is at most one binomial tree of every height in the forest. A binomial tree of height 0 has one node. A binomial tree of height k has 2k nodes. A binomial tree of height k, Bk, is formed by attaching a binomial tree of height k-1, Bk-1’ to another binomial tree of height k-1, Bk-1 . CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

3 Fundamental Structures of Computer Science II
Binomial Trees B0 B1 B2 B3 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

4 Fundamental Structures of Computer Science II
B4 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

5 Fundamental Structures of Computer Science II
A binomial tree Bk consists of root with children B0, B1, …, Bk-1 Binomial trees of height k have exactly 2k nodes. The number of nodes at depth d is binomial coefficient CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

6 Fundamental Structures of Computer Science II
k = 4 d=0 d=1 d=2 d=3 d=4 B4 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

7 Heaps and binomial trees
Assume all binary trees in a forest are in heap order. Assume we have at most one binomial tree of any height. Then we can represent a heap of any size uniquely by a binomial tree forest. Example: heap of size 13 could be represented by forest: B3’ B2, B0, We call this heap ordered binomial forest a binomial queue. CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

8 Example H1 A binomial queue H1 of size 6 is shown above
16 12 18 21 24 65 H1 A binomial queue H1 of size 6 is shown above Can be represented as: 110 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

9 Binomial Queue Operations
FindMin Merge Insert DeleteMin CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

10 Fundamental Structures of Computer Science II
FindMin The minimum element in the binomial queue can be found by scanning the roots of all trees in the forest. There are at most logN different trees The cost of findMin is therefore O(logN) in the worst case. CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

11 Fundamental Structures of Computer Science II
Merge Merging two binomial queues is conceptually simple. Merge H1 and H2 Just add them in binary. Assume H1 has 6 nodes. H2 has 7 nodes. H3 = node (H1) + nodes(H3) = 13. H1: 0110 H2: 0111 H3: 1101 (this is the resulting bin. heap). CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

12 Fundamental Structures of Computer Science II
Merge - example 1 2 H1 16 12 18 21 24 65 13 14 23 H2 26 51 24 65 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

13 Fundamental Structures of Computer Science II
Merge - example - steps B0 H1 Merge 1 node tree with empty tree. The result is the one node tree itself. B0 H3 13 13 H2 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

14 Fundamental Structures of Computer Science II
Merge - example - steps B2 B1 H1 16 14 18 26 16 18 14 H2 26 H2 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

15 Fundamental Structures of Computer Science II
H1 12 21 24 12 65 23 21 24 23 H2 51 24 65 51 24 65 65 14 B3 H3 26 16 H3 B2 18 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

16 Fundamental Structures of Computer Science II
Final Binomial Queue H3 13 14 12 23 B0 26 16 21 24 51 24 18 65 65 B2 B3 H3 = 1101 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

17 Fundamental Structures of Computer Science II
Insert Insertion is a special case of merge Insert an item to a binomial heap H1 Make a new heap, H2, of one node (item to be inserted) Merge H1 and H2. Example Insert items 1, 2, 3, 4, 5, 6, 7, in the given order into an empty binomial heap. CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

18 Fundamental Structures of Computer Science II
1 1 3 1 2 2 After 1 is inserted After 2 is inserted After 3 is inserted 1 5 1 2 3 2 3 4 4 After 4 is inserted After 5 is inserted CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

19 Fundamental Structures of Computer Science II
5 1 6 2 3 4 After 6 is inserted 5 1 7 6 2 3 4 After 7 is inserted CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

20 Fundamental Structures of Computer Science II
DeleteMin Sketch of the Algorithm Assume we want to delete minimum item from binomial queue H. Find the binomial tree in H that has the minimum root. Let say this is Bk in H. Take tree Bk from H. Let the remaining trees in H make a new binomial queue H1 Remove the root from Bk (root is the minimum that you should return as a result of algorithm). The children of root of Bk make a new heap H2 that can consists of tree B0 through Bk-1 Merge H1 and H2. CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

21 Fundamental Structures of Computer Science II
DeletMin - Example 13 14 12 B0 23 26 16 21 24 51 24 18 65 B2 B3 65 H Delete the minimum item from the binomial queue above. CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

22 Fundamental Structures of Computer Science II
DeletMin - Example 13 14 12 B0 23 26 16 21 24 51 24 18 65 B2 B3 65 H The root that has the minimum item is 12 and belongs to tree B3 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

23 Fundamental Structures of Computer Science II
13 14 12 B0 23 26 16 21 24 51 24 18 65 B2 H B3 65 13 14 21 23 24 B0 B0 26 16 51 24 65 B1 18 65 B2 B2 MERGE H1 H2 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

24 Fundamental Structures of Computer Science II
13 14 21 24 23 B0 B0 26 16 51 24 65 B1 18 65 B2 B2 H1 H2 101 111 1100 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

25 Fundamental Structures of Computer Science II
13 14 21 24 23 B0 B0 26 16 51 24 65 B1 18 65 B2 B2 H1 H2 13 13 21 21 24 65 B2 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

26 Fundamental Structures of Computer Science II
14 13 23 26 16 51 24 21 24 18 65 65 13 23 51 24 21 24 14 B2 65 65 26 16 B3 Remaining Bino. Heap After DeleteMin 18 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University


Download ppt "Fundamental Structures of Computer Science II"

Similar presentations


Ads by Google