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 � Lazy Union + Cascading Cut � Decrease Key � Analysis Fibonacci Heaps http://www.cp.eng.chula.ac.th/faculty/spj

  2. Lazy Bino. Heap vs. Fibonacci Fibonacci Heaps • Make-Heap : π ( 1 ) : π ( 1 ) � Lazy union : π (1) • Insert : π ( 1 ) : π ( 1 ) � Cascading cut in Decrease-Key • Minimum : π ( 1 ) : π ( 1 ) � achieve π (1) amortized time in Decrease-Key • Extract-Min : ν ( log n ) * : ν ( log n ) * � maintain O(log n ) amortized time in Extract-Min • Union : π ( 1 ) : π ( 1 ) • Decrease-Key : π ( log n ) : π ( 1 ) * • Delete : π ( log n ) : O( log n ) * * amortized cost http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Fibonacci Heaps : Simple Cut Fibonacci Heaps : Simple Cut H H 20 2 5 20 2 5 21 9 32 20 21 9 32 20 11 13 40 4 13 40 18 18 x Fib-Heap-Decrease-Key( H, x, 4 ) http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj

  3. Fibonacci Heaps : Simple Cut Simple Cut Doesn�t Work H H 20 4 2 5 5 18 21 9 32 20 9 32 20 13 40 11 13 40 x 18 � Decrease-Key : Simple Cut is π (1). Fib-Heap-Delete( H, x ) � More than one tree of the same rank. { � Trees are not necessarily binomial trees Fib-Heap-Decrease-Key( H, x, -inf ) Fib-Heap-Extract-Min( H ) � Extract-Min is no longer amortized O( log n ). } http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Simple Cut Dosen�t Work Simple Cut Dosen�t Work H H Λ⊗ 5 5 9 32 20 9 32 20 11 13 40 11 13 40 Fib-Heap-Delete( H, x ) Fib-Heap-Delete( H, x ) { { Fib-Heap-Decrease-Key( H, x, -inf ) Fib-Heap-Decrease-Key( H, x, -inf ) Fib-Heap-Extract-Min( H ) Fib-Heap-Extract-Min( H ) } } http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj

  4. Simple Cut Dosen�t Work Lazy Binomial Heap : Analysis H � Invarient : one baht credited at each root node The rank of the root is 5 no longer O( log n ) for future consolidating task. 9 32 20 H 11 13 40 Fib-Heap-Delete( H, x ) { Fib-Heap-Decrease-Key( H, x, -inf ) Fib-Heap-Extract-Min( H ) } http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Lazy Binomial Heap : Analysis Lazy Binomial Heap : Analysis � Insert : amortized cost = 2 � Extract-Min : amortized cost = O( log n ) H H � one for list insertion + one stored at the root http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj

  5. Lazy Binomial Heap : Analysis Lazy Binomial Heap : Analysis � Extract-Min : amortized cost = O( log n ) � Extract-Min : amortized cost = O( log n ) O( log n ) children H consolidate Delete minimum root using the credit stored at the root http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Simple Cut Doesn�t Work Cascading Cut rank is π ( n ) Extract-Min � Guarantees any node rank to be O( log n ) � Extract-Min : O( log n ) delete root Amortized cost � Decrease-Key : π ( 1 ) consolidate costs π ( n ) To maintain O( log n ) Extract-Min, node rank must be O( log n ) http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj

  6. Marking Nodes Cascading Cuts H � Mark a (nonroot) node the first time that it loses 20 2 4 a child (because of a simple cut). 12 21 5 19 31 12 � If a marked node loses another child, then cut it 30 22 33 45 9 32 20 from its parent causing cascading cut 7 11 13 40 28 18 Fib-Heap-Decrease-Key( H, x, 7 ) http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Cascading Cuts Cascading Cuts H H 2 8 2 20 7 4 20 7 4 18 12 21 5 18 12 21 5 19 31 12 19 31 12 30 30 22 33 45 22 33 45 9 32 20 9 32 8 11 13 40 28 13 40 28 Fib-Heap-Decrease-Key( H, x, 8 ) Fib-Heap-Decrease-Key( H, x, 11 ) http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj

  7. Cascading Cuts Cascading Cuts H H 8 11 2 8 11 9 2 20 7 4 20 7 4 12 21 12 21 18 5 18 5 19 31 12 19 31 12 30 30 22 33 45 22 33 45 9 32 32 40 28 40 28 http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Cascading Cuts Cascading Cuts : Two Issues H � Why is Decrease-Key Θ (1) ? 8 11 9 5 2 20 7 4 12 21 � Why is any node rank O( log n ) ? 18 32 19 31 12 40 30 22 33 45 � i.e., Extract-Min is O( log n ) 28 http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj

  8. Decrease-Key is Θ (1) Decrease-Key is Θ (1) � Let w be a cut node � Let w be a cut node � The amortized cost of Decrease-Key is 4 � The amortized cost of Decrease-Key is 4 1 1 1 w ... � two is credited at w �s parent node � two is credited at w �s parent node A 2 2 � one is for a simple cut at node w � one is for a simple cut at node w w A � one is credited at w after being a new root One baht at each root node will be used during consolidation. http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Decrease-Key is Θ (1) Decrease-Key is Θ (1) x 1 0 1 A 0 0 z z 2 0 2 +2 x 0 0 y 0 0 y 0 A B C B C Cut( x ) Cut( x ) amortized cost : (2 amortized cost : (2+1 actual cost : actual cost : (1 http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj

  9. Decrease-Key is Θ (1) Decrease-Key is Θ (1) +1 0 x 1 x 1 1 1 A A 0 0 z z 2 2 y 0 y 0 0 0 B C B C Cut( x ) Cut( y ) amortized cost : (2+1+1 amortized cost : (2+1+1)+ actual cost : (1 actual cost : (1)+ http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Decrease-Key is Θ (1) Decrease-Key is Θ (1) 1 x 1 x y 1 0 1 A A C 0 0 z z 4 2 4 +2 y 0 0 0 B C B Cut( y ) Cut( y ) amortized cost : (2+1+1)+(2+ amortized cost : (2+1+1)+(2+1 actual cost : (1)+ actual cost : (1)+(1 http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj

  10. Decrease-Key is Θ (1) Decrease-Key is Θ (1) 1 x 1 x y y 1 0 1 1 1 A C A C +2 0 2 0 z z +1 4 4 2 0 0 B B Cut( y ) Cut( y ) -> Cut( z ) amortized cost : (2+1+1)+(2+1+1 amortized cost : (2+1+1)+(2+1+1) actual cost : (1)+(1 actual cost : (1)+(1 http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Decrease-Key is Θ (1) Money at Marked Nodes 1 x z y � Four bahts are used to cut node w whose parent 1 1 1 A C 2 is node v 0 � two bahts are transferred to v B � one baht is for the cut � one baht is stored on w for future consolidation Cut( y ) -> Cut( z ) v 2 amortized cost : (2+1+1)+(2+1+1) w actual cost : (1)+(1+1) 0 http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj

  11. Money at Marked Nodes Money at Marked Nodes � If node v loses only one child, it has two bahts � If node v loses two children, it has four bahts � two bahts are tranferred to its parent (may cause � one baht is for future cut (caused by cascading cut) further cut). � the other baht is for future consolidation � one baht is for cutting the node � another baht is for future consolidation v 1 1 1 1 2 v 0 2 2 v v 4 2 http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Ranks of Children Ranks of Chlidren x x Rank of x is i ... v i -1 ... v 3 v 2 v 1 v i Rank of v 1 ≥ 0 Rank of v i -1 ≥ i -3 Rank of v 1 ≥ 0 Rank of v 2 ≥ 0 Rank of v i ≥ i -2 Rank of v 3 ≥ 1 http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj

  12. Ranks of Chlidren Ranks of Chlidren x x Rank of v 1 ≥ 0 Rank of v 1 ≥ 0 http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Ranks of Chlidren Ranks of Chlidren x x Rank of x ≥ i -1 Rank of x is i ... v i -1 ... v 3 v 2 v 1 v i v i Rank of v 1 ≥ 0 a node could have lost at most one child Rank of v i -1 ≥ i -3 Rank of v 2 ≥ 0 Rank of v i ≥ i -2 Rank of v i ≥ i -1 i -2 Rank of v 3 ≥ 1 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