Section 8: Priority Queues and Disjoint Sets - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

Section 8: Priority Queues and Disjoint Sets

Description:

Find the child of x with the smallest key ... While doing a Find, remember which nodes we touch on our path to the root. After the Find, make all of these nodes ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 32
Provided by: Nitish7
Category:

less

Transcript and Presenter's Notes

Title: Section 8: Priority Queues and Disjoint Sets


1
Section 8 Priority Queues and Disjoint Sets
  • CS 225 Data Structures Software Principles

2
Priority Queues
  • Definition A Priority Queue is a set ADT of
    pairs (K,I) where K is a key and I represents
    some information associated with that key.
  • Keys need to have some linear order i.e., we
    can define "less than" on them
  • The information can be of any type (usually, we
    use templates for this)

3
Priority Queues
  • Priority Queue operations
  • MakeEmptySet( ) Makes an empty priority queue
  • IsEmptySet(S) Returns true if S is empty
  • Insert(K,I,S) Add pair (K,I) to S
  • FindMin(S) Find the smallest key in S, and
    return the associated data
  • DeleteMin(S) Just like FindMin, except we remove
    the (key, data) pair as well

4
Priority Queues
  • Conceptually, each key represents a priority for
    the associated data
  • Normally, the item with lowest key value is
    considered to have highest priority
  • We try to optimize FindMin(), DeleteMin()
  • To do this, we implement Priority Queues using
    either balanced trees or heaps.

5
Priority Queues
  • A Partially ordered tree is a tree of keys, such
    that
  • The key at each node is no larger than the keys
    at its children
  • The smallest key in the tree is located at the
    root (this follows from the first property)
  • When we add or remove something, we might have to
    adjust the tree to maintain this property

6
Priority Queues
  • In other words, a nodes key value (priority) is
    lt the key value of its children
  • No conclusion can be drawn about the relative
    order of the items in the left and right
    sub-trees of a node

7
Heaps
  • Heap
  • A heap is a partially ordered complete tree
  • Run-time analysisFor a heap containing n nodes,
  • FindMin takes O(1) time just look at the root
  • Insert takes O(log n) time
  • DeleteMin takes O(log n) time
  • Operations on heaps to be discussed
  • Insert
  • DeleteMin

8
Heaps
  • Insert
  • Step 1 Place the new element in its natural
    position as a new leaf node in the tree call
    that node x
  • Step 2
  • if( xs parents priority gt xs priority )
  • Swap contents of x and xs parent and reassign x
    to xs parent
  • Repeat from Step 2
  • else
  • Algorithm TERMINATES

9
Heaps
  • Given tree

10
Heaps
  • Insert node of priority 7

7
11
Heaps
7
7
10
10
12
Heaps
  • DeleteMin
  • Step 1 Let minimum_value contents of the root
    node.
  • Step 2 Find the rightmost leaf on the lowest
    level of the tree copy its contents to the root
    node, then delete the leaf.
  • Step 3 set x to point to the root.
  • Step 4
  • if (x's key gt either of xs children's keys)
  • Find the child of x with the smallest key
  • Swap contents of x and that child, and reassign x
    to point that child
  • Repeat from Step 3
  • else
  • Return minimum_value, and STOP

13
Heaps
  • DeleteMin on given tree

13
5
13
14
Heaps
13
13
15
Heaps
  • Heap Implementation
  • Heaps being are usually implemented using arrays
  • We number the nodes in level order, then store
    keys in an array in that order
  • Equations
  • LeftChild(i) 2 i
  • RightChild(i) 2 i 1
  • Parent(i) ? i/2 ?

1 2 3 4
9
16
Heaps
  • In practice, we don't really "swap" values.
    Instead,
  • Hold the element that's being moved up or down
    the tree off to the side
  • While we're looking for the proper position for
    this element, we move a "hole" up or down the
    tree by shifting values around
  • (Review the library code for Insert / DeleteMin!)

17
Heaps
  • Another optimization is to use a sentinel value.
  • heap0 sentinel, where heap is an array used
    to represent the heap
  • The sentinel value is a key value less than any
    value that could be present in the heap
  • Used so that swapping with parent at the root
    node is also handled without any special
    consideration
  • real key values start at index 1 in the array

18
Heaps
Sentinel value
-1
0 1 2 3 4
9
19
More on Heaps
  • We can have a method called BuildHeap which will
    initialize a Heap in O(n) time rather than simply
    doing n Insertions into the heap which will take
    O(n log n) time
  • It converts an array into a heap
  • for(i n/2 to 1)
  • PercolateDown(i)
  • n/2 represents the first element from the right
    end of the array that has children

20
More on Heaps
  • HeapSort
  • We can do sorting using a Heap
  • Simply build a heap using BuildHeap from the
    given array and then empty it
  • This means of sorting will have a run-time
    complexity of O(n log n)
  • Heaps
  • MaxHeaps key at each node is larger than keys
    at its children
  • MinHeaps key at each node is smaller than keys
    at its children

21
Disjoint Sets
  • Disjoint SetsWe want to represent a universe
    that has been partitioned into several
    nonoverlapping sets. Formally,
  • We have a fixed set U of Elements Xi
  • U is divided into a number of disjoint subsets
    S1, S2 , S3 , Sk
  • Si ? Sj is empty ? i ? j
  • S1 ? S2 ? S3 ? Sk U

22
Disjoint Sets
  • Our representation should optimize these
    operations
  • Union(S,T) Return the set S ? T, which replaces
    S and T in the data base(In other words, combine
    two sets into one)
  • Find(X) Return that set S such that X ? S

23
Up-Trees
  • We can use a tree structure known as an up-tree
    can be used to maintain the sets
  • In an up-tree we have pointers up the tree from
    children to parents
  • Up-Tree Properties
  • Each node has a single pointer field to point to
    its parent at the root this field is empty
  • A node can have any number of children
  • The sets are identified by their root nodes

24
Up-Trees
B
F
Disjoint Sets A,C,D,E,G,H,J and B,F
25
Up-Trees
  • Union
  • To form S ? T, make the root of one tree point to
    the root of the other
  • If we make the root of S point to the root of T,
    we say we are merging S into T
  • To prevent the linear growth of the height of the
    tree, we always merge the "smaller" tree into the
    larger one
  • Union by height merge the shorter tree into the
    larger
  • Union by size merge the tree with fewer nodes
    into the tree with more nodes

26
Merging Up-Trees
B
F
B
F
(a)
(b)
Correct Way
Incorrect Way
27
Up-Trees
  • Find
  • To find which set an element belongs to, follow
    the pointers up the tree until we reach the root
  • If we assume that when doing a Find(X) we know
    the location of the node X in the tree, then Find
    can be implemented in worst-case O(log n) time
  • This is assuming we use union by height, which
    ensures that the height of the tree is at most
    log n
  • Union by size can mean O(n) worst-case
    complexity, but will maintain O(log n)
    average-case complexity

28
Up-Trees
  • Path Compression
  • A Find would take less time in a shallow, bushy
    tree than it would in a tall, skinny tree
  • By using balanced merging, we have ensured that
    the height can at worst be logarithmic in the
    tree size
  • However, since any number of nodes can have the
    same parent, we can restructure our up-tree to
    make it bushier

29
Up-Trees
  • This restructuring is called Path Compression.
  • While doing a Find, remember which nodes we touch
    on our path to the root.
  • After the Find, make all of these nodes point
    directly to the root.
  • Any subsequent Find on any one of these nodes, or
    their descendants, will take less time since the
    node is now closer to the root.
  • Find now takes almost constant time O(log n)
    amortized worst-case time to be precise
  • This is practically constant-time, but not quite.

30
Up-Trees
  • Array Implementation
  • If we assume all elements of the universe to be
    integers from 0 to N, then we can represent the
    Up-Trees as one Array of size N1
  • At each index x, we store the parent of x
  • At the root of an up-tree, we store a negative
    number to indicate that it's the root.
  • If we're using union by height, we store the
    height of the up-tree, except negated
  • If we're using union by size, we store the size
    of the up-tree, negated
  • When we want to perform a union, we have to find
    the root anyway once we do, we already know the
    height or size!

31
Up-Trees
Assuming union by size these values give the
sizes of the respective up-trees
a
-2
2
4
0
-7
2
4
2
6
0
1
2
3
4
5
6
7
8
ai parent of i
Write a Comment
User Comments (0)
About PowerShow.com