Data Structures Intro Slides
Data Structures Intro Slides
(CSN12101)
Prerequisite: C programming and Basic of Mathematics
L-T-P: 2-0-2, Credits: 3
Type: Engineering Essential/ Core Essential
Dr Deepak Gupta
Assistant Professor, SMIEEE
CSED, MNNIT Allahabad, Prayagraj
Email: [email protected]
Course Description
This course introduces the student’s fundamentals of data structures and takes
them forward to software design along with the course on Algorithms. It details
how the choice of data structures impacts the performance of programs for given
software application. This is a precursor to DBMS and Operating Systems. A lab
course is associated with it to strengthen the concepts.
Syllabus
UNIT 1: Introduction: Basic Terminology, Elementary Data Organization, Algorithm,
Efficiency of an Algorithm, Time and Space Complexity, Asymptotic notations: Theta,
Big-O, and Omega, Time-Space trade-off. Abstract Data Types (ADT)
UNIT II: Arrays: Definition, Single and Multidimensional Arrays, Representation of
Arrays: Row Major Order, and Column Major Order, Application of arrays, Sparse
Matrices and their representations.
Linked Lists: Array Implementation and Dynamic Implementation of Singly Linked
Lists, Doubly Linked List, Circularly Linked List, Operations on a Linked List.
Insertion, Deletion, Traversal, Polynomial Representation and Addition, Generalized
Linked List
Stacks: Abstract Data Type, Primitive Stack operations: Push & Pop, Array and Linked
Implementation of Stack in C, Application of stack: Prefix and Postfix Expressions,
Evaluation of postfix expression, Recursion, Tower of Hanoi Problem, Simulating
Recursion, Principles of recursion, Tail recursion, Removal of recursion
Queues: Abstract Data Type, Operations on Queue: Create, Add, Delete, Full and
Empty, Circular queues, Array and linked implementation of queues in C, Deque and
Priority Queue.
Syllabus
UNIT III: Basic terminology, k-ary trees, Binary Trees, Binary Tree Representation: Array
Representation and Dynamic Representation, Complete Binary Tree, Algebraic Expressions,
Extended Binary Trees, Array and Linked Representation of Binary trees, Tree Traversal
algorithms: In order, Preorder and Post order, Binary Search Trees, Threaded Binary trees,
Traversing Threaded Binary trees, Forest, Huffman algorithm, Heap, B/B+ Tree, AVL tree
UNIT IV: Sequential search, Binary Search, Comparison and Analysis Internal Sorting:
Bubble Sort, Selection Sort, Insertion Sort, Two Way Merge Sort, Heap Sort, Quick Sort,
Hashing
Unit V: Terminology, Sequential and linked Representations of Graphs: Adjacency Matrices,
Adjacency List, Adjacency Multi list, Graph Traversal: Depth First Search and Breadth First
Search, Connected Component, Spanning Trees, Minimum Cost Spanning Trees: Prims and
Kruskal algorithm. Shortest Path algorithm: Dijikstra Algorithm
Books
Text Books:
Reference Books:
1. Horowitz and Sahani, “Fundamentals of Data Structures”, Galgotia
Publication
2. Donald Knuth, “The Art of Computer Programming”, vol. 1 and vol. 3.
3. Jean Paul Trembley and Paul G. Sorenson, “An Introduction to Data
Structures with applications”, McGraw Hill
4. R. Kruse et al, “Data Structures and Program Design in C”, Pearson
Education
5. Lipschutz, “Data Structures” Schaum’s Outline Series, TM
Time Table
9:00-10:00 10:00-11:00 11:00-12:00 12:00-1:00 1:00-2:00 2:00-3:00 3:00-4:00 4:00-5:00
CSN12101(L)D
MON
GS5
CSN12101(L)C CSN12101(L)B
FRI
GS7 GS6
Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India
Introduction
Data: Data is the plural of “datum” (which is rarely used) and may be thought
of as representing numbers, words, facts, figures, images, etc.
Data Type: A data type is a collection of objects and a set of operations that
act on those objects.
For example in C, int data type can take values in a range and operations that
can be performed are addition, subtraction, multiplication, division, bitwise
operations etc.
Data type or Primitive Data types are the predefined data types that are
supported in the programming language. The size depends upon the type of
data type. Primitive Data types can hold only a single value in one specific
location.
int marks[10];
marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7] marks[8] marks[9]
Linked Lists
A Linked List is another example of a linear data structure used to store a
collection of data elements dynamically. Data elements in a linked list are
represented by the nodes, connected using links or pointers.
Queue
It follows the First-In-First-Out (FIFO) principle. Elements are inserted at the
back of the queue and removed from the front.
Non-Linear Data Structures
Non-Linear Data Structures are data structures where the data elements are
not arranged in sequential order. Here, the insertion and removal of data are
not feasible in a linear manner. There exists a hierarchical relationship
between the individual data items.
G = (V, E)
Algorithm: An algorithm is a finite set of instructions that, if followed,
accomplishes a particular task.
In addition, all algorithms must satisfy the following criteria:
1. Input: There are zero or more quantities that are externally supplied.
The fixed part includes space needed for storing instructions, constants,
variables, and structured variables. It does not depend on the size of
program’s inputs and outputs.
float abc(float a, float b, float c)
{ Sabc(I) = 0.
return a+b+b*c+(a+b-c)/(a+b)+4
}
Variable part consists of the space needed by structured variables whose
size depends on the particular instance (I), of the problem being solved.
It also includes space needed for the recursion stack, and for structured
variables that are allocated space dynamically during the run-time of the
program.
Type Name Number of bytes
Ex: Recursive function for summing a list
Parameter: array pointer list[] 4
of numbers
float rsum(float list[], int n) Parameter: integer n 4
{
return address 4
if (n) return rsum(list, n-1)+list(n-1);
return 0; Total per recursive call 12
}
Srsum(Max_size) = 12*Max_size.
Ex: Iterative function for summing a list of numbers
float sum(float list[], int n)
{
float tempsum = 0;
int i; Sabc(I) = 0.
for(i=0; i<n; i++)
tempsum += list[i];
return tempsum
} Time Complexity
Ex: Recursive function for summing a list of numbers
float rsum(float list[], int n)
{
if (n)
return rsum(list, n-1)+list(n-1);
return 0;
}
Time Complexity
Ex: Matrix Addition
void add (int a[ ][MAX_SIZE], int b[ ][MAX_SIZE], int c[ ][MAX_SIZE], int rows, int cols)
{
int i, j;
for (i=0; i<rows; i++)
for (j=0; j<cols; j++)
c[i][j] = a[i][j] + b[i][j];
}
Categories of Algorithms
1 1 1 1 1 1 1 2
2 1 1 2 2 4 8 4
4 1 2 4 8 16 64 8
8 1 3 8 24 64 512 256
data_type name[size];
data_type - what kind of values it can store. For example, int, char, float
name - to identify the array
size - the maximum number of values that the array can hold
int marks[10];
marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7] marks[8] marks[9]
int i, marks[10];
for(i=0;i<10;i++)
printf(“%d ”,marks[i]);
For example:
int marks[5]={99,78,55,23,91};
int marks[ ]={99,78,55,23,91};
int marks[5]={99,78};
Here, the size of the array is 5 while there are only 2 initializers. The value of
the elements are:
marks[0]=99 marks[1]=78 marks[2]=0 marks[3]=0 marks[4]=0
We will have to copy all elements of the array one by one by using a for
loop:
for(i=0;i<5;i++)
b[i]=marks[i];
(0,0) (0, 1) (0,2) (0,3) (1,0) (1,1) (1,2) (1,3) (2,0) (2,1) (2,2) (2,3)
In the column-major order, the elements of the first column are stored
before the elements of the second and third columns. That is, the elements
of the array are stored column by column where n elements of the first
column will occupy the first nth locations.
(0,0) (1,0) (2,0) (3,0) (0,1) (1,1) (2,1) (3,1) (0,2) (1,2) (2,2) (3,2)
Initializing Two-dimensional Arrays
A two-dimensional array is initialized in the same way as a 1-D array is
initialized.
For example,
int *ptr;
ptr = &arr[0];
If pointer variable ptr holds the address of the first element in the array, then
the address of the successive elements can be calculated by writing ptr++.
int arr[2][2][2]={1,2,3,4,5,6,7,8};
int (*parr)[2][2];
parr=arr;
arr[i][j][k]= *(*(*(arr+i)+j)+k)