DATA STRUCTURES AND ALGORITHMS Module 2 INTRODUCTION
Sep 10, 2015
DATA STRUCTURES AND ALGORITHMS Module 2 INTRODUCTION
What will happen if the words in a dictionary is not arranged alphabetically ?
?
DATA STRUCTURE
A data structure is used to store and organize data in a computer so that it can be used efficiently.
It considers not only the items stored, but also their relationship to each other.
Eg: Arrays, Stacks, Queues, Linked Lists, Tree , Graph
Stack And Queue
Linked list
Tree and Graph
Categories of data structuresLinear Data Structure:Those data structures in which data elements are organized in some sequence.The operations on a linear data structure are only possible in a sequence. (ie. we cannot insert the element directly into some location without traversing the previous element ).
Non-linear Data StructureThose data structures in which data elements are organized in any arbitrary order ( i.e. they are not organized in any sequence) are called non-linear data structures.
Categories of data structuresLinear Data Structure:Those data structures in which data elements are organized in some sequence or maintain a linear ordering.The operations on a linear data structure are only possible in a sequence. (ie. we cannot insert the element directly into some location without traversing the previous element )Eg: arrays, stacks, queues, linked lists.
Non-linear Data StructureThose data structures in which data elements are organized in any arbitrary order ( i.e. they are not organized in any sequence) are called non-linear data structures.Eg: tree , graph, sets
Common operations on Data structures
Create: this operation reserves the memory for the data elementsTraverse : accessing or visiting each element of data structure exactly onceSearch : to find the location of a particular element in the data structureInsert : to add some element into the data structureDelete: to delete an existing element from the data structureUpdate : to change the existing value of the data elementSort : arranging data element in some specific orderMerge : this operation is used when elements of two sorted lists, composed of similar elements, are combined to form a new sorted list
ABSTRACT DATA TYPESADT is a collection of data values and the functions which operate on these data values.It is implementation independent and isolates the program from the representation.
Advantages of ADT:language independentprevents direct manipulation of data values, except through the defined functionsby using ADT, we can change implementation quickly
ABSTRACT DATA TYPESEg:ADT student{Data:Roll_No;Student_Name;Marks;Age;Operations:Read_Info();Display_Info();Print_marks();}
For each operation, pre-conditions and post conditions may be given, where pre-conditions are required to perform the operations and post-conditions are the result of the operation.
For eg: PUSH() operationpre-condition - stack should not be full post condition - item is inserted and top is changed.
GENERIC ABSTRACT DATA TYPESIn generic ADT, the data type of elements are changed according to the data type supplied externally, which are also called generic parameters.Eg:ADT {Data:T stack[max];T item1, item2;int top;operations:init()push();pop();}
DATA STRUCTURES
ArraysStacksQueues
ARRAYSArray is a finite, ordered and collection of homogeneous elements referred to collectively by a single name.Finite: it contains only limited number of elementsOrdered: all the elements are stored one by one in contiguous memory location of computer memory in linear ordered fashionHomogeneous: all the elements of the array are of same typeExample: Array of integers to store the mark of all students.
ONE DIMENSIONAL ARRAYS Syntax:storage_class data_type array_name[size];Example:auto int A[5];
storage class specifies the scope of the array variables. storage class can be external, static or auto. storage class specifier is optional. If it is not used then compiler automatically takes it as auto.
MEMORY ALLOCATION FOR ARRAYSAddr( A(i))= M+ (i-L)w
Where L -> lower bound of the array w-> no of words to store an element
Operations on 1D array
Insertion:
Insertion can be performed at any position at beginning, end or middleif insertion is required at the beginning or in the middle then the array elements are shifted so that the element can be inserted in a given position.An element can be inserted at the end of the array only if sufficient memory space is allocated to accommodate the element.
ARRAY : insertionInsert at beginning, middle or end
123450
Insert at beginning1234590MAX
Insert at beginning1234951MAX
Insert at beginning912345MAX
ARRAY : Insertion (1)Insert 9 at the beginning, POS=1, MAX =5
123450
123455
123445
123345
122345
112345
912345
Insert at middle1234590MAX
Insert at middle1234953MAX
Insert at middle123495MAX
ARRAY : Insertion (2)Insert 9 at the middle, say POS=2, MAX =5
123450
123455
123445
123345
122345
192345
Insert at end1234590
Insert at end123459
ARRAY : Insertion
Algorithm: insert_array(A,MAX, K, ITEM)Input: Array with MAX no. of elementsOutput: Array with MAX +1 no. of elementsData Structure used: Array
Steps:set I = MAXwhile (I>=K)A[I+1]=A[I];set I=I-1;End whileset A[K]=ITEMEnd
Operations on 1D array
Deletion:
Deletion can be performed at any position at beginning, end or middlewhen the element is deleted from middle or beginning of the array, the elements are shifted to fill the deleted space
Delete from beginning123456MAX
Delete from beginning1234561MAX
Delete from beginning2345660MAX
Delete from middle123456MAX
Delete from middle3214563MAX
Delete from middle1245660MAX
ARRAY : DeletionAlgorithm: delete_array(A,MAX,K,ITEM)Input: Array with MAX no. of elementsOutput: Array with MAX -1 no. of elementsData Structure used: Array
Steps:Set ITEM=A[K]Repeat for J=K to MAX-1-Set A[J]=A[J+1][End of loop]Set N=N-1Exit
Operations on 1D array
3.Merging
Merging is used to combine two different arrays in to single array.Destination array must have enough size to occupy two arrays.
TWO DIMENSIONAL ARRAYSsyntax:storage_class data_type array_name[row] [col] ;Eg:int M[3][4]={ 1, 2, 3, 0, 4, 5, 6,0, 7, 8};orint M[3][4]={ {1,2,3}, {4,5,6} , {7,8} };
123045607800
MEMORY REPRESENTATION OF MATRIXTwo conventionsRow Major Order: The elements are arranged row-wise, ie. all the elements of first row are arranged, then all elements of second row and so on..
Column Major Order: The elements are arranged column-wise, ie. all the elements of first column are arranged, then all elements of second column and so on..
Reference of elements in a matrixThough matrix is linear, physically it is stored in a linear fashion
Indexing formula for different orders will be different
Row-major orderAssume base address is the first location in memory
Address of a[i[[j]= storing all the elements of first (i-1)th row + no. of elements in the ith row upto jth column = (i-1)*n+(j-1) n->no of columnsIf base address is M, Address of a[i[[j]= M+(i-1)*n+(j-1)
Column-majorAssume base address is the first location in memory
Address of a[i[[j]= storing all the elements of first (j-1)th column + no. of elements in the jth column upto ith row = (j-1)*m+(i-1) n->no of columnsIf base address is M, Address of a[i[[j]= M+(j-1)*m+(i-1)
3D arrays3D arrays can be compared with a book whereas 2D array can be compared with a page and 1D array with a line
Let x-> no. of rows y-> no of columns z-> no of pages
Add fig
3 D array In row-major orderAddr.(a[i][j][k])= number of elements in first (k-1)pages + no. of elements in kth page upto (i-1) rows + no. of elements in ith row upto (j-1) columns
=xy(k-1)+(i-1)y+(j-1)
3 D array
**************************************************************************