Unit-i Notes
Unit-i Notes
Asymptotic
Notations.
Algorithm:
An algorithm is a set of well-defined instructions to solve a particular problem. It
takes a set of input(s) and produces the desired output.
For example,
problem.
languages.
Step 1: Start
Step 4: Add num1 and num2 and assign the result to sum.
Sum= num1+num2
Step 1: Start
Step 4: If a > b
If a > c
Else
Else
If b > c
Else
Step 5: Stop
Step 1: Start
factorial = 1, i = 1
5.2: i = i+1
Step 7: Stop
Performance Analysis
can go by flight, by bus, by train and also by bicycle. Depending on the availability
and convenience, we choose the one which suits us. Similarly, in computer science,
there are multiple algorithms to solve a problem. When we have more than one
algorithm to solve a problem, we need to select the best one. Performance analysis
helps us to select the best algorithm from multiple algorithms to solve a problem.
When there are multiple alternative algorithms to solve a problem, we analyse them
and pick the one, which is best suitable for our requirements. The formal definition is
as follows...
algorithms.
That means when we have multiple algorithms to solve a problem, we need to select
We compare algorithms with each other which are solving the same problem, to select
elements like memory required by that algorithm, the execution speed of that
1. Whether that algorithm is providing the exact solution for the problem?
When we want to analyse an algorithm, we consider only the space and time required
as follows...
Space Complexity:
complete its execution. For any algorithm, memory is required for the following
purposes...
4. And for few other things like function calls, jumping statements etc,.
of instructions.
3. Data Space: It is the amount of memory used to store all the variables and
constants.
To calculate the space complexity, we must know the memory required to store
Example 1
int square(int a)
return a*a;
}
In the above sample code, it requires 2 bytes of memory to store variable 'a' and
That means, totally it requires 4 bytes of memory to complete its execution. And this
Time Complexity:
Every algorithm requires some amount of computer time to execute its instruction to
perform the task. This computer time required is called time complexity.
machine.
5. Input data
very difficult task because the configuration changes from one system to another
system. To solve this problem, we must assume a model machine with a specific
Now, we calculate the time complexity of following example code by using the above-
return a+b;
In the above sample code, it requires 1 unit of time to calculate a+b and 1 unit of time
to return the value. That means, totally it takes 2 units of time to complete its
execution. And it does not change based on the input values of a and b. That means
for all input values, it requires the same amount of time i.e. 2 units.
Asymptotic Analysis:
The efficiency of an algorithm depends on the amount of time, storage and other
resources(Type and size of the processor) required to execute the algorithm. The
efficiency is measured with the help of asymptotic notations.
An algorithm may not have the same performance for different types of inputs. With
the increase in the input size, the performance will change.
The study of change in performance of the algorithm with the change in the order of
the input size is defined as asymptotic analysis.
Asymptotic Notations:
Asymptotic notations are the mathematical notations used to describe the running
time of an algorithm when the input tends towards a particular value or a limiting
value.
For Example: In bubble sort, when the input array is already sorted, the time taken by
the algorithm is linear i.e. the best case.
But, when the input array is in reverse condition, the algorithm takes the maximum
time (quadratic) to sort the elements i.e. the worst case.
When the input array is neither sorted nor in reverse order, then it takes average time.
These durations are denoted using asymptotic notations.
1. Big-O notation
2. Omega notation
3. Theta notation
Big-O notation represents the upper bound of the running time of an algorithm.
The above expression can be described as a function f(n) belongs to the set
O(g(n)) if there exists a positive constant c such that it lies between 0 and
For any value of n, the running time of an algorithm does not cross the time
algorithm.
The above expression can be described as a function f(n) belongs to the set
Ω(g(n)) if there exists a positive constant c such that it lies above cg(n), for
sufficiently large n.
For any value of n, the minimum time required by the algorithm is given by
Omega Ω(g(n)).
Theta notation encloses the function from above and below. Since it represents
the upper and the lower bound of the running time of an algorithm, it is used
The above expression can be described as a function f(n) belongs to the set
Θ(g(n)) if there exist positive constants c1 and c2 such that it can be fitted in
If a function f(n) lies anywhere in between c1g(n) and c2g(n) for all n ≥ n0,
difference between the heights of left and right subtrees of every node in the tree is
either -1, 0 or +1. In other words, a binary tree is said to be balanced if the height of
left and right children of every node differ by either -1, 0 or +1. In an AVL tree, every
node maintains an extra information known as balance factor. The AVL tree was
An AVL tree is a balanced binary search tree. In an AVL tree, balance factor of
Balance factor of a node is the difference between the heights of the left and right
subtrees of that node. The balance factor of a node is calculated either height of left
subtree - height of right subtree (OR) height of right subtree - height of left subtree.
Note: Every AVL Tree is a binary search tree but every Binary Search Tree need
In AVL tree, after performing operations like insertion and deletion we need to
check the balance factor of every node in the tree. If every node satisfies the
balance factor condition then we conclude the operation otherwise, we must
make it balanced. Whenever the tree becomes imbalanced due to any operation
we use rotation operations to make the tree balanced.
Rotation is the process of moving nodes either to left or to right to make the tree
balanced.
There are four rotations and they are classified into two types.
In LL Rotation, every node moves one position to left from the current position.
To understand LL Rotation, let us consider the following insertion operation in
AVL Tree...
Single Right Rotation (RR Rotation)
In RR Rotation, every node moves one position to right from the current position. To
understand RR Rotation, let us consider the following insertion operation in AVL
Tree...
The RL Rotation is sequence of single right rotation followed by single left rotation. In
RL Rotation, at first every node moves one position to right and one position to left
from the current position. To understand RL Rotation, let us consider the following
1. Search
2. Insertion
3. Deletion
Search Operation in AVL Tree
• Step 1 - Insert the new element into the tree using Binary Search Tree
insertion logic.
• Step 2 - After insertion, check the Balance Factor of every node.
• Step 3 - If the Balance Factor of every node is 0 or 1 or -1 then go for next
operation.
• Step 4 - If the Balance Factor of any node is other than 0 or 1 or -1 then
that tree is said to be imbalanced. In this case, perform
suitable Rotation to make it balanced and go for next operation.
The deletion operation in AVL Tree is similar to deletion operation in BST. But
after every deletion operation, we need to check with the Balance Factor
condition. If the tree is balanced after deletion go for next operation otherwise
perform suitable rotation to make the tree Balanced.
Applications of AVL Trees
1. Most in-memory sets and dictionaries are stored using AVL trees.
2. Database applications, where insertions and deletions are less common
but frequent data lookups are necessary, also frequently employ AVL
trees.
3. In addition to database applications, it is employed in other applications
that call for better searching.
4. A balanced binary search tree called an AVL tree uses rotation to keep
things balanced.
5. It may be used in games with plotlines as well.
6. It is mostly utilized in business sectors where it is necessary to keep
records on the employees that work there and their shift changes.
B - Tree Data structure
In search trees like binary search tree, AVL Tree, Red-Black tree, etc., every node
contains only one value (key) and a maximum of two children. But there is a special
type of search tree called B-Tree in which a node contains more than one value (key)
and more than two children. B-Tree was developed in the year 1972 by Bayer and
McCreight with the name Height Balanced m-way Search Tree. Later it was named
as B-Tree.
B-Tree can be defined as follows...
Note1: The number of keys in a node and number of children for a node depends on
the order of B-Tree.
Note2: Every B-Tree has an order.
2. Property 2 - All nodes except root must have at least [m/2]-1 keys and
3. Property 3 - All non leaf nodes except root (i.e. all internal nodes) must have
4. Property 4 - If the root node is a non leaf node, then it must have atleast
2 children.
5. Property 5 - A non leaf node with n-1 keys must have n number of children.
Example
Operations on a B-Tree
1. Search
2. Insertion
3. Deletion
The search operation in B-Tree is similar to the search operation in Binary Search
Tree. In a Binary search tree, the search process starts from the root node and we make
a 2-way decision every time (we go to either left subtree or right subtree). In B-Tree
also search process starts from the root node but here we make an n-way decision
every time. Where 'n' is the total number of children the node has. In a B-Tree, the
search operation is performed with O(log n) time complexity. The search operation is
performed as follows...
Step 2 - Compare the search element with first key value of root node in the tree.
Step 3 - If both are matched, then display "Given node is found!!!" and terminate
the function
Step 4 - If both are not matched, then check whether search element is smaller or
subtree.
Step 6 - If search element is larger, then compare the search element with next key
value in the same node and repeat steps 3, 4, 5 and 6 until we find the exact match
or until the search element is compared with last key value in the leaf node.
Step 7 - If the last key value in the leaf node is also not matched then display
In a B-Tree, a new element must be added only at the leaf node. That means, the new key Value is
always attached to the leaf node only. The insertion operation is performed as follows...
Step 2 - If tree is Empty, then create a new node with new key value and insert it into the tree
as a root node.
Step 3 - If tree is Not Empty, then find the suitable leaf node to which the new key value is
added using Binary Search Tree logic.
Step 4 - If that leaf node has empty position, add the new key value to that leaf node in
ascending order of key value within the node.
Step 5 - If that leaf node is already full, split that leaf node by sending middle value to its
parent node. Repeat the same until the sending value is fixed into a node.
Step 6 - If the spilting is performed at root node then the middle value becomes new root node
for the tree and the height of the tree is increased by one.
Example
Construct a B-Tree of Order 3 by inserting numbers from 1 to 10.
The insertion operation for a B Tree is done similar to the Binary Search Tree but the
elements are inserted into the same node until the maximum keys are reached. The
Step 1 − Calculate the maximum (m−1) and, minimum (⌈m/2⌉−1) number of keys a
Step 2 − The data is inserted into the tree using the binary search insertion and once
the keys reach the maximum number, the node is split into half and the median key
becomes the internal node while the left and right keys become its children .
property but if we add the key 22, it will violate the maximum key property. Hence,
the node is split in half, the median key is shifted to the parent node and the insertion
is then continued.
Another interruption occurs during the insertion of 11, so the node is split and
median is shifted to the parent.
While inserting 16, even if the node is split in two parts, the parent node also overflows
as it reached the maximum keys. Hence, the parent node is split first and the median
key becomes the root. Then, the leaf node is split in half the median of leaf node is
Let us say the node to be deleted is called the target key. The target key can either be at
the leaf node or an internal node
The deletion of node in a B-Tree can be broadly classified into two cases:
If the target key is at the leaf node, we further study the given data to check if any of
Case 1: If the leaf node consists of the min number of keys according to the given
Case 2a: The node can borrow a key from the immediate left sibling node, if it has
more than the minimum number of keys. The transfer of the keys take place through
the parent node, i.e, the maximum key of the left sibling moves upwards and replaces
the parent; while the parent key moves down to the target node from where the target
Case 2b: The node can borrow a key from the immediate right sibling node, if it has
more than the minimum number of keys. The transfer of the keys take place through
the parent node, i.e, the minimum key of the right sibling moves upwards and replaces
the parent; while the parent key moves down to the target node from where the target
Case 2c: If neither of the siblings have keys more than the minimum number of keys
required then, merge the target node with either the left or the right sibling along with
If the target key is at an internal node, we further study the given data to check if
Case 1: If the left child has more than the minimum number of keys, the target key in
the internal node is replaced by its inorder predecessor ,i.e, the largest element of the
Case 2: If the right child has more than the minimum number of keys, the target key
in the internal node is replaced by it’s inorder successor ,i.e, the smallest element of
Applications of B Tree:
1. Large databases employ it to access information stored on discs.
2. Using the B-Tree, finding data in a data set can be done in a great deal less time.
3. Multilevel indexing is possible with the indexing feature.
4. The B-tree method is also used by the majority of servers.
5. In CAD systems, B-Trees are used to catalogue and search geometric data.
6. Other applications of B-Trees include encryption, computer networks, and
natural language processing.
7. Since accessing values stored in a large database that is stored on a disc takes a
long time, B trees are used to index the data and provide quick access to the
actual data stored on the disks.
8. In the worst case, it takes O (n) running time to search a database with n key
values that is not sorted or indexed. However, if we use B Tree to index this
database, it will be searched in O (log n) time in worst case.