Fbca DATA STRUCTURE (Paper No.-1.4) UNIT-1: Asymptotic Complexity Main Component Square
Fbca DATA STRUCTURE (Paper No.-1.4) UNIT-1: Asymptotic Complexity Main Component Square
4) UNIT-1
1. What is algorithm? Discuss various algorithmic notation techniques used in data structure?
Ans.
A problem may have numerous solutions. In order to choose the best algorithm for a particular task, you need to be able to judge how long a particular
solution will take to run. Or, more accurately, you need to be able to judge how long two solutions will take to run, and choose the better of the two. You don't
need to know how many minutes and seconds they will take, but you do need some way to compare algorithms against one another.
Asymptotic complexity is a way of expressing the main component of the cost of an algorithm, using idealized (not comparable) units of computational
work. Consider, for example, the algorithm for sorting a deck of cards, which proceeds by repeatedly searching through the deck for the lowest card. The
asymptotic complexity of this algorithm is the square of the number of cards in the deck. This quadratic behavior is the main term in the complexity formula, it
says, e.g., if you double the size of the deck, then the work is roughly quadrupled.
The exact formula for the cost is more complex, and contains more details than are needed to understand the essential complexity of the algorithm. With
our deck of cards, in the worst case, the deck would start out reverse-sorted, so our scans would have to go all the way to the end. The first scan would
involve scanning 52 cards, the next would take 51, etc. So the cost formula is 52 + 51 + ..... + 2. Generally, letting N be the number of cards, the formula is 2 +
difference does the constant factor of 1/2 make, at this level of abstraction? So the behavior is said to be .
Now let us consider how we would go about comparing the complexity of two algorithms. Let f(n) be the cost, in the worst case, of one algorithm,
expressed as a function of the input size n, and g(n) be the cost function for the other algorithm. E.g., for sorting algorithms, f(10) and g(10) would be the
maximum number of steps that the algorithms would take on a list of 10 items. If, for all values of n >= 0, f(n) is less than or equal to g(n), then the algorithm
with complexity function f is strictly faster. But, generally speaking, our concern for computational cost is for the cases with large inputs; so the comparison of
f(n) and g(n) for small values of n is less significant than the "long term" comparison of f(n) and g(n), for n larger than some threshold.
Note that we have been speaking about bounds on the performance of algorithms, rather than giving exact speeds. The actual number of steps required
to sort our deck of cards (with our naive quadratic algorithm) will depend upon the order in which the cards begin. The actual time to perform each of our steps
will depend upon our processor speed, the condition of our processor cache, etc., etc. It's all very complicated in the concrete details, and moreover not
relevant to the essence of the algorithm.
2. What do you mean by Data Structure? Differentiate between primitive and non-primitive data structure?
Ans.
An implementation of abstract data type is data structure i.e. a mathematical or logical model of a particular organization of data is called data structure.
Thus, a data structure is the portion of memory allotted for a model, in which the required data can be arranged in a proper fashion.
Types:-
A data structure can be broadly classified into (i) Primitive data structure
(ii) Non-primitive data structure
(i) Primitive data structure
The data structures, typically those data structure that are directly operated upon by machine level instructions i.e. the fundamental data types
such as int, float, double incase of ‘c’ are known as primitive data structures.
(ii) Non-primitive data structure
The data structures, which are not primitive are called non-primitive data structures.
There are two types of-primitive data structures.
(a) Linear Data Structures:-
A list, which shows the relationship of adjacency between elements, is said to be linear data structure. The most, simplest linear data structure is a 1-D
array, but because of its deficiency, list is frequently used for different kinds of data.
(b) Non-linear data structure:-
A list, which doesn’t show the relationship of adjacency between elements, is said to be non-linear data structure.
Linear Data Structure:
A list is an ordered list, which consists of different data items connected by means of a link or pointer. This type of list is also called a linked list. A
linked list may be a single list or double linked list.
Single linked list: - A single linked list is used to traverse among the nodes in one direction.
Double linked list: - A double linked list is used to traverse among the nodes in both the directions.
A linked list is normally used to represent any data used in word-processing applications, also applied in different DBMS packages.
A list has two subsets. They are: -
Stack: - It is also called as last-in-first-out (LIFO) system. It is a linear list in which insertion and deletion take place only at one end. It is used to
evaluate different expressions.
Queue: - It is also called as first-in-first-out (FIFO) system. It is a linear list in which insertion takes place at once end and deletion takes place at other
end. It is generally used to schedule a job in operating systems and networks.
Non-linear data structure:-
The frequently used non-linear data structures are
1
PRESIDENCY COLLEGE
FBCA DATA STRUCTURE(Paper No.-1.4) UNIT-1
(a) Trees : - It maintains hierarchical relationship between various elements
(b) Graphs : - It maintains random relationship or point-to-point relationship between various elements.
3. Discuss the representation of linear array. What is the difference between Row-major and column major implementation.
Ans.
It is not to find a large no. of programs which process the elements of an array in sequence. But, does it mean that the elements of an array are also
stored sequence in memory. The answer depends on the operating system under which the program is running. However, the elements of an array are stored
in sequence to the extent possible. If they are being stored in sequence, then how are they sequenced? Is it that the elements are stored row wise? Again it
depends on the operating system. The former is called row major order and the later it is called column major order.
Row Major Representation:
The first method of representing a two-dimensional array in memory is the row major representation .Under this representation, the first row of array
occupies the first set of memory location for the array; the second row occupies the next set, and so forth.
The schematic of row major representation of an array is:
a b cd
e f gh
i j kl
To make its row major representation, we perform the following process:
Move the elements of the second row starting from the first element to the memory location adjacent to the last element of the first row.
When this step is applied to the all rows except for the first row, we have a single row element. This is known as row major representation.
By above process, we get the row major order elements are {a, b, c, d, e, f, g, h, I, j, k, l)
Row 0 Row 1 Row 2 Row 3 …. Row n
4. a) Define array. What is the procedure to define & initialize a two dimensional Array and write an algorithm accessing of two-dimensional array
elements?
b) What is parallel array? Discuss briefly.
Ans.
(a) This is the simplest type of data structure, is a linear (one Dimensional) array. Programs very often deal with multiple data items processing common
characteristics. In such cases, it is often convenient to place the data items in array, so that they all share the common name. An array is a finite
collection of homogeneous data elements. Following are some Following are some of the example:
An array of integers to store the roll numbers of the students.
An array of character to store the name of an organization.
An array of doubles to store the salary of different employees.
Array size: Number of elements in array is called the size of the array. It is otherwise known as and length or dimension.
Type: Before using an array its type and dimension must be declared. Type of an array represents the kind of data type it is meant for. Ex: array of integer,
array of floats, array of characters etc.
Base: however, big an array its elements are always stored in contiguous memory location. Base of an array is the address of memory location where the
first element of an array is located.
Index: All the array elements can be referenced by a subscript like a[1]. This subscription is known as Index.
Initializing a two dimensional array:
A two dimensional array can be initialized like this:
Int array [3][3]={1, 2, 3, 4, 5,6, 7, 8,9}
The results are the same; it’s just as obvious. Also, like a normal array, you don’t have to specify the numbers in the brackets when you initialize an array,
the compiler will figure it out.
ACCING TWO-DIMENSIONAL ARRAY ELEMENTS
Elements of two-dimensional arrays can be accessed as follows:
a [0][0] = 10; //assigns 10 to element at first row and first column
2
PRESIDENCY COLLEGE
FBCA DATA STRUCTURE(Paper No.-1.4) UNIT-1
a [0][1]=3; // assigns 3 to element at first row and second column
a [2][2]=45; //assigns 45 to the element at second row and second column
Ex. The following program illustrates the working of two-dimensional arrays.
# include<stdio.h>
# include<conio.h>
main( )
{
Int a[3][3], I,j ;
print f (“Enter the array elements”);
}
}
print f (“The elements of the array are”);
for (i=0;i<=2;i++)
{
for (j=0;j<=2;i++)
{
print f(“%d”, a[i][ij]);
}
}//End of the main program
Output:
Enter the array elements:
4 6 8
7 2 1
1 1 3
The elements of the array are
4 6 8
7 2 1
1 1 3
(b) A parallel array is a simple data structure for representing arrays of records. It keeps a separate, homogeneous array for each field of the record, each
having the same number of elements. Then, objects located at the same index in each array are implicitly the fileds of a single record.
Pointers from one object to another are replaced by array indices. This contrasts with the normal approach of storing all fields of single record.
For Example:- one might declare an array of 100 names, each a string, and 100 ages, each an integer, associating each name with the age that has
the same index.
Example,
Int ages [] ={ 0, 17, 2, 52,25};
Char*names[]={“Sachi”, “Seetal”, “Sirish”, “Sankar”, “Rabi”);
Int parent[ ]={0 /* Sachi*/, 3/* Seetal*/,/* Sirish */,/* Sankar */,/* Rabi */};
For(i=1;i<=4;i++)
{
Printf(“Name:%s, Age:%d, Paent:%s\n”, names[i],ages[i],names[parent[i]]);
}
Advantages & Disadvantages
They can be used in languages which support only arrays of primitive types and not of records.
Parallel arrays are simple to understand and use, and are often used where declaring a record is more trouble than it’s worth.
They can save a substantial amount of space in some cases by avoiding, alignment issues.
Ex:- One of the fields of the record can be a single bit, and its array would only need to reserve one bit for each record, where as in the normal approach
many more bits would “pad” the field so the field so that it consumes an entire byte or a word.
3
PRESIDENCY COLLEGE
FBCA DATA STRUCTURE(Paper No.-1.4) UNIT-1
For inserting an element in to a linear array insert(a, len, pos, num) where ‘a’ is linear array ‘len’ be total no. of elements within array ’pos’ is the
position at which number ‘nu m’ will be inserted.
Algorithm:
1. (Initialize the value of I). Set I = len
Repeat for I =len down to pos.
2. (Shift the elements shown by 1 position)Set a[i+1]=a[i]
(End of loop)
3. (Insert the element at required position)
Set (pos) =num
4. (Reset len) set len =len+1
5. Display the new list of array.
6. End.
Deletion: For deleting an element from the array, the logic is straight forward. Deleting an element at the end of an array presents no difficulties, but
deleting element somewhere in the middle of array would require shifting all the elements to fill the space implied by the deletion element.
The following algorithm deletes the element stored at position pos from a linear array a and assign it to a variable item.
Delete (a, pos, n)
Algorithm:
1. Set item=a[pos];
2. Repeat for j=pos to n-1
(Shifting element 1 position upwards)
Set a[j]=a[j+1]
(End loop)
3. Reset n=n-1
4. Display the new list of elements of arrays.
5. End.
6. Write the Traversing Algorithm in Linear Array.
Ans: It is not to find a large no. of programs which process the elements of an array in sequence. But, does it mean that the elements of an array are also
stored sequence in memory. The answer depends on the operating system under which the program is running. However, the elements of an array are
stored in sequence to the extent possible. If they are being stored in sequence, then how are they sequenced? Is it that the elements are stored row wise?
Again it depends on the operating system. The former is called row major order and the later it is called column major order.
Array is a container which can hold a fix number of items and these items should be of the same type. Most of the data structures make use of arrays to
implement their algorithms.
Traverse − print all the array elements one by one.or process the each element one by one .
Let A be a collection of data elements stored in the memory of the computer. Suppose we want to print the content of each element of A or suppose we
want to count the number of elements of A with given property. This can be accomplished by traversing A, that is, by accessing and processing (frequently
called visiting) each element of An exactly once.
Algorithm
Step 5: Exit
4
PRESIDENCY COLLEGE