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

Design and Analysis Unit2

Uploaded by

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

Design and Analysis Unit2

Uploaded by

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

Noida Institute of Engineering and Technology, Greater

Noida

Advanced Data Structure

Unit: 2

Design & Analysis of Algorithms


ANKITA SHARMA
ACSE0501
Assistant Professor
CSE 4th Sem CSE.

ANKITA SHARMA ACSE0401 DAA Unit 2


1
12/24/2024
Evaluation Scheme

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 2


Syllabus

ANKITA SHARMA ACSE0401 DAA Unit 2


12/24/2024 3
Branch wise Application

• In Data mining
• Image Processing
• Digital Signature.
• DNA Matching.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 4


Course Objective

• Upon completion of this course, students will be able to do the


following:
• Analyze the asymptotic performance of algorithms.
• Write rigorous correctness proofs for algorithms.
• Demonstrate a familiarity with major algorithms and data structures.
• Apply important algorithmic design paradigms and methods of
analysis.
• Synthesize efficient algorithms in common engineering design
situations.

ANKITA SHARMA ACSE0401 DAA Unit 2


12/24/2024 5
Course Outcome

At the end of the semester, the student will be able:


Description Bloom’s Taxonomy
CO1 To have knowledge of basic principles of algorithm design and Knowledge, analysis
Analysis, asymptotic notations and growth of functions for time And design
and space complexity analysis and applying the same in different
sorting algorithms
CO2 To apply different problem-solving approaches for advanced data Knowledge, analysis
structures And apply
CO3 To apply divide and conquer method for solving merge sort, quick Knowledge, analysis and
sort, matrix multiplication and Greedy Algorithm for solving Apply
different Graph Problem.

CO4 To analyze and apply different optimization techniques like Knowledge, Analysis And
dynamic programming, backtracking and Branch & Bound to solve Apply
the complex problems
CO5 To understand the advanced concepts like NP Completeness and Knowledge, Analysis and
Fast Fourier Transform, to analyze and apply String Matching, Apply
Approximation and Randomized Algorithms to solve the complex
problems

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 6


Program Outcome

At the end of the semester, the student will be able:


POs Engineering Graduates will be able to
PO1 Engineering Knowledge
PO2 Problem Analysis
PO3 Design & Development of solutions
PO4 Conduct Investigation of complex problems
PO5 Modern Tool Usage
PO6 The Engineer and Society
PO7 Environment and sustainability
PO8 Ethics
PO9 Individual & Team work
PO10 Communication
PO11 Project management and Finance
PO12 Life Long Learning

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 7


CO-PO and PSO Mapping

Design and Analysis of Algorithm (kCS-502)

PO1
CO.K PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO12
1

ACSE0401.1 3 3 3 3 2 - - - 2 2 - 3

ACSE0401.2 3 3 3 3 2 2 - 1 1 1 - 3

ACSE0401.3 3 3 2 3 3 2 - 2 1 1 2 3

ACSE0401.4 3 3 3 3 2 2 - 2 2 1 3 3

ACSE0401.5 2 2 2 2 2 2 - 2 1 1 1 2

Average 2.8 2.8 2.6 2.8 2.2 1.6 - 1.8 1.4 1.2 1.2 2.8

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 8


Program Educational Objectives(PEOs)

PEO1: To have an excellent scientific and engineering breadth so as to


comprehend, analyze, design and provide sustainable solutions for
real-life problems using state-of-the-art technologies.
PEO2:To have a successful career in industries, to pursue higher studies or
to support enterpreneurial endeavors and to face global challenges.
PEO3:To have an effective communication skills, professional attitude,
ethical values and a desire to learn specific knowledge in emerging
trends, technologies for research, innovation and product
development and contribution to society.
PEO4: To have life-long learning for up-skilling and re-skilling for
successful professional career as engineer, scientist, enterpreneur
and bureaucrat for betterment of society

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 9


End Semester Question Paper Template

B TECH
(SEM-V) THEORY EXAMINATION 20__-20__
COMPILER DESIGN
Time: 3 Hours Total
Marks: 100
Note: 1. Attempt all Sections. If require any missing data; then choose
suitably.
SECTION A
1.Q.No.
Attempt all questions in brief.
Question Marks 2 xCO10 =
20
1 2
2 2
. .
10 2

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 10


End Semester Question Paper Templates

SECTION B
2. Attempt any three of the following: 3 x 10 = 30

Q.No. Question Mark CO


s
1 10
2 10
. .
5 SECTION C 10
3. Attempt any one part of the following: 1 x 10 = 10

Q.No. Question Marks CO

1 10
2 10
12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 11
End Semester Question Paper Templates
4. Attempt any one part of the following: 1 x 10 = 10
Q.No. Question Marks CO

1 10
2 10
5. Attempt any one part of the following: 1 x 10 = 10
Q.No. Question Marks CO
1 10
2 10

6. Attempt any one part of the following: 1 x 10 = 10


Q.No. Question Mark CO
s

1 10
2 10

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 12


Prerequisite and Recap

• Prerequisite
• Basic concept of c programming language.
• Concept of stack, queue and link list.

• Recap
• Flow Chart
• Algorithm

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 13


Unit Content

• Red-Black trees
• B – trees
.
• Binomial Heap
• Fibonacci Heaps
• Tries
• Skip list.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 14


Unit Objective

• Upon. completion of this course, students will be able to do the


following:
• Analyze the asymptotic performance of algorithms.
• Write rigorous correctness proofs for algorithms.
• Demonstrate a familiarity with major algorithms and data structures.
• Apply important algorithmic design paradigms and methods of
analysis.
• Synthesize efficient algorithms in common engineering design
situations.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 15


Topic Objective

• This Objective of this unit is to make the students aware about


• Advanced data structures
• Red Black Tree
• B Trees
• Fibonacci Heap
• Binomial Heap
• Tries
• Skip list.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 16


Red Black Trees(CO2)

Image Source: Comp 122,


Spring 2004

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 17


Red Black Trees(CO2)

• Why Red Black Tree

– Most of the BST operations (e.g., search, max, min, insert,


delete.. etc) take O(h) time where h is the height of the BST.

– Red-black trees are a variation of binary search trees to ensure


that the tree is balanced.

– Height is O(lg n), where n is the number of nodes.

– Operations take O(lg n) time in the worst case.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 18


Red Black Trees(CO2)

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 19


Red Black Trees(CO2)

Properties

• Binary search tree + 1 bit per node: the attribute color, which is
either red or black.
• These color bits are used to ensure that height of the tree remains
approximately balanced during insertions and deletions.
• A null leaf node is always considered to be a black node, not red.
• All other attributes of BSTs are inherited:
– key, left, right, and p.
• All empty trees (leaves) are colored black.
– We use a single sentinel, nil, for all the leaves of red-black
tree T, with color[nil] = black.
– The root’s parent is also nil[T ].
12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 20
Red Black Trees(CO2)

Properties
In simple words

1. Every node is either red or black.


2. The root is black.
3. Every leaf (nil) is black.
4. If a node is red, then both its children are black.
5. For each node, all paths from the node to descendant leaves
contain the same number of black nodes.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 21


Red Black Trees(CO2)

Example 26
Remember: every internal
node has two children,
even though nil leaves are
17 41 not usually shown.

30 47

38 50

nil[T]

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 22


Red Black Trees(CO2)

Height of a Red-black Tree


• Height of a node:
– h(x) = number of edges in a longest path to a leaf.
• Black-height of a node x, bh(x):
– bh(x) = number of black nodes (including nil[T ])
on the path from x to leaf, not counting x.
• Black-height of a red-black tree is the black-height of its root.

ANKITA SHARMA ACSE0401 DAA Unit


12/24/2024 23
2
Red Black Trees(CO2)

Example h=4
26 bh=2
• Height of a node:
h(x) = # of edges in a longest
path to a leaf. h=3
17 h=1 41 bh=2
• Black-height of a node bh(x) = bh=1
# of black nodes on path from x
to leaf, not counting x. h=2
h=2 30
47 bh=1
bh=1 h=1
• How are they related?
bh=1
– bh(x) ≤ h(x) ≤ 2 bh(x) h=1 50
38
bh=1

nil[T]
12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 24
2
RBBlack
Red TreeTrees(CO2)
• The leaf nodes of red–black trees do not contain data.

• A null child pointer (like NIL in previous figure) can be used as a leaf.

• In the figure, the leaves are considered to be explicit nodes.

• But in order to save some execution time, these NIL-leaves may be


implemented as sentinel nodes (instead of null pointers).

• In order to save memory, a single sentinel node (instead of many


individuals) may perform the role of all leaf nodes

ANKITA SHARMA ACSE0401 DAA Unit


12/24/2024 25
2
Red Black Trees(CO2)

Red Black Trees- Examples

ANKITA SHARMA ACSE0401 DAA Unit


12/24/2024 26
2
Red Black Trees(CO2)
Theorem
Black Height of a Red-Black Tree :

Black height is number of black nodes on a path from a node to a leaf.


Leaf nodes are also counted black nodes, According to property 4 and 5,
at least half the nodes on any simple path from the root to a leaf , not
including the root must be black. Therefore the black height of the root
must be at least h/2.

ANKITA SHARMA ACSE0401 DAA Unit


12/24/2024 27
2
Red Black Trees(CO2)
Theorem
Every Red Black Tree with n nodes has height <= 2Log2(n+1)
• This can be proved using following facts:
The subtree rooted at any node x contains at least 2bh(x) – 1 internal
nodes.
• We can prove this by induction on height of subtree rooted at x. if x is
the leaf(nil[T]), so the height of x is 0, then the subtree rooted at x
contains at least 2bh(x) – 1 = 20 – 1= 0 internal nodes .
• Consider an internal node x(black height bh(x)) with two children. Each
child has a black height of either bh(x) or bh(x)-1, depending on
whether its colour is red or black, respectively.(since atleast so we will
consider bh(x)-1 to be conservative).
• Therefore each child has atleast 2bh(x) – 1 internal nodes and the subtree
rooted at x contains at least (2bh(x)-1 – 1) + (2bh(x)-1 – 1) +1= 2. 2bh(x)-1 – 1=
2bh(x) – 1 internal nodes.
ANKITA SHARMA ACSE0401 DAA Unit
12/24/2024 28
2
Red Black Trees(CO2)

• Since bh(x)>= h/2


n>=2bh(x) – 1
n>= 2 h/2 – 1
(n+1)>= 2 h/2
log(n+1).>=h/2log2
h<=2log(n+1)

ANKITA SHARMA ACSE0401 DAA Unit


12/24/2024 29
2
Operations of Red Black Tree(CO2)

1. Insertion
2. Deletion

 The TREE-INSERT and TREE-DELETE operations in RBT are same as


BST, which runs in time O(h) in BST, where h is height of tree.
 However, the result may violate the red-black properties in RBT.
 To restore these properties, we must recolor some of the nodes in the
tree, and make some pointer changes.
 We use rotations to change the pointer structure.

ANKITA SHARMA ACSE0401 DAA Unit


12/24/2024 30
2
Operations of Red Black Tree(CO2)

Rotations
• Rotations are presented by the following figures.

• LEFT-ROTATION and RIGHT-ROTATION run in O(1) units of


time.
• Only the pointers are changed – the other fields remain the same.

x Left-Rotate(T, x) y

 Right-Rotate(T, y) x 
y

   

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2


31
Operations of Red Black Tree(CO2)
Left-Rotate(T,
Left-Rotate (T, x)
x)

1.1. yy right[x] //// Set


 right[x] Sety.y.
right[x]
2.2. right[x] left[y] //Turn
 left[y] //Turny’s
y’sleft
leftsubtree
subtreeinto
intox’s
x’sright
rightsubtree.
subtree.
left[y]
3.3. ififleft[y]  nil[T
nil[T]]
4.4. then p[left[y]]
thenp[left[y]]  xx
p[y]
5.5. p[y]  p[x]
p[x] //// Link
Linkx’s
x’sparent
parenttotoy.y.
p[x]=
6.6. ififp[x] = nil[T
nil[T]]
7.7. then root[T]]
thenroot[T  yy x Left-Rotate(T, x) y
8.8. elseififxx=
else = left[p[x]]
left[p[x]] 
x y Left-Rotate(T, x) y)
Right-Rotate(T, yx
9.9. then left[p[x]]
thenleft[p[x]]  yy
10.
10. else right[p[x]]
elseright[p[x]]  yy  y 
Right-Rotate(T, y) x 
11. left[y]
11. left[y]  xx ////Put
Putxxon
ony’s
y’sleft.
left.
   
12. p[x]
12. p[x] yy
(Similarly we can write algorithm for right rotation by exchanging x, y and
left, right in above algorithm)
ANKITA SHARMA ACSE0401 DAA Unit
12/24/2024 2 32
Operations of Red Black Tree(CO2)

Rotations

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 33


Operations of Red Black Tree(CO2)
Insertion

General description:

• We begin by inserting a node z into a tree T, as if T is an


ordinary BST.

• We color z red.

• We fix up the modified tree by re-coloring nodes and


performing rotations, to guarantee that the red-black
properties are preserved.
Insertion is accomplished in O( logn ) time.

ANKITA SHARMA ACSE0401 DAA Unit


12/24/2024 34
2
Operations of Red Black Tree(CO2)

RB-Insert(T,
RB-Insert(T,z)z) RB-Insert(T,
RB-Insert(T,z)z)Contd.
Contd.
1.1. yy nil[T]
nil[T] 14. left[z]
14. left[z] nil[T]
nil[T]
2.2. xx root[T]
root[T] 15. right[z]
15. right[z] nil[T]
nil[T]
whilexxnil[T]
3.3. while nil[T] 16. color[z]
16. color[z] RED
RED
4.4. doyy
do xx 17.
17. RB-Insert-Fixup
RB-Insert-Fixup(T,(T,z)z)
5.5. ififkey[z]
key[z]<<key[x]
key[x]
6.6. thenxx
then left[x]
left[x]
7.7. elsexx
else right[x]
right[x]
p[z]
8.8. p[z] yy
9.9. ififyy==nil[T]
nil[T] Which of the RB properties
10.
10. then root[T]
thenroot[T] zz might be violated?
11.
11. else
elseififkey[z]
key[z]<<key[y]
key[y]
12.
12. then left[y]
then left[y] zz Fix the violations by calling
13.
13. else right[y]
elseright[y] zz RB-Insert-Fixup.
12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 35
OperationsInsertion
of Red Black Tree(CO2)

RB-Insert-Fixup(T,
RB-Insert-Fixup (T, z)
z)
1.
1. while color[p[z]]=
whilecolor[p[z]] = RED
RED
2.
2. do p[z]=
doififp[z] = left[p[p[z]]]
left[p[p[z]]]
3.
3. thenyy
then  right[p[p[z]]]
right[p[p[z]]]
4.
4. color[y]=
ififcolor[y] = RED
RED
5.
5. then color[p[z]]
thencolor[p[z]] BLACK //// Case
 BLACK Case11
6.
6. color[y]
color[y] BLACK //// Case
 BLACK Case11
7.
7. color[p[p[z]]]
color[p[p[z]]] RED //// Case
 RED Case11
8.
8. zz p[p[z]]
p[p[z]] //// Case
Case11

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 36


OperationsInsertion
of Red Black Tree(CO2)

RB-Insert-Fixup(T,
RB-Insert-Fixup(T, z) z) (Contd.)
(Contd.)
9.
9. elseififzz=
else = right[p[z]] color[y]
right[p[z]] ////color[y] RED RED
10.
10. thenzz
then  p[z]
p[z] //// Case
Case22
11.
11. LEFT-ROTATE(T,
LEFT-ROTATE(T, z) z) //// Case
Case22
12.
12. color[p[z]]
color[p[z]]  BLACK
BLACK //// Case
Case33
13.
13. color[p[p[z]]]
color[p[p[z]]]  RED
RED //// Case
Case33
14.
14. RIGHT-ROTATE(T,
RIGHT-ROTATE(T, p[p[z]]) p[p[z]]) //// Case
Case33
15.
15. else
else(if
(ifp[z]
p[z]==right[p[p[z]]])(same
right[p[p[z]]])(sameas as10-14
10-14
16.
16. with
with“right”
“right”and
and“left”
“left”exchanged)
exchanged)
17. color[root[T]]]]
17.color[root[T  BLACK
BLACK

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 37


Operations of Red Black Tree(CO2)
Insertion – Analysis of the code
• The only property that might be violated in line 16 of RB-INSERT is
property 4: if z’s parent is red, then we have two reds in a row.
• The RB-INSERT-FIXUP code pushes this violation up the tree. It is either
corrected somewhere on the way up, or in the root. The other properties
are maintained.
• Assuming that each move up the tree takes O(1) time, the whole process
ends in O(h) time, as desired.
• We consider 6 cases, but 3 are symmetric copies of the other 3. It all
depends on whether z’s parent p[z] is a left or a right child of z’s
grandparent p[p[z]]].
*Note that there is an important assumption: the root of the tree is black!

ANKITA SHARMA ACSE0401 DAA Unit


12/24/2024 38
2
Operations of Red Black Tree(CO2)

Insertion – Analysis of the code

• Case 1 is distinguished from Cases 2 & 3 by the color of z’s


uncle (denoted by y). If y is red then Case 1 is executed.
Otherwise control passes to Cases 2 & 3.
• In all cases, z’s grandfather is black (since it’s father is red).
• Case 1 is shown in the following figure. [Both p[z] and y are
red, and their father is black. The two sons are colored black,
their father is colored red, and the possible problem is pushed
up the tree].

ANKITA SHARMA ACSE0401 DAA Unit


12/24/2024 39
2
Insertion Caseof1 –Red
Operations uncle y is red
Black Tree(CO2)
p[p[z]] new z
C C
p[z] y
A D A D

 z     
B
B
z is a right child here.
Similar steps if z is a left child.
 
 

• p[p[z]] (z’s grandparent) must be black, since z and p[z] are both red
and there are no other violations of property 4.
• Make p[z] and y black so now z and p[z] are not both red. But property
5 might now be violated.
• Make p[p[z]] red to restore property 5.
• The next iteration has p[p[z]] as the new z (i.e., z moves up 2 levels).
ANKITA SHARMA ACSE0401 DAA Unit
12/24/2024 40
2
Operations of Red Black Tree(CO2)

Insertion – Analysis of the code

• In Cases 2 & 3 the color of z’s uncle, y is black.


• The two are distinguished by whether z is the right child or the
left child of p[z].
• Using a left rotation we can move from Case 2 to Case 3.
• After that, z is the left son of p[z], both are red, and the uncle,
y, is black.
• Some color changes and a right rotation are necessary, but
there is no need to continue the while loop, since p[z] is
colored black.
• The following figure sums this up.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 41


Operations of Red Black Tree(CO2)
C C
p[z] p[z]
 y B  y
A D D
z
 z A 
B

  

Insertion Case 2 – y is black, z is a right child


• Left rotate around p[z], p[z] and z switch roles so now z is a left child,
and both z and p[z] are red.
• Takes us immediately to case 3.

ANKITA SHARMA ACSE0401 DAA Unit


12/24/2024 42
2
Operations of Red Black Tree(CO2)

C B
p[z]

B  y A C

z
   
A 

 

Insertion Case 3 – y is black, z is a left child


• Make p[z] black and p[p[z]] red.
• Then right rotate on p[p[z]]. Ensures property 4 is maintained.
• No longer have 2 reds in a row.
• p[z] is now black so no more iterations.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 43


Operations of Red Black Tree(CO2)

Example of Insertion in RB Tree “ inserting 4”

ANKITA SHARMA ACSE0401 DAA Unit


12/24/2024 44
2
Operations of Red Black Tree(CO2)

Example of Insertion in RB Tree

ANKITA SHARMA ACSE0401 DAA Unit


12/24/2024 45
2
Operations of Red Black Tree(CO2)

Example of Insertion in RB Tree

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 46


Operations of Red Black Tree(CO2)

Example-2 of Insertion in RB Tree

ANKITA SHARMA ACSE0401 DAA Unit


12/24/2024 47
2
Operations of Red Black Tree(CO2)

Deletion Operation in RBT

• The procedure we use is called RB-DELETE.


• It deletes a node like in a “regular” binary search tree, and
then calls the procedure RB-DELETE-FIXUP to fix colors
and perform rotations, to restore the red-black properties.

ANKITA SHARMA ACSE0401 DAA Unit


12/24/2024 48
2
OperationsDeletion
of Red Black Tree(CO2)

RB-Delete(T,
RB-Delete(T, z) z)
1. left[z]=
1. ififleft[z] = nil[T]
nil[T]or right[z]=
orright[z] = nil[T]
nil[T]
2.
2. thenyy
then  zz
3.
3. elseyy
else  TREE-SUCCESSOR(z)
TREE-SUCCESSOR(z)
4. left[y]=
4. ififleft[y] = nil[T
nil[T]]
5.
5. thenxx
then  left[y]
left[y]
6.
6. elsexx
else  right[y]
right[y]
7. p[x]
7. p[x]  p[y]
p[y] ////Do
Dothis,
this,even
evenififxxisisnil[T]
nil[T]

ANKITA SHARMA ACSE0401 DAA Unit


12/24/2024 2 49
OperationsDeletion
of Red Black Tree(CO2)

RB-Delete(T,
RB-Delete (T, z)z) (Contd.)
(Contd.)
8. p[y]=
8. ififp[y] = nil[T
nil[T]]
9.
9. then root[T]]
thenroot[T  xx
10.
10. elseelseififyy== left[p[y]]
left[p[y]]
11.
11. then left[p[y]]
thenleft[p[y]]  xx
12.
12. else right[p[y]]
elseright[p[y]]  xx
13.ififyy=
13. = zz
14.
14. then key[z]
thenkey[z]  key[y]
key[y] The node passed to
15.
15. copycopyy’s y’ssatellite
satellitedata
datainto
intozz the fixup routine is
16. color[y]=
16.ififcolor[y] = BLACK
BLACK the lone child of the
17. spliced up node, or
17. then thenRB-Delete-Fixup(T,
RB-Delete-Fixup(T, x) x)
18. the sentinel.
18.return
returnyy

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 50


Operations of Red Black Tree(CO2)

RB-Delete-Fixup(T,
RB-Delete-Fixup(T, x) x)
1. whilexx
1. while  root[T
root[T]]and color[x]=
andcolor[x] = BLACK
BLACK
2.
2. doififxx=
do = left[p[x]]
left[p[x]]
3.
3. thenw
then w  right[p[x]]
right[p[x]]
4.
4. color[w]=
ififcolor[w] = RED
RED
5.
5. then
thencolor[w]
color[w] BLACK
BLACK //// Case
Case11
6.
6. color[p[x]]
color[p[x]] RED
RED //// Case
Case11
7.
7. LEFT-ROTATE(T,
LEFT-ROTATE(T, p[x])p[x]) //// Case
Case11
8.
8. w
w  right[p[x]]
right[p[x]] //// Case
Case11

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 51


Operations of Red Black Tree(CO2)
RB-Delete-Fixup(T,
RB-Delete-Fixup(T, x) x) (Contd.)
(Contd.)
/*/*xxisisstill
stillleft[p[x]]
left[p[x]]*/ */
9.9. color[left[w]]=
ififcolor[left[w]] =BLACK
BLACKand color[right[w]]=
andcolor[right[w]] =BLACK
BLACK
10.
10. then color[w]
thencolor[w] RED
RED ////Case
Case22
11.
11. xx p[x]
p[x] ////Case
Case22
12.
12. else color[right[w]]=
elseififcolor[right[w]] =BLACK
BLACK
13.
13. then color[left[w]]
thencolor[left[w]] BLACK ////Case
BLACK Case33
14.
14. color[w]
color[w] RED
RED ////Case
Case33
15.
15. RIGHT-ROTATE(T,w)
RIGHT-ROTATE(T,w) ////Case
Case33
16.
16. ww right[p[x]]
right[p[x]] ////Case
Case33
17.
17. color[w]
color[w] color[p[x]]
color[p[x]] ////Case
Case44
18.
18. color[p[x]]
color[p[x]] BLACK
BLACK ////Case
Case44
19.
19. color[right[w]]
color[right[w]] BLACK
BLACK ////CaseCase44
20.
20. LEFT-ROTATE(T,
LEFT-ROTATE(T,p[x]) p[x]) ////Case
Case44
21.
21. xx root[T
root[T]] ////Case
Case44
22.
22. else
else(same
(sameas asthen
thenclause
clausewith
with“right”
“right”and
and“left”
“left”exchanged)
exchanged)
23. color[x]
23. color[x]
12/24/2024
BLACK
BLACK 52
ANKITA SHARMA ACSE0401 DAA Unit 2
Operations of Red Black Tree(CO2)
p[x]

B D
x w
A D B E

  x new w C  
A
C E
   
   

Case 1 – w is red
• w must have black children.
• Make w black and p[x] red (because w is red p[x] couldn’t have been
red).
• Then left rotate on p[x].
• New sibling of x was a child of w before rotation so it must be black.
• Go immediately to case 2, 3, or 4.

ANKITA SHARMA ACSE0401 DAA Unit


12/24/2024 53
2
Operations of Red Black Tree(CO2)
p[x] new x
c
B c
B
x w
A D A D
   
C E C E

       
Case 2 – w is black, both w’s children are black
• Take 1 black off x ( singly black) and off w ( red).
• Move that black to p[x].
• Do the next iteration with p[x] as the new x.
• If entered this case from case 1, then p[x] was red so new x is red &
black and color attribute of new x is RED hence loop terminates. Then new
x is made black in the last line.
ANKITA SHARMA ACSE0401 DAA Unit
12/24/2024 54
2
Operations of Red Black Tree(CO2)
c
B c
B
x w
x new w
A D A C
    D
C E 
 E
   

 
Case 3 – w is black, w’s left child is red, w’s right child is black

• Make w red and w’s left child black.


• Then right rotate on w.
• New sibling w of x is black with a red right child  case 4.
ANKITA SHARMA ACSE0401 DAA Unit
12/24/2024 55
2
Operations of Red Black Tree(CO2)
c
B D
x w
A D B E

  x  
c’ A C
C E
   
   

Case 4 – w is black, w’s right child is red


• Make w be p[x]’s color (c).
• Make p[x] black and w’s right child black.
• Then left rotate on p[x].
• Remove the extra black on x ( x is now singly black) without violating
any red-black properties.
• Setting x to root causes the loop to terminate.
12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 56
Operations of Red Black Tree(CO2)
Deletion Flow Chart

ANKITA SHARMA ACSE0401 DAA Unit


12/24/2024 57
2
Operations of Red Black Tree(CO2)
Points to Remember While Considering Symmetric Cases for
Insertion in R-B Tree

A- If P[z] is left child B- If P[z] is right child


Case1: If z’s uncle y is red. Case1: If z’s uncle y is red.
Case2: If z’s uncle y is black and Case2: If z’s uncle y is black and
z is the right child. z is the left child.
Case3: If z’s uncle y is black and Case3: If z’s uncle y is black and
z is the left child. z is the right child.

Earlier we have discussed different cases according to A(If P[z] is left


child ).For B(If P[z] is right child) just exchange every left and right
i.e. left rotation will become right rotation and left child will become
right child and vice versa.

ANKITA SHARMA ACSE0401 DAA Unit


12/24/2024 58
2
Operations of Red Black Tree(CO2)
Points to Remember While Considering Symmetric Cases for
Deletion in R-B Tree
A- If x is left child B- If x is right child
Case1: If x’s sibling w is red. Case1: If x’s sibling w is red.
Case2: If x’s sibling w is black and Case2: If x’s sibling w is black and
both of w’s children are black. both of w’s children are black.
Case3: If x’s sibling w is black , w’s Case3: If x’s sibling w is black , w’s
left child is red and w’s right child right child is red and w’s left child
is black. is black.
Case 4: If x’s sibling w is black and Case 4: If x’s sibling w is black and
w’s right child is red. w’s left child is red.

Earlier we have discussed different cases according to A(If x is left child ).


For B(If x is right child) just exchange every left and right i.e. left rotation
will become right rotation and left child will become right child and vice
versa.
ANKITA SHARMA ACSE0401 DAA Unit
12/24/2024 59
2
B Trees(CO2)- Objective

The objective of this topic is make students understand about

– B Tree
– Properties
– Structure
– Operations

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 60


Prerequisite and Recap

• Prerequisite
– Trees
– Different Operations of trees (Insertion, Deletion, updating,
searching)
– Binary Search Trees.

• Recap
– Algorithm analysis
– Red Black Trees

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 61


B-Tree(CO2)

• B-Tree is a self-balancing search tree.


• In most of the other self-balancing search trees (like AVL and Red-
Black Trees), it is assumed that everything is in main memory.
• To understand the use of B-Trees, we must think of the huge amount
of data that cannot fit in main memory.
• When the number of keys is high, the data is read from disk in the
form of blocks.
• Disk access time is very high compared to main memory access
time.
• The main idea of using B-Trees is to reduce the number of disk
accesses.
• Most of the tree operations (search, insert, delete, max, min, ..etc )
require O(h) disk accesses where h is the height of the tree.

ANKITA SHARMA ACSE0401 DAA Unit


12/24/2024 62
2
B-Tree(CO2)
Properties of B-Tree
1) All leaves are at same level.
2) A B-Tree is defined by the term minimum degree ‘t’. The value of t
depends upon disk block size.
3) Every node except root must contain at least t-1 keys. Root may
contain minimum 1 key.
4) All nodes (including root) may contain at most 2t – 1 keys.
5) Number of children of a node is equal to the number of keys in it
plus 1.
6) All keys of a node are sorted in increasing order. The child between
two keys k1 and k2 contains all keys in the range from k1 and k2.
7) B-Tree grows and shrinks from the root which is unlike Binary
Search Tree. Binary Search Trees grow downward and also shrink from
downward.
8) Like other balanced Binary Search Trees, time complexity to search,
insert and delete is O(Logn).
ANKITA SHARMA ACSE0401 DAA Unit
12/24/2024 63
2
B-Tree(CO2)

Following is an example B-Tree of minimum degree 3.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 64


B-Tree(CO2)

• Search Operation
Search is similar to the search in Binary Search Tree.
• Let the key to be searched be k.
• We start from the root and recursively traverse down.
• For every visited non-leaf node, if the node has the key,
we simply return the node.
• Otherwise, we recur down to the appropriate child (The
child which is just before the first greater key) of the
node.
• If we reach a leaf node and don’t find k in the leaf node,
we return NULL.

ANKITA SHARMA ACSE0401 DAA Unit


12/24/2024 65
2
B-Tree(CO2)

Nomenclature
x = a pointer to some object
DISK-READ(x)
//operations that access and/or modify the attributes of x
DISK-WRITE(x)
// omitted if no attributes of x were changed
other operations that access but do not modify attributes of x

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 66


B-Tree(CO2)

B-TREE -SEARCH(x, k)

1. i = 1
2. while i <= x.n and k > x.keyi
3. i=i+1
4. if i .x<= n and k == x.keyi
5. return (x,i)
6. elseif x.leaf
7. return NIL
8. else DISK-READ (x.ci)
9. return B-TREE-SEARCH(x. ci ,k)

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 67


B-Tree(CO2)

B-TREE -SEARCH(x, k)

• As in the TREE-SEARCH procedure for binary search trees, the


nodes encountered during the recursion form a simple path
downward from the root of the tree.
• The B-TREE-SEARCH procedure therefore accesses O(h)=
O(logtn) disk pages, where h is the height of the B-tree and n is
the number of keys in the B-tree.
• Since x.n < 2t, so it takes O(t) time within each node, and the
total CPU time is O(th)= O(tlogt n).

ANKITA SHARMA ACSE0401 DAA Unit


12/24/2024 68
2
B-Tree(CO2)

Creation of a Node

B-TREE-CREATE(T)
1. x = ALLOCATE-NODE
2. x.leaf= TRUE
3 x:n=0
4. DISK-WRITE(x)
5. T.root=x

ANKITA SHARMA ACSE0401 DAA Unit


12/24/2024 69
2
B-Tree(CO2)
BTree Insertion
B-TREE-INSERT(T, k)
1 r = T.root
2 if r.n == 2t-1
3. s = ALLOCATE-NODE()
4. T.root = s
5. s.leaf = FALSE
6. s.n = 0
7. s.c1 = r
8. B-TREE-SPLIT-CHILD(s,1)
9. B-TREE-INSERT-NONFULL(s, k)
10. else B-TREE-INSERT-NONFULL(r, k)

We insert a key k into a B-tree T of height h in a single pass down the tree,
requiring O(h) disk accesses. The CPU time required is O(th)=O(tlog tn).
12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 70
Insertion in B-Tree(CO2)
In a B-Tree, the new element must be added only at leaf node. That means, always
the new keyValue is attached to leaf node only. The insertion operation is
performed as follows...

• Step 1: Check whether tree is Empty.


• Step 2: If tree is Empty, then create a new node with new key value and
insert into the tree as a root node.
• Step 3: If tree is Not Empty, then find a leaf node to which the new key
value cab be added using Binary Search Tree logic.
• Step 4: If that leaf node has an empty position, then add the new key value to
that leaf node by maintaining ascending order of key value within the node.
• Step 5: If that leaf node is already full, then split that leaf node by sending
middle value to its parent node. Repeat tha same until sending value is fixed
into a node.
• Step 6: If the spilting is occuring to the root node, then the middle value
becomes new root node for the tree and the height of the tree is increased by
one.
ANKITA SHARMA ACSE0401 DAA Unit
12/24/2024 71
2
Insertion in B-Tree(CO2)

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 72


Insertion in B-Tree(CO2)

Example- Insert the sequence of integers


10,20,30,40,50,60,70,80,90 in an initially empty B-Tree.
• Initially root is NULL. Let us first insert 10 .

• Let us now insert 20, 30, 40 and 50. They all will be inserted in root
because the maximum number of keys a node can accommodate is
2*t – 1 which is 5.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 73


Insertion in B-Tree(CO2)

• Let us now insert 60. Since root node is full, it will first split into
two, then 60 will be inserted into the appropriate child.

ANKITA SHARMA ACSE0401 DAA Unit


12/24/2024 74
2
Insertion in B-Tree(CO2)
• Let us now insert 70 and 80. These new keys will be inserted into
the appropriate leaf without any split.

• Let us now insert 90. This insertion will cause a split. The middle
key will go up to the parent.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 75


Deletion in BTree(CO2)

If a node doesn’t maintain the minimum key values it leads to the condition
of underflow. In the condition of underflow some pointers need to be
adjusted.
If it is a leaf node
Case1: After deletion the node doesn’t suffer from underflow then there is
no problem.
Case2: By deleting the key the node suffer from underflow.
A: If the sibling of the node has more than one key value then BORROW
from the sibling.
B: If A not possible then underflow node will be combined with its sibling
together with parent which separate them-FUSION.

If it is a internal nodes
Case 3: Deletion of a key in internal nodes, find the in order successor/
predecessor and replace the desired key with in order successor / in order
Predecessor and remove the key.
12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 76
Deletion in BTree(CO2)

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 77


Binomial Heap(CO2)- Objective

The objective of this topic is make students understand about

– Binomial Trees
– Binomial Heap
– Structure
– Operations

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 78


Prerequisite and Recap

• Prerequisite
– Trees
– Different Operations of trees (Insertion, Deletion, updating,
searching)
– Binary Search Trees.

• Recap
– Algorithm analysis
– B-Tree

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 79


Binomial Heap (CO2)

 If we don’t need the UNION operation, ordinary binary heaps, as used in


heapsort work well.

 Operations other than UNION run in worst-case time O(lg n) (or better) on
a binary heap.

 If the UNION operation must be supported, however, binary heaps perform


poorly.

 By concatenating the two arrays that hold the binary heaps to be merged
and then running MIN-HEAPIFY , the UNION operation takes (n) time in
the worst case.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 80


Binomial Heap (CO2)

 A Binomial Heap is a collection of Binomial Trees.

 Binomial Tree: A binomial tree of order k is defined recursively: ・


 Order 0: single node.
 Order k: one binomial tree of order k – 1 linked to another of order
k – 1.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 81


Binomial Heap (CO2)

Structure of Binomial Tree

U can place value of k to make tree.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 82


Binomial Heap (CO2)

B2
B1
B1 B0
B1

B0 B1 B2 B3

B3
B2 B0

B1

B4
12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 83
Binomial Heap (CO2)

 Properties Binomial Tree


1. It has exactly 2k nodes.
2. It has depth as k or we can say height of the tree is k
3. There are exactly kCi nodes at depth i for i = 0, 1, . . . , k.
4. The root has degree k which is greater than that of any other
node

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 84


Binomial Heap (CO2)

4. if i the children of root are numbered from left to right by k-1,


k-2,.. 0 child i is the root of subtree Bi
(each subtree is bionomial tree).

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 85


Binomial Heap (CO2)

A Binomial Heap is a set of Binomial Trees where each


Binomial Tree follows the Min Heap property. And there
can be at most one Binomial Tree of any degree.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 86


Binomial Heap (CO2)

A BINOMIAL HEAP H is a set of BINOMIAL TREES that


satisfies the following “Binomial Heap Properties” (min heap)

1. Each binomial tree in H is HEAP-ORDERED


• the key of a node is ≥ the key of the parent
• Root of each binomial tree in H contains the smallest key
in that tree.

2. There is at most one binomial tree in H whose root has a degree


k , n-node binomial heap H consists of at most lgn+ 1
binomial
trees.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 87


Binomial Heap (CO2)

Binary Representation of a number and Binomial


Heaps
A Binomial Heap with n nodes has the number of
Binomial Trees equal to the number of set bits in the
binary representation of n. For example, let n be 13, there
are 3 set bits in the binary representation of n
(00001101), hence 3 Binomial Trees. We can also relate
the degree of these Binomial Trees with positions of set
bits. Parent
Key
degree
L R
child child
12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 88
Binomial Heap (CO2)
Binary Representation of a number and Binomial
Heaps

1101

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 89


Binomial Heap(CO2)

Fields of a node(x) in Binomial heaps

node x contains following fields


-p[x] :point to its parent,
-child[x] : point to its leftmost child
-sibling[x] :point to the sibling of x immediately to its right
-degree[x]: which is the number of children of x.
-Key[x]: key value of x

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 90


Binomial Heap(CO2)

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 91


Representation of Binomial Heap(CO2)

HEAD [H]

10 10
10 ROOT LIST (LINKED LIST)
10 10 10

parent
key 10 10 10 10 10
degree 10 10 10 10 10

child 10 10 10 10
10 sibling
10 10 10

10

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 92


Operations on Binomial Heap(CO2)

CREATING A NEW BINOMIAL HEAP

MAKE-BINOMIAL-HEAP ( )
allocate H
head [ H ]  NIL RUNNING-TIME= Θ(1)
return H
end

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 93


Operations on Binomial Heap(CO2)

BINOMIAL-HEAP-MINIMUM (H)

x  Head [H]
min  key [x]
x  sibling [x]
while x ≠ NIL do
if key [x] < min then
min  key [x]
yx
endif
x  sibling [x]
endwhile
return y
end

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 94


Operations on Binomial Heap(CO2)

BINOMIAL-HEAP-MINIMUM (H)

• Since binomial heap is HEAP-ORDERED


• The minimum key must reside in a ROOT NODE
• Above procedure checks all roots
• NUMBER OF ROOTS ≤ lgn + 1
• RUNNING TIME = O (lgn)

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 95


Operations on Binomial Heap(CO2)

Uniting Two Binomial Heaps

BINOMIAL-HEAP-UNION
Procedure repeatedly link binomial trees whose roots have the
same degree

BINOMIAL-LINK
Procedure links the Bk-1 tree rooted at node y to the Bk-1 tree rooted
at node z it makes z the parent of y

i.e. Node z becomes the root of a Bk tree

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 96


Operations on Binomial Heap(CO2)

Uniting Two Binomial Heaps

BINOMIAL-LINK (y,z)
p [y]  z
sibling [y]  child [z]
child [z]  y
degree [z] degree [z] + 1
end

ANKITA SHARMA ACSE0401 DAA Unit


12/24/2024 97
2
Operations on Binomial Heap(CO2)

Uniting Two Binomial Heaps NIL

z
+1
child[z]

NIL
p[y]

sibling [y]

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 98


Operations on Binomial Heap(CO2)

Uniting Two Binomial Heaps


We maintain 3 pointers into the root list

x = points to the root currently being examined

prev-x = points to the root PRECEDING x on the root list sibling


[prev-x] = x

next-x = points to the root FOLLOWING x on the root list sibling


[x] = next-x

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 99


Operations on Binomial Heap(CO2)

Uniting Two Binomial Heaps

• Initially, there are at most two roots of the same degree


• Binomial-heap-merge guarantees that if two roots in h have the
same degree they are adjacent in the root list
• During the execution of union, there may be three roots of the same
degree appearing on the root list at some time

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 100


Union Operation on Binomial Heap(CO2)

CASE 1: Occurs when degree [x] ≠ degree [next-x]

prev-x
a bx next-x
c sibling
d { next-x}

Bk Bl

l >k
prev-x x next-x

a b c d

Bk Bl

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 101


Union Operation on Binomial Heap(CO2)

CASE 2: Occurs when x is the first of 3 roots of equal degree

degree [x] = degree [next-x] = degree [sibling[next-x]]


prev-x x next-x sibling [next-x]

a b c d

BK BK BK
prev-x x next-x
a b c d

BK BK BK

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 102


Union Operation on Binomial Heap(CO2)

CASE 3 & 4: Occur when x is the first of 2 roots of equal degree


degree [x] = degree [next-x] ≠ degree [sibling [next-x]]

• Occur on the next iteration after any case

• Always occur immediately following CASE 2

• Two cases are distinguished by whether x or next-x has the smaller key

• The root with the smaller key becomes the root of the linked tree

ANKITA SHARMA ACSE0401 DAA Unit


12/24/2024
2
103
Union Operation on Binomial Heap(CO2)

CASE 3 & 4 CONTINUED


prev-x x next-x sibling [next-x]

a b c d

Bk Bk Bl l>k
prev-x x next-x
CASE 3
a b d
key [b] ≤ key [c]
c

prev-x x next-x
CASE 4
a c d
b key [c] ≤ key [b]

ANKITA SHARMA ACSE0401 DAA Unit


12/24/2024
2
104
Union Operation on Binomial Heap(CO2)

The running time of binomial-heap-union operation is O (lgn)

Let H1 & H2 contain n1 & n2 nodes respectively where n= n1+n2

Then, H1 contains at most lgn1 +1 roots


H2 contains at most lgn2 +1 roots
So H contains at most
lgn1 + lgn2 +2 ≤ 2 lgn +2= O (lgn) roots
immediately after BINOMIAL-HEAP-MERGE

Therefore, BINOMIAL-HEAP-MERGE runs in O(lgn)


time and

BINOMIAL-HEAP-UNION runs in O (lgn) time


ANKITA SHARMA ACSE0401 DAA Unit
12/24/2024
2
105
Union Operation on Binomial Heap(CO2)

BINOMIAL-HEAP-MERGE PROCEDURE
-Merges the root lists of H1 & H2 into a single linked-list

- Sorted by degree into monotonically increasing order

ANKITA SHARMA ACSE0401 DAA Unit


12/24/2024
2
106
Operations on Binomial Heap(CO2)

Uniting Two Binomial Heaps

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 107


Operations on Binomial Heap(CO2)

Uniting Two Binomial Heaps

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 108


Operations on Binomial Heap(CO2)

Uniting Two Binomial Heaps


We can see that there are two binomial heaps, so,
first, we have to combine both heaps. To combine
the heaps, first, we need to arrange their binomial
trees in increasing order.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 109


Operations on Binomial Heap(CO2)

Uniting Two Binomial Heaps


In the above heap first, the pointer x points to the node 12 with
degree B0, and the pointer next[x] points the node 18 with degree
B0. Node 7 with degree B1 is the sibling of 18, therefore, it is
represented as sibling[next[x]].

Now, first apply Case1 that says 'if degree[x] ≠ degree[next x]


then move pointer ahead' but in the above example, the
degree[x] = degree[next[x]], so this case is not valid.

Now, apply Case2 that says 'if degree[x] = degree[next x] =


degree[sibling(next x)] then Move pointer ahead'. So, this case
is also not applied in the above heap.
12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 110
Operations on Binomial Heap(CO2)

Uniting Two Binomial Heaps


Now, apply Case3 that says ' If degree[x] = degree[next x] ≠
degree[sibling[next x]] and key[x] < key[next x] then remove
[next x] from root and attached to x'. We will apply this case
because the above heap follows the conditions of case 3 -

degree[x] = degree[next x] ≠ degree[sibling[next x]] {as, B 0 = B0¬


≠ B1} and key[x] < key[next x] {as 12 < 18}.

So, remove the node 18 and attach it to 12 as shown below -

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 111


Union Operation on Binomial Heap(CO2)

Uniting Two Binomial Heaps

In the above heap first, the pointer x points to


the node 12 with degree B0, and the pointer
next[x] points the node 18 with degree B0.
Node 7 with degree B1 is the sibling of 18,
therefore, it is represented as sibling[next[x]].
12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 112
Union Operation on Binomial Heap(CO2)
x = 12, next[x] = 7, sibling[next[x]] = 3, and degree[x] = B 1,
dgree[next[x]] = BUniting
1
, Two Binomial Heaps
degree[sibling[next[x]]] = B1

Now we will reapply the cases in the above binomial heap. First,
we will apply case 1. Since x is pointing to node 12 and next[x] is
pointing to node 7, the degree of x is equal to the degree of next x;
therefore, case 1 is not valid.

Here, case 2 is valid as the degree of x, next[x], and


sibling[next[x]] is equal. So, according to the case, we have to
move the pointer ahead.

Therefore, x = 7, next[x] = 3, sibling[next[x]] =


15, and degree[x] = B 1, dgree[next[x]] = B 1,
degree[sibling[next[x]]]
12/24/2024
= B2
ANKITA SHARMA ACSE0401 DAA Unit 2 113
Union Operation on Binomial Heap(CO2)

Now, let's try to apply


Unitingcase
Two3,Binomial
here, first condition of case3 is
Heaps
satisfied as degree[x] = degree[next[x]] ≠
degree[sibling[next[x]]], but second condition (key[x] < key[next
x]) of case 3 is not satisfied.

Now, let's try to apply case 4. So, first condition of case4 is


satisfied and second condition (key[x] > key[next x]) is also
satisfied. Therefore, remove x from the root and attach it to
[next[x]].

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 114


Union Operation on Binomial Heap(CO2)
Now, the pointer x points to node 3, next[x] points to
node 15, and sibling[next[x]] points to the node 6.
Since, the degree of x is equal to the degree of next[x]
but not equal to the degree[sibling[next[x]]], and the
key value of x is less than the key value of next[x], so
we have to remove next[x] and attach it to x as shown
below -

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 115


Union Operation on Binomial Heap(CO2)

Now, x represents to the node 3, and next[x] points to


node 6. Since, the degree of x and next[x] is not equal, so
case1 is valid. Therefore, move the pointer ahead. Now,
the pointer x points the node 6.
The B4 is the last binomial tree in a heap, so it leads to
the termination of the loop. The above tree is the final
tree after the union of two binomial heaps.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 116


Union Operation on Binomial Heap(CO2)

BINOMIAL-HEAP-UNION (H1,H2)
H  MAKE-BINOMIAL-HEAP ( )
head [ H ]  BINOMIAL-HEAP-MERGE (H1,H2)
free the objects H1 & H2 but not the lists they point to
prev-x  NIL
x  HEAD [H]
next-x  sibling [x]
while next-x ≠ NIL do
if ( degree [x] ≠ degree [next-x] OR
(sibling [next-x] ≠ NIL and degree[sibling [next-x]] = degree
[x]) then
prev-x  x CASE 1
and 2
x  next-x CASE 1
and 2
elseif key [x] ≤ key [next-x] then
12/24/2024 sibling
ANKITA  sibling
[x]SHARMA [next
ACSE0401 DAA -x] Unit 2 CASE 3 117
Union Operation on Binomial Heap(CO2)
Binomial-Heap-Union Procedure (Cont.)
BINOMIAL- LINK (next-x, x) CASE 3
else
if prev-x = NIL then
head [H]  next-x CASE 4
else CASE 4
sibling [prev-x]  next-x CASE 4
endif
BINOMIAL-LINK(x, next-x)
CASE 4
x  next-x CASE 4
endif
next-x  sibling [x]
endwhile
return H
end
12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 118
Insert Operation on Binomial Heap(CO2)

• Inserting an element in the heap can be done by simply creating a


new heap only with the element to be inserted, and then merging
it with the original heap.
• Due to the merging, the single insertion in a heap takes O(logn)
time. Now, let's understand the process of inserting a new node
in a heap using an example.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 119


Insert Operation on Binomial Heap(CO2)

In the above heap, there are three binomial trees of degrees 0, 1,


and 2 are given where B0 is attached to the head of the heap.
Suppose we have to insert node 15 in the above heap.

First, we have to combine both of the heaps. As both node 12 and


node 15 are of degree 0, so node 15 is attached to node 12 as
shown

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 120


Insert Operation on Binomial Heap(CO2)

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 121


Insert Operation on Binomial Heap(CO2)

• Now, assign x to B0 with value 12, next(x) to B0 with value 15,


and assign sibling(next(x)) to B1 with value 7. As the degree of x
and next(x) is equal.
• The key value of x is smaller than the key value of next(x), so
next(x) is removed and attached to the x. It is shown in the
image –

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 122


Insert Operation on Binomial Heap(CO2)

Now, x points to node 12 with degree B1, next(x) to node 7 with


degree B1, and sibling(next(x)) points to node 15 with degree B2.

The degree of x is equal to the degree of next(x) but not equal to


the degree of sibling(next(x)). The key value of x is greater than
the key value of next(x); therefore, x is removed and attached to
the next(x) as shown in the image –

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 123


Insert Operation on Binomial Heap(CO2)

Now, x points to node 7, and next(x) points to node 15. The


degree of both x and next(x) is B2, and the key value of x is less
than the key value of next(x), so next(x) will be removed and
attached to x as shown in the below image -

Now, the degree of the


above heap is B3, and it is
the final binomial heap after
inserting node 15
12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 124
Insert Operation on Binomial Heap(CO2)

BINOMIAL-HEAP-INSERT (H,x)

H'  MAKE-BINOMIAL-HEAP (H, x)


P [x]  NIL
child [x]  NIL
sibling [x]  NIL RUNNING-TIME= O(lg n)
degree [x]  O
head [H’]  x
H  BINOMIAL-HEAP-UNION (H, H’)
end

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 125


Extract Min Operation on Binomial Heap(CO2)

• It means that we have to remove an element with the


minimum key value. As we know, in min-heap, the
root element contains the minimum key value. So, we
have to compare the key value of the root node of all
the binomial trees.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 126


Extract Min Operation on Binomial Heap(CO2)

Now, compare the key values of the root node of the


binomial trees in the above heap. So, 12, 7, and 15 are
the key values of the root node in the above heap in
which 7 is minimum; therefore, remove node 7.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 127


Extract Min Operation on Binomial Heap(CO2)

• Now, the degree of node 12 and node 25 is B 0,


and the degree of node 15 is B2.
• Pointer x points to the node 12, next(x) points to
the node 25, and sibling(next(x)) points to the
node 15.
• Since the degree of x is equal to the degree of
next(x) but not equal to the degree of
sibling(next(x)).
• Value of pointer x is less than the pointer
next(x), so node 25 will be removed and
attached to node 12
12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 128
Extract Min Operation on Binomial Heap(CO2)

Now, the degree of node 12 is changed to B1. The


above heap is the final heap after extracting the
minimum key.
12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 129
Minimum key from Binomial Heap(CO2)

BINOMIAL-HEAP-EXTRACT-MIN (H)
(1) find the root x with the minimum key in the
root list of H and remove x from the root list of H
(2) H’  MAKE-BINOMIAL-HEAP ( )
(3) reverse the order of the linked list of x’ children
and set head [H’]  head of the resulting list
(4) H  BINOMIAL-HEAP-UNION (H, H’)
return x
end

Analysis
• Unite binomial heaps H= {B0 ,B1,B4} and H’ = {B0 ,B1,B2}
• Running time if H has n nodes each of lines 1-4 takes O(lgn) time

ANKITA SHARMA ACSE0401 DAA Unit


12/24/2024
2
130
Minimum key from Binomial Heap(CO2)

Extracting the Node with the Minimum Key

x
head [H]

B0 B1
B4

B1 B0
B2

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 131


Minimum key from Binomial Heap(CO2)

Extracting the Node with the Minimum Key

x
head [H]

B0 B1
B4

B2 B1 B0
head [H’]

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 132


Decrease key from Binomial Heap(CO2)
BINOMIAL-HEAP-DECREASE-KEY (H, x, k)
key [x]  k
y  x
z  p[y]
while z ≠ NIL and key [y] < key [z] do
exchange key [y]  key [z]
exchange satellite fields of y and z
yz
z  p [y]
endwhile
End

Analysis
• Similar to DECREASE-KEY in BINARY HEAP
• BUBBLE-UP the key in the binomial tree it resides in.
• RUNNING TIME: O(lgn)
12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 133
Decrease key from Binomial Heap(CO2)

• Once the value of the key is decreased, it might be


smaller than its parent's key that results in the
violation of min-heap property.
• If such case occurs after decreasing the key, then
exchange the element with its parent, grandparent,
and so on until the min-heap property is satisfied.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 134


Decrease key from Binomial Heap(CO2)

Decrease the key 45 by 7 of the heap. After


decreasing 45 by 7, the heap will be

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 135


Decrease key from Binomial Heap(CO2)

After decreasing the key, the min-heap property


of the above heap is violated.
Now, compare 7 wits its parent 30, as it is lesser
than the parent, swap 7 with 30, and after
swapping, the heap will be -

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 136


Decrease key from Binomial Heap(CO2)

• Again compare the element 7 with its parent 8, again


it is lesser than the parent, so swap the element 7 with
its parent 8, after swapping the heap will be -

Now, the min-heap property


of the above heap is
satisfied. So, the above
heap is the final heap after
decreasing a key.
12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 137
Deleting key from Binomial Heap(CO2)

BINOMIAL- HEAP- DELETE (H,x)


y←x
z ← p [y]
while z ≠ NIL do RUNNING-TIME= O(lg n)
key [y] ← key [z]
satellite field of y ← satellite field of z
y ← z ; z ← p [y]
endwhile
H’← MAKE-BINOMIAL-HEAP
remove root z from the root list of H reverse the order of the linked list of z’s
children and set head [H’] ← head of the resulting list
H ← BINOMIAL-HEAP-UNION (H, H’)
H’ ← MAKE-BINOMIAL-HEAP
remove root z from the root list of H reverse the order of the linked list of z’s
children set head [H’] ← head of the resulting list
H ← BINOMIAL-HEAP-UNION (H, H’)
end
ANKITA SHARMA ACSE0401 DAA Unit
12/24/2024
2
138
Deleting key from Binomial Heap(CO2)

• To delete a node from the heap, first, we have to


decrease its key to negative infinity (or -∞) and then
delete the minimum in the heap.
• Now we will see how to delete a node with the help
of an example. Consider the below heap, and suppose
we have to delete the node 41 from the heap –

ANKITA SHARMA ACSE0401 DAA Unit


12/24/2024
2
139
Deleting key from Binomial Heap(CO2)

First, replace the node with negative infinity (or -∞) as


shown below –

ANKITA SHARMA ACSE0401 DAA Unit


12/24/2024
2
140
Deleting key from Binomial Heap(CO2)

Now, swap the negative infinity with its root node in


order to maintain the min-heap property.

Now, again swap the


negative infinity with its root
node
ANKITA SHARMA ACSE0401 DAA Unit
12/24/2024
2
141
Deleting key from Binomial Heap(CO2)

The next step is to extract the minimum key from the


heap. Since the minimum key in the above heap is -
infinity so we will extract this key, and the heap would
be:

Here , we again check cases at first case 4


is applicable so pointer moved further now
after case 4 , x =7 , next x =15 .
Now apply cases , so case 3 is valid now

ANKITA SHARMA ACSE0401 DAA Unit


12/24/2024
2
142
Deleting key from Binomial Heap(CO2)

After checking case 3 , next x which is 15 , now


attached to x.
The above is the final heap after deleting the node
41.
ANKITA SHARMA ACSE0401 DAA Unit
12/24/2024
2
143
Deleting key from Binomial Heap(CO2)

• Basic idea.
– Similar to binomial heaps, but less rigid structure.
– Binomial heap: eagerly consolidate trees after each insert.
– Fibonacci heap: lazily defer consolidation until next delete-
min.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2


144
Fibonacci Heap(CO2)- Objective

The objective of this topic is make students understand about

– Fibonacci Heap
– Structure
– Operations

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 145


Prerequisite and Recap

• Prerequisite
– Trees
– Different Operations of trees (Insertion, Deletion, updating,
searching)

• Recap
– Binomial Heaps

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 146


Fibonacci Heap(CO2)

 A Fibonacci heap is defined as the collection of rooted-tree in


which all the trees must hold the property of Min-heap.

 Root list has the root of all trees. Min[h] pointer point to the
min. key node in root list.

 All the nodes in the heap are connected by doubly circular link
list.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 147


Fibonacci Heap(CO2)

doubly-linked list
– Each node in a double-linked list contains two pointers left and
right pointers.

– The left-pointer points to its left node, and the right-pointer points
to the right node.

– The right pointer of the last node points to the first node, and the
left pointer of the first node points to the last node.

– The Tree in fibonnaci heap are not constrained to be binomial tree

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 148


Fibonacci Heap(CO2)

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 149


Fibonacci Heap(CO2)
• First fig. shows Fibonacci Heap consists of five
rooted min-heap-ordered trees with 14 nodes.
• The min-heap-ordered tree means the tree which
holds the property of a min-heap.
• The dashed line shows the root list. The minimum
node in the Fibonacci heap is the node containing the
key = 3 pointed by the pointer FH-min.

• Second fig. we can observe that each node contains


four pointers, the parent points to the parent
(Upward), the child points to the child (downward),
and the left and right pointers for the siblings
(sideways).
12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 150
Fibonacci Heap(CO2)

Properties of Fibonacci Heap:


1.It can have multiple trees of equal degrees, and each
tree doesn't need to have 2^k nodes.
2.All the trees in the Fibonacci Heap are rooted but not
ordered.(like not placed in order of b0,b1..and so on)
3.All the roots and siblings are stored in a separated
circular-doubly-linked list.(means each element have
left and right pointer)
4. The degree of a node is the number of its children.
Node X -> degree = Number of X's children.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 151


Fibonacci Heap(CO2)
5. Each node has a mark-attribute in which it is marked TRUE
or FALSE. The FALSE indicates the node has not any of its
children. The TRUE represents that the node has lost one
child. The newly created node is marked FALSE.
6. The potential function of the Fibonacci heap is F(FH) = t[FH]
+ 2 * m[FH] (**t[FH] =rooted tree , m[FH]= marked node*)
7. The Fibonacci Heap (FH) has some important technicalities
listed below:
I. min[FH] - Pointer points to the minimum node in the
Fibonacci Heap
II. n[FH] - Determines the number of nodes
III.t[FH] - Determines the number of rooted trees
IV. m[FH] - Determines the number of marked nodes
V. F(FH) - Potential Function.(generally used to determine
performance of fibonnaci heap)
12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 152
Fibonacci Heap(CO2)
INSERTION

FIB-HEAP-INSERT(H, x)
1. degree[x] =0
2. p[x] =NIL
3. child[x] =NIL
4 .left[x]=x
5. right[x]=x
6. mark[x]=FALSE
7. concatenate the root list containing x with root list
H
8 .if min[H] = NIL or key[x] < key[min[H]]
9. then min[H] =x
10. n[H] =n[H] + 1

ANKITA SHARMA ACSE0401 DAA Unit


12/24/2024 153
2
Fibonacci Heap(CO2)
Analysis of Insertion Algorithm

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

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 154


Fibonacci Heap(CO2)

Insertion
• Inserting a node into an already existing heap
follows the steps below.
• Create a new node for the element.
• Check if the heap is empty.
• If the heap is empty, set the new node as a root
node and mark it min.
• Else, insert the node into the root list and
update min.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 155


Fibonacci Heap(CO2)

The minimum element is always given by


the min pointer.
12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 156
Union of Fibonacci Heaps(CO2)

• Procedure unites Fibonacci heaps H1 and H2, destroying H1 and H2 in


the process.
FIB-HEAP-UNION(H1,H2)
• H =MAKE-FIB-HEAP()
• min[H] =min[H1]
• concatenate the root list of H2 with the root list of H
• if (min[H1] = NIL) or (min[H2] ≠ NIL and min[H2] < min[H1])
• then min[H]=min[H2]
• n[H] =n[H1] + n[H2]
• free the objects H1 and H2
• return H

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 157


Union of Fibonacci Heaps(CO2)

• 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, and 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.
• As in the FIB-HEAP-INSERT procedure, no consolidation of trees
occurs.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2


158
Union of Fibonacci Heaps(CO2)

• Union of two fibonacci heaps consists of


following steps.
• Concatenate the roots of both the heaps.
• Update min by selecting a minimum key from
the new root lists.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2


159
Decreasing a key
• Decreasing a key is performed on the
Fibonacci heap to decrease the key of any
node and can be performed in the O(1)
amortized time. In the below pseudo code, we
have assumed that removing a from the linked
list does not change any of the structural
attributes in the removed node.

12/24/2024 ANKITA SHARMA ACSE0401 DAA 160


Unit 2
Decreasing a key

1.Is the actual Fibonacci heap and


2.represents the heap after decreasing the
node storing key = 45 to 15.

12/24/2024 ANKITA SHARMA ACSE0401 DAA 161


Unit 2
Fibonacci Heaps(CO2)

• Linking operation. Make larger root be a child of smaller


root.

larger root smaller root still heap-ordered

15 3 3

56 24 18 52 41 15 18 52 41

77 39 44 56 24 39 44

tree T1 tree T2
77
tree T'

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 162


Fibonacci Heaps(CO2)

• Deleting minimum key


– The Operation is accompalished by deleting the minimum
key node and move all its children to root list.it uses
process CONSOLIDATE to merge the tree with same
degree
– Consolidate trees so that no two roots have
min same rank.

7 24 23 17 3

30 26 46 18 52 41

35 39 44

12/24/2024 163
ANKITA SHARMA ACSE0401 DAA Unit 2
Fibonacci Heaps(CO2)

Delete min; meld its children into root list; update


min.

min

7 24 23 17 18 52 41

30 26 46 39 44

35

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 164


Fibonacci Heaps(CO2)

Delete Min
– Consolidate trees so that no two roots have same rank.

min
current

7 24 23 17 18 52 41

30 26 46 39 44

35

12/24/2024 165
ANKITA SHARMA ACSE0401 DAA Unit 2
Fibonacci Heaps(CO2)

– Comparing the Ranks

rank

0 1 2 3

min
current

7 24 23 17 18 52 41

30 26 46 39 44

35

12/24/2024 166
ANKITA SHARMA ACSE0401 DAA Unit 2
Fibonacci Heaps(CO2)

– Comparing the Ranks

rank

0 1 2 3

min
current

7 24 23 17 18 52 41

30 26 46 39 44

35

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 167


Fibonacci Heaps(CO2)

• Delete min.
– Delete min; meld its children into root list; update min.
– Consolidate trees so that no two roots have same rank .
rank

0 1 2 3

min

7 24 23 17 18 52 41

30 26 46 current 39 44

35

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 168


2
Fibonacci Heaps(CO2)
• Delete min.
– Delete min; meld its children into root list; update min.
– Consolidate trees so that no two roots have same rank.
rank

0 1 2 3
link 23 into 17

min

7 24 23 17 18 52 41

30 26 46 current 39 44

35

ANKITA SHARMA ACSE0401 DAA Unit 169


12/24/2024
2
Fibonacci Heaps(CO2)
• Delete min.
– Delete min; meld its children into root list; update min.
– Consolidate trees so that no two roots have same rank.
rank

0 1 2 3
link 17 into 7

min

7 24 17 18 52 41

30 26 46 current 23 39 44

35

ANKITA SHARMA ACSE0401 DAA Unit


12/24/2024 170
2
Fibonacci Heaps(CO2)
• Delete min.
– Delete min; meld its children into root list; update min.
– Consolidate trees so that no two roots have same rank.
rank

0 1 2 3
link 24 into 7

current
min
24 7 18 52 41

26 46 17 30 39 44

35 23

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 171


Fibonacci Heap- Example

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 172


Fibonacci Heap- Example

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 173


Tries(CO2)- Objective

The objective of this topic is make students understand about

– Tries
– Structure
– Operations

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 174


Prerequisite and Recap

• Prerequisite
– Trees
– Different Operations of trees (Insertion, Deletion, updating,
searching)

• Recap
– Fibonacci Heap

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 175


Tries(CO2)

• Trie is an efficient information reTrieval data structure.


• Using Trie, search complexities can be brought to optimal limit (key
length).
• If we store keys in binary search tree, a well balanced BST will need
time proportional to M * log N, where M is maximum string length
and N is number of keys in tree.
• Using Trie, we can search the key in O(M) time.
• Every node of Trie consists of multiple branches.
• Each branch represents a possible character of keys.
• We need to mark the last node of every key as end of word node.
• A Trie node field isEndOfWord is used to distinguish the node as end
of word node.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 176


2
Tries(CO2)

A simple structure to represent nodes of the English alphabet can be as


following,

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 177


2
Skip List(CO2)- Objective

The objective of this topic is make students understand about

– Skip List
– Structure
– Operations

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 178


Prerequisite and Recap

• Prerequisite
– Trees
– Different Operations of trees (Insertion, Deletion, updating,
searching)

• Recap
– Tries

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 179


Skip List(CO2)

• Skip lists are a linked list like structure which allows for fast
search.

• It consists of a base list holding the elements, together with a tower


of lists maintaining a linked hierarchy of subsequences, each
skipping over fewer elements.

• We create multiple layers so that we can skip some nodes.

• The upper layer works as an “express lane” which connects only


main outer stations, and the lower layer works as a “normal lane”
which connects every station.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 180


2
Skip List(CO2)
• See the following example list with 16 nodes and two layers..
Suppose we want to search for 50, we start from first node of
“express lane” and keep moving on “express lane” till we find a
node whose next is greater than 50. Once we find such a node (30 is
the node in following example) on “express lane”, we move to
“normal lane” using pointer from this node, and linearly search for
50 on “normal lane”. In following example, we start from 30 on
“normal lane” and with linear search, we find 50.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 181


2
Faculty Video Links, Youtube & NPTEL Video Links and
Online Courses Details

• Self Made Video Link:


• https://www.youtube.com/watch?v=CuNHn-1ipbY

• Youtube/other Video Links


• https://encrypted-vtbn2.gstatic.com/video?q=tbn:ANd9GcQE-7aKy6n8mzl
WvUrja3-jij9Z7xJLNvazDhF1--P0D1gBLkPj
• https://www.youtube.com/watch?v=3RQtq7PDHog
• https://www.youtube.com/watch?v=qA02XWRTBdw
• https://www.youtube.com/watch?v=w5cvkTXY0vQ
• https://www.youtube.com/watch?v=7dAdqJH1WDg
• https://www.youtube.com/watch?v=2WNQSgoQx-M
• https://www.youtube.com/watch?v=iIiGWLP3qYE
• https://www.youtube.com/watch?v=GUML34YFCZs

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 182


Daily Quiz

Q1. What are the differences between AVL and RB Tree.


Q2. Discuss the rotation operation of RB Tree.
Q3. what is the complexity of insertion Operation in RBT.
Q4. Discuss all the field of a node of binomial tree.
Q5. How to find the degree of a binomial tree.
Q6. How is Fibonacci heap different from Binomial heap.
Q7. What are the two steps for deleting minimum from the fibonacci
heap?
Q8. What are Tries.
Q9. What is the role of skip list in real world problems.
Q10. How to perform linking operation in Fibonacci Heap.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 183


Weekly Assignment

Q1. Insert the the following nodes in RBT


<10,30,20,50,40,90,80,15,60>
[CO2]
Q2. Delete the following node from RBT of Q1 in given order 20,
10, 30, 15.
[CO2]
Q3. Perform the union operation on following binomial heap [CO2]

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 184


Weekly Assignment

Q4. Write a procedure which extract the minimum key node from
. binomial heap. [CO2]
Q5. Write the algorithm for deleting an element from a binomial-heap.
Show the binomial-heap that results when the element 21 is
removed from H given below [CO2]

Q6. Decrease of a node as the following


key a) key 52 decrease by 10.
b) Key 24 decrease by 5.

[CO2]
12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 185
MCQ s

1. What is the special property of red-black trees and what


root should always be?
a) a color which is either red or black and root should
always be black color only
b) height of the tree
c) pointer to next node
d) a color which is either green or black

2. What are the operations that could be performed in O(logn) time


complexity by red-black tree?
a) insertion, deletion, finding predecessor, successor
b) only insertion
c) only finding predecessor, successor
d) for sorting

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 186


MCQ s

3. Which of the following is an application of Red-black trees and


why?
a) used to store strings efficiently
b) used to store integers efficiently
c) can be used in process schedulers, maps, sets
d) for efficient sorting

4. Why Red-black trees are preferred over hash tables though hash
tables have constant time complexity?
a) no they are not preferred
b) because of resizing issues of hash table and better ordering in
redblack trees
c) because they can be implemented using trees
d) because they are balanced

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 187


MCQ s

5. How can you save memory when storing color information in Red-
Black tree?
a) using least significant bit of one of the pointers in the node for
color information
b) using another array with colors of each node
c) storing color information in the node structure
d) using negative and positive numbering

6. When to choose Red-Black tree, AVL tree and B-trees?


a) many inserts, many searches and when managing more items
respectively
b) many searches, when managing more items respectively and
many inserts respectively
c) sorting, sorting and retrieval respectively
d) retrieval, sorting and retrieval respectively

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 188


MCQ s

7. The main distinguishable characterstic of a binomial heap from a


binary heap is that
a) it allows union operations very efficiently
b) it does not allow union operations that could easily be
implemented in binary heap
c) the heap structure is not similar to complete binary tree
d) the location of child node is not fixed i.e child nodes could be at
level (h-2) or (h-3), where h is height of heap and h>4

8.The number of trees in a binomial heap with n nodes is


a) logn
b) n
c) nlogn
d) n/2

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 189


MCQ s

9. What is order of resultant heap after merging two tree of order k?


a) 2*k
b) k+1 This Objective of this unit is to make the
c) k*k students aware about
d) k+logk • Advanced data structures
• Red Black Tree
10. Time taken in
• decreasing
B Trees the node value in a binomial heap is
a) O(n) • Fibonacci Heap
b) O(1)
• Binomial Heap
c) O(logn)
d) O(nlogn) • Tries
• Skip list.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 190


Glossary Question

Q.1_______operations could be performed in O(logn) time complexity by red-black tree?


a. insertion, deletion, finding predecessor, successor
b. only insertion
c. only finding predecessor, successor
d. for sorting
Q.2 When inserting into a red-black tree, _________condition might happen?
a. double-red
b. double-black
c. triple-red
d. triple-black
Q.3 When deleting a node from a red-black tree, _____ condition might happen?
a. double-black
b. double-red
c. triple-red
d. triple-black

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 191


Glossary Question

Q.4The number of trees in a binomial heap with n nodes is______.


a) logn
b) n
c) nlogn
d) n/2
Q.5 Time taken in decreasing the node value in a binomial heap is______.
a) O(n)
b) O(1)
c) O(logn)
d) O(nlogn)
Q.6 Given a heap of n nodes.The maximum number of tree for building the heap
is______.
a) n
b) n-1
c) n/2
d) logn

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 192


Glossary Question

Q.7 ________ function having same complexity for a fibonacci heap.


a) Insertion, Union
b) Insertion, Deletion
c) extract_min, insertion
d) Union, delete
Q.8 In red-black trees root should always be_______.
a) black color only
b) Red color only
c) either Red or Black
d) a color which is either green or black
Q.9 _________are the operations that could be performed in O(logn) time complexity
by red-black tree?
a) insertion, deletion, finding predecessor, successor
b) only insertion
c) only finding predecessor, successor
d) for sorting

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 193


Old Question Papers(2020-2021)

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 194


Old Question Papers(2020-2021)

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 195


Old Question Papers(2020-2021)

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 196


Old Question Papers(2020-2021)

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 197


Old Question Papers(2020-2021)

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 198


Old Question Papers(2021-2022)

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 199


Old Question Papers(2021-2022)

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 200


Old Question Papers(2021-2022)

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 201


Old Question Papers(2021-2022)

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 202


Old Question Papers(2021-2022)

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 203


Old Question Papers(2021-2022)

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 204


Old Question Papers(2021-2022)

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 205


Old Question Papers(2021-2022)

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 206


Old Question Papers(2021-2022)

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 207


Old Question Papers(2021-2022)

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 208


Expected Questions for University Exam

Q1. Explain insertion in Red Black 'free. Show steps for inserting 1,2,3, 4,5,
6 ,7 ,8 & 9 into empty RB tree. [CO2]
Q2. What are the advantages of Red Black Tree over Binary Search Tree? Write
algorithms to insert a key in a red black tree. Insert the following sequence of
information in an empty red black tree 1, 2, 3, 4, 5, 5. [CO2]
Q3. Calculate the complexity of procedure that decrease a key in fib. Heap.
[CO2]
Q4. Write the algorithm for extract an min, key node fib.heap. [CO2]
Q5. write the algorithm to decrease a key value of a node in fib. Heap. [CO2]
Q6. Write the procedure which extract min. key node from fib. Heap. [CO2]

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 209


Recap of Unit

This purpose of this unit is to make the students aware


about the advanced data structures like Red Black Tree, B
Trees, Fibonacci Heap, Binomial Heap, Tries and Skip list. It
gives the insights of different Operations like union , deletion
and decreasing key values.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 210


References

1. Thomas H. Coreman, Charles E. Leiserson and Ronald L.


Rivest, “Introduction to
Algorithms”, Printice Hall of India.
2. E. Horowitz & S Sahni, "Fundamentals of Computer
Algorithms",
3. Aho, Hopcraft, Ullman, “The Design and Analysis of Computer
Algorithms” Pearson Education, 2008.
4. LEE “Design & Analysis of Algorithms(POD)”, McGraw Hill
5. Richard E.Neopolitan ‘Foundations of Algorithms” Jones &
Bartlett Learning.
6. Gilles Brassard and Paul Bratley, Algorithmics: Theory and
Practice, Prentice Hall, 1995.

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 211


Unit 2

Thank You

12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 212

You might also like