UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2001 - PowerPoint PPT Presentation

1 / 47
About This Presentation
Title:

UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2001

Description:

Ch21 Fibonacci Heaps. You're responsible for material in this chapter ... DECREASE-KEY Fibonacci Heap operation may violate Unordered Binomial Tree properties. ... – PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 48
Provided by: murrayd
Learn more at: https://www.cs.uml.edu
Category:

less

Transcript and Presenter's Notes

Title: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2001


1
UMass Lowell Computer Science 91.503 Analysis
of Algorithms Prof. Karen Daniels Fall, 2001
  • Lecture 11
  • Tuesday, 12/4/01
  • Advanced Data Structures
  • Chapters 20-21

2
Relevant Sections of Chapters
Youre responsible for material in this chapter
that we discuss in lecture. (Note that this
includes all sections.)
Ch20 Binomial Heaps
Ch7 HeapSort
Youre responsible for material in this chapter
that we discuss in lecture. (Note that this
includes all sections.)
Ch21 Fibonacci Heaps
Note that Chapter 22 has been removed.
3
Overview
  • Chapter 7 Heap Review
  • Chapter 20 Binomial Heaps
  • Chapter 21 Fibonacci Heaps

4
Chapter 7
  • Heap Review

5
Review of Heap Basics
  • Structure
  • Nearly complete binary tree
  • Convenient array representation
  • HEAP Property (for MAX HEAP)
  • Parents label not less than that of each child

6
Operations on a Heap
assuming array representation
  • HEAPIFY
  • for a given node that is the root of a subtree,
    if both subtrees of that node are already HEAPs,
    HEAPIFY enforces the HEAP PROPERTY via downward
    swaps so that the node together with its
    subtrees form a HEAP
  • BUILD-HEAP
  • builds a HEAP from scratch using HEAPIFY
  • HEAPSORT
  • sorts an array by first using BUILD-HEAP then
    repeatedly swapping out root and calling HEAPIFY

7
Operations on a Heap
assuming array representation
  • HEAPIFY
  • BUILD-HEAP
  • HEAPSORT

T(n) T(2n/3) Q(1) is in O(lgn) using Master
Theorem
Asymptotic worst-case running time is in O(lg n).
For a node at height h, time is in O(h).
Asymptotic worst-case running time is in O(n lg
n). However this is a loose bound! Time is
also in O(n).
Asymptotic worst-case running time is in O(n lg
n).
8
Operations on a Heap
assuming array representation
  • PRIORITY QUEUE SUPPORT
  • HEAP-INSERT
  • adds new leaf to the tree and then swaps up to
    restore HEAP PROPERTY
  • HEAP- MAXIMUM
  • HEAP PROPERTY guarantees that maximum is at the
    root of a MAX HEAP
  • HEAP- EXTRACT-MAX
  • removes the maximum value from the root by
    swapping it out
  • restores HEAP PROPERTY using HEAPIFY

Applications Job Scheduling, Event Scheduling
9
Operations on a Heap
assuming array representation
  • PRIORITY QUEUE SUPPORT
  • HEAP-INSERT
  • HEAP- MAXIMUM
  • HEAP- EXTRACT-MAX

Asymptotic worst-case running time is in O(lg n).
For a node at height h, time is in O(h).
Asymptotic worst-case running time is in O(1).
Asymptotic worst-case running time is in O(lg n).
O(1)
For a node at height h, time is in O(h).

O(lgn)
10
Building a Heap usingHEAPIFY vs. HEAP-INSERT
  • HEAPIFY
  • swaps down
  • compares parent with both children before each
    swap
  • HEAP-INSERT
  • swaps up
  • compares parent with one child before each swap

maximum number of swaps length of path from
this level up to root
number of levels
number of levels
maximum number of swaps length of path from
this level down to leaf
number of nodes in this level
number of nodes in this level
O(n) as in HEAPIFY
Asymptotic worst-case running time of BUILD-HEAP
using HEAPIFY is in O(n). However, using
HEAP-INSERT the time would only be in O(n lg n).

11
Chapter 20
  • Binomial Heaps

12
Mergeable Heap Operations
If UNION not needed, binary heap (Ch7) suffices
Ch20/21 Goal
Mergable Heaps supporting fast UNION
source 91.503 textbook Cormen et al.
13
Mergeable Heap Operations
Ch20/21 Goal
Mergable Heaps supporting fast UNION
source 91.503 textbook Cormen et al.
14
Binomial Tree Definition
Binomial Heap is a collection of Binomial Trees
source 91.503 textbook Cormen et al.
15
Binomial Tree Properties
source 91.503 textbook Cormen et al.
16
Binomial Tree Properties
source 91.503 textbook Cormen et al.
17
Binomial Tree Properties
Proof (continued)
source 91.503 textbook Cormen et al.
18
Binomial Heap Definition
root degrees increase along root list
source 91.503 textbook Cormen et al.
19
Binomial Heap OperationsMAKE-HEAP, MINIMUM
MAKE-HEAP worst-case running time is in Q(1)
HEAP-MINIMUM worst-case running time is in O(lgn)
Minimum key must be in a root node due to
heap-ordering. There are at most roots to check.
search root list
source 91.503 textbook Cormen et al.
20
Binomial Heap OperationsUNION
Merge root lists into single linked list sorted
by nondecreasing degree
Links 2 binomial heaps whose roots have same
degree
Link roots of equal degree until at most one root
remains of each degree
Pointers into root list
HEAP-UNION worst-case running time is in O(lgn)
source 91.503 textbook Cormen et al.
orphan, consolidate degree roots
21
Binomial Heap OperationsUNION (continued)
3 roots of same degree
source 91.503 textbook Cormen et al.
22
Binomial Heap OperationsUNION (continued)
source 91.503 textbook Cormen et al.
23
Binomial Heap OperationsUNION (continued)
source 91.503 textbook Cormen et al.
24
Binomial Heap OperationsINSERT
HEAP-INSERT worst-case running time is in O(lgn)
UNION
source 91.503 textbook Cormen et al.
25
Binomial Heap OperationsEXTRACT-MIN
HEAP-EXTRACT-MIN worst-case running time is in
Q(lgn)
orphan, reverse, UNION
source 91.503 textbook Cormen et al.
26
Binomial Heap OperationsDECREASE-KEY
If violate heap property, swap up. No change in
structure.
HEAP-DECREASE-KEY worst-case running time is in
Q(lgn)
swap up if heap property violation
source 91.503 textbook Cormen et al.
27
Binomial Heap Operations DELETE
  • HEAP-DELETE worst-case running time is in Q(lgn)

DECREASE-KEY, EXTRACT-MIN
source 91.503 textbook Cormen et al.
28
Chapter 21
  • Fibonacci Heaps

29
Mergeable Heap Operations
Ch20/21 Goal
Mergable Heaps supporting fast UNION
source 91.503 textbook Cormen et al.
30
Fibonacci Heap Basics
For asymptotically fast MST, shortest paths
Collection of trees Relaxed structure Lazy delay
work Amortized (potential) cost
Minimum root
t(H) trees in root list
m(H) marked nodes
Circular linked lists
source 91.503 textbook Cormen et al.
31
Potential Method (review)
  • Potential Method
  • amortized cost can differ across operations (as
    in accounting method)
  • overcharge some operations early in sequence (as
    in accounting method)
  • store overcharge as potential energy of data
    structure as a whole
  • (unlike accounting method)
  • Let ci be actual cost of ith operation
  • Let Di be data structure after applying ith
    operation
  • Let F(Di ) be potential associated with Di
  • Amortized cost of ith operation
  • Total amortized cost of n operations
  • Must have to pay in advance

terms telescope
32
Unordered Binomial Tree Properties
DECREASE-KEY Fibonacci Heap operation may violate
Unordered Binomial Tree properties.
DIFFERENCE
source 91.503 textbook Cormen et al.
33
Fibonacci Heap OperationsINSERT
add to root list update min pointer
Increase in potential
Actual cost is in O(1)
Amortized cost is in O(1)
source 91.503 textbook Cormen et al.
34
Fibonacci Heap OperationsUNION
source 91.503 textbook Cormen et al.
add to root list update min pointer
35
Fibonacci Heap OperationsEXTRACT-MIN
First, disassemble old min tree.
disassemble
Next, update min
Process one tree at a time, starting with new
min.
Consolidate wherever possible Link roots of
degree until at most one root remains of each
degree
orphan, consolidate degree roots
source 91.503 textbook Cormen et al.
36
Fibonacci Heap OperationsEXTRACT-MIN
Consolidation
Degree 0 merge Combine 7 with 23
Degree 1 merge (keep going) Combine 7/23 with
17/30
Degree 2 merge (keep going) Combine 7 with 24
source 91.503 textbook Cormen et al.
37
Fibonacci Heap OperationsEXTRACT-MIN
Consolidation
source 91.503 textbook Cormen et al.
38
Fibonacci Heap OperationsEXTRACT-MIN Pseudocode
source 91.503 textbook Cormen et al.
39
Fibonacci Heap OperationsEXTRACT-MIN Pseudocode
may do multiple degree merges
source 91.503 textbook Cormen et al.
40
Fibonacci Heap OperationsEXTRACT-MIN Analysis
D(n) upper bound on maximum degree of any node
in an n-node Fibonacci heap (shown in Section
21.3 to be in O(lgn))
Actual Work gt O(D(n)) since at most D(n)
children of minimum node When CONSOLIDATE is
called, size of root list lt D(n) t(H) - 1 Work
in CONSOLIDATEs for loop in O(D(n) t(H)) due
to tree linking in each iteration Total Actual
Cost is in O(D(n) t(H))
O(D(n))
Amortized cost is in O(lgn)
source 91.503 textbook Cormen et al.
41
Fibonacci Heap Operations DECREASE-KEY PseudoCode
cascading cut if heap property violation
source 91.503 textbook Cormen et al.
42
Fibonacci Heap Operations DECREASE-KEY Analysis
source 91.503 textbook Cormen et al.
As soon as 2nd child of x is lost, cut x from
parent, making it a new root.
Actual Work Dominated by cost of CASCADING-CUT
Assume CASCADING-CUT called recursively c
times Total Actual Cost is in O(c)
t(H) to start with, c-1 added from cuts, tree
rooted at x
c-1 unmarked by cascading cuts
at most 1 from last call to CASCADING-CUT
43
Fibonacci Heap Operations DELETE
DECREASE-KEY, EXTRACT-MIN
Amortized cost is in O(lgn)
O(D(n))
source 91.503 textbook Cormen et al.
44
Bounding the Maximum Degree
EXTRACT-MIN ( DELETE) O(lgn) bounds require D(n)
in O(lgn)
Show Cutting node when it loses 2nd child
Link only occurs in EXTRACT-MIN, DELETE
45
Bounding the Maximum Degree
source 91.503 textbook Cormen et al.
46
Bounding the Maximum Degree
Exercise 2.2-8
source 91.503 textbook Cormen et al.
47
Bounding the Maximum Degree
Thus, EXTRACT-MIN ( DELETE) have O(lgn) time
bounds.
source 91.503 textbook Cormen et al.
Write a Comment
User Comments (0)
About PowerShow.com