0% found this document useful (0 votes)
13 views12 pages

Group 2 Exposé Dsa

The document provides an overview of data structures and algorithms related to heaps, including definitions and construction methods for Max Heaps, Min Heaps, Binomial Heaps, and Fibonacci Heaps. It discusses merging techniques for binary heap trees and compares the time complexities of various heap operations. Key takeaways highlight the advantages of Binomial and Fibonacci heaps over Binary heaps in terms of time efficiency for specific operations.

Uploaded by

Hardy Goddy
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)
13 views12 pages

Group 2 Exposé Dsa

The document provides an overview of data structures and algorithms related to heaps, including definitions and construction methods for Max Heaps, Min Heaps, Binomial Heaps, and Fibonacci Heaps. It discusses merging techniques for binary heap trees and compares the time complexities of various heap operations. Key takeaways highlight the advantages of Binomial and Fibonacci heaps over Binary heaps in terms of time efficiency for specific operations.

Uploaded by

Hardy Goddy
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/ 12

REPUBLIC OF CAMEROON REPUBLIQUE DU CAMEROUN

Peace-Work-Fatherland Paix-Travail-Patrie
MINISTRY OF HIGHER EDUCATION MINISTERE DE L’ENSEIGNEMENT SUPERIEUR
THE UNIVERSITY OF BAMENDA UNIVERSITE DE BAMENDA
FACULTY OF SCIENCE FACULTE DES SCIENCES
P.O. Box 39 Bambili P.B. 39 Bambili
Tel/Fax: (237) 233 366 040 Tel/Fax: (237) 233 366 040

Data Structure and Algorithms

Group 2 Members Registration Number

1 BETANGA HELEN (Leader)

2 AWAH GOTHARD NDEMOFOR UBa23S0259

3 SALAH VINCENT UBa23S0372

Course Code: CSCS3102


Course Instructor: MR NJONGWE JAMES
8.6: BUILDING A NEW HEAP TREE FROM SCRATCH
Heap is a special case of balanced binary tree data structure where the root-node key is compared
with its children and arranged accordingly. If α has child node β then −
key(α) ≥ key(β)
As the value of parent is greater than that of child, this property generates Max Heap. Based on
this criteria, a heap can be of two types −
For Input → 35 33 42 10 14 19 27 44 26 31
Min-Heap − Where the value of the root node is less than or equal to either of its children as can
be seen below.

Max-Heap − Where the value of the root node is greater than or equal to either of its children.

Max Heap Construction Algorithm


We shall use the same example to demonstrate how a Max Heap is created. The procedure to
create Min Heap is similar but we go for min values instead of max values.
We are going to derive an algorithm for max heap by inserting one element at a time. At any
point of time, heap must maintain its property. While inserting, we also assume that we are
inserting a node in an already heapified tree.

Step 1 − Create a new node at the end of heap.


Step 2 − Assign new value to the node.
Step 3 − Compare the value of this child node with its parent.
Step 4 − If value of parent is less than child, then swap them.
Step 5 − Repeat step 3 & 4 until Heap property holds.
Note − In Min Heap construction algorithm, we expect the value of the parent node to be less
than that of the child node.
Also note – Both max heap and min heap can be built using a better approach:

Efficient approach: Bottom up approach to build heap


Solution idea
The idea is to start from the bottom and apply the top-down heapify procedure at all internal
nodes!
The elements in the heap from index n/2 to n are all leaf nodes and a small heap of size 1. So we
simply skip the smaller heaps of size 1 and apply the top-down heapify process on all the
remaining nodes from index n/2 to 1(here we assume our array starts from 1, for simpler
computation).
In other words, this process establishes the heap order iteratively. We proceed from right to left
from index n/2 to 1 and make the smaller heaps as we go. The idea is that every position in the
array is the root of a small heap. So before applying the top-down heapify process to each node,
this algorithm ensures that both children of the current node are heap as well. There could be a
chance of violation of heap property at the current index, so we can easily apply the top-down
heapify procedure to fix this. We will use the example on the CSCS3102 handout to illustrate
this.

8.7 MERGING BINARY HEAP TREES


Merging two binary heap trees into a single priority queue is a common operation in computer
science. There are three ways to achieve this:

Method 1: Moving Items from the Smaller Tree


Move all items from the smaller tree to the larger tree using the standard insert algorithm. This
involves:
- Moving O(n) items
- Bubbling up each item at a cost of O(log2 n)
- Overall time complexity: O(nlog2 n)

Method 2: Repeatedly Moving Last Items


Repeatedly move the last items from one tree to the other until the new binary tree is complete.
Then:
- Move the last item of the new tree to replace the dummy root "0"
- Bubble down the new root
- Time complexity depends on the sizes of the two trees

Method 3: Using the makeTree Procedure


Use the makeTree procedure to merge the two trees. This involves:
- Creating a new binary tree
- Moving items from the original trees to the new tree
- Time complexity: O(n) for array-based trees

How to Choose the Best Method?


The best method depends on the sizes of the two trees. If the trees are similarly sized, Method 3
is usually the best choice. If the trees have very different sizes, Method 1 may be more efficient.

Key Words and Definitions


1. Binary Heap Tree: A type of heap that consists of a single binary tree.
2. Priority Queue: A data structure that allows for efficient insertion and deletion of elements
based on their priority.
3. Merge: The operation of combining two or more heaps into a single heap.
4. Time Complexity: The measure of the amount of time an algorithm takes to complete.
5. O(n): A time complexity notation that represents the number of operations required to
complete an algorithm.
6. O(log2 n): A time complexity notation that represents the number of operations required to
complete an algorithm.
7. Array-Based: A type of implementation where the heap is stored in an array.
8. Pointer-Based: A type of implementation where the heap is stored using pointers.
9. makeTree Procedure: A procedure used to merge two binary heap trees into a single tree.

8.8 BINOMIAL HEAP

Definitions of some key terms used in binomial heaps:


• Binomial Tree: A binomial tree is a tree data structure in which each node has at most
two children (i.e., left child and right child), and the degree of each node (number of
children) is either 0 or 1.
• Degree of a Node: The degree of a node in a binomial tree is the number of children it
has. In a binomial heap, the degree of each node is either 0 (no children) or 1 (one child).
• Order of a Binomial Tree: The order of a binomial tree is the number of nodes in the
tree. A binomial tree of order k has 2^k - 1 nodes.
• Binomial Heap: A binomial heap is a collection of binomial trees, each with a distinct
degree (number of children). The trees in the heap are ordered in a way that the root of
each tree is greater than (or less than) the roots of all other trees.
• Root Node: The root node of a binomial tree is the topmost node, which has no parent.
• Child Node: A child node is a node that has a parent node.
• Sibling Node: A sibling node is a node that has the same parent as another node.
• Parent Node: A parent node is a node that has one or more child nodes.
• Heap Property: The heap property states that for any given node, the value of the node
is either greater than (or less than) the values of its child nodes.
• Extract Min/Max: An extract min/max operation removes and returns the minimum (or
maximum) node from the binomial heap.
• Insert: An insert operation adds a new node to the binomial heap, maintaining the heap
property.
• Merge :A merge operation combines two binomial heaps into one, maintaining the heap
property.
8.9 FIBONACCI HEAPS

Recall:
Definition (Binary Heap): A binary heap is a complete binary tree where each parent node is
either greater than or less than its child nodes. Two types of Binary trees exist which are the max
heap and min heap.
Definition (Complete Binary tree): A binary tree is complete if every level, except maybe the
last is completely filled and all the leaf nodes are placed as far to the left as possible.
Definition (Binomial Heap): A binomial heap is similar to a binary tree and is a collection of
binary trees used for priority queuing but has the advantage for insertion and merging.

Similar to the Binomial Heap, the Fibonacci Heap can also satisfy the standard priority-ordering
property
Definition (Fibonacci Heap)
A Fibonacci heap is a data structure used for priority-queuing consisting of a collection of min-
heap-ordered trees with each tree having its own root node. The roots of these trees are linked
together using a circular doubly-linked list, often referred to as the “root list”

Structure of a Fibonacci Heaps


Fibonacci heaps are more flexible and complex structure than Binomial or Binary heaps. The
trees in a Fibonacci heap don’t have a fixed shape, and in extreme cases, every element in the
heap could be a possibly separate tree. The roots of all the trees are stored using a circular doubly
linked list, and the children of each node are handled in the same way.

Operations on Fibonacci Heaps


Some of the following operations can be supported by Fibonacci heaps: Insert, Merge, Extract-
min, Decrease-key
1. Insert: Inserting of a new element into a heap can be done by creating a new tree with
the added element as it’s root and adding it to the circular doubly linked list. This has a
time complexity of O(1).
2. Merge (Union): Merging two Fibonacci heaps can be done by joining the two lists of
root nodes. This operation has the time complexity of O(1).
3. Extract-min: The Extract-min is done by first finding the minimum root in the tree
which should be the root node with the lowest value. We then remove the minimum root
from the tree and add its children to the root linked list if any by merging. We then return
the minimum value
4. Decrease-key: Decreasing the priority of an element involves updating the mark of the
node and potentially cascading cuts. This operation has a time complexity of O(1).
5. Delete: The deleting program involves removing the highest priority root node and
updating the pointer to the highest priority root. This has a time complexity of O(log n)
The delete algorithm in
Time Complexity Analysis
The time complexity of Fibonacci heap operations is analyzed using the potential function:
φ(H) = t(H) + 2mH
where t(H) is the number of trees in the heap and m(H) is the number of marked nodes

Question: What makes Fibonacci particularly an efficient data structure to other data structure?
Answer: Fibonacci heaps have a flexible tree structure which allows them to adapt to different
scenarios which enables them to achieve better time complexities.
Fibonacci heaps and be merged and inserted very fast with merging in O(1) time and insertion by
merging the existing heap with a new heap consisting only of the new node resulting in an O(n)
time complexity for creating a heap from scratch.
8.10 COMPARING HEAP TIME COMPLEXES

When it comes to heaps, time complexity matters. Heaps are data structures that allow for
efficient sorting and prioritization. There are three main types of heaps: Binary, Binomial, and
Fibonacci. Each has its own strengths and weaknesses.

Main Procedures
The key operations that affect time complexity are:
1. Insert: Adding a new element to the heap.
2. Delete: Removing an element from the heap.
3. Merge: Combining two or more heaps into a single heap.
4. Heapify Up Priority: Rearranging the heap to maintain the heap property after an element's
priority is increased.

Time Complexity Comparison


Here's a comparison of the average time complexities for each heap type:

Heap Type Insert Delete Merge Heapify Up Priority


Binary O(log2 n) O(log2 n) O(n) O(log2 n)
Binomial O(1) O(log2 n) O(log2 n) O(log2 n)
Fibonacci O(1) O(log2 n) O(1) O(1)

Key Takeaways
- Binomial and Fibonacci heaps offer better time complexity than Binary heaps for certain
operations.
- The choice of heap depends on the specific application and requirements.
- Fibonacci heaps are particularly useful in practice, as they're used in efficient algorithms like
Dijkstra's and Prim's.

Key Words and Definitions


1. Binary Heap Tree: A type of heap that consists of a single binary tree.
2. Binomial Heap: A type of heap that consists of a collection of binomial trees.
3. Fibonacci Heap: A type of heap that consists of a collection of trees that satisfy the Fibonacci
heap property.
4. Time Complexity: The measure of the amount of time an algorithm takes to complete.
5. Priority Queue: A data structure that allows for efficient insertion and deletion of elements
based on their priority.
6. Dijkstra's Algorithm: An algorithm for finding the shortest route between two nodes in a
graph.
7. Prim's Algorithm: An algorithm for finding the minimal spanning tree of a graph.
8. O(log2 n): A time complexity notation that represents the number of operations required to
complete an algorithm.
9. O(n): A time complexity notation that represents the number of operations required to
complete an algorithm.

References:
1. Advance data structure by Shahin kamali, York university.
2. Binomial heaps by Vuillimen.
3. Tutorialspoint.

You might also like