SlideShare a Scribd company logo
K-aryHeap
K-ary Heap
• K-ary heaps are a generalization of binary
heap(K=2) in which each node have K children
instead of 2.
K-ary Heap
• K-ary heaps are a generalization of binary heap(K=2)
in which each node have K children instead of 2.
K-ary Heap
• Just like binary heap, it follows two properties:
1) Nearly complete binary tree, with all levels having
maximum number of nodes except the last, which is filled
in left to right manner.
2) Like Binary Heap, it can be divided into two categories:
(a) Max k-ary heap (key at root is greater than all descendants
and same is recursively true for all nodes).
(b) Min k-ary heap (key at root is lesser than all descendants
and same is recursively true for all nodes).
K-ary Heap: Implementation
Assuming 0 based indexing of array, an array
represents a K-ary heap such that for any node
we consider:
– Parent of the node at index i (except root node) is
located at index (i-1)/k
– Children of the node at index i are at indices
(k*i)+1 , (k*i)+2 …. (k*i)+k
– The last non-leaf node of a heap of size n is
located at index (n-2)/k
Sparse Set
Sparse Set
• The sparse set is a useful storage container
designed to store unsigned integers.
• It is essentially a simplified concept of a hash
map, allowing for guaranteed constant-time
insertion and lookup.
• Clearing the set is also extremely efficient
whilst removing individual elements.
• How to do the following operations efficiently if
there are large number of queries for them.
– Insertion
– Deletion
– Searching
– Clearing/Removing all the elements.
Sparse Set
• One solution is to use a Self-Balancing Binary Search
Tree like Red-Black Tree, AVL Tree, etc.
• Time complexity of this solution for insertion,
deletion and searching is O(Log n).
• We can also use Hashing. With hashing, time
complexity of first three operations is O(1). But time
complexity of the fourth operation is O(n).
Sparse Set
• Sparse Set outperforms BST and Hashing.
• We assume that we are given range of data (or
maximum value an element can have) and maximum
number of elements (capacity) that can be stored in
set.
• The idea is to maintain two arrays: sparse[] and
dense[].
Sparse Set
• The underlying structure of both the arrays be like
Dense
Sparse
Sparse Set: Insertion
• When an integer, x, is inserted, it is appended to
array Dense[].
• Its resultant index in Dense[] is then inserted
into Sparse[] at index x.
• For example, if inserting the integer 5 into the set in
the image above, we would
set Dense[3] to 5 and Sparse[5] to 3.
Sparse Set : Search
• To search an element x, we use x as index in sparse[].
• The value sparse[x] is used as index in dense[].
• And if value of dense[sparse[x]] is equal to x, we
return dense[x]. Else we return -1.
Sparse Set : Deletion
• To delete an element x, we replace it with last
element in dense[] and update index of last element in
sparse[]. Finally decrement n by 1.
• We can print all elements by simply traversing
dense[].
Sparse Set
• Let there be a set with two elements {3, 5}, maximum
value as 10 and capacity as 4. The set would be
represented as below.
• MaxVal = 10 // Size of sparse
• Capacity = 4 // Size of dense
• n = 2 // Current number of elements in set
Sparse Set
// dense[] Stores actual elements
• dense[] = {3, 5, _, _}
// Uses actual elements as index and stores indexes of
dense[]
• sparse[] = {_, _, _, 0, _, 1, _, _, _, _,}
Sparse Set
Insert 7:
n = 3
dense[] = {3, 5, 7, _}
sparse[] = {_, _, _, 0, _, 1, _, 2, _, _,}
Sparse Set
Insert 4:
n = 4
dense[] = {3, 5, 7, 4}
sparse[] = {_, _, _, 0, 3, 1, _, 2, _, _,}
Sparse Set
Delete 3:
n = 3
dense[] = {4, 5, 7, _}
sparse[] = {_, _, _, _, 0, 1, _, 2, _, _,}
Sparse Set
Clear (Remove All):
n = 0
dense[] = {_, _, _, _}
sparse[] = {_, _, _, _, _, _, _, _, _, _,}

More Related Content

Similar to sparse set.pdf

Unit 2 linear data structures
Unit 2   linear data structuresUnit 2   linear data structures
Unit 2 linear data structuresSenthil Murugan
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.pptLegesseSamuel
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Anand Ingle
 
03-data-structures.pdf
03-data-structures.pdf03-data-structures.pdf
03-data-structures.pdfNash229987
 
USING JAVAThere are at least two types of nearly sorted array.pdf
USING JAVAThere are at least two types of nearly sorted array.pdfUSING JAVAThere are at least two types of nearly sorted array.pdf
USING JAVAThere are at least two types of nearly sorted array.pdflohithkart
 
Master of Computer Application (MCA) – Semester 4 MC0080
Master of Computer Application (MCA) – Semester 4  MC0080Master of Computer Application (MCA) – Semester 4  MC0080
Master of Computer Application (MCA) – Semester 4 MC0080Aravind NC
 
Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Getachew Ganfur
 
chapter 6.1.pptx
chapter 6.1.pptxchapter 6.1.pptx
chapter 6.1.pptxTekle12
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1blessyboban92
 
data structure unit -1_170434dd7400.pptx
data structure unit -1_170434dd7400.pptxdata structure unit -1_170434dd7400.pptx
data structure unit -1_170434dd7400.pptxcoc7987515756
 

Similar to sparse set.pdf (20)

Unit 2 linear data structures
Unit 2   linear data structuresUnit 2   linear data structures
Unit 2 linear data structures
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 
Randamization.pdf
Randamization.pdfRandamization.pdf
Randamization.pdf
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
 
Arrays.pptx
Arrays.pptxArrays.pptx
Arrays.pptx
 
Chapter 15
Chapter 15Chapter 15
Chapter 15
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure
 
03-data-structures.pdf
03-data-structures.pdf03-data-structures.pdf
03-data-structures.pdf
 
Unit III Heaps.ppt
Unit III Heaps.pptUnit III Heaps.ppt
Unit III Heaps.ppt
 
USING JAVAThere are at least two types of nearly sorted array.pdf
USING JAVAThere are at least two types of nearly sorted array.pdfUSING JAVAThere are at least two types of nearly sorted array.pdf
USING JAVAThere are at least two types of nearly sorted array.pdf
 
Heap sort
Heap sort Heap sort
Heap sort
 
Linear sorting
Linear sortingLinear sorting
Linear sorting
 
Master of Computer Application (MCA) – Semester 4 MC0080
Master of Computer Application (MCA) – Semester 4  MC0080Master of Computer Application (MCA) – Semester 4  MC0080
Master of Computer Application (MCA) – Semester 4 MC0080
 
Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
 
‏‏Lecture 2.pdf
‏‏Lecture 2.pdf‏‏Lecture 2.pdf
‏‏Lecture 2.pdf
 
chapter 6.1.pptx
chapter 6.1.pptxchapter 6.1.pptx
chapter 6.1.pptx
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1
 
data structure unit -1_170434dd7400.pptx
data structure unit -1_170434dd7400.pptxdata structure unit -1_170434dd7400.pptx
data structure unit -1_170434dd7400.pptx
 
sorting
sortingsorting
sorting
 

Recently uploaded

Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfQucHHunhnh
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...Sayali Powar
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxEduSkills OECD
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismDeeptiGupta154
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...Nguyen Thanh Tu Collection
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdfCarlosHernanMontoyab2
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsCol Mukteshwar Prasad
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdfTechSoup
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativePeter Windle
 
Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resourcesdimpy50
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXMIRIAMSALINAS13
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...Jisc
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfTamralipta Mahavidyalaya
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptxJosvitaDsouza2
 
Accounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfAccounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfYibeltalNibretu
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationDelapenabediema
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleCeline George
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxakshayaramakrishnan21
 
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxSolid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxDenish Jangid
 

Recently uploaded (20)

Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resources
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Accounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfAccounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdf
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
 
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxSolid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
 

sparse set.pdf

  • 2. K-ary Heap • K-ary heaps are a generalization of binary heap(K=2) in which each node have K children instead of 2.
  • 3. K-ary Heap • K-ary heaps are a generalization of binary heap(K=2) in which each node have K children instead of 2.
  • 4. K-ary Heap • Just like binary heap, it follows two properties: 1) Nearly complete binary tree, with all levels having maximum number of nodes except the last, which is filled in left to right manner. 2) Like Binary Heap, it can be divided into two categories: (a) Max k-ary heap (key at root is greater than all descendants and same is recursively true for all nodes). (b) Min k-ary heap (key at root is lesser than all descendants and same is recursively true for all nodes).
  • 5. K-ary Heap: Implementation Assuming 0 based indexing of array, an array represents a K-ary heap such that for any node we consider: – Parent of the node at index i (except root node) is located at index (i-1)/k – Children of the node at index i are at indices (k*i)+1 , (k*i)+2 …. (k*i)+k – The last non-leaf node of a heap of size n is located at index (n-2)/k
  • 7. Sparse Set • The sparse set is a useful storage container designed to store unsigned integers. • It is essentially a simplified concept of a hash map, allowing for guaranteed constant-time insertion and lookup. • Clearing the set is also extremely efficient whilst removing individual elements.
  • 8. • How to do the following operations efficiently if there are large number of queries for them. – Insertion – Deletion – Searching – Clearing/Removing all the elements.
  • 9. Sparse Set • One solution is to use a Self-Balancing Binary Search Tree like Red-Black Tree, AVL Tree, etc. • Time complexity of this solution for insertion, deletion and searching is O(Log n). • We can also use Hashing. With hashing, time complexity of first three operations is O(1). But time complexity of the fourth operation is O(n).
  • 10. Sparse Set • Sparse Set outperforms BST and Hashing. • We assume that we are given range of data (or maximum value an element can have) and maximum number of elements (capacity) that can be stored in set. • The idea is to maintain two arrays: sparse[] and dense[].
  • 11. Sparse Set • The underlying structure of both the arrays be like Dense Sparse
  • 12. Sparse Set: Insertion • When an integer, x, is inserted, it is appended to array Dense[]. • Its resultant index in Dense[] is then inserted into Sparse[] at index x. • For example, if inserting the integer 5 into the set in the image above, we would set Dense[3] to 5 and Sparse[5] to 3.
  • 13. Sparse Set : Search • To search an element x, we use x as index in sparse[]. • The value sparse[x] is used as index in dense[]. • And if value of dense[sparse[x]] is equal to x, we return dense[x]. Else we return -1.
  • 14. Sparse Set : Deletion • To delete an element x, we replace it with last element in dense[] and update index of last element in sparse[]. Finally decrement n by 1. • We can print all elements by simply traversing dense[].
  • 15. Sparse Set • Let there be a set with two elements {3, 5}, maximum value as 10 and capacity as 4. The set would be represented as below. • MaxVal = 10 // Size of sparse • Capacity = 4 // Size of dense • n = 2 // Current number of elements in set
  • 16. Sparse Set // dense[] Stores actual elements • dense[] = {3, 5, _, _} // Uses actual elements as index and stores indexes of dense[] • sparse[] = {_, _, _, 0, _, 1, _, _, _, _,}
  • 17. Sparse Set Insert 7: n = 3 dense[] = {3, 5, 7, _} sparse[] = {_, _, _, 0, _, 1, _, 2, _, _,}
  • 18. Sparse Set Insert 4: n = 4 dense[] = {3, 5, 7, 4} sparse[] = {_, _, _, 0, 3, 1, _, 2, _, _,}
  • 19. Sparse Set Delete 3: n = 3 dense[] = {4, 5, 7, _} sparse[] = {_, _, _, _, 0, 1, _, 2, _, _,}
  • 20. Sparse Set Clear (Remove All): n = 0 dense[] = {_, _, _, _} sparse[] = {_, _, _, _, _, _, _, _, _, _,}