Data Structures Fibonacci Heaps, Union Find Algorithm Theory WS - - PowerPoint PPT Presentation

data structures
SMART_READER_LITE
LIVE PREVIEW

Data Structures Fibonacci Heaps, Union Find Algorithm Theory WS - - PowerPoint PPT Presentation

Chapter 4 Data Structures Fibonacci Heaps, Union Find Algorithm Theory WS 2012/13 Fabian Kuhn Fibonacci Heaps: Marks Cycle of a node: 1. Node is removed from root list and linked to a node . 2. Child node


slide-1
SLIDE 1

Chapter 4

Data Structures

Fibonacci Heaps, Union Find

Algorithm Theory WS 2012/13 Fabian Kuhn

slide-2
SLIDE 2

Algorithm Theory, WS 2012/13 Fabian Kuhn 2

Fibonacci Heaps: Marks

Cycle of a node:

  • 1. Node is removed from root list and linked to a node

.

  • 2. Child node of is cut and added to root list

.

  • 3. Second child of is cut

node is cut as well and moved to root list .

  • The boolean value . indicates whether node has lost a

child since the last time was made the child of another node.

slide-3
SLIDE 3

Algorithm Theory, WS 2012/13 Fabian Kuhn 3

Potential Function

System state characterized by two parameters:

  • : number of trees (length of . )
  • : number of marked nodes that are not in the root list

Potential function: ≔ Example:

  • 7, 2  Φ 11

14 25 5 20 12 22 9 18 2 8 17 1 13 15 3 7 19 31

slide-4
SLIDE 4

Algorithm Theory, WS 2012/13 Fabian Kuhn 4

Actual Time of Operations

  • Operations: initialize‐heap, is‐empty, insert, get‐min, merge

actual time: 1

– Normalize unit time such that

, , , , 1

  • Operation delete‐min:

– Actual time: length of . – Normalize unit time such that

length of .

  • Operation descrease‐key:

– Actual time: length of path to next unmarked ancestor – Normalize unit time such that

length of path to next unmarked ancestor

slide-5
SLIDE 5

Algorithm Theory, WS 2012/13 Fabian Kuhn 5

Amortized Times

Assume operation is of type:

  • initialize‐heap:

– actual time: 1, potential: Φ Φ 0 – amortized time: Φ Φ 1

  • is‐empty, get‐min:

– actual time: 1, potential: Φ Φ (heap doesn’t change) – amortized time: Φ Φ 1

  • merge:

– Actual time: 1 – combined potential of both heaps: Φ Φ – amortized time: Φ Φ 1

slide-6
SLIDE 6

Algorithm Theory, WS 2012/13 Fabian Kuhn 6

Amortized Time of Insert

Assume that operation is an insert operation:

  • Actual time: 1
  • Potential function:

– remains unchanged (no nodes are marked or unmarked, no marked nodes are moved to the root list) – grows by 1 (one element is added to the root list) , 1 Φ Φ 1

  • Amortized time:
slide-7
SLIDE 7

Algorithm Theory, WS 2012/13 Fabian Kuhn 7

Amortized Time of Delete‐Min

Assume that operation is a delete‐min operation: Actual time: . Potential function :

  • : changes from . to at most
  • : (# of marked nodes that are not in the root list)

– no new marks – if node is moved away from root list, . is set to false  value of does not change!

, . Φ Φ |. | Amortized Time:

slide-8
SLIDE 8

Algorithm Theory, WS 2012/13 Fabian Kuhn 8

Amortized Time of Decrease‐Key

Assume that operation is a decrease‐key operation at node : Actual time: length of path to next unmarked ancestor Potential function :

  • Assume, node and nodes , … , are moved to root list

– , … , are marked and moved to root list, . mark is set to true

  • marked nodes go to root list, 1 node gets newly marked
  • grows by 1, grows by 1 and is decreased by

1, 1 Φ Φ 1 2 1 Φ 3 Amortized time:

slide-9
SLIDE 9

Algorithm Theory, WS 2012/13 Fabian Kuhn 9

Complexities Fibonacci Heap

  • Initialize‐Heap:
  • Is‐Empty:
  • Insert:
  • Get‐Min:
  • Delete‐Min:
  • Decrease‐Key:
  • Merge (heaps of size and , ):
  • How large can get?
  • amortized
slide-10
SLIDE 10

Algorithm Theory, WS 2012/13 Fabian Kuhn 10

Rank of Children

Lemma: Consider a node of rank and let , … , be the children of in the order in which they were linked to . Then, . Proof:

slide-11
SLIDE 11

Algorithm Theory, WS 2012/13 Fabian Kuhn 11

Size of Trees

Fibonacci Numbers:

  • 0,
  • 1,

∀ 2:

  • Lemma:

In a Fibonacci heap, the size of the sub‐tree of a node with rank is at least

.

Proof:

  • : minimum size of the sub‐tree of a node of rank
slide-12
SLIDE 12

Algorithm Theory, WS 2012/13 Fabian Kuhn 12

Size of Trees

1, 2, ∀ 2: 2

  • Claim about Fibonacci numbers:

∀ 0:

1

slide-13
SLIDE 13

Algorithm Theory, WS 2012/13 Fabian Kuhn 13

Size of Trees

1, 2, ∀ 2: 2 ,

  • 1
  • Claim of lemma:
slide-14
SLIDE 14

Algorithm Theory, WS 2012/13 Fabian Kuhn 14

Size of Trees

Lemma: In a Fibonacci heap, the size of the sub‐tree of a node with rank is at least

.

Theorem: The maximum rank of a node in a Fibonacci heap of size is at most . Proof:

  • The Fibonacci numbers grow exponentially:
  • 1

5 ⋅ 1 5 2

  • 1

5 2

  • For , we need

nodes.

slide-15
SLIDE 15

Algorithm Theory, WS 2012/13 Fabian Kuhn 15

Summary: Binomial and Fibonacci Heaps

Binomial Heap Fibonacci Heap initialize

  • insert
  • get‐min
  • delete‐min

* decrease‐key * merge

  • is‐empty
  • ∗ amortized time
slide-16
SLIDE 16

Algorithm Theory, WS 2012/13 Fabian Kuhn 16

Minimum Spanning Trees

Prim Algorithm:

  • 1. Start with any node ( is the initial component)
  • 2. In each step:

Grow the current component by adding the minimum weight edge connecting the current component with any other node Kruskal Algorithm:

  • 1. Start with an empty edge set
  • 2. In each step:

Add minimum weight edge such that does not close a cycle

slide-17
SLIDE 17

Algorithm Theory, WS 2012/13 Fabian Kuhn 17

Implementation of Prim Algorithm

Start at node , very similar to Dijkstra’s algorithm:

  • 1. Initialize 0 and ∞ for all
  • 2. All nodes are unmarked
  • 3. Get unmarked node which minimizes :

4. For all , ∈ , min , 5. mark node

  • 6. Until all nodes are marked
slide-18
SLIDE 18

Algorithm Theory, WS 2012/13 Fabian Kuhn 18

Implementation of Prim Algorithm

Implementation with Fibonacci heap:

  • Analysis identical to the analysis of Dijkstra’s algorithm:

insert and delete‐min operations decrease‐key operations

  • Running time:
slide-19
SLIDE 19

Algorithm Theory, WS 2012/13 Fabian Kuhn 19

Kruskal Algorithm

3 14 4 6 1 10 13 23 21 31 8 25 20 11 18 17 16 19 9 12 7 2 28

  • 1. Start with an

empty edge set

  • 2. In each step:

Add minimum weight edge such that does not close a cycle

slide-20
SLIDE 20

Algorithm Theory, WS 2012/13 Fabian Kuhn 20

Implementation of Kruskal Algorithm

  • 1. Go through edges in order of increasing weights
  • 2. For each edge :

if does not close a cycle then add to the current solution

slide-21
SLIDE 21

Algorithm Theory, WS 2012/13 Fabian Kuhn 21

Union‐Find Data Structure

Also known as Disjoint‐Set Data Structure… Manages partition of a set of elements

  • set of disjoint sets

Operations:

  • _: create a new set that only contains element
  • : return the set containing
  • , : merge the two sets containing and
slide-22
SLIDE 22

Algorithm Theory, WS 2012/13 Fabian Kuhn 22

Implementation of Kruskal Algorithm

  • 1. Initialization:

For each node : make_set

  • 2. Go through edges in order of increasing weights:

Sort edges by edge weight

  • 3. For each edge , :

if then add to the current solution ,

slide-23
SLIDE 23

Algorithm Theory, WS 2012/13 Fabian Kuhn 23

Managing Connected Components

  • Union‐find data structure can be used more generally to manage

the connected components of a graph … if edges are added incrementally

  • make_set for every node
  • find returns component containing
  • union, merges the components of and

(when an edge is added between the components)

  • Can also be used to manage biconnected components
slide-24
SLIDE 24

Algorithm Theory, WS 2012/13 Fabian Kuhn 24

Basic Implementation Properties

Representation of sets:

  • Every set of the partition is identified with a representative,

by one of its members ∈ Operations:

  • make_set: is the representative of the new set
  • find: return representative of set containing
  • union, : unites the sets and containing and and

returns the new representative of ∪

slide-25
SLIDE 25

Algorithm Theory, WS 2012/13 Fabian Kuhn 25

Observations

Throughout the discussion of union‐find:

  • : total number of make_set operations
  • : total number of operations (make_set, find, and union)

Clearly:

  • There are at most 1 union operations

Remark:

  • We assume that the make_set operations are the first
  • perations

– Does not really matter…

slide-26
SLIDE 26

Algorithm Theory, WS 2012/13 Fabian Kuhn 26

Linked List Implementation

Each set is implemented as a linked list:

  • representative: first list element (all nodes point to first elem.)

in addition: pointer to first and last element

  • sets: 1,5,8,12,43 , 7,9,15 ; representatives: 5, 9
slide-27
SLIDE 27

Algorithm Theory, WS 2012/13 Fabian Kuhn 27

Linked List Implementation

_:

  • Create list with one element:

time: :

  • Return first list element:

time:

slide-28
SLIDE 28

Algorithm Theory, WS 2012/13 Fabian Kuhn 28

Linked List Implementation

, :

  • Append list of to list of :

Time:

slide-29
SLIDE 29

Algorithm Theory, WS 2012/13 Fabian Kuhn 29

Cost of Union (Linked List Implementation)

Total cost for 1 union operations can be Θ:

  • make_set , make_set , … , make_set,

union , , union , , … , union ,

slide-30
SLIDE 30

Algorithm Theory, WS 2012/13 Fabian Kuhn 30

Weighted‐Union Heuristic

  • In a bad execution, average cost per union can be Θ
  • Problem: The longer list is always appended to the shorter one

Idea:

  • In each union operation, append shorter list to longer one!

Cost for union of sets and : min , Theorem: The overall cost of operations of which at most are make_set operations is .

slide-31
SLIDE 31

Algorithm Theory, WS 2012/13 Fabian Kuhn 31

Weighted‐Union Heuristic

Theorem: The overall cost of operations of which at most are make_set operations is . Proof:

slide-32
SLIDE 32

Algorithm Theory, WS 2012/13 Fabian Kuhn 32

  • Represent each set by a tree
  • Representative of a set is the root of the tree
  • Disjoint‐Set Forests
slide-33
SLIDE 33

Algorithm Theory, WS 2012/13 Fabian Kuhn 33

Disjoint‐Set Forests

_: create new one‐node tree : follow parent point to root (parent pointer to itself) , : attach tree of to tree of

slide-34
SLIDE 34

Algorithm Theory, WS 2012/13 Fabian Kuhn 34

Bad Sequence

Bad sequence leads to tree(s) of depth Θ

  • make_set , make_set , … , make_set,

union , , union , , … , union ,

slide-35
SLIDE 35

Algorithm Theory, WS 2012/13 Fabian Kuhn 35

Union‐By‐Size Heuristic

Union of sets and :

  • Root of trees representing and :

and

  • W.l.o.g., assume that ||
  • Root of ∪ :

( is attached to as a new child)

Theorem: If the union‐by‐rank heuristic is used, the worst‐case cost of a ‐operation is Proof:

slide-36
SLIDE 36

Algorithm Theory, WS 2012/13 Fabian Kuhn 36

Union‐Find Algorithms

Recall: operations, of the operations are make_set‐operations Linked List with Weighted Union Heuristic:

  • make_set: worst‐case cost 1
  • find

: worst‐case cost 1

  • union

: amortized worst‐case cost log Disjoint‐Set Forest with Union‐By‐Size Heuristic:

  • make_set: worst‐case cost 1
  • find

: worst‐case cost log

  • union

: worst‐case cost log Can we make this faster?

slide-37
SLIDE 37

Algorithm Theory, WS 2012/13 Fabian Kuhn 37

Path Compression During Find Operation

: 1. if . then 2. . ≔ find . 3. return .

slide-38
SLIDE 38

Algorithm Theory, WS 2012/13 Fabian Kuhn 38

Complexity With Path Compression

When using only path compression (without union‐by‐rank): : total number of operations

  • of which are find‐operations
  • of which are make_set‐operations

 at most 1 are union‐operations Total cost: ⋅ ⋅

slide-39
SLIDE 39

Algorithm Theory, WS 2012/13 Fabian Kuhn 39

Union‐By‐Size and Path Compression

Theorem: Using the combined union‐by‐size and path compression heuristic, the running time of disjoint‐set (union‐find)

  • perations on elements (at most make_set‐operations) is

⋅ , , Where , is the inverse of the Ackermann function.

slide-40
SLIDE 40

Algorithm Theory, WS 2012/13 Fabian Kuhn 40

Ackermann Function and its Inverse

Ackermann Function: For , ℓ 1, , ℓ ≔ ℓ, , ℓ , , , ℓ , , ℓ , , ℓ Inverse of Ackermann Function: , ≔ | , ⁄

slide-41
SLIDE 41

Algorithm Theory, WS 2012/13 Fabian Kuhn 41

Inverse of Ackermann Function

  • , ≔ min 1 | ,

⁄ log

⟹ , ⁄ , 1 ⟹ , min 1| , 1 log

  • 1, ℓ 2ℓ, , 1 1,2,

, ℓ 1, , ℓ 1