priority queue heap -...

51
Algorithm Theory, WS 2012/13 Fabian Kuhn 1 Priority Queue / Heap Stores (key,data) pairs (like dictionary) But, different set of operations: InitializeHeap: creates new empty heap IsEmpty: returns true if heap is empty Insert(key,data): inserts (key,data)pair, returns pointer to entry GetMin: returns (key,data)pair with minimum key DeleteMin: deletes minimum (key,data)pair DecreaseKey(entry,newkey): decreases key of entry to newkey Merge: merges two heaps into one

Upload: others

Post on 04-Jan-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 1

Priority Queue / Heap• Stores (key,data) pairs (like dictionary)• But, different set of operations:

• Initialize‐Heap: creates new empty heap• Is‐Empty: returns true if heap is empty• Insert(key,data): inserts (key,data)‐pair, returns pointer to entry • Get‐Min: returns (key,data)‐pair with minimum key• Delete‐Min: deletes minimum (key,data)‐pair• Decrease‐Key(entry,newkey): decreases key of entry to newkey• Merge: merges two heaps into one

Page 2: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 2

Implementation of Dijkstra’s AlgorithmStore nodes in a priority queue, use  , as keys:

1. Initialize  , 0 and  , ∞ for all 2. All nodes are unmarked

3. Get unmarked node  which minimizes  , :

4. mark node 

5. For all  , ∈ ,  , min , , ,

6. Until all nodes are marked

Page 3: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 3

AnalysisNumber of priority queue operations for Dijkstra:

• Initialize‐Heap:

• Is‐Empty:

• Insert:

• Get‐Min:

• Delete‐Min:

• Decrease‐Key:

• Merge:

| |

| |

| |

| |

| |

Page 4: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 4

Priority Queue ImplementationImplementation as min‐heap:

complete binary tree,e.g., stored in an array

• Initialize‐Heap:

• Is‐Empty:

• Insert:

• Get‐Min:

• Delete‐Min:

• Decrease‐Key:

• Merge (heaps of size  and  ,  ):

Page 5: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 5

Better Implementation• Can we do better?

• Cost of Dijkstra with complete binary min‐heap implementation:

log

• Can be improved if we can make decrease‐key cheaper…

• Cost of merging two heaps is expensive

• We will get there in two steps:

Binomial heap  Fibonacci heap

Page 6: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 6

Definition: Binomial TreeBinomial tree  of order  0 :

Page 7: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 7

Binomial Trees

Page 8: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 8

Properties1. Tree  has 2 nodes

2. Height of tree  is 

3. Root degree of  is 

4. In  , there are exactly  nodes at depth 

Page 9: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 9

Binomial Coefficients• Binomial coefficient:

: #of element subsetsofasetofsize

• Property: Pascal triangle:

Page 10: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 10

Number of Nodes at Depth  in Claim: In  , there are exactly  nodes at depth 

Page 11: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 11

Binomial Heap• Keys are stored in nodes of binomial trees of different order

nodes: there is a binomial tree  or order  iffbit  of base‐2 representation of  is 1.

• Min‐Heap Property:

Key of node  keys of all nodes in sub‐tree of 

Page 12: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 12

Example• 10 keys:  2, 5, 8, 9, 12, 14, 17, 18, 20, 22, 25

• Binary representation of  :  11 1011 trees  ,  , and  present

14

25

5

20

12

22

9

18

2

8

17

Page 13: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 13

Child‐Sibling Representation

Structure of a node:parent

key degree

child sibling

Page 14: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 14

Link Operation• Unite two binomial trees of the same order to one tree:

⨁ ⇒

• Time: 

12

18

20

15

22

40

25

30

Page 15: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 15

Merge OperationMerging two binomial heaps:• For  , , … , :

If there are 2 or 3 binomial trees  : apply link operation to merge 2 trees into one binomial tree 

Time:

Page 16: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 16

Example

14

25

5

2012

22

9

18

2

8

1713

Page 17: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 17

OperationsInitialize: create empty list of trees

Get minimum of queue: time  1 (if we maintain a pointer)

Decrease‐key at node  :• Set key of node  to new key• Swap with parent until min‐heap property is restored• Time:  log

Insert key  into queue  :1. Create queue  of size 1 containing only 2. Merge  and 

• Time for insert:  log

Page 18: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 18

OperationsDelete‐Min Operation:

1. Find tree  with minimum root 

2. Remove  from queue  queue  ′

3. Children of  form new queue  ′′

4. Merge queues  ′ and  ′′

• Overall time: 

Page 19: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 19

Delete‐Min Example

14

25

2

20

12

22

9

18

5

8

17

Page 20: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 20

Complexities Binomial Heap

• Initialize‐Heap:

• Is‐Empty:

• Insert:

• Get‐Min:

• Delete‐Min:

• Decrease‐Key:

• Merge (heaps of size  and  ,  ):

Page 21: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 21

Can We Do Better?• Binomial heap:

insert, delete‐min, and decrease‐key cost  log

• One of the operations insert or delete‐min must cost Ω log :– Heap‐Sort:

Insert  elements into heap, then take out the minimum  times– (Comparison‐based) sorting costs at least Ω log .

• But maybe we can improve decrease‐key and one of the other two operations?

• Structure of binomial heap is not flexible:– Simplifies analysis, allows to get strong worst‐case bounds– But, operations almost inherently need at least logarithmic time

Page 22: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 22

Fibonacci HeapsLacy‐merge variant of binomial heaps:• Do not merge trees as long as possible…

Structure:A Fibonacci heap  consists of a collection of trees satisfying the min‐heap property.

Variables:• . : root of the tree containing the (a) minimum key• . : circular, doubly linked, unordered list containing

the roots of all trees• . : number of nodes currently in 

Page 23: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 23

Trees in Fibonacci HeapsStructure of a single node  :

• . : points to circular, doubly linked and unordered list ofthe children of 

• . ,  . : pointers to siblings (in doubly linked list)• . : will be used later…

Advantages of circular, doubly linked lists:• Deleting an element takes constant time• Concatenating two lists takes constant time

left

parent

rightkey degree

child mark

Page 24: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 24

Example

Figure: Cormen et al., Introduction to Algorithms

Page 25: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 25

Simple (Lazy) OperationsInitialize‐Heap : • . ≔ . ≔

Merge heaps  and  ′:• concatenate root lists• update  .

Insert element  into  :• create new one‐node tree containing  H′• merge heaps  and  ′

Get minimum element of  :• return  .

Page 26: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 26

Operation Delete‐MinDelete the node with minimum key from  and return its element:

1. ≔ . ;2. if . 0 then3. remove  . from  . ;4. add  . . (list) to  .5. . ;

// Repeatedly merge nodes with equal degree in the root list// until degrees of nodes in the root list are distinct.// Determine the element with minimum key

6. return

Page 27: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 27

Rank and Maximum DegreeRanks of nodes, trees, heap:

Node  :• : degree of 

Tree  :• : rank (degree) of root node of 

Heap  :• : maximum degree of any node in 

Assumption ( : number of nodes in  ):

– for a known function 

Page 28: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 28

Merging Two TreesGiven: Heap‐ordered trees  ,  ′ with 

• Assume: min‐key of  min‐key of  ′

Operation  , :• Removes tree  ′ from root list

and adds  to child list of 

• ≔ 1• . ≔

Page 29: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 29

Consolidation of Root ListArray  pointing to find roots with the same rank:

Consolidate:1. for ≔ 0 to do ≔ null;2. while  . null do3. ≔ “delete and return first element of  . ”4. while  null do5. ≔ ;6. ≔ ;7. ≔ ,8. ≔9. Create new  . and  .

0 1 2

| .Time:| .

Page 30: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 30

Consolidate Example

14

255

20

12 22

9

18

2

8

171

13

15 3

71931

14

255

20

12 22

9

18

2

8

171

13

15 3

719

31

Page 31: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 31

Consolidate Example

14

255

20

12 22

9

18

2

8

171

13

15 3

71931

14 25

5

20

12 22

9

18

2

8

171

13

15 3

719

31

Page 32: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 32

Consolidate Example

14 25

5

20

12 22

9

18

2

8

171

13

15

3

71931

14 25

5

20

12 22

9

18

2

8

171

13

15 3

719

31

Page 33: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 33

Consolidate Example

14

255

20

12 22

9

18

2

8

171

13

15

3

71931

14 25

5

20

12 22

9

18

2

8

171

13

15

3

71931

Page 34: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 34

Consolidate Example

14

255

20

12 22

9

18

2

8 17

1

13

15

3

71931

14

255

20

12 22

9

18

2

8

171

13

15

3

71931

Page 35: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 35

Consolidate Example

14

255

20

12 22

9

18

2

8

17

1

13 153

71931

14

255

20

12 22

9

18

2

8 17

1

13

15

3

71931

Page 36: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 36

Operation Decrease‐KeyDecrease‐Key , : (decrease key of node  to new value  )

1. if . then return;2. . ≔ ; update  . ;3. if  ∈ . ∨ . . then return4. repeat5. ≔ . ;6. . ;7. ≔ ;8. until  . ∨ ∈ . ;9. if  ∉ . then  . ≔ ;

Page 37: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 37

Operation Cut( )Operation  . :• Cuts  ’s sub‐tree from its parent and adds  to rootlist

1. if  ∉ . then2. // cut the link between  and its parent3. . ≔ . 1;4. remove  from  . . (list)5. . ≔ null;6. add  to  .

25

2

8

1

13

15

3

71931

25

2

8

1

13

153

719

31

Page 38: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 38

Decrease‐Key Example• Green nodes are marked

14 25

5

20

12

22

9

18

2

8

171

13

15

3

71931

Decrease‐Key ,

14 25

5

20 1222

918 2

8

171

13

15

3

71931

Page 39: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 39

Fibonacci Heap MarksHistory of a node  :

is being linked to a node                          . ≔

a child of  is cut                                           . ≔

a second child of  is cut                             .

• Hence, the boolean value  . indicates whether node has lost a child since the last time  was made the child of another node.

Page 40: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 40

Cost of Delete‐Min & Decrease‐KeyDelete‐Min:1. Delete min. root  and add  . to  .

time: 12. Consolidate  .

time: lengthof .• Step 2 can potentially be linear in  (size of  )

Decrease‐Key (at node  ):1. If new key  parent key, cut sub‐tree of node 

time: 12. Cascading cuts up the tree as long as nodes are marked

time: numberofconsecutivemarkednodes• Step 2 can potentially be linear in 

Exercises: Both operations can take  time in the worst case!

Page 41: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 41

Cost of Delete‐Min & Decrease‐Key• Cost of delete‐min and decrease‐key can be Θ …

– Seems a large price to pay to get insert and merge in  1 time

• Maybe, the operations are efficient most of the time?– It seems to require a lot of operations to get a long rootlist and thus, 

an expensive consolidate operation– In each decrease‐key operation, at most one node gets marked:

We need a lot of decrease‐key operations to get an expensive decrease‐key operation

• Can we show that the average cost per operation is small?

• We can  requires amortized analysis

Page 42: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 42

Amortization• Consider sequence  , , … , of  operations

(typically performed on some data structure  )

• : execution time of operation • ≔ ⋯ : total execution time

• The execution time of a single operation might

vary within a large range (e.g.,  ∈ 1, )

• The worst case overall execution time might still be small

average execution time per operation might be small inthe worst case, even if single operations can be expensive

Page 43: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 43

Analysis of Algorithms• Best case

• Worst case

• Average case

• Amortized worst case

What it the average cost of an operationin a worst case sequence of operations?

Page 44: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 44

Example: Binary CounterIncrementing a binary counter: determine the bit flip cost:

Operation Counter Value Cost

00000

1 00001 1

2 00010 2

3 00011 1

4 00100 3

5 00101 1

6 00110 2

7 00111 1

8 01000 4

9 01001 1

10 01010 2

11 01011 1

12 01100 3

13 01101 1

Page 45: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 45

Accounting MethodObservation:• Each increment flips exactly one 0 into a 1

00100 1111 ⟹ 00100 0000

Idea:• Have a bank account (with initial amount 0)• Paying  to the bank account costs • Take “money” from account to pay for expensive operations

Applied to binary counter:• Flip from 0 to 1: pay 1 to bank account (cost: 2)• Flip from 1 to 0: take 1 from bank account (cost: 0)• Amount on bank account = number of ones

We always have enough “money” to pay!

Page 46: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 46

Accounting Method

Op. Counter Cost To Bank From Bank Net Cost Credit0 0 0 0 0

1 0 0 0 0 1 12 0 0 0 1 0 23 0 0 0 1 1 14 0 0 1 0 0 35 0 0 1 0 1 16 0 0 1 1 0 27 0 0 1 1 1 18 0 1 0 0 0 49 0 1 0 0 1 110 0 1 0 1 0 2

Page 47: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 47

Potential Function Method• Most generic and elegant way to do amortized analysis!

– But, also more abstract than the others…

• State of data structure / system:  ∈ (state space)

Potential function  : →

• Operation  :– : actual cost of operation – : state after execution of operation  ( : initial state)– ≔ Φ : potential after exec. of operation – : amortized cost of operation  :

Page 48: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 48

Potential Function MethodOperation  :

actual cost:  amortized cost:  Φ ΦOverall cost:

≔ Φ Φ

Page 49: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 49

Binary Counter: Potential Method• Potential function:

:

• Clearly, Φ 0 and Φ 0 for all  0

• Actual cost  :  1 flip from 0 to 1 1 flips from 1 to 0

• Potential difference: Φ Φ 1 1 2

• Amortized cost:  Φ Φ 2

Page 50: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 50

Back to Fibonacci Heaps• Worst‐case cost of a single delete‐min or decrease‐key 

operation is Ω

• Can we prove a small worst‐case amortized cost fordelete‐min and decrease‐key operations?

Remark:• Data structure that allows operations  , … ,

• We say that operation  has amortized cost  if for every execution the total time is

⋅ ,

where  is the number of operations of type 

Page 51: Priority Queue Heap - uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algotheo/...Algorithm Theory, WS 2012/13 Fabian Kuhn 2 Implementation of Dijkstra’sAlgorithm Store

Algorithm Theory, WS 2012/13 Fabian Kuhn 51

Amortized Cost of Fibonacci Heaps• Initialize‐heap, is‐empty, get‐min, insert, and merge

have worst‐case cost 

• Delete‐min has amortized cost • Decrease‐key has amortized cost 

• Starting with an empty heap, any sequence of  operations with at most  delete‐min operations has total cost (time)

.

• Cost for Dijkstra:  | | | | log | |