0% found this document useful (0 votes)
26 views

Data Structures Module 3

This document discusses priority queues and heap data structures. It begins by defining a priority queue as a special type of queue where each element has an associated priority and elements are served according to their priority. It then discusses how priorities can be assigned, the difference between priority queues and normal queues, and the heap data structure. Heap operations like heapify, insertion, and deletion are explained along with applications of heaps like implementing priority queues and Dijkstra's algorithm. External sorting is also briefly discussed.

Uploaded by

privatedummyacc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Data Structures Module 3

This document discusses priority queues and heap data structures. It begins by defining a priority queue as a special type of queue where each element has an associated priority and elements are served according to their priority. It then discusses how priorities can be assigned, the difference between priority queues and normal queues, and the heap data structure. Heap operations like heapify, insertion, and deletion are explained along with applications of heaps like implementing priority queues and Dijkstra's algorithm. External sorting is also briefly discussed.

Uploaded by

privatedummyacc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

MODULE-III

PRIORITY QUEUES, HEAP DATA STRUCTURE

Ashwini S
Assistant Professor
Ashwini S Data Structures with C++ 25-01-2024

2 PRIORITY QUEUE

• A priority queue is a special type of queue in which each element is associated with a
priority value.
• And, elements are served on the basis of their priority. That is, higher priority elements
are served first.
• However, if elements with the same priority occur, they are served according to their
order in the queue.
Ashwini S Data Structures with C++ 25-01-2024

3 ASSIGNING PRIORITY VALUE

• Generally, the value of the element itself is considered for


assigning the priority. For example,
• The element with the highest value is considered the highest
priority element.
• However, in other cases, we can assume the element with the
lowest value as the highest priority element.
• We can also set priorities according to our needs.
Ashwini S Data Structures with C++ 25-01-2024

4 DIFFERENCE BETWEEN PRIORITY QUEUE AND


NORMAL QUEUE
• In a queue, the first-in-first-out rule is implemented whereas, in a priority queue, the
values are removed on the basis of priority.
• The element with the highest priority is removed first.
Ashwini S Data Structures with C++ 25-01-2024

5
Ashwini S Data Structures with C++ 25-01-2024

6
Ashwini S Data Structures with C++ 25-01-2024

7
Ashwini S Data Structures with C++ 25-01-2024

8 HEAP DATA STRUCTURE

• Heap data structure is a complete binary tree that


satisfies the heap property, where any given node
is:
• always greater than its child node/s and the key of
the root node is the largest among all other
nodes. This property is also called max heap
property.
• always smaller than the child node/s and the key
of the root node is the smallest among all other
nodes. This property is also called min heap
property.
Ashwini S Data Structures with C++ 25-01-2024

9 HEAP OPERATIONS

Heapify
• Heapify is the process of creating a heap data structure from a binary tree. It is used to
create a Min-Heap or a Max-Heap.
1. Let the input array be
Ashwini S Data Structures with C++ 25-01-2024

10

2. Create a complete binary tree from the array


3. Start from the first index of non-leaf node whose index
is given by n/2 -1
Ashwini S Data Structures with C++ 25-01-2024

11

4.Set current element i as largest.


5. The index of left child is given by 2i + 1 and the right child is given
by 2i + 2.
6. If leftChild is greater than currentElement (i.e. element at ith index),
set leftChildIndex as largest.
7. If rightChild is greater than element in largest, set rightChildIndex as
largest.
8. Swap largest with currentElement
9. Repeat steps 3-7 until the subtrees are also heapified.
Ashwini S Data Structures with C++ 25-01-2024

12 ALGORITHM
Ashwini S Data Structures with C++ 25-01-2024

13 INSERT ELEMENT INTO HEAP

• Algorithm for insertion in Max Heap

1. Insert the new element at the end of the tree.


Ashwini S Data Structures with C++ 25-01-2024

14

2. Heapify the tree.


Ashwini S Data Structures with C++ 25-01-2024

15 DELETE ELEMENT FROM HEAP

• Algorithm for deletion in Max Heap


Ashwini S Data Structures with C++ 25-01-2024

16 1. SELECT THE ELEMENT TO BE DELETED


Ashwini S Data Structures with C++ 25-01-2024

17 2. SWAP IT WITH THE LAST ELEMENT.


Ashwini S Data Structures with C++ 25-01-2024

18 3. REMOVE THE LAST ELEMENT.


Ashwini S Data Structures with C++ 25-01-2024

19 4. HEAPIFY THE TREE.


Ashwini S Data Structures with C++ 25-01-2024

20
Ashwini S Data Structures with C++ 25-01-2024

21
Ashwini S Data Structures with C++ 25-01-2024

22
Ashwini S Data Structures with C++ 25-01-2024

23
Ashwini S Data Structures with C++ 25-01-2024

24 HEAP DATA STRUCTURE APPLICATIONS

• Heap is used while implementing a priority queue.


• Dijkstra's Algorithm

• Heap Sort
Ashwini S Data Structures with C++ 25-01-2024

25 RELATIONSHIP BETWEEN ARRAY INDEXES AND


TREE ELEMENTS
• A complete binary tree has an interesting property
that we can use to find the children and parents of
any node.
• If the index of any element in the array is i, the
element in the index 2i+1 will become the left
child and element in 2i+2 index will become the
right child.
• Also, the parent of any element at index i is given
by the lower bound of (i-1)/2.
Ashwini S Data Structures with C++ 25-01-2024

26
Ashwini S Data Structures with C++ 25-01-2024

27 BUILD MAX-HEAP

• To build a max-heap from any tree, we can thus start


heapifying each sub-tree from the bottom up and end
up with a max-heap after the function is applied to all
the elements including the root element.
• In the case of a complete tree, the first index of a
non-leaf node is given by n/2 - 1. All other nodes
after that are leaf-nodes and thus don't need to be
heapified.
• So, we can build a maximum heap as
Ashwini S Data Structures with C++ 25-01-2024

28
Ashwini S Data Structures with C++ 25-01-2024

29
Ashwini S Data Structures with C++ 25-01-2024

30 WORKING OF HEAP SORT

1. Since the tree satisfies Max-Heap property, then the largest item is stored at the root node.
2. Swap: Remove the root element and put at the end of the array (nth position) Put the last item
of the tree (heap) at the vacant place.
3. Remove: Reduce the size of the heap by 1.
4. Heapify: Heapify the root element again so that we have the highest element at root.
5. The process is repeated until all the items of the list are sorted.
Ashwini S Data Structures with C++ 25-01-2024

31
Ashwini S Data Structures with C++ 25-01-2024

32
Ashwini S Data Structures with C++ 25-01-2024

33
Ashwini S Data Structures with C++ 25-01-2024

34 DEQUE DATA STRUCTURE

• Deque or Double Ended Queue is a type of queue in which insertion and removal of
elements can either be performed from the front or the rear. Thus, it does not follow
FIFO rule (First In First Out).
Ashwini S Data Structures with C++ 25-01-2024

35 TYPES OF DEQUE

1. Input Restricted Deque


In this deque, input is restricted at a single end but allows deletion at both the ends.
2. Output Restricted Deque
In this deque, output is restricted at a single end but allows insertion at both the ends.
Ashwini S Data Structures with C++ 25-01-2024

36
Ashwini S Data Structures with C++ 25-01-2024

37
Ashwini S Data Structures with C++ 25-01-2024

38
Ashwini S Data Structures with C++ 25-01-2024

39
Ashwini S Data Structures with C++ 25-01-2024

40 APPLICATIONS OF DEQUE DATA STRUCTURE

1. In undo operations on software.


2. To store history in browsers.
3. For implementing both stacks and queues.
Ashwini S Data Structures with C++ 25-01-2024

41 EXTERNAL SORTING

• External sorting is a term for a class of sorting algorithms that can handle massive
amounts of data. External sorting is required when the data being sorted does not fit into
the main memory of a computing device (usually RAM) and instead, must reside in the
slower external memory (usually a hard drive).

• External sorting typically uses a hybrid sort-merge strategy. In the sorting phase, chunks
of data small enough to fit in the main memory are read, sorted, and written out to a
temporary file. In the merge phase, the sorted sub-files are combined into a single larger
file.
Ashwini S Data Structures with C++ 25-01-2024

42 EXTERNAL SORTING PHASES

1. Distribution Phase: The data is divided into smaller


chunks called runs, which are sorted using an internal
sorting algorithm like merge sort or quicksort. These
runs are then written to secondary storage (e.g.,
disk).
2. Merging Phase: The sorted runs are repeatedly
merged in pairs until a single sorted file is obtained.
Different merge algorithms can be used in this phase,
such as polyphase merge and multiway merge.
Ashwini S Data Structures with C++ 25-01-2024

43 POLYPHASE MERGE

• Polyphase merge is a variation of the bottom-up merge sort algorithm optimized for
external sorting. It uses an uneven distribution of sub-lists in the initial merging phase,
making it more efficient than traditional merge sort when there are fewer than 8 external
working files.

Key Features of Polyphase Merge:


• Employs a balanced merge in the final phase.
• Reduces disk seeks by minimizing rewinds and fast-forwards.
• Not a stable sorting algorithm (doesn't preserve the original order of equal elements).
Ashwini S Data Structures with C++ 25-01-2024

44 MULTIWAY MERGE

• Multiway merge is another external sorting algorithm that merges multiple sorted runs
simultaneously. It requires more storage space than polyphase merge (one input file and one
output file per way) but can be faster for larger datasets and with more available storage
devices.

Key Features of Multiway Merge:


• Faster for large datasets due to parallel merging.
• Requires more storage space compared to polyphase merge.
• Utilizes priority queues for efficient merging.
Ashwini S Data Structures with C++ 25-01-2024

45 APPLICATIONS OF EXTERNAL SORTING

Finance:
• Fraud detection by analyzing millions of transactions
• Portfolio optimization with large market data sets
• Credit risk assessment from vast credit histories
Healthcare:
• Genomic analysis of DNA sequencing data
• Efficient comparison of medical images for diagnosis
• Tracking and analyzing disease outbreaks
Ashwini S Data Structures with C++ 25-01-2024

46

Other Industries:
• E-commerce: personalizing recommendations, targeted advertising, and fraud detection
• Log analysis: website traffic, server logs, and security issues
• Scientific Computing: analyzing simulations and measurements
Everyday Applications:
• Email sorting algorithms utilize external sorting to organize large inboxes and prioritize
messages based on sender, keywords, and importance.
• Personal finance software may use external sorting to categorize and analyze personal
expenses for budgeting and financial planning.
Ashwini S Data Structures with C++ 25-01-2024

47

THANK YOU

You might also like