Ds Unit2
Ds Unit2
Ds Unit2
II
Algorithms and Linear Data Structure: Array
Introduction: Data, Data Structure and their types.
Algorithm and their Complexity, String processing
operations, Pattern matching algorithms: fast and
slow. Array: Types of array, memory representation
of array, Algorithm and operations on Array:
traversing, searching, insertion, deletion.
Applications
DATA STRUCTURES
Data may be organized in many different ways; the
logical or mathematical model of a particular
organization of data is called a data structure.
1. Start
2. Create an Array of a desired datatype and size.
3. Initialize a variable ‘i’ as 0.
4. Enter the element at ith index of the array.
5. Increment i by 1.
6. Repeat Steps 4 & 5 until the end of the array.
7. Stop
#include <stdio.h>
int main()
{
int LA[3], i;
printf("Array Before Insertion:\n");
for(i = 0; i < 3; i++)
printf("LA[%d] = %d \n", i, LA[i]);
printf("Inserting Elements.. ");
printf("The array elements after insertion :\n"); //
prints array values
for(i = 0; i < 3; i++)
{
LA[i] = i + 2;
printf("LA[%d] = %d \n", i, LA[i]);
}
return 0;
}
Output
Array Before Insertion:
LA[0] = 587297216
LA[1] = 32767
LA[2] = 0
Inserting Elements..
The array elements after insertion :
LA[0] = 2
LA[1] = 3
LA[2] = 4
Deletion Operation
In this array operation, we delete an element from the
particular index of an array.
This deletion operation takes place as we assign the value in
the consequent index to the current index.
Algorithm
Consider LA is a linear array with N elements and K is a
positive integer such that K<=N.
Following is the algorithm to delete an element available at the
Kth position of LA.
1. Start
2. Set J = K
3. Repeat steps 4 and 5 while J < N
4. Set LA[J] = LA[J + 1]
5. Set J = J+1
6. Set N = N-1
7. Stop
#include <stdio.h>
void main()
{
int LA[] = {1,3,5};
int n = 3;
int i;
printf("The original array elements are :\n");
for(i = 0; i<n; i++)
printf("LA[%d] = %d \n", i, LA[i]);
for(i = 1; i<n; i++)
{
LA[i] = LA[i+1];
n = n – 1; }
printf("The array elements after deletion :\n");
for(i = 0; i<n-1; i++)
printf("LA[%d] = %d \n", i, LA[i]);
}
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
The array elements after deletion :
LA[0] = 1
LA[1] = 5
Search Operation
Searching an element in the array using a key;
The key element sequentially compares every value in the
array to check if the key is present in the array or not.
Algorithm
Consider LA is a linear array with N elements and K is a
positive integer such that K<=N.
Following is the algorithm to find an element with a value of
ITEM using sequential search.
1. Start
2. Set J = 0
3. Repeat steps 4 and 5 while J < N
4. IF LA[J] is equal ITEM THEN GOTO STEP 6
5. Set J = J +1
6. PRINT J, ITEM
7. Stop
#include <stdio.h>
void main()
{
int LA[] = {1,3,5,7,8};
int item = 5, n = 5;
int i = 0, j = 0;
printf("The original array elements are :\n");
for(i = 0; i<n; i++)
{
printf("LA[%d] = %d \n", i, LA[i]);
}
for(i = 0; i<n; i++)
{
if( LA[i] == item )
{
printf("Found element %d at position %d\n",
item, i+1);
}
}
}
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
Found element 5 at position 3
Multidimensional Array
The data items in a multidimensional array are stored in
the form of rows and columns.
Also, the memory allocated for the multidimensional
array is contiguous. So the elements in multidimensional
arrays can be stored in linear storage using two methods
i.e., row-major order or column-major order
data_type array_name[size1][size2]....[sizeN];
Time-Space Tradeoff:
• Each of algorithms will involve a particular data structure.
Accordingly, we may not always be able to use the most efficient
algorithm, since the choice of data structure depends on many things,
including the type of data structure and the frequency with which
various data operations are applied.
Steps, Control, Exit: The steps of the algorithm are executed one after
other, beginning with step 1, unless indicated otherwise. Control may be
transferred to step n of the algorithm by the statement “Go to step n”.
Generally Go to statements may be practically eliminated by using certain
control structures. The algorithm is completed when statement Exit is
encountered.
Variable Names: Variable names will use capital letters, as in MAX and
DATA.
Ex. Max: =DATA [1] Assigns the value in DATA [1] to MAX.
Input and Output: Data may be input and assigned to variables by
means of a Read statement with following form:
Read: Variable names.
Suppose f(n) and g(n) are functions defined on the positive integers with
the property that f(n) is bounded by some multiple of g(n) for almost all n.
this is, suppose there exist a positive integer n0 and a positive number M
such that, for all n> n0, we have |f(n)|<=M|g(n)| Then we may write
f(n)=O(g(n))
which is read “f(n) is of order g(n).”
LINEAR ARRAYS
A Linear array is a list of a finite number n of homogeneous data elements
such that:
The elements of the array are referenced respectively by an index set
consisting of n consecutive numbers.
The elements of the array are stored respectively in successive memory
locations.
The number n of elements is called the length or size of the array
If not explicitly stated, we will assume the index set consists of the integers
1,2,…n.
In general, the length or the number of elements of the array can be obtained
from the index set by the formula.
Length = UB-LB+1
Where,
UB- Largest Index
LB- Smallest Index
Notation:
LOC (LA [K]) =address of the element LA [K] of the array LA
Computer does not need to keep track of the address of every element of LA, but
needs
to keep track only of the address of the first element of LA, denoted by
Base (LA) Called base address of LA
Using Base(LA), the computer calculates the address of any element of LA by the
following formula:
Using Base(LA), the computer calculates the address of any
element of LA by the following formula:
LOC (LA [K]) =Base (LA) + w (K-lower bound)
Where,
w is the number of words per memory cell for the array
LA
Given any subscript K, one can locate and access the
content of LA[K] without scanning any other element of
LA.
TRAVERSING LINEAR ARRAY
characters respectively.
Variables: Each programming language has its own rules for forming character
may vary during the execution of the program as long as the length does not
exceed a maximum value determined by the program before the program is
executed.
Dynamic: By dynamic character variable, we mean a variable whose length can
Substring:
Accessing a substring from a given string requires three pieces of
information:
1. The name of the string or the string itself
2. The position of the first character of the substring in the given
string
3. The length of the substring or the position of the last character of
the substring.
We call this operation SUBSTRING. Specifically, we write
SUBSTRING (string, initial, length)
To denote the substring of a string S beginning in a position K
having a length L.
Ex. SUBSTRING (‘TO BE OR NOT BE’, 4, 7) =’BE OR N’
SUBSTRING (‘THE END’, 4, 4) =’□END’
INDEXING
If the pattern P does not appear in the text T, then INDEX is
assigned the value 0. the arguments “text” and “pattern” can
be either string constants or string variables.
Ex: INSERT(‘ABCDEFG’,3,’XYZ’)=’ABXYZCDEFG’
INSERT (‘ABCDEFG’,6,’XYZ’)=’ABCDEXYZFG’
P=AB
Algorithm executed twice.
During first execution, the first occurrence of AB in T is deleted.
Result T=XYABZ
During the second execution, the remaining occurrence of AB
in T
REPLACEMENT
Suppose in a given text T we want to replace the first occurrence of
a pattern P1 is by a pattern P2. We will denote this operation by
REPLACE (text, pattern1, pattern2)
P=AB,
Q=C
Algorithm
executed
twice.
During first
execution, the
first
PATTERN MATCHING ALGORITHM
Pattern matching is the problem of deciding whether or not
a given string pattern appears in a string text T. We assume
that the length of P does not exceed the length of T.
Wk=SUBSTRING(T,K,LENGTH(P))
W2=baa
W3=aab
W4=abb
W5=bba
W6=baa
W7=aab
FIRST PATTERN MATCHING ALGORITHM
Algorithm:(Pattern matching ) P and T are T=abaabbaabba
strings with lengths R and S, Respectively, and
are stored as arrays with one character per and pattern
element. This algorithm finds the INDEX of P in P=bba
T.
Step 1: [Initialize.] Set K:=1 and MAX:=S-R+1.
Step 2: Repeat Steps 3 to 5 while K!=MAX:
Step 3: Repeat for L=1 to R: [Tests each
character of P.]
If P[L]!=T[K+L-1]. Then : Go to step 5.
[End of inner loop.]
Step 4:[Success.] Set INEDX=K, and
Exit.
Step 5:K:=K+1.
[End of step 2 outer loop.]
Step 6: [Failure.] Set INDEX=0.
Step 7: Exit.
COMPLEXITY OF FIRST PATTERN MATCHING
Best case complexity of First pattern matching algorithm
is???
When P is r character string and T is s character string, the
data size for the algorithm is
n=r+s
Worst case occurs when every character of P except last
matches with every substring Wk
C(n)=r(s-r+1) for fixed n we have s=n-r
C(n)=r(n-2r+1)
=nr-2r2+r
C’=dc/dr=0
C’=n-4r+1
0=n-4r+1
r=(n+1)/
4
The maximum value of C(n) occurs when r=(n+1)/4
so
FIND INDEX AND COMPARISIONS WITH FIRST PATTERN
MATCHING?????
3. P=abc T=(ab)5
SECOND PATTERN MATCHING ALGORITHM
: Second pattern matching algorithm uses a table which is derived from a
particular pattern P but is independent of the text T.
Ex. Suppose P = aaba
Suppose T = T1, T2, T3 . . . . , where T1 denotes the ith character of T; and
suppose the first two characters of T match those of P; i.e. , suppose T = aa.
. . . Then T has one of the following three forms:
1.T = aab . . . . ,
2.T = aaa . . . . ,
3.T = aax
a b x
Q0 Q1 Q0 Q0
Q1 Q2 Q0 Q0
Q2 Q2 Q3 Q0
Q3 P Q0 Q0
a b x
Q0 Q1 Q0 Q0
Q1 Q2 Q0 Q0
Q2 Q2 Q3 Q0
Q3 P Q0 Q0
SECOND PATTERN MATCHING/FAST PATTERN MATCHING ALGORITHM
SEARCHING: LINEAR SEARCH:
Let DATA be a collection of data elements in
memory, and suppose a specific ITEM of information
is given.
Searching refers to the operation of finding the
Algorithm:
(Linear search) LINEAR (DATA, N, ITEM, LOC),
here DATA is a linear array with N elements, and
ITEM is a given item of information. This algorithm
finds the location LOC of ITEM in DATA, or sets
LOC: =0 if the search is unsuccessful.
1. [Insert ITEM at the end of DATA.] Set
DATA [N+1]:= ITEM.
2. [Initialize counter.] Set LOC: =1.
3. [Search for ITEM.]
Repeat while DATA [LOC]! =ITEM:
Set LOC: =LOC+1
[End of loop]
4. [Successful?] If LOC= N+1, then: Set LOC: =0.
5. Exit.
COMPLEXITY OF THE LINEAR SEARCH ALGORITHM
Columnmajor order, or
Row major order
The computer does not keep track of the address LOC(LA[K]) of every
element LA[K], but does keep track of Base(LA), the address of the first
element of LA. The computer uses the formula
The computer keeps track of Base (A)- the address of the first element A[1, 1] of
A-and computes the address LOC(A[J, K]) of A[J, K] using the formula.