You are on page 1of 10

Amortize Analysis

This analysis is used when the occasional operation is very slow, but most of the operations
which are executing very frequently are faster. Data structures we need amortized analysis for
Hash Tables, Disjoint Sets etc.

In the Hash-table, the most of the time the searching time complexity is O(1), but sometimes it
executes O(n) operations. When we want to search or insert an element in a hash table for most
of the cases it is constant time taking the task, but when a collision occurs, it needs O(n) times
operations for collision resolution.

Aggregate Method
The aggregate method is used to find the total cost. If we want to add a bunch of data, then we
need to find the amortized cost by this formula.

For a sequence of n operations, the cost is −

Example on Amortized Analysis


For a dynamic array, items can be inserted at a given index in O(1) time. But if that index is not
present in the array, it fails to perform the task in constant time. For that case, it initially doubles
the size of the array then inserts the element if the index is present.
For the dynamic array, let = cost of ith insertion.

Amortized AnalysisSupplemental reading in CLRS:


Chapter 17Data structures typically support several different types of operations,
each with its own cost (e.g.,time cost or space cost). The idea behind amortized
analysis is that, even when expensive operationsmust be performed, it is often
possible to get away with performing them rarely, so that the aver-age cost per
operation is not so high. It is important to realize that these “average costs” are
notexpected values—there needn’t be any random events. 1Instead, we are
considering the worst-caseaverage cost per operation in a sequence of many
operations. In other words, we are interested inthe asymptotic behavior of the
functionC(n)=1n·(worst-case total cost of a sequence ofnoperations)(possibly with
some condition on how many times each type of operation may occur). “Worst-
case”means that no adversary could choose a sequence ofnoperations that gives a
worse running time.In this lecture we discuss three methods of amortized analysis:
aggregate analysis, the account-ing method, and the potential method

Here we will see what are the B+ Trees. The B+ Trees are extended version of B-Trees. This tree
supports better insertion, deletion and searching over B-Tree.

B-trees, the keys and the record values are stored in the internal as well as leaf nodes. In B+ tree
records, can be stored at the leaf node, internal nodes will store the key values only. The leaf
nodes of the B+ trees are also linked like linked list

Example of B+ Tree −
This supports basic operations like searching, insertion, deletion. In each node, the item will be
sorted. The element at position i has child before and after it. So children sored before will hold
smaller values, and children present at right will hold bigger values.

Advantages over B-Tree


 Records can be fetched in equal number of disk accesses
 Height of the tree remains balanced, and less as compared to B-Trees
 As the leafs are connected like linked list, we can search elements in sequential manner
also
 Keys are used for indexing
 The searching is faster, as data are stored at leaf level only.

Rules for B+ Tree


Here are essential rules for B+ Tree.
 Leaves are used to store data records.
 It stored in the internal nodes of the Tree.
 If a target key value is less than the internal node, then the point just to its left side is
followed.
 If a target key value is greater than or equal to the internal node, then the point just to its
right side is followed.
 The root has a minimum of two children.

Why use B+ Tree


Here, are reasons for using B+ Tree:

 Key are primarily utilized to aid the search by directing to the proper Leaf.
 B+ Tree uses a "fill factor" to manage the increase and decrease in a tree.
 In B+ trees, numerous keys can easily be placed on the page of memory because they do
not have the data associated with the interior nodes. Therefore, it will quickly access tree
data that is on the leaf node.
 A comprehensive full scan of all the elements is a tree that needs just one linear pass
because all the leaf nodes of a B+ tree are linked with each other.

B+ Tree vs. B Tree


Here, are the main differences between B+ Tree vs. B Tree
B + Tree B Tree
Search keys can be repeated. Search keys cannot be redundant.
Both leaf nodes and internal nodes can store
Data is only saved on the leaf nodes.
data
Data stored on the leaf node makes the search Searching is slow due to data stored on Leaf
more accurate and faster. and internal nodes.
Deletion is not difficult as an element is only Deletion of elements is a complicated and time-
removed from a leaf node. consuming process.
Linked leaf nodes make the search efficient and
You cannot link leaf nodes.
quick.

Here we will see, how to perform the insertion into a B+ Tree. Suppose we have a B+ Tree like
below −

Example of B+ Tree −

To insert an element, the idea is very similar to the B-Tree, if one element is inserted, that will be
stored at the leaf node. If that is present in some internal node, then it will also be there, at the
leaf as right child of itself.

Suppose we want to insert 65 into the tree. So that is greater than 60, and less than 75. Then it
will be inserted into the middle sub-tree. Now, 65, will be inserted into node after 63, then that
node will be divided into two parts, 65 will go up, and 65 will also be there at the right node of it.

B+ Tree after inserting 65.


Algorithm
BPlusTreeInsert(root, key) −

Input − The root of the tree, and key to insert

We will assume, that the key is not present into the list
Start from root node, perform exact match for key as ‘key’ till a leaf node.
Let the search path
be x1, x2, … , xh. The x1 is first node so root, then xh is leaf node. Each node
xi is parent of xi+1
Insert the new object where key is ‘key’, and value is v into xh.
i := h
while xi overflows, do
   divide xi into two nodes, by moving the larger half of the keys into a new
node p.
   if xi is leaf node, link p into the linked list among leaf nodes.
   identify a key k, to be inserted into the parent level along with child
pointer pointing p.
   The choice of k depends on the type of the node xi. If xi is leaf node, we
will perform
   copy up. So smallest key in p, is copied as k to the parent level. On the
other hand, if xi is
   non-leaf node, then we will perform push up. So smallest key in p, will be
copied into k,
   in the parent node.
   if i = 0, then
      create a new index node as the new root. In the new root store node with
key k,
      and two child xi and p.
      return
   else
      insert a key k and a child pointer pointing to p, into node xi-1.
      i := i – 1
   end if
done
During insertion following properties of B+ Tree must be followed: Each node except root can have a
maximum of M children and at least ceil(M/2) children. Each node can contain a maximum of M – 1 keys
and a minimum of ceil(M/2) – 1 keys. The root has at least two children and atleast one search key.

trassen’s Matrix Multiplication in algorithms

In this article, we are going to discuss about the strassen matrix multiplication, formula of
matrix multiplication and algorithms for strassen matrix multiplication.
Submitted by Prerana Jain, on June 22, 2018

Introduction

Strassen in 1969 which gives an overview that how we can find the multiplication of two 2*2
dimension matrix by the brute-force algorithm. But by using divide and conquer technique
the overall complexity for multiplication two matrices is reduced. This happens by decreasing
the total number if multiplication performed at the expenses of a slight increase in the number of
addition.

For multiplying the two 2*2 dimension matrices Strassen's used some formulas in which there
are seven multiplication and eighteen addition, subtraction, and in brute force algorithm, there is
eight multiplication and four addition. The utility of Strassen's formula is shown by its
asymptotic superiority when order n of matrix reaches infinity. Let us consider two matrices A
and B, n*n dimension, where n is a power of two. It can be observed that we can contain four
n/2*n/2 submatrices from A, B and their product C. C is the resultant matrix of A and B.

Procedure of Strassen matrix multiplication

There are some procedures:

1. Divide a matrix of order of 2*2 recursively till we get the matrix of 2*2.
2. Use the previous set of formulas to carry out 2*2 matrix multiplication.
3. In this eight multiplication and four additions, subtraction are performed.
4. Combine the result of two matrixes to find the final product or final matrix.

Formulas for Stassen’s matrix multiplication

In Strassen’s matrix multiplication there are seven multiplication and four addition, subtraction
in total.

1. D1 = (a11 + a22) (b11 + b22)


2. D2 = (a21 + a22).b11
3. D3 = (b12 – b22).a11
4. D4 = (b21 – b11).a22
5. D5 = (a11 + a12).b22
6. D6 = (a21 – a11) . (b11 + b12)
7. D7 = (a12 – a22) . (b21 + b22)

C11 = d1 + d4 – d5 + d7
C12 = d3 + d5
C21 = d2 + d4
C22 = d1 + d3 – d2 – d6

Algorithm for Strassen’s matrix multiplication


Algorithm Strassen(n, a, b, d)

begin
If n = threshold then compute
C = a * b is a conventional matrix.
Else
Partition a into four sub matrices a11, a12, a21, a22.
Partition b into four sub matrices b11, b12, b21, b22.
Strassen ( n/2, a11 + a22, b11 + b22, d1)
Strassen ( n/2, a21 + a22, b11, d2)
Strassen ( n/2, a11, b12 – b22, d3)
Strassen ( n/2, a22, b21 – b11, d4)
Strassen ( n/2, a11 + a12, b22, d5)
Strassen (n/2, a21 – a11, b11 + b22, d6)
Strassen (n/2, a12 – a22, b21 + b22, d7)

C = d1+d4-d5+d7 d3+d5
d2+d4 d1+d3-d2-d6

end if

return (C)
end.

Code for Strassen matrix multiplication

#include <stdio.h>

int main()
{
int a[2][2],b[2][2],c[2][2],i,j;
int m1,m2,m3,m4,m5,m6,m7;

printf("Enter the 4 elements of first matrix: ");


for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);

printf("Enter the 4 elements of second matrix: ");


for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&b[i][j]);

printf("\nThe first matrix is\n");


for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
printf("%d\t",a[i][j]);
}

printf("\nThe second matrix is\n");


for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
printf("%d\t",b[i][j]);
}

m1= (a[0][0] + a[1][1])*(b[0][0]+b[1][1]);


m2= (a[1][0]+a[1][1])*b[0][0];
m3= a[0][0]*(b[0][1]-b[1][1]);
m4= a[1][1]*(b[1][0]-b[0][0]);
m5= (a[0][0]+a[0][1])*b[1][1];
m6= (a[1][0]-a[0][0])*(b[0][0]+b[0][1]);
m7= (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);

c[0][0]=m1+m4-m5+m7;
c[0][1]=m3+m5;
c[1][0]=m2+m4;
c[1][1]=m1-m2+m3+m6;

printf("\nAfter multiplication using \n");


for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
printf("%d\t",c[i][j]);
}

return 0;
}

Output:

Enter the 4 elements of first matrix:


5 6 1 7
Enter the 4 element of second matrix:
6 2 8 7
The first matrix is
5 6
1 7
The second matrix is
6 2
8 7
After multiplication
78 52
62 51

Complexity: The time complexity is O(N2.8074).

TOP Interview Coding Problems/Challenges

 Run-length encoding (find/print frequency of letters in a string)


 Sort an array of 0's, 1's and 2's in linear time complexity
 Checking Anagrams (check whether two string is anagrams or not)
 Relative sorting algorithm
 Finding subarray with given sum
 Find the level in a binary tree with given sum K
 Check whether a Binary Tree is BST (Binary Search Tree) or not
 1[0]1 Pattern Count
 Capitalize first and last letter of each word in a line
 Print vertical sum of a binary tree
 Print Boundary Sum of a Binary Tree
 Reverse a single linked list
 Greedy Strategy to solve major algorithm problems
 Job sequencing problem
 Root to leaf Path Sum
 Exit Point in a Matrix
 Find length of loop in a linked list
 Toppers of Class
 Print All Nodes that don't have Sibling
 Transform to Sum Tree
 Shortest Source to Destination Path

Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.

You might also like