0% found this document useful (0 votes)
8 views36 pages

DAA Unit 2 Part 4

The document provides an overview of Binomial Heaps and Fibonacci Heaps, detailing their operations such as Make-Heap, Minimum, Union, Insert, Extract-Min, Decrease-Key, and Delete. It also introduces Tries and Skip Lists, explaining their structure and applications. The focus is on the efficiency and properties of these data structures in managing and retrieving data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views36 pages

DAA Unit 2 Part 4

The document provides an overview of Binomial Heaps and Fibonacci Heaps, detailing their operations such as Make-Heap, Minimum, Union, Insert, Extract-Min, Decrease-Key, and Delete. It also introduces Tries and Skip Lists, explaining their structure and applications. The focus is on the efficiency and properties of these data structures in managing and retrieving data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Binomial Heap

Binomial Heap

All the operations in Binomial Heap except Make-HEAP takes


O(logn) time. That’s why it is most efficient Heap
Binomial Heap - Operations
MAKE-BINOMIAL-HEAP
The Make-Heap operation is used to create an empty binomial heap, which you can
then use to insert elements or merge with other heaps.

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.

x: A pointer to the current root node (starts at the head


of the binomial heap).

min: A pointer to the root node with the smallest key


found so far (initialized to x).

y: A pointer to traverse the siblings of x (the other root


nodes in the root list).
Binomial Heap - Operations
BINOMIAL-HEAP-UNION
• The Union operation (also called Binomial-Heap-Union) combines two binomial
heaps into a single binomial heap.
• This is one of the key operations that makes binomial heaps efficient, especially
in applications like graph algorithms where merging heaps frequently occurs.
Steps Involved in the Union Operation:
1.Merge the Root Lists of the two heaps, ensuring that the trees in the resulting list
are sorted by degree (from smallest to largest). This is similar to merging two sorted
linked lists.
2.Traverse the Merged List and adjust the trees so that no two trees have the
same degree. This is done by linking trees of the same degree (i.e., making one
tree a child of the other, thereby increasing its degree).
3.Return the Resulting Heap, which will have trees of unique degrees, and the
heap property will be maintained.
Binomial Heap - Operations
BINOMIAL-HEAP-UNION

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.

Uniting two Fibonacci heaps

• 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.

4. Update the Min Pointer:


•After consolidation, scan the root list to find the new minimum node.
Fibonacci Heap - Operations
Consolidation Process
Fibonacci Heap - Operations
Consolidation Process
1. Remove the Minimum Node
2. Initialize an Array to Track Degrees
3. Traverse the Root List
4. Merge Trees of the Same Degree
5. Update the Root List
6. Find the New Minimum

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

(a) The initial Fibonacci heap.


(b) The node with key 46 has its key decreased
to 15.
The node becomes a root, and its parent
(with key 24), which had previously been
unmarked, becomes marked.
(c)-(e) The node with key 35 has its key
decreased to 5
In part (c), the node, now with key 5, becomes a
root. Its parent, with key 26, is marked,
so a cascading cut occurs
The node with key 26 is cut from its parent and
made an unmarked root in (d)
Another cascading cut occurs, since the node
with key 24 is marked as well.
This node is cut from its parent and made an unmarked root in part (e).
The cascading cuts stop at this point, since the node with key 7 is a root
The result of the FIB-HEAP-DECREASE-KEY operation is shown in part (e), with min[H] pointing to the
new minimum node
Tries
• The word "Trie" is an excerpt from the word "retrieval".
• Trie is a sorted tree-based data-structure that stores the set of strings.
• The Trie data structure is a tree-like data structure used for storing a dynamic
set of strings.
• It is commonly used for efficient retrieval and storage of keys in a large dataset.

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:

1. A Trie data structure consists of nodes connected by edges.


2. Each node represents a character or a part of a string.
3. The root node, the starting point of the Trie, represents an empty string.
4. Each edge emanating from a node signifies a specific character.
5. The path from the root to a node represents the prefix of a string stored in the
Trie.
Tries
Skip Lists
• A skip list is a probabilistic data structure.
• The skip list is used to store a sorted list of elements or data with a linked list.
• It allows the process of the elements or data to view efficiently.
• In one single step, it skips several elements of the entire list, which is why it is
known as a skip list.
• In a skip list, elements are organized in layers, with each layer having a smaller
number of elements than the one below it.
• The bottom layer is a regular linked list, while the layers above it contain
“skipping” links that allow for fast navigation to elements that are far apart in the
bottom layer.
• The idea behind this is to allow for quick traversal to the desired element,
reducing the average number of steps needed to reach it.
Skip Lists

You might also like