design and analysis of algorithms
play

Design and Analysis of Algorithms - PowerPoint PPT Presentation

Design and Analysis of Algorithms


  1. �������� Design and Analysis of Algorithms ����������������������� ������ �����ก��� ������ ��� ��������� ���������������������� �������������������� ��. ��. ����� ��������������ก�� ������������������������������ก������� ������ก�� �����������ก�������������� ��������������� ������������กก�������ก��������� ������ก�������������� 2542 http://www.cp.eng.chula.ac.th/faculty/spj Outline Advanced Data Structures � Priority Queue : ADT � Binary Heap � Binomial Trees Binomial Heaps � Binomial Heaps � Operations http://www.cp.eng.chula.ac.th/faculty/spj

  2. Priority Queue : ADT Binary Heap 4 • Make-Heap() • Insert( H, x ) 10 30 π ( log n ) • Minimum( H ) 14 20 43 100 • Extract-Min( H ) 17 • Union( H 1 , H 2 ) 4 10 30 14 20 43 100 17 • Decrease-Key( H, x, k ) • Delete( H, x ) http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Binary Heap : Insert Binary Heap : Insert 4 4 Bubble up 10 30 7 30 π ( log n ) 14 20 43 100 10 20 43 100 17 7 17 14 4 10 30 14 20 43 100 17 7 4 7 30 10 20 43 100 17 14 http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj

  3. Binary Heap : Extract-Min Binary Heap : Extract-Min 4 4 7 30 7 30 10 20 43 100 10 20 43 100 17 14 17 14 4 7 30 10 20 43 100 17 14 4 7 30 10 20 43 100 17 14 http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Binary Heap : Extract-Min Binary Heap : Extract-Min 14 7 Percolate down 10 7 30 30 π ( log n ) 10 20 43 100 14 20 43 100 17 17 14 7 30 10 20 43 100 17 7 10 30 14 20 43 100 17 http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj

  4. Binary Heap : Union Binary Heap : Union 7 4 7 10 50 10 30 23 30 14 20 43 100 14 20 43 100 17 17 4 50 23 7 10 30 14 20 43 100 17 4 50 23 7 10 30 14 20 43 100 17 4 50 23 http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Binary Heap : Union Binary Heap : Union 7 7 10 10 30 30 14 20 43 100 14 20 43 100 17 4 50 23 17 4 50 23 http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj

  5. Binary Heap : Union Binary Heap : Union 7 7 10 10 30 30 14 20 43 100 4 20 43 100 17 4 50 23 17 14 50 23 http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Binary Heap : Union Binary Heap : Union 7 7 10 4 30 30 4 20 43 100 10 20 43 100 17 14 50 23 17 14 50 23 http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj

  6. Binary Heap : Union Binary Heap : Union : Analysis n 4 x h 2 h 7 lg n 30 � � n n ∴ � � x 3 � O ( h ) O ( n ) 2 3 h � 2 � 10 20 43 100 h 0 ∴ n x 2 2 2 17 14 50 23 n x 1 2 1 http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Binary Heap & Binomial Heap Binomial Trees B 0 B 1 B 2 B 3 B 4 • Make-Heap : π ( 1 ) : π ( 1 ) • Insert : π ( log n ) : ν ( log n ) • Minimum : π ( 1 ) : ν ( log n ) • Extract-Min : π ( log n ) : π ( log n ) B k • Union : ν ( n ) : ν ( log n ) • Decrease-Key : π ( log n ) : π ( log n ) • Delete : π ( log n ) : π ( log n ) B k -1 B k -1 http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj

  7. Properties of Binomial Trees Properties of Binomial Trees B 4 # nodes Root of B 4 has degree 4 � For the binomial tree B k 1 = 4 C 0 � there are 2 k nodes 4 = 4 C 1 Binomial 6 = 4 C 2 � the height of the tree is k Coef. 4 = 4 C 3 � there are exactly k C i nodes at depth i 1 = 4 C 4 � the root has degree k The height of B 4 is 4 2 4 � The max. degree of any node in an n -node binomial tree is lg n http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Binomial Heaps Heap-Ordered Property � A list of binomial trees satisfying these properties 20 2 5 � each binomial tree is heap-ordered H � (the root of a tree contains the smallest key in the tree) 12 21 9 32 20 � there is at most one B k for any given k in the heap � (an n -node binomial heap consists of at most 1 + lg n 30 11 13 40 binomial trees) 18 http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj

  8. Structural Property Binomial Heaps: Minimum B 0 B 2 B 3 20 20 20 2 2 5 H 12 21 9 32 20 B 3 B 2 B 0 30 11 13 40 H = ( B 3 , B 2 , B 0 ) has 2 3 + 2 2 + 2 0 = 13 10 = 1101 2 nodes has 4 bits The binary representation of 13 10 18 The binary representation of n has (1 + lg n ) bits π ( #trees )= O( 1 + lg n ) = O( log n ) An n -node binomial heap has at most (1 + lg n ) binomial trees http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Binomial Heap : Union Binomial Heap : Union 20 2 5 2 5 H 1 H 1 12 21 9 32 20 12 21 9 32 20 20 30 11 13 40 30 11 13 40 18 18 10 10 8 8 H 2 H 2 31 12 31 12 44 44 http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj

  9. Binomial Heap : Union Binomial Heap : Union 10 2 5 2 5 H 1 H 1 20 12 21 9 32 20 12 21 9 32 20 B 2 30 11 13 40 30 11 13 40 18 18 10 8 8 20 H 2 B 1 H 2 31 12 31 12 B 2 44 44 http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Binomial Heap : Union Binomial Heap : Union 10 10 5 5 2 H 1 H 1 20 20 9 32 20 9 32 20 12 21 11 13 40 11 13 40 30 2 B 3 18 18 8 8 12 21 31 12 31 12 30 H 2 H 2 44 44 B 3 http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj

  10. Binomial Heap : Union Binomial Heap : Union 5 10 10 H 1 H 1 20 20 9 32 20 11 13 40 2 2 18 5 8 8 12 21 12 21 9 32 20 31 12 31 12 30 30 H 2 H 2 11 13 40 44 44 18 http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Binomial Heap : Union Binomial Heap Union : Analysis ≤ 1+lg n 1 roots ≤ 1+lg n 2 roots 2 10 ... ... H 1 20 5 8 12 21 H 1 H 2 n 1 nodes n 2 nodes 9 32 20 31 12 30 Binomial_Heap_Union 11 13 40 44 ≤ 2+lg n 1 + lg n 2 < 2+ lg n + lg n = O( log n ) 18 ... H n = n 1 + n 2 nodes http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj

  11. Binomial Heap Union : Analysis Binomial Heap Union : Analysis B 0 B 2 B 3 B 0 B 2 � T 1 : Union H 1 and H 2 of size n 1 and n 2 , respectively. � T 2 : Carry-ripple adding numbers n 1 and n 2 in binary H 1 H 2 � T 1 = O( T 2 ) 13 nodes 5 nodes B 0 B 2 B 3 Binomial_Heap_Union � Let n = n 1 + n 2 ⇒ 1 + lg n bits H 1 � T 2 = Θ ( 1 + lg n ) = Θ ( log n ) B 1 B 4 1 1 0 1 1 0 1 � T 1 = O( log n ) H 1 1 0 1 1 0 0 1 0 18 nodes http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Binomial Heap : Insert Building Binomial Heap Binomial_Heap_Insert � Build an n -node binomial heap by n successive ... insertions H H * � An insertion costs O( log n ) Binomial_Heap_Union � n insertions cost O( n log n ) � Correct but not tight ! ... O( log n ) H http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj

  12. Building Binomial Heap Building Binomial Heap H H 0 0 0 1 0 0 b 0 b 1 b 2 b 0 b 1 b 2 http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Building Binomial Heap Building Binomial Heap H H 0 1 0 1 1 0 b 0 b 1 b 2 b 0 b 1 b 2 http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj

  13. Building Binomial Heap Building Binomial Heap H H 0 0 1 1 0 1 b 0 b 1 b 2 b 0 b 1 b 2 http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Building Binomial Heap Building Binomial Heap H H 0 1 1 1 1 1 b 0 b 1 b 2 b 0 b 1 b 2 http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj

  14. Building Binomial Heap Binomial Heap : Extract Min. � Calling binary counter Increment s n times cost 2 10 H O( n ) in worst case (remember ?) 20 5 8 12 21 � Building an n -node binomial heap using n 9 32 20 31 12 30 successive insertions is O( n ) time complexity 11 13 40 44 � Later we will show that the amortized time of 18 Binomial-Heap-Insert is Θ (1) Minimum : O( log n ) http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Binomial Heap : Extract Min. Binomial Heap : Extract Min. 10 10 Union : O( log n ) H H 20 20 5 8 12 21 H * 21 5 9 32 20 31 12 30 H Delete min. root : Θ (1) 8 9 32 20 11 13 40 44 21 12 8 5 H * 10 31 12 11 13 40 18 � the root of B k has degree k 30 31 12 9 32 20 � k is Θ ( log n ) worst case 12 20 44 18 44 11 13 40 � Θ ( log n ) children 30 18 � Reverse list : Θ ( log n ) http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend