DAA Unit 2 Part 4
DAA Unit 2 Part 4
Binomial Heap
Binomial-Heap-Make-Heap():
return NIL # Creates an empty heap with no trees
BINOMIAL-HEAP-MINIMUM(H)
Key Concept:
•A binomial heap is composed of several binomial trees.
•In a min-binomial heap, the minimum key is always located at one of the
root nodes of these trees.
•The operation simply scans the root list (i.e., the roots of the binomial trees in
the heap) to find the root with the smallest key.
Binomial Heap - Operations
BINOMIAL-HEAP-MINIMUM(H) Input: The input is a binomial heap H where H.head
points to the first binomial tree in the heap.
Explanation
Input:
•y is the root of the first binomial tree.
•z is the root of the second binomial tree (which has a smaller key than y).
Steps:
1.Set z as the parent of y (i.e., make y a child of z).
2.Make y the new child of z by linking y as the first child of z.
3.Update the degree of z (the number of children it has) by adding 1, as y is
now its child.
Binomial Heap - Operations
BINOMIAL-HEAP-UNION
Binomial Heap - Operations
BINOMIAL-HEAP-UNION
Binomial Heap - Operations
BINOMIAL-HEAP-UNION
Binomial Heap - Operations
BINOMIAL-HEAP-INSERT
Explanation
•Binomial-Heap-Insert creates a new binomial tree with the inserted node and
unions it with the original heap.
•The union operation ensures that the resulting heap maintains the binomial heap
structure, with no two trees of the same degree.
Binomial Heap - Operations
BINOMIAL-HEAP-EXTRACT-MIN(H)
Operations:
1.Find minimum root: Traverse the root list to identify the minimum root.
2.Remove minimum: Extract the root with the minimum key.
3.Reverse children: Reverse the children of the removed root to maintain binomial
heap properties.
4.Merge heaps: Merge the original heap (minus the extracted root) with the newly
formed heap from the children.
5.Return result: Return the minimum key, which was the key of the extracted root.
Binomial Heap - Operations
BINOMIAL-HEAP-EXTRACT-MIN(H)
.
Binomial Heap - Operations
BINOMIAL-HEAP-DECREASE-KEY(H)
Operations:
1.Update the key: Change the key of the node x to the new value k.
2.Bubble up the node: If the new key violates the min-heap property, move the
node upwards by swapping it with its parent.
3.Stop when the heap property is restored: Continue bubbling up until the heap
property is no longer violated.
Binomial Heap - Operations
BINOMIAL-HEAP-EXTRACT-MIN(H)
.
Binomial Heap - Operations
BINOMIAL-HEAP-DELETE(H)
Operations:
1. BINOMIAL-HEAP-DECREASE-KEY(H, x, -∞):
•This operation decreases the key of node x to negative infinity.
•If the parent of x has a larger key, swap x with its parent.
•Continue this process until x becomes the root of a binomial tree, ensuring
the heap property is maintained.
2. BINOMIAL-HEAP-EXTRACT-MIN(H):
•This operation removes the root with the minimum key (which is now x with
key -∞).
•Remove x from the heap by adjusting the pointers in the root list.
•Reverse the order of x's children and form a new heap.
•Merge the new heap (formed from x's children) with the remaining heap
using the BINOMIAL-HEAP-UNION operation.
Fibonacci Heap - Introduction
• A Fibonacci heap is a collection of trees (like a forest), where each tree follows
the min-heap property.
• This means the key of any node is greater than or equal to the key of its parent.
• The heap can have many trees, and nodes are connected through pointers
rather than fixed positions in an array (as in binary heaps).
• Unlike trees within binomial heaps, which are ordered, trees within Fibonacci
heaps are rooted but unordered.
Fibonacci Heap - Introduction
Each node in a Fibonacci heap consists of the following fields:
•Key: The value of the node. In a min-heap, this key is always less than or equal to
the keys of its children.
•Degree: The number of children the node has. This helps manage the structure of
the heap and is useful during the consolidation process.
•Parent Pointer: A pointer to the node's parent. If the node is part of the root list,
this pointer is null.
•Child Pointer: A pointer to one of the node's children. The children themselves
form a circular doubly linked list.
•Sibling Pointers: Each node also has pointers to its next and previous sibling.
These pointers allow the child nodes and the root list to be maintained as circular
doubly linked lists.
•Mark Bit: A boolean flag that indicates whether the node is "marked." Nodes are
marked when they lose a child. The marking system is part of the mechanism that
ensures that Fibonacci heaps perform efficiently in the DECREASE-KEY operation.
Fibonacci Heap - Operations
Creating a new Fibonacci heap
Steps:
•Step 1: This creates a new Fibonacci heap object H in memory. In a real
implementation, this might involve allocating memory and setting up a structure to
hold the key properties of the heap.
•Step 2: Initializes H.min to NULL. This means the heap has no minimum node yet
because it is empty.
•Step 3: Sets the number of nodes H.n to 0, indicating the heap currently holds no
nodes.
•Step 4: Returns the new, empty Fibonacci heap. This heap is now ready to have
nodes inserted into it.
Fibonacci Heap - Operations
Creating a new Fibonacci heap
• After lines 1-6 initialize the structural fields of node x, making it its own circular,
doubly linked list
• line 7 adds x to the root list of H in O(1) actual time.
• Thus, node x becomes a single-node min-heap-ordered tree, and thus an unordered
binomial tree, in the Fibonacci heap.
• It has no children and is unmarked.
• Lines 8-9 then update the pointer to the minimum node of Fibonacci heap H if
necessary.
• Finally, line 10 increments n[H] to reflect the addition of the new node.
Fibonacci Heap - Operations
Creating a new Fibonacci heap
• Fibonacci heap H after the node with key 21 has been inserted.
• The node becomes its own min-heap ordered tree and is then added to the root
list, becoming the left sibling of the root.
• Unlike the BINOMIAL-HEAP-INSERT procedure, FIB-HEAP-INSERT makes no
attempt to consolidate the trees within the Fibonacci heap.
• If k consecutive FIB-HEAP-INSERT operations occur, then k single-node trees
are added to the root list.
Fibonacci Heap - Operations
Finding Minimum Node
The minimum node of a Fibonacci heap H is given by the pointer min[H ], so we can find
the minimum node in O(1) actual time.
• Lines 1-3 concatenate the root lists of H1 and H2 into a new root list H.
• Lines 2, 4, and 5 set the minimum node of H
• line 6 sets n[H] to the total number of nodes
• The Fibonacci heap objects H1 and H2 are freed in line 7, and line 8 returns the
resulting Fibonacci heap H.
Fibonacci Heap - Operations
Extracting the Minimum Node
1. Find the Minimum Node: The minimum node z in the Fibonacci heap is pointed
to by the min pointer.
2. Remove the Minimum Node:
•Remove z from the root list of the heap.
•For each child x of z, add x to the root list of the heap, and set its parent pointer
to None.
Fibonacci Heap - Operations
Extracting the Minimum Node
3. Consolidate the Heap:
•To maintain the Fibonacci heap properties, we consolidate nodes by merging trees
of the same degree.
•Use an auxiliary array A where each index corresponds to a degree value.
•For each node in the root list, link trees of the same degree until each root in the
list has a unique degree. This involves repeatedly merging trees of the same degree
by linking nodes to form larger trees.
Suppose we have three trees in the root list with degrees 2, 2, and 3. During
consolidation:
1.The two trees of degree 2 are merged into a tree of degree 3.
2.Now, there are two trees of degree 3 in the root list, which are also merged.
3.After merging, we have one tree of degree 4 in the root list.
Fibonacci Heap - Operations
Example
Fibonacci Heap - Operations
Example
Fibonacci Heap - Operations
Example
Fibonacci Heap - Operations
Decreasing a key and deleting a node
Fibonacci Heap - Operations
Decreasing a key and deleting a node
• Lines 1-3 ensure that the new key is no greater than the current key of x and
then assign the new key to x.
• If x is a root or if key[x] ≥ key[y], where y is x's parent, then no structural changes
need occur, since min-heap order has not been violated.
• Lines 4-5 test for this condition.
• If min-heap order has been violated, many changes may occur.
• We start by cutting x in line 6. The CUT procedure "cuts" the link between x and
its parent y, making x a root.
• Once all the cascading cuts have occurred, lines 8-9 of FIB-HEAP-DECREASE-
KEY finish up by updating min[H] if necessary
Fibonacci Heap - Operations
Decreasing a key and deleting a node
Tries Applications
1.Autocomplete: Tries are commonly used in autocomplete features. Given a
prefix, the Trie allows finding all words that start with the prefix efficiently.
2.Spell Checkers: Tries can quickly identify whether a word exists in a dictionary,
and they also help find suggestions based on prefixes.
3.IP Routing: In networking, Tries are used to represent IP addresses and perform
routing efficiently.
4.Longest Prefix Matching: This is used in dictionaries, routers, and other
applications that need to match the longest possible prefix in a set.
Tries
Representation of of Trie Node: