SPPU Pattern2019 Fds Unit 2
SPPU Pattern2019 Fds Unit 2
Unit 2
Sequential organization
The physical order during which the records are placed within the file
determines the sequence of records.
The relationships among records within the file don't change, except that the
file is often extended. There are not any keys. Both the files including database
files and files available on device can have sequential organization.
Each record within the file, except the primary, features a unique predecessor
record, and every record, except the last, also features a unique successor record.
In data structures, a knowledge structure is claimed to possess sequential access
if one can only visit the values it contains in one particular order. The canonical
example is that the linked list. Indexing into an inventory that has sequential access
requires O(n) time, where n is that the index.
Sequential access is a term defined as a bunch of elements (such as data during a
memory array or a computer file or on magnetic tape data storage) being accessed
during a predetermined, ordered sequence.
It’s the other of random access, the power to access an arbitrary element of a
sequence as easily and efficiently as the other at any time.
Sequential access is usually the sole way of accessing the info , for instance if
it's on a tape
It’s going to even be the access method of choice, for instance if all that's
wanted is to process a sequence of knowledge elements.
As a result of sequential access, many sorting and searching algorithms like
quicksort and binary search may degenerate into bad algorithms that are even less
efficient than their naive alternatives; these algorithms are impractical without
random access.
The write and skim of records during a physical instead of a logical sequence.
On the opposite hand, some algorithms, typically people who don't have index,
require only sequential access, like mergesort, and face no penalty.
Array
Data_typearray_name [array_size];
• Elements have a selected value and data type, like "ABC", TRUE or FALSE,
etc.
• Each element also has its own index, which is employed to access the element.
• In terms of syntax, any variable that's declared as an array can store multiple
values.
Arrays are best for storing multiple values during a single variable
int[] arrA = new int[1];
Advantages
Very memory efficient, little or no memory is required aside from that needed to
store the contents.
Disadvantages
Slow insertion and deletion of elements
Array size must be known when the array is made and is fixed (static)
There is some specific operation which can be performed or people that are
supported by the array. These are:
Array representation
int array[10]={10,20,11,30,40,39,23,12,34,16};
10 20 11 30 40 39 23 12 34 16
elements
index 0 1 2 3 4 5 6 7 8 9
• For an array
• Index always start with 0
Size of data types may differ for different data types in array
2. Inserting- Insert operation is used to insert one or more data elements into an
array. Based on thenecessary requirement, a new element can be added at the
beginning, end, or any given index of array.
3. Deleting -Deletion refers to removing an existing element from the array and re-
organizing all elements of an array.
Set J = K
Set N = N-1
Stop
Set J = 0
Set J = J +1
PRINT J, ITEM
Stop
Stop
Output: arr3 [] = {1, 2, 3, 4, 4, 5, 6, 8}
Output: arr3 [] = {4, 5, 7, 8, 8, 9}
Method 1
• Traverse arr2[] and one by one insert elements (like insertion sort) of arr3[] to
arr1[]. This step take O(n1 * n2) time.
• Space complexity O(1)
Method 2
• Pick smaller of current elements in arr1[] and arr2[], copy this smaller element
to next position in arr3[] and move ahead in arr3[] and the array whose element is
picked.
• If there are remaining elements in arr1[] or arr2[], copy them also in arr3[].
Advantages
Reading /parsing an array element is simple and efficient. As shown within the above
table, the read time of array is O(1) in both best and worst cases. This is often because
any element are often instantly read using indexes (base address calculation behind the
scene) without traversing the whole array.
Array could also be a foundation of other data structures. For instance other data
structures like LinkedList, Stack, Queue etc. are implemented using array.
All the weather of an array are often accessed employing one name (array name) in
conjunction with the index, which is readable, user-friendly and efficient rather than
storing those elements in different-2 variables.
Disadvantages
While using array, we must need to make the selection of the size of the array within
the start, so if we aren't aware what percentage elements we are becoming to store in
array, it'd make the task difficult.
The size of the array is fixed so if at later point; if we'd wish to store more elements in
it then it can’t be done. On the other hand, if we store less number of elements than the
declared size, the remaining allocated memory is wasted.
2.7 Storage Representation and their Address Calculation: Row major and
Column Major
• A vector size is fixed and thus requires a hard and fast number of memory locations.
• L0 is that the address of the primary word allocated to the primary element of vector A.
• Let’s consider the more general case of representing a vector A whose boundary for its
subscript is given by some variable b.
Index Row major Column major
0 A11 A11
1 A12 A21
2 A13 A12
3 A21 A 22
4 A22 A 13
5 A23 A 23
• In C programming , multidimensional arrays are stored in row-major order, and the
array indexes are written row-first (lexicographical access order):
• In Fortran, arrays are stored in column-major order, while the array indexes are still
written row-first (colexico graphical access order):
• Where :
• B is the base address (address of the first block in the array).
• W is the width in bytes (size in bytes for each block in the array).
• Two dimensional arrays also are called table or matrix, two dimensional arrays have two
subscripts
• Two dimensional array during which elements are stored column by column is named as
column major matrix
• Two dimensional array during which elements are stored row by row is named as row major
matrix
• First subscript denotes number of rows and second subscript denotes the amount of columns
• Two dimensional array consisting of two rows and 4 columns as above Fig is stored
sequentially by columns : A [ 1, 1 ], A [ 2 , 1 ], A [ 1 , 2 ], A [ 2 , 2 ], A [ 1 , 3 ], A [ 2 , 3 ], A
[ 1, 4 ], A [ 2, 4 ]
• The address of element A [ i , j ] are often obtained by expression Loc (A [ i , j ]) = L0 + (j-
1)*2 + i-1 generally for 2 dimensional array consisting of n rows and m columns the address
element A [ i, j ] is given by Loc (A [ i , j ]) = L0 + (j-1)*n + (i – 1)
• In row major matrix, array are often generalized to arbitrary lower and boundary in its
subscripts, assume that b1 ≤ I ≤ u1 and b2 ≤ j ≤u2
Applications of Array
Symbol Manipulation (matrix representation of polynomial equation)
Sparse Matrix
Ordered list
• The structure of an ordered list is a collection of items where each item holds a relative
position that is based upon some underlying characteristic of the item.
• The ordering is typically either ascending or descending and we assume that list items have a
meaningful comparison operation that is already defined.
• Many of the ordered list operations are the same as those of the unordered list.
• add(item) adds a new item to the list making sure that the order is preserved. It needs the
item and returns nothing. Assume the item is not already in the list.
• remove(item) removes the item from the list. It needs the item and modifies the list. Assume
the item is present in the list.
• search(item) searches for the item in the list. It needs the item and returns a boolean value.
• isEmpty() tests to see whether the list is empty. It needs no parameters and returns
a boolean value.
• size() returns the number of items in the list. It needs no parameters and returns an integer.
• index(item) returns the position of item in the list. It needs the item and returns the index.
Assume the item is in the list.
• pop() removes and returns the last item in the list. It needs nothing and returns an item.
Assume the list has at least one item.
• pop(pos) removes and returns the item at position pos. It needs the position and returns the
item. Assume the item is in the list.
Symbol Manipulation using Array
• We will use array for various quite operations in polynomial equation like addition,
subtraction, division, differentiation etc…
• We have an interest find suitable representation for polynomial in order that different
operations like addition, subtraction etc… are often performed in efficient manner
• Once we've algorithm for converting the polynomial equation to an array representation and
another algorithm for converting array to polynomial equation, then different operations in
array (matrix) are going to be corresponding operations of polynomial equation
Representing power of x 0 1 2 3 4 5 6 7
Element 18 12 -11 10 0 0 5 0
index 0 1 2 3 4 5 6 7
Advantage
Disadvantage
• Huge array size required for sparse polynomials waste of space and runtime.
• Array representation cost too much memory to be wasted for higher degree of polynomial
• Will consume too much time for searching element and wasting too much memory that used
for allocation .]
• polynomial[50] = 6
• polynomial[3] = 10
• polynomial[2] = -11
• polynomial[1] = 12
• polynomial[0] = 18
The basic idea of polynomial addition is to add coefficient parts of the polynomials having
same exponent.
Polynomial addition
A polynomial is an expression that contains quite two terms. A term is formed from
coefficient and exponent.
A polynomial could also be represented using array or structure. A structure could also be
defined such it contains two parts – one is that the coefficient and second is that the
corresponding exponent. The structure definition could also be given as shown below:
Structpolynomial{
int coefficient;
int exponent;
};
If p1[i].expo=p2[j].expo, then
3. p3[i].coeff=p1[i].coeff+p2[i].coeff
p3[k].expo=p1[i].expo
p3[k].coeff=p1[i].coeff
p3[k].expo=p1[i].expo
[Increase counter] Set i=i+1,k=k+1
else
p3[k].coeff=p2[j].coeff
p3[k].expo=p2[j].expo
Set j=j+1,k=k+1
[End of If]
[End of loop]
4. Repeat while i<t1
p3[k].coeff=p1[i].coeff
p3[k].expo=p1[i].expo
Set i=i+1,k=k+1
[End of loop]
p3[k].coeff=p2[j].coeff
p3[k].expo=p2[j].expo
Set j=j+1,k=k+1
[End of loop]
6. Return k
7. Exit
add (A[0..m-1], B[0..n01])
1) Create a sum array sum[] of size equal to maximum of 'm' and 'n'
2) Copy A[] to sum[].
5) Return sum[].
Example:
Example 2
P2=5x +8x+7
2
Time complexity
Time complexity of adding two polynomial O(m+n)
Multiplication of two polynomials however requires manipulation of each node such that the
exponents are added up and the coefficients are multiplied. After each term of first
polynomial is operated upon with each term of the second polynomial, then the result has to
be added up by comparing the exponents and adding the coefficients for similar exponents
and including terms as such with dissimilar exponents in the result
Algorithm:
multiply(A[0..m-1], B[0..n01])
4) Return prod[].
A simple solution is to one by one consider every term of first polynomial and multiply it
with every term of second polynomial.
Example:-
p1=4x +3x+2
2
Enter thecoeff of x^2 =4
Sparse matrix
Sparse matrix is amatrix in which most of the elements are zero
• By contrast if most of the elements are non zero then the matrix is considered as ‘dense’ .
• Sparcity of a sparse matrix is the number of zero valued element divided by total number of
elements
• We can device a simple representation scheme whose space requirement equals the size of
the nonzero elements.
Example:-
• The non-zero entries of a sparse matrix could also be mapped into a linear list in row-major
order.
Figure A 4 x 4 matrix
• So each element of the array into which the sparse matrix is mapped need to have
three fields: row, column and value
• A corresponding amount of time is saved creating the linear list representation over
initialization of two dimension array.
• Here from 6X7=42 elements, only 10 are non zero. A[1,3]=6, A[1,5]=9, A[2,1]=2, A[2,4]=7,
A[2,5]=8, A[2,7]=4, A[3,1]=10, A[4,3]=12, A[6,4]=3, A[6,7]=5.
• One basic method for storing such a sparse matrix is to store non-zero elements in one
dimensional array and to spot each array elements with row and column indices
• A more efficient representation in terms of storage requirement and access time to the row of
the matrix is shown in fid (d). The row vector changed in order that its ith element is that
the index to the primary of the column indices for the element in row I of the matrix.
To Transpose a matrix, we will simply change every column value to the row value and vice-versa,
However, during this case, the resultant matrix won’t be sorted as we require.
Hence, we initially determine the number of elements less than the current element’s column being
inserted in order to get the exact index of the resultant matrix where the current element should be
placed.
SparseMatrixSparseMatrix::Transpose()
{
}
}
Return b;
}
To Multiply the matrices, we first calculate transpose of the second matrix to simplify our
comparisons and maintain the sorted order.
So, the resultant matrix is obtained by traversing through the whole length of both matrices and
summing the acceptable multiplied values.
Any row value adequate to x within the first matrix and row value adequate to y within the second
matrix (transposed one) will contribute towards result[x][y].
This is obtained by multiplying all such elements having col value in both matrices and adding
only those with the row as x in first matrix and row as y within the second transposed matrix to urge
the result[x][y].
1 void mm_mul_sa (sa_t *A, sa_t *B, sa_t *C)
2 {
3 inti, j, k;
4 int sum;
5
8 sum = 0;
11 }
12 saSet (C,i,j,sum);
13 }
14 }
15 }
1 2 10 1 1 2
1 3 12 1 2 5
2 1 1 2 2 1
2 3 2 3 1 8
1 2 10 1 1 2
1 3 12 1 3 8
2 1 1 2 1 5
2 3 2 2 2 1
1 1 96
1 2 10
2 1 18
Matrix operations
Matrix 1: (4x4)
Row ColumnValue
1 2 10
1 4 12
3 3 5
4 1 15
4 2 12
Matrix 2: (4X4)
1 3 8
2 4 23
3 3 9
4 1 20
4 2 25
Output:
1 2 10
1 3 8
1 4 12
2 4 23
3 3 14
4 1 35
4 2 37
1 1 240
1 2 300
1 4 230
3 3 45
4 3 120
4 4 276
1 4 15
2 1 10
2 4 12
3 3 5
4 1 1
Time Complexity
Addition operation of sparse matrix has time complexity of, hence, has a time complexity of O(n),
where n is the number of non-zero elements in the larger matrix amongst the
two.
Transpose of sparse matrix has a time complexity of O(n+m)