Unit 5 - Analysis and Design of Algorithms - WWW - Rgpvnotes.in PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Subject Name: Analysis & Design of Algorithm

Subject Code: CS-4004


Semester: 4th
Downloaded from be.rgpvnotes.in

Unit-5
1. Binary Search Tree
A Binary Search Tree (BST) is a tree (fig 1) in which all the nodes follow the below- e tio ed properties −
 The left sub-tree of a node has a key less than or equal to its parent node's key.
 The right sub-tree of a node has a key greater than to its parent node's key.
Thus, BST divides all its sub-trees into two segments: the left sub-tree and the right sub-tree.
Those a e defi ed as −
left_subtree (keys ≤ ode (key ≤ right_subtree (keys)

Root Node / 10 10
Root

6 18

Child Node /
Node
4 8 15 25

Right Sub Tree

Left Sub Tree

Fig 1 : Binary Search Tree


1.1 Basic Operations
1. “ear h − “ear hes a ele e t i a tree.
2. I sert − I serts a ele e t i a tree.
3. Delete – Delete an element in a tree.
4. Traversing – Traverse a tree.
1.Search: To search a given key in Binary Search Tree, we first compare it with root, if the key is
prese t at root, e retur root. If ke is greater tha root s ke , e ill sear h it i the right su tree
of root node. Otherwise we will search it in the left sub tree.
2.Insert: A new key is always inserted at leaf. We start searching a key from root till we hit a leaf
node. Once a leaf node is found, the new node is added as a child of the leaf node.
100 100
/ \ Insert 40 / \
20 500 ---------> 20 500
/ \ / \
10 30 10 30
\
40
Page 1
Page no: 1 Follow us on facebook to get real-time updates from RGPV
Downloaded from be.rgpvnotes.in

Department of Computer Science and Engineering

3.Delete: When we delete a node, there will be three possibilities:


1) Node to be deleted is leaf: Simply remove from the tree.
50 50
/ \ delete (20) / \
30 70 ---------> 30 70
/ \ / \ \ / \
20 40 60 80 40 60 80
2) Node to be deleted has only one child: Node to be deleted replaced by the child nodes.
50 50
/ \ delete (30) / \
30 70 ---------> 40 70
\ / \ / \
40 60 80 60 80
3) Node to be deleted has two children: Find inorder successor of the node. Copy contents of the
inorder successor to the node and delete the inorder successor. Note that inorder predecessor can
also be used.
50 60
/ \ delete(50) / \
40 70 ---------> 40 70
/ \ \
60 80 80
4.Traversing: There are three common ways to traverse a binary tree:
1. Preorder
2. Inorder
3. postorder
2. Height Balanced tree
A tree is a self-balancing Binary Search Tree (BST) where the difference between heights of left and
right subtrees cannot be more than one for all nodes. It means the Tree is balanced if the Balance
Factor of Every Node is 1 , 0 , or -1.
If the balance factor of any node in an AVL tree becomes less than -1 or greater than 1, the tree has
to be balanced by making a simple modifications in the tree called rotation. An AVL tree causes
imbalance, he a o e of the follo i g o ditio s o ur. α – is the node must be rebalanced.
Case 1: A i sertio fro the left su tree of the left hild of ode α.
Case 2: A i sertio i to the right su tree of the left hild of α.
Case 3: An insertion into the left sub tree of the right hild of α.
Case 4: A i sertio i to the right su tree of the right hild of α.
These imbalances can be overcome by
i. Single Rotation
ii. Double Rotation
Case 1 and Case 4 imbalance (left – left or right – right) is fixed by a single rotation of the tree. Case
2 and Case 3 imbalance (left – right or right – left) is fixed by double rotation.

Analysis & Design of Algorithm (CS-4004) Page 2


Page no: 2 Follow us on facebook to get real-time updates from RGPV
Downloaded from be.rgpvnotes.in

Department of Computer Science and Engineering

a) Left Left Case: T1, T2, T3 and T4 are subtrees.


z y
/\ / \
y T4 Right Rotate (z) x z
/\ - - - - - - - - -> / \ / \
x T3 T1 T2 T3 T4
/ \
T1 T2
b) Left Right Case:
z z x
/\ / \ / \
y T4 Left Rotate (y) x T4 Right Rotate(z) y z
/\ - - - - - - - - -> / \ - - - - - - - -> / \ / \
T1 x y T3 T1 T2 T3 T4
/\ /\
T2 T3 T1 T2
c) Right Right Case:
z y
/ \ / \
T1 y Left Rotate(z) z x
/ \ - - - - - - - -> / \ /\
T2 x T1 T2 T3 T4
/\
T3 T4
d) Right Left Case:
z z x
/ \ /\ / \
T1 y Right Rotate (y) T1 x Left Rotate(z) z y
/\ - - - - - - - - -> / \ - - - - - - - -> / \ /\
x T4 T2 y T1 T2 T3 T4
/\ / \
T2 T3 T3 T4
3. 2-3 tree
 A 2-3 Tree is a specific form of a B tree (Fig.2).
 A 2-3 tree is a search tree.
 A 2-3 search tree is a tree that either is empty or:
 A 2-node, with one key (and associated value) and two links, a left link to a 2-3 search
tree with smaller keys, and a right link to a 2-3 search tree with larger keys
 A 3-node, with two keys (and associated values) and three links, a left link to a 2-3
search tree with smaller keys, a middle link to a 2-3 search tree with keys between the node's
keys and a right link to a 2-3 search tree with larger keys.

Analysis & Design of Algorithm (CS-4004) Page 3


Page no: 3 Follow us on facebook to get real-time updates from RGPV
Downloaded from be.rgpvnotes.in

Department of Computer Science and Engineering

A perfectly balanced 2-3 search tree (or 2-3 tree for short) is one whose null links are all the
sa e dista ce fro the root.

Example ------------
| 4 | 12 |
------------
/ | \
/ | \
------------ ------------ -------------
| 2 | 4 | | 7 | 10 | | 15 | 20 |
------------ ------------ -------------
Fig 2: 2-3 Tree

Insertion algorithm
Insertion Into a two-three tree is quite different from the insertion algorithm into a binary search
tree. In a two-three tree, the algorithm will be as follows:
1. If the tree is empty, create a node and put value into the node
2. Otherwise find the leaf node where the value belongs.
3. If the leaf node has only one value, put the new value into the node
4. If the leaf node has more than two values, split the node and promote the median of the three
values to parent.
5. If the parent then has three values, continue to split and promote, forming a new root node if
necessary
The delete operation
Deleting key k is similar to inserting: there is a special case when T is just a single (leaf) node
containing k (T is made empty); otherwise, the parent of the node to be deleted is found, then the
tree is fixed up if necessary so that it is still a 2-3 tree.
Once node n (the parent of the node to be deleted) is found, there are two cases, depending on how
many children n has:
case 1: n has 3 children
 Remove the child with value k, then fix n.leftMax, n.middleMax, and n's ancestors' leftMax and
middleMax fields if necessary.
case 2: n has only 2 children
 If n is the root of the tree, then remove the node containing k. replace the root node with the
other child (so the final tree is just a single leaf node).
 If n has a left or right sibling with 3 kids, then:
o remove the node containing k
o "steal" one of the sibling's children
o fix n.leftMax, n.middleMax, and the leftMax and middleMax fields of n's sibling and ancestors
as needed.
 If n's sibling(s) have only 2 children, then:
o remove the node containing k
o make n's remaining child a child of n's sibling
o fix leftMax and middleMax fields of n's sibling as needed
Analysis & Design of Algorithm (CS-4004) Page 4
Page no: 4 Follow us on facebook to get real-time updates from RGPV
Downloaded from be.rgpvnotes.in

Department of Computer Science and Engineering

o Remove n as a child of its parent, using essentially the same two cases (depending on how
many children n's parent has) as those just discussed.
4. B-Tree
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 use of B-Trees, we
must think of 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
height of the tree. B-tree is a fat tree. Height of B-Trees is kept low by putting maximum possible
keys in a B-Tree node(fig 3). Generally, a B-Tree node size is kept equal to the disk block size. Since h
is low for B-Tree, total disk accesses for most of the operations are reduced significantly compared to
balanced Binary Search Trees like AVL Tree, Red Black Tree, ..etc.
Properties of B-Tree
1) All leaves are at same level.
2) A B-Tree is defined by the term minimum degree t . The alue of t depe ds upo disk lo k 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 range from k1 and k2.
7) B-Tree grows and shrinks from 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).

Fig 3: B-Tree
Search
Search is similar to search in Binary Search Tree. Let the key to be searched be k. We start from root
and recursively traverse down. For every visited non-leaf node, if the node has key, we simply return
the node. Otherwise we recur down to the appropriate child (The child which is just before the first
greater ke of the ode. If e rea h a leaf ode a d do t fi d k i the leaf ode, e retur NULL.
Traversal
Traversal is also similar to Inorder traversal of Binary Tree. We start from the leftmost child,
recursively print the leftmost child, then repeat the same process for remaining children and keys. In
the end, recursively print the rightmost child.
Analysis & Design of Algorithm (CS-4004) Page 5
Page no: 5 Follow us on facebook to get real-time updates from RGPV
Downloaded from be.rgpvnotes.in

Department of Computer Science and Engineering

Insertion
A new key is always inserted at leaf node. Let the key to be inserted be k. Like BST, we start from root
and traverse down till we reach a leaf node. Once we reach a leaf node, we insert the key in that leaf
node. Unlike BSTs, we have a predefined range on number of keys that a node can contain. So before
inserting a key to node, we make sure that the node has extra space (Fig 4).
How to make sure that a node has space available for key before the key is inserted? We use an
operation called splitChild() that is used to split a child of a node.
Following are the steps for insertion:
1) Initialize x as root.
2) While x is not leaf, do following
a) Find the child of x that is going to to be traversed next. Let the child be y.
b) If y is not full, change x to point to y.
c) If y is full, split it and change x to point to one of the two parts of y. If k is smaller than mid key
in y, then set x as first part of y. Else second part of y. When we split y, we move a key from y to its
parent x.
3) The loop in step 2 stops when x is leaf. x must have space for 1 extra key as we have been splitting
all nodes in advance. So simply insert k to x.

Fig 4: Insertion if B – Tree (Here the Order is 5)

Analysis & Design of Algorithm (CS-4004) Page 6


Page no: 6 Follow us on facebook to get real-time updates from RGPV
Downloaded from be.rgpvnotes.in

Department of Computer Science and Engineering

Deletion
The deletion procedure deletes the key k from the subtree rooted at x. This procedure guarantees
that whenever it calls itself recursively on a node x, the number of keys in x is at least the minimum
degree t. Note that this condition requires one more key than the minimum required by the usual B-
tree conditions, so that sometimes a key may have to be moved into a child node before recursion
descends to that child. This strengthened condition allows us to delete a key from the tree in one
do ard pass ithout ha i g to a k up ith o e e eptio , hi h e ll explain). You should
interpret the following specification for deletion from a B-tree with the understanding that if the root
node x ever becomes an internal node having no keys (this situation can occur in cases 2c and 3b
the e delete , a d s o l hild x.c1 becomes the new root of the tree, decreasing the height of
the tree by one and preserving the property that the root of the tree contains at least one key (unless
the tree is empty).
We sketch how deletion works with various cases of deleting keys from a B-tree (fig 5).
1. If the key k is in node x and x is a leaf, delete the key k from x.
2. If the key k is in node x and x is an internal node, do the following.
a) If the child y that precedes k in node x has at least t keys, then find the predecessor k0 of k in the
sub-tree rooted at y. Recursively delete k0, and replace k by k0 in x. (We can find k0 and delete it in a
single downward pass.)
b) If y has fewer than t keys, then, symmetrically, examine the child z that follows k in node x. If z
has at least t keys, then find the successor k0 of k in the subtree rooted at z. Recursively delete k0,
and replace k by k0 in x. (We can find k0 and delete it in a single downward pass.)
c) Otherwise, if both y and z have only t-1 keys, merge k and all of z into y, so that x loses both k
and the pointer to z, and y now contains 2t-1 keys. Then free z and recursively delete k from y.
3. If the key k is not present in internal node x, determine the root x.c(i) of the appropriate subtree
that must contain k, if k is in the tree at all. If x.c(i) has only t-1 keys, execute step 3a or 3b as
necessary to guarantee that we descend to a node containing at least t keys. Then finish by recursing
on the appropriate child of x.
a) If x.c(i) has only t-1 keys but has an immediate sibling with at least t keys, give x.c(i) an extra key
o i g a ke fro do i to . i , o i g a ke fro . i s i ediate left or right si li g up
into x, and moving the appropriate child pointer from the sibling into x.c(i).
b) If . i a d oth of . i s i ediate si li gs ha e t-1 keys, merge x.c(i) with one sibling, which
involves moving a key from x down into the new merged node to become the median key for that
node.
Since most of the keys in a B-tree are in the leaves, deletion operations are most often used to delete
keys from leaves. The recursive delete procedure then acts in one downward pass through the tree,
without having to back up. When deleting a key in an internal node, however, the procedure makes a
downward pass through the tree but may have to return to the node from which the key was deleted
to replace the key with its predecessor or successor (cases 2a and 2b).

Analysis & Design of Algorithm (CS-4004) Page 7


Page no: 7 Follow us on facebook to get real-time updates from RGPV
Downloaded from be.rgpvnotes.in

Department of Computer Science and Engineering

Example:

Fig 5: Deletion in B-Tree


5. Basic Search and Traversal Techniques for Tree and Graph
Tree Traversal Techniques:

 Inorder : L N R
Suppose N- Node, L- Left Child and R- Right Child

 Preorder : N L R
 Postorder : L R N

Inorder: 5 12 6 1 9
Preorder: 1 12 5 6 9
Postorder: 5 6 12 9 1

Analysis & Design of Algorithm (CS-4004) Page 8


Page no: 8 Follow us on facebook to get real-time updates from RGPV
Downloaded from be.rgpvnotes.in

Department of Computer Science and Engineering

Graph Traversal Techniques:


Depth First Search (DFS)
The aim of DFS algorithm is to traverse the graph in such a way that it tries to go far from the root
node. Stack is used in the implementation of the depth first search.
Algorithmic Steps
Step 1: Push the root node in the Stack.
Step 2: Loop until stack is empty.
Step 3: Peek the node of the stack.
Step 4: If the node has unvisited child nodes, get the unvisited child node, mark it as traversed and
push it on stack.
Step 5: If the node does not have any unvisited child nodes, pop the node from the stack.
Breadth First Search (BFS)
This is a very different approach for traversing the graph nodes. The aim of BFS algorithm is to
traverse the graph as close as possible to the root node. Queue is used in the implementation of the
breadth first search.
Algorithmic Steps
Step 1: Push the root node in the Queue.
Step 2: Loop until the queue is empty.
Step 3: Remove the node from the Queue.
Step 4: If the removed node has unvisited child nodes, mark them as visited and insert the
unvisited children in the queue.

Breadth First search: 1 2 3 4 5


Depth First Search: 1 2 4 5 3
6. NP-Completeness
We have been writing about efficient algorithms to solve complex problems, like shortest path, Euler
graph, minimum spanning tree, etc. Those were all success stories of algorithm designers. In this
post, failure stories of computer science are discussed.
Can all computational problems be solved by a computer? There are computational problems that
cannot be solved by algorithms even with unlimited time. For example Turing Halting problem (Given
a program and an input, whether the program will eventually halt when run with that input, or will
run forever). Alan Turing proved that general algorithm to solve the halting problem for all possible
program-input pairs cannot exist. A key part of the proof is, Turing machine was used as a
mathematical definition of a computer and program (Source Halting Problem).

Analysis & Design of Algorithm (CS-4004) Page 9


Page no: 9 Follow us on facebook to get real-time updates from RGPV
Downloaded from be.rgpvnotes.in

Department of Computer Science and Engineering

Status of NP Complete problems is another failure story, NP complete problems are problems whose
status is unknown. No polynomial time algorithm has yet been discovered for any NP complete
problem, nor has anybody yet been able to prove that no polynomial-time algorithm exist for any of
them. The interesting part is, if any one of the NP complete problems can be solved in polynomial
time, then all of them can be solved.
What are NP, P, NP-complete and NP-Hard problems?
P is set of problems that can be solved by a deterministic Turing machine in Polynomial time.
NP is set of decision problems that can be solved by a Non-deterministic Turing Machine in
Polynomial time. P is subset of NP (any problem that can be solved by deterministic machine in
polynomial time can also be solved by non-deterministic machine in polynomial time) figure.
I for all , NP is set of de isio pro le s hi h a e sol ed a pol o ial ti e ia a Lu k
Algorith , a agi al algorith that al a s akes a right guess among the given set of choices.
NP-complete problems are the hardest problems in NP set. A decision problem L is NP-complete if:
1) L is in NP (Any given solution for NP-complete problems can be verified quickly, but there is no
efficient known solution).
2) Every problem in NP is reducible to L in polynomial time (Reduction is defined below).
A problem is NP-Hard if it follo s propert e tio ed a o e, does t eed to follo propert .
Therefore, NP-Complete set is also a subset of NP-Hard set (fig 6).

NP Hard

NP Complete

P=NP

NP

Fig 6: Relationship between P,NP, NP Complete & NP Hard

Analysis & Design of Algorithm (CS-4004) Page 10


Page no: 10 Follow us on facebook to get real-time updates from RGPV
We hope you find these notes useful.
You can get previous year question papers at
https://qp.rgpvnotes.in .

If you have any queries or you want to submit your


study notes please write us at
rgpvnotes.in@gmail.com

You might also like