0% found this document useful (0 votes)
4 views68 pages

Introduction To Data Structuretosend

The document outlines the syllabus for a Data Structures course (CS306) presented by Dr. Sangeeta Sharma, covering topics such as data types, algorithms, trees, graphs, sorting, and searching techniques. It also discusses the classification of data structures into primitive and non-primitive types, as well as abstract data types and memory allocation methods. Additionally, it provides insights into algorithm properties and efficiency, including time and space complexity.

Uploaded by

mutkarsh674
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views68 pages

Introduction To Data Structuretosend

The document outlines the syllabus for a Data Structures course (CS306) presented by Dr. Sangeeta Sharma, covering topics such as data types, algorithms, trees, graphs, sorting, and searching techniques. It also discusses the classification of data structures into primitive and non-primitive types, as well as abstract data types and memory allocation methods. Additionally, it provides insights into algorithm properties and efficiency, including time and space complexity.

Uploaded by

mutkarsh674
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 68

Introduction

to
Data Structures (CS306)
Presented By:
Dr. Sangeeta Sharma

1
Syllabus
■ Unit-I: Introduction: Data types, data structures, abstract
data types, the running time of a program, the running time
and storage cost of algorithms, complexity, asymptotic
complexity, big O notation, obtaining the complexity of an
algorithm.

2
Syllabus
■ Unit-II: Development of Algorithms: Notations and
Analysis, Storage structures for arrays – sparse matrices -
structures and arrays of structures, Stacks and Queues:
Representations, implementations and applications. Linked
Lists: Singly linked lists, Linked stacks and queues,
operations on Polynomials, Doubly Linked Lists,
Circularly Linked Lists, Operations on linked lists-
Insertion, deletion and traversal, dynamic storage
management – Garbage collection and compaction.

3
Syllabus

■ Unit-III: Trees: Basic terminology, General Trees, Binary


Trees, Tree Traversing: in-order, pre-order and post-order
traversal, building a binary search tree, Operations on Binary
Trees - Expression Manipulations - Symbol Table
construction, Height Balanced Trees(AVL), B-trees, B+-trees.

4
Syllabus

■ Unit-IV: Graphs: Basic definitions, representations of


directed and undirected graphs, the single source shortest
path problem, the all-pair shortest path problem, traversals
of directed and undirected graphs, directed acyclic graphs,
strong components, minimum cost spanning tress,
articulation points and bi-connected components, graph
matching.

5
Syllabus

■ Unit-V: Sorting and Searching Techniques: Bubble sorting,


Insertion sort, Selection sort, Shell sort, Merge sort, Heap
and Heap sort, Quick sort, Radix sort and Bucket sort,
Address calculation, Sequential searching, Binary
Searching, Index searching, Hash table methods.

6
Books and References
■ Data Structures with C (Schaum's Outline
Series) Seymour Lipschutz
■ Data Structures and Algorithms Made Easy:
Data Structures and Algorithmic Puzzles
Narasimha Karumanchi
■ Data Structure Using C E. Balagurusamy
■ Data structures, Algorithms ad Applications in
C++ by Sartaj Sahni, WCB/McGraw Hill.

7
Data Structure
■ Data (in a raw form)
■ Structure (organized manner)
■ Information: Concise form of data
■ Knowledge: Conclusion from information.
■ Data structure is a well organized manner of
keeping data.
■ It saves searching time and reduces the
space also.

8
Definition
■ Data structure is representation of the logical
relationship existing between individual
elements of data.
■ In other words, a data structure is a way of
organizing all data items that considers not
only the elements stored but also their
relationship to each other.

9
Introduction
■ Data structure affects the design of both
structural & functional aspects of a program.
Program = Algorithm + Data Structure
■ You know that a algorithm is a step by step
procedure to solve a particular function.

10
Introduction
■ That means, algorithm is a set of instruction
written to carry out certain tasks & the data
structure is the way of organizing the data
with their logical relationship retained.
■ To develop a program of an algorithm, we
should select an appropriate data structure
for that algorithm.
■ Therefore algorithm and its associated data
structures form a program.
11
Classification of Data Structure
■ Data structure are normally divided into two
broad categories:
■ Primitive Data Structure

■ Non-Primitive Data Structure

12
Classification of Data Structure
Data structure

Non-Primitive
Primitive DS
DS

Intege Characte
Float Pointer
r r

13
Classification of Data Structure
Non-Primitive
DS

Linear Non-Linear
List List

Arra Queu Grap


Trees
y e h
Link
Stack
List
14
Primitive Data Structure
■ These are basic structures and directly
operated upon by the machine instructions.
■ In general, there are different representation
on different computers.
■ Integer, Floating-point number, Character
constants, string constants, pointers etc., fall
in this category.

15
Non-Primitive Data Structure
■ These are more sophisticated data
structures.
■ These are derived from the primitive data
structures.
■ The non-primitive data structures emphasize
on structuring of a group of homogeneous
(same type) or heterogeneous (different
type) data items.
16
Non-Primitive Data Structure
■ Lists, Stack, Queue, Tree, Graph are
example of non-primitive data structures.
■ The design of an efficient data structure
must take operations to be performed on the
data structure.

17
Non-Primitive Data Structure
■ The most commonly used operation on data
structure are broadly categorized into
following types:
■ Create
■ Insert
■ Updating
■ Searching
■ Sorting
■ Merging
■ Destroy or Delete 18
Different between them
■ A primitive data structure is generally a
basic structure that is usually built into the
language, such as an integer, a float.
■ A non-primitive data structure is built out
of primitive data structures linked together
in meaningful ways, such as linked-list,
binary search tree, AVL Tree, graph etc.

19
Abstract Data Type (ADT)
■ 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.
■ ADT only mentions what operations are
to be performed but not how these
operations will be implemented.

20
Abstract Data Type (ADT)
■ It does not specify how data will be organized in
memory and what algorithms will be used for
implementing the operations.
■ It is called abstract because it gives a
implementation independent i.e. The process of
providing only the essentials and hiding the details
is known as abstraction.
Example: Stack, Queue etc.

21
Implementation aspects: Memory
representation
There are 2 types of memory allocations
possible:
■ Compile time or static allocation
■ Runtime or Dynamic allocation

22
Implementation aspects: Memory
representation
Compile time or static allocation:
■ Compile time memory allocation means reserving memory
for variables, constants during the compilation process
■ This type of allocation is done with the help of Array.
■ The biggest disadvantage of compile time memory
allocation, we do not have control on allocated memory.
You cannot increase, decrease or free memory, the
compiler takes care of memory management.
■ We can also refer compile time memory allocation as
static.

23
Implementation aspects: Memory
representation
Runtime or Dynamic allocation:
■ Memory allocated at runtime either through malloc(),

calloc() or realloc().
■ You can also refer runtime memory allocation as dynamic

or heap memory allocation.


■ Professional programmers prefer dynamic memory
allocation more over static memory allocation. Since, we
have full control over the allocated memory. Which means
we can allocate, de-allocate and can also reallocate
memory when needed.

24
Algorithm
An algorithm is a finite sequence of
instructions, each of which has a clear
meaning and can be performed with a
finite amount of effort in a finite length of
time.
No matter what the input values may be, an
algorithm terminates after executing a finite
number of instructions.
25
Algorithm Property
In addition every algorithm must satisfy the
following criteria:
■ Input: there are zero or more quantities,
which are externally supplied;
■ Output: at least one quantity is produced;
■ Definiteness: each instruction must be clear
and unambiguous;

26
Algorithm Property
■ Finiteness: if we trace out the instructions
of an algorithm, then for all cases the
algorithm will terminate after a finite
number of steps;
■ Effectiveness: every instruction must be
sufficiently. It is not enough that each
operation be definite, but it must also be
feasible.
27
Algorithm Property
■ We represent an algorithm using pseudo
language that is a combination of the
constructs of a programming language
together with informal English statements.
■ The basic difference between algorithm and
program is that algorithm must be free from
syntax but program must follow the syntax
(language dependent).
28
Algorithm Efficiency
■ Choosing an efficient algorithm or data
structure is just one part of the design
process.
■ There are two basic design goals that
we should strive for in an algorithm:
1. Try to save time (Time complexity).
2. Try to save space (Space complexity).

29
Space Complexity

The space complexity of a program is


the amount of memory it needs to run
to completion.

30
Time Complexity

31
Time Complexity

32
Time Complexity

33
Classification of Data Structure
■ Linear : Elements form a sequence (linear
list)
The linear relationship between the elements
represented by means of
(a) sequential memory location Eg: Arrays

(b) Pointers or links Eg: Linked Lists

■ Nonlinear: Trees and Graphs

34
Arrays

• Simply, declaration of array is as follows:


int arr[10]
 Where int specifies the data type or type of
elements arrays stores.
 “arr” is the name of array & the number
specified inside the square brackets is the
number of elements an array can store, this is
also called sized or length of array.
Represent a Linear Array in
memory

 The elements of linear array are


stored in consecutive memory
locations.
Arrays

◦ The elements of array will always be stored in the


consecutive (continues) memory location.
Size of the array = (Upperbound-lowerbound)+1
◦ For the above array it would be (9-0)+1=10,where 0
is the lower bound of array and 9 is the upper bound
of array.
◦ Array can always be read or written through loop.
For(i=0;i<=9;i++)
{ scanf(“%d”,&arr[i]);
printf(“%d”,arr[i]); }
Arrays
types

 Single Dimension Array


◦ Array with one subscript
 Two Dimension Array
◦ Array with two subscripts (Rows and
Column)
 Multi Dimension Array
◦ Array with Multiple subscripts
Memory representation of the one
dimensional array:-

a[0] a[1] a[2] a[3] a[4] a[5]

2 4 6 7 5 8

100 102 104 106 108 110



The memory blocks a[0],a[1],a[2],a[3 ],a[4] ,
a[5] with base addresses 100,102,104,106,108,
110 store the values 2,4,6,7,5,8 respectively.
• We can track the memory location of any
element of the linear array by using the
base address of the array.
• To calculate the memory location of an
element in an array by using formulae.
Loc(a[k])=base_address+w(k-lower
bound)
• Here k specifies the element whose
location to find.
• W means word length.
• Ex: We can find the location of the
element 5, present at a[3],base address is
100, then
loc(a[3])=100+2(3-0)
=100+6
=106.
Basic operations of Arrays

 Some common operation performed on array are:


◦ Traversing: Processing each element in a list.
◦ Searching: Finding the location of the element
with a given value with a given key.
◦ Insertion: Adding a new element to the list.
◦ Deletion: Removing an element from the list.
◦ Sorting: Arranging the elements in some type of
order.
◦ Merging: Combining two lists into a single list.
Traversing
Arrays
 Traversing: It is used to access each data item exactly
once so
that it can be processed.
E.g.
We
 1 have2 linear 3array A 4
as below:
5
 10 20 30 40 50

Here we will start from beginning and will go till last


element and during this process we will access value of
each element exactly once as below:

A [1] = 10
A [2] = 20
A [3] = 30
A [4] = 40
A [5] = 50
Array
 Insertion: It is used to add a new data item in the given
collection of data items.
E.g.We have linear array A as below:

1 2 3 4 5
10 20 50 30 15

New element to be inserted is 100 and location for insertion is 3.


So shift the elements from 5th location to 3rd location downwards
by 1 place.And then insert 100 at 3rd location. It is shown below:
Deletion from
Array
 Deletion: It is used to delete an existing data item from
the given
collection of data items.
Searching in
Arrays
 Searching: It is used to find out the location of the data item if it exists
in the given
collection of data items.
E.g. We have
1 linear2array A as3below: 4 5
15 50 35 20 25

Suppose item to be searched is 20.We will start from beginning and will compare 20
with each element. This process will continue until element is found or array is
finished. Here:
1)Compare 20 with 15
20 # 15, go to next element.
2)Compare 20 with 50
20 # 50, go to next element.

3)Compare 20 with 35
20 #35, go to next element.

4)Compare 20 with 20
20 = 20, so 20 is found and its location is 4.
Sortin
g
Two dimensional array
A two dimensional array is a collection of elements and each
element is identified by a pair of subscripts. ( A[3] [3] )
 The elements are stored in continuous memory locations.
 The elements of two-dimensional array as rows and
columns.
 The number of rows and columns in a matrix is called as
the order of the matrix and denoted as m x n.
 The number of elements can be obtained by multiplying
number of rows and number of columns. A[0] A[1] A[2]
A[0] 10 20 30
A[1] 40 50 60
A[2] 70 80 90
Representation of Two
Dimensional Array:

 A is the array of order m x n.


 To store m*n number of elements, we need m*n
memory locations.
 The elements should be in contiguous memory
locations.
 There are two methods:
 o Row-major method
o Column-major method
Two Dimensional Array:
 Row-Major Method: All the first-row elements are stored in
sequential memory locations and then all the second-row
elements are stored and so on. Ex: A[Row][Col]
 Column-Major Method: All the first column elements are
stored in sequential memory locations and then all the second-
column elements are stored and so on. Ex: A [Col][Row]
1000 10 A[0][0] 1000 10 A[0][0]
1002 20 A[0][1] 1002 40 A[1][0]
1004 30 A[0][2] 1004 70 A[2][0]
1006 40 A[1][0] 1006 20 A[0][1]
1008 50 A[1][1] 1008 50 A[1][1]
1010 60 A[1][2] 1010 80 A[2][1]
1012 70 A[2][0] 1012 30 A[0][2]
1014 80 A[2][1] 1014 60 A[1][2]

1016 90 A[2][2] 1016 90 A[2][2]

Row-Major Method Col-Major Method


• Syntax to declare the two dimensional
array is as fallows
 Syntax:
<data type> <array name> [row size]
[column size];

• Syntax to initialize the two dimensional


array is as fallows
 Syntax:
<data type> <array name> [row size]
[column size]={values};

Example:
int num[3][2]={4,3,5,6,,8,9};
or
int num[3][2]={{4,3},{5,6},{8,9}};

values column size row size array name


data type
Representation of the 2-D
array:-
Rows columns
0th column 1st column

0th row a[0][0] a[0][1]

1st row a[1][0] a[1][1]

2nd a[2][0] a[2][1]


row
 Row major arrangement:
0th row 1st row 2nd row 4 3 5
6 8 9
502 504 506 508 510 512
 Column major arrangement:
0th column 1st column
45 8 3 6 9
502 504 506 508 510 512
• We can access any element of the array
once we know the base address of the array
and number of row and columns present in
the array.
• In general for an array a[m][n] the address
of element a[i][j] would be,
• In row major arrangement
Base address+w(i*n+j)
• In column major arrangement
Base adress+w(j*m+i)
 Ex:
we can find the location of the element 8 then
an array a[3][2] , the address of element
would be a[2][0] would be
• In row major arrangement
loc(a[2][0])=502+2(2*2+0)
=502+8
=510
• In column major arrangement
loc(a[2][0])=502+2(0*3+2)
=502+4
=506
Multi dimensional arrays:-

 An array haves 2 or more subscripts, that


type of array is called multi dimensional
array.
 The 3 –D array is called as
multidimensional array this can be thought
of as an array of two dimensional arrays.
 Each element of a 3-D array is accessed
using subscripts, one for each dimension.
Advantages of Array:

 It is used to represent multiple data items of same type


by using single name.
 It can be used to implement other data structures like
stacks, queues, tree, graphs etc.
 Two-dimensional arrays are used to represent matrices.
 Many databases include one-dimensional arrays whose
elements are records.
Disadvantages of Array

 We must know in advance the how many elements are


to be stored in array.
 Array is static structure. It means that array is of fixed
size. The memory which is allocated to array cannot be
increased or decreased.
 Array is fixed size; if we allocate more memory than
requirement then the memory space will be wasted.
 The elements of array are stored in consecutive
memory locations. So insertion and deletion are very
difficult and time consuming.
Sparse Matrices

sparse … many elements are zero


dense … few elements are zero

60
What is Sparse Matrix?
In computer programming, a matrix can be defined with a
2-dimensional array. Any array with 'm' columns and 'n' rows
represent a m X n matrix. There may be a situation in which a
matrix contains more number of ZERO values than
NON-ZERO values. Such matrix is known as sparse matrix.
Sparse matrix is a matrix which contains very few
non-zero elements.
When a sparse matrix is represented with a 2-dimensional
array, we waste a lot of space to represent that matrix. For
example, consider a matrix of size 100 X 100 containing only
10 non-zero elements. In this matrix, only 10 spaces are filled
with non-zero values and remaining spaces of the matrix are
filled with zero. That means, totally we allocate 100 X 100 X 2
= 20000 bytes of space to store this integer matrix. And to
access these 10 non-zero elements we have to make scanning
for 10000 times. To make it simple we use the following sparse
matrix representation. 61
Sparse Matrix Representations
A sparse matrix can be represented by using
TWO representations, those are as follows...
1.Triplet Representation (Array Representation)
2.Linked Representation
Triplet Representation (Array
Representation)
In this representation, we consider only
non-zero values along with their row and column
index values. In this representation, the 0th row
stores the total number of rows, total number of
columns and the total number of non-zero
values in the sparse matrix. 62
For example, consider a matrix of size 5 X 6
containing 6 number of non-zero values. This matrix
can be represented as shown in the image...

63
In above example matrix, there are only 6 non-zero
elements ( those are 9, 8, 4, 2, 5 & 2) and matrix size
is 5 X 6. We represent this matrix as shown in the
above image. Here the first row in the right side table
is filled with values 5, 6 & 6 which indicates that it is a
sparse matrix with 5 rows, 6 columns & 6 non-zero
values. The second row is filled with 0, 4, & 9 which
indicates the non-zero value 9 is at the 0th-row 4th
column in the Sparse matrix. In the same way, the
remaining non-zero values also follow a similar
pattern. 64
Example:

65
Negative Compression:
(k+1) x 3 > n x m

Where,
k = no. of non-zero element

66
Time Complexity will be O(k)

Where: k = no. of non-zero elements

67
Query???

68

You might also like