SUBJECT NAME:Data Structures and Algorithms Subject Code: Scsa1205 Unit: I
SUBJECT NAME:Data Structures and Algorithms Subject Code: Scsa1205 Unit: I
A
INSTITUTE OF SCIENCE AND TECHNOLOGY
Deemed to be University
Declared as category ‘A’ University by MHRD, Govt. of India
Jeppiaar Nagar, Rajiv Gandhi Salai, Chennai – 600 119, Tamil Nadu. India.
UNIT : I
SATHYABAM
A
INSTITUTE OF SCIENCE AND TECHNOLOGY
Deemed to be University
Declared as category ‘A’ University by MHRD, Govt. of India
Jeppiaar Nagar, Rajiv Gandhi Salai, Chennai – 600 119, Tamil Nadu. India.
Unit-I
• Introduction Data Structures - Need - classification - operations –
Abstract data types (ADT) - Array - characteristics - types - storage
representations. Array Order Reversal-Array Counting or Histogram-
Finding the maximum Number in a Set, Recursion- Towers of Hanoi-
Fibonacci series-Factorial.
Algorithm
• Algorithm is finite set of logic or instructions, written in order for
accomplishing the certain predefined task.
• Each algorithm must have:
• Specification: Description of the computational procedure.
• Pre-conditions: The condition(s) on input.
• Body of the Algorithm: A sequence of clear and unambiguous instructions.
• Post-conditions: The condition(s) on output.
Characteristics of an Algorithm
• Input: An algorithm must have 0 or well defined inputs.
• Output: An algorithm must have 1 or well defined outputs, and
should match with the desired output.
• Finiteness: An algorithm must be terminated after the finite number
of steps.
• Effectiveness: An algorithm must have step-by-step directions which
is independent of any programming code.
• Definiteness: An algorithm must be unambiguous and clear.
Major categories of algorithms
• Sort: Algorithm developed for sorting the items in certain order.
• Search: Algorithm developed for searching the items inside a data
structure.
• Delete: Algorithm developed for deleting the existing element from the
data structure.
• Insert: Algorithm developed for inserting an item inside a data
structure.
• Update: Algorithm developed for updating the existing element inside a
data structure.
performance of algorithm
• Time complexity: It is a way of representing the amount of time
needed by a program to run to the completion.
• Space complexity: It is the amount of memory space required by an
algorithm, during a course of its execution.
Data
• Data means a value or set of values.
• for example, student’s name and its id are the data about the student.
Structure
• A structure is an arrangement of and relations between parts or
elements.
• It has a form or shape.
Data Structure
• A data structure is an arrangement of data elements.
• Data Structure can be defined as the group of data elements which provides an efficient way of storing and
organizing data in the computer so that it can be used efficiently.
• Examples of Data Structures are arrays, Linked List, Stack, Queue, etc
Data Structure
• Data Structure mainly specifies the following three things
o Organization of Data
o Accessing methods
o Processing alternatives for information
• As applications are getting complexed and amount of data is increasing day by day,
there may arise the following problems:
• Processor speed: To handle very large amount of data, high speed processing is
required.
• Data Search:If our application needs to search for a particular item, it needs to traverse
all the items every time, results in slowing down the search process.
• Multiple requests:If thousands of users are searching the data simultaneously on a
web server, then there are the chances that a very large server can be failed during that
process.
• In order to solve the above problems, data structures are used. Data is organized to
form a data structure in such a way that all items are not required to be searched and
required data can be searched instantly.
Advantages of Data Structures
• Efficiency: Efficiency of a program depends upon the choice of data
structures.
• Reusability: Data structures are reusable, i.e. once we have
implemented a particular data structure, we can use it at any other
place.
• Abstraction: Data structure is specified by the ADT which provides a
level of abstraction.
Classification of Data Structure
• This data structure does not form a sequence i.e. each item or element is connected with two or more
other items in a non-linear arrangement.
• The data elements are not arranged in sequential structure.
Trees
• Trees are multilevel data structures with a hierarchical relationship among its elements known as
nodes.
• The bottommost nodes in the hierarchy are called leaf node while the topmost node is called root node.
• Each node contains pointers to point adjacent nodes.
• Trees are used to represent data that has some hierarchical relationship among the data elements.
• Tree data structure is based on the parent-child relationship among the nodes.
Graphs:
• Graphs can be defined as the pictorial representation of the set of elements (represented by vertices)
connected by the links known as edges.
• A graph is different from tree in the sense that a graph can have cycle while the tree cannot have the one.
• Graph is used to represent data that has relationship between pair of elements not necessarily hierarchical in
nature.
Operations applied on non-linear data structures
1. Add elements
2.Delete elements
3.Display the elements
4.Sort the list of elements
5.Search for a data element
Arrays
• An array is a collection of similar type of data items stored in
consecutive memory location and is referred by common name; each
data item is called an element of the array.
• The data type of the element may be any valid data type like char, int,
float or double.
• The elements of array share the same variable name but each one
carries a different index number known as subscript.
• The array can be one dimensional, two dimensional or
multidimensional.
Syntax
• datatype [] arrayName
• arrayName = new dataType[size of the array]
• datatype [] arrayName = new dataType[size of the array]
• Example
• int [] i;
• i = new int[10];
• int [] i = new int[10];
Initialization – Single Dimension Array
• int [] x = new int[1];
• int [] x = {10}; x 10
• int [] x = { 10, 20 };
x 10 20
Characteristics
(i)Array stores elements of same data type.
(ii)The elements of an array are stored in subsequent memory locations.
(iii)The name of array represents the starting address of the elements.
•
(i)When data objects are stored in array, individual objects are selected by an index.
(ii)By default an array index starts from 0 and ends with (n-1). Index is also referred
as subscripts.
(iii)The size of the array is mentioned at the time of declaring array.
•
(i)While declaring 2D array, number of columns should be specified whereas for
number of rows there is no such rule.
(ii)Size of array can’t be changed at run time.
Array Types
• Single Dimension Array
• Multi Dimension Array
• Double Dimension Array (or) Matrix
• Three Dimension Arrays
Initialization – Double Dimension Array
• int [][] x = new int[1][1];
• Int [][] x = new int[4][3]; x 10
• int [][] x = {10};
• int [][] x = { {10, 20}, {30, 40} };
x 10 20
30 40
Three Dimensional Array
int [][][] A = new int[3][4][3];
1 2 3
Reversing an Array
• Input: A = [1, 2, 3, 4, 5]
• Output: A = [5, 4, 3, 2, 1]
• Algorithm:
• n = length of the Array (A)
• for i = n – 1, j = 0, i >= 0, increment j by 1 and decrement i by
• B[j] = A[i]
• for i = 0 to n
• A[i] = B[i]
Finding the maximum Number in a Set
• Algorithm
• Traverse array elements from second and compare every element with
current max
2 5 3 0 2 3 0 3
1 2 3 4 5 6 7 8
C[A[J]]=C[A[J]]+1
0 1 2 3 4 5
2 0 2 3 0 1
Storage Representation
• An array is a set of homogeneous elements.
• Every element is referred by an index.
• Memory storage locations of the elements are not arranged as a
rectangular array with rows and columns. Instead they are arranged in
a linear sequence beginning with location 1,2,3 and so on.
• The nature of storage depends on how the programming language
stores the 2D array elements.
• The elements are stored either column-by-column (column-major
order) or row-by-row (row-major order).
Example – Row Major Order
Data (A):
• Rows : 3 1 2 3 4
• Columns : 4 5 6 7 8
9 10 11 12
Linear Arrangement of Array A
Row 1 2 3
Index (1,1) (1,2) (1,3) (1,4) (2,1) (2,2) (2,3) (2,4) (3,1) (3,2) (3,3) (3,4)
Memory 100 102 104 106 108 110 112 114 116 118 120 122
Data 1 2 3 4 5 6 7 8 9 10 11 12
Row Major Order
• Location (A [j,k] ) = Base Address of (A) + w [ (N * (j-1)) + (k-1) ]
Memory 100 102 104 106 108 110 112 114 116 118 120 122
Data 1 5 9 2 6 10 3 7 11 4 8 12
Column Major Order
• Location (A [j, k] ) = Base Address of (A) + w [ (M * (k-1)) + (j-1) ]
Memory 100 102 104 106 108 110 112 114 116 118 120 122
Data 1 5 9 2 6 10 3 7 11 4 8 12
• Binary Recursion
• Linear Recursion: the function calls itself repeatedly until it hits the termination
condition (Base condition). Eg.Factorial
• Binary Recursion:This form of recursion has the potential for calling itself
twice instead of once as before. Eg:Binary search
Design Methodology and Implementation of Recursion
• Each call of a recursive algorithm either solves one part of the problem or it reduces the size of
the problem.
• The general part of the solution is the recursive call. At each recursive call, the size of the
problem is reduced.
• The statement that solve the problem is known as the base case.
• Every recursive algorithm must have a base case.
• The rest of the algorithm is known as the general case. The general case contains the logic
needed to reduce the size of the problem.
• Once the base case has been reached, the solution begins.
• We now know one part of the answer and can return that part to the next, more
general statement.
• This allows us to solve the next general case.
• As we solve each general case in turn, we are able to solve the next-higher general case until
we finally solve the most general case, the original problem.
Rules for Designing a Recursive Algorithm
• Combine the base case and the general cases into an algorithm.
• Each recursive call must reduce the size of the problem and move it toward the base
case.
• The base case, when reached, must terminate without a call to the recursive algorithm;
that is, it must execute a return.
FACTORIAL
• RecursiveFactorial (n)
if (N equals 0)
Return 1
else
end if
end recursiveFactorial
Calling a Recursive Factorial Algorithm with n=3
FIBONACCI SERIES
•Fibonacci Series generates subsequent number by adding two previous numbers. Fibonacci series
starts from two numbers ? F0 & F1. The initial values of F0 & F1 can be
• 0 and 1
• 1 and 1 respectively.
Fibo (n)
If n = 0
Return 0
Else
If n = 1
Return 1
Else
Fibo (n) = Fibo (n-1) + Fibo (n-2)
Return Fibo (n)
Int fibo(n) {
n=3 If (n==0) return 0;
If (n==1) return 1;
return fibo(n-1)+fibo(n-2);
}
n=1 n=0
Int fibo(n) { Int fibo(n) {
If (n==0) return 0; If (n==0) return 0;
If (n==1) return 1; If (n==1) return 1;
return fibo(n-1)+fibo(n-2); return fibo(n-1)+fibo(n-2);
} }
1 +1=2
n=1
Int fibo(n) {
If (n==0) return 0;
If (n==1) return 1;
return fibo(n-1)+fibo(n-2);
}
Towers of Hanoi
Disks
Smallest
Largest
Rules
• Only one disk can be moved among the towers at any given time.
A function call is said to be tail recursive if there is nothing to do after the function returns except return
its value.
void print(int n)
{
if (n < 0)
return;
printf(“%d”, n);
print(n-1);
}
ABSTRACT DATA TYPES
• Abstract Data Type (ADT) is a type or class for objects whose behavior is defined by a set of value and a
set of operations.
• The user of data type does not need to know how that data type is implemented
• Three ADTs namely List ADT, Stack ADT, Queue ADT.
List ADT
• The data is generally stored in key sequence in a list which has a head structure consisting of count, pointers
and address of compare function needed to compare the data in the list.
• The data node contains the pointer to a data structure and a self-referential pointer which points to the next
node in the list.
struct node{
*rear;
int (*compare) (void *argument1, void *argument2);
} LIST;
List Functions
Stack ADT
• In Stack ADT Implementation instead of data being stored in each node, the pointer to data is stored.
• The stack head structure also contains a pointer to top and count of number of entries currently in stack.
typedefstruct node{
node *link;
} StackNode;
StackNode *top;
} STACK;
Operations
• All operations take place at a single end that is top of the stack and following operations can
be performed:
•
• push() – Insert an element at one end of the stack called top.
•
• pop() – Remove and return the element at the top of the stack, if it is not empty.
•
• peek() – Return the element at the top of the stack without removing it, if the stack is not empty.
•
• size() – Return the number of elements in the stack.
•
• isEmpty() – Return true if the stack is empty, otherwise return false.
•
• isFull() – Return true if the stack is full, otherwise return false.
Queue ADT
•