Design and Analysis Unit2
Design and Analysis Unit2
Noida
Unit: 2
• In Data mining
• Image Processing
• Digital Signature.
• DNA Matching.
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
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
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
SECTION B
2. Attempt any three of the following: 3 x 10 = 30
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
1 10
2 10
• Prerequisite
• Basic concept of c programming language.
• Concept of stack, queue and link list.
• Recap
• Flow Chart
• Algorithm
• Red-Black trees
• B – trees
.
• Binomial Heap
• Fibonacci Heaps
• Tries
• Skip list.
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
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]
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.
1. Insertion
2. Deletion
Rotations
• Rotations are presented by the following figures.
x Left-Rotate(T, x) y
Right-Rotate(T, y) x
y
Rotations
General description:
• We color z red.
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]
whilexxnil[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
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
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)
C B
p[z]
B y A C
z
A
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]
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
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
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.
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
x
c’ A C
C E
– B Tree
– Properties
– Structure
– Operations
• Prerequisite
– Trees
– Different Operations of trees (Insertion, Deletion, updating,
searching)
– Binary Search Trees.
• Recap
– Algorithm analysis
– Red Black Trees
• 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.
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
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)
B-TREE -SEARCH(x, k)
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
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...
• 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.
• 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.
• Let us now insert 90. This insertion will cause a split. The middle
key will go up to the parent.
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)
– Binomial Trees
– Binomial Heap
– Structure
– Operations
• Prerequisite
– Trees
– Different Operations of trees (Insertion, Deletion, updating,
searching)
– Binary Search Trees.
• Recap
– Algorithm analysis
– B-Tree
Operations other than UNION run in worst-case time O(lg n) (or better) on
a binary heap.
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.
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)
1101
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
MAKE-BINOMIAL-HEAP ( )
allocate H
head [ H ] NIL RUNNING-TIME= Θ(1)
return H
end
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]
yx
endif
x sibling [x]
endwhile
return y
end
BINOMIAL-HEAP-MINIMUM (H)
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
BINOMIAL-LINK (y,z)
p [y] z
sibling [y] child [z]
child [z] y
degree [z] degree [z] + 1
end
z
+1
child[z]
NIL
p[y]
sibling [y]
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
a b c d
BK BK BK
prev-x x next-x
a b c d
BK BK BK
• 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
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]
BINOMIAL-HEAP-MERGE PROCEDURE
-Merges the root lists of H1 & H2 into a single linked-list
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.
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)
BINOMIAL-HEAP-INSERT (H,x)
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
x
head [H]
B0 B1
B4
B1 B0
B2
x
head [H]
B0 B1
B4
B2 B1 B0
head [H’]
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)
• 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.
– Fibonacci Heap
– Structure
– Operations
• Prerequisite
– Trees
– Different Operations of trees (Insertion, Deletion, updating,
searching)
• Recap
– Binomial Heaps
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.
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.
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
• 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.
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.
• 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.
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'
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)
min
7 24 23 17 18 52 41
30 26 46 39 44
35
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)
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)
rank
0 1 2 3
min
current
7 24 23 17 18 52 41
30 26 46 39 44
35
• 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
0 1 2 3
link 23 into 17
min
7 24 23 17 18 52 41
30 26 46 current 39 44
35
0 1 2 3
link 17 into 7
min
7 24 17 18 52 41
30 26 46 current 23 39 44
35
0 1 2 3
link 24 into 7
current
min
24 7 18 52 41
26 46 17 30 39 44
35 23
– Tries
– Structure
– Operations
• Prerequisite
– Trees
– Different Operations of trees (Insertion, Deletion, updating,
searching)
• Recap
– Fibonacci Heap
– Skip List
– Structure
– Operations
• Prerequisite
– Trees
– Different Operations of trees (Insertion, Deletion, updating,
searching)
• Recap
– Tries
• Skip lists are a linked list like structure which allows for fast
search.
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]
[CO2]
12/24/2024 ANKITA SHARMA ACSE0401 DAA Unit 2 185
MCQ s
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
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
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]
Thank You