0% found this document useful (0 votes)
32 views

DS Unit - 1

Uploaded by

kamblisoham28
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)
32 views

DS Unit - 1

Uploaded by

kamblisoham28
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/ 47

Introduction to

Data Structures
3. 4. Operations
Types of Data
Structures Linear and
on Data
Nonlinear Structures

Module 1
Introduction to
Data Structures
1. Introduction
to Data 2. Concept
Structures
of ADT
What is efficient program

Is easy to
Runs read and
Is easy to Is easy to Runs
correctly understand debug modify Fast
Data Structure=Organized data +Allowed operations

Data structures represents the


organization of data in
computer memory on which
operations like insert, delete,
1
update can be performed.

Data Data structure is basically a


group of data elements

Structures included under one name,


e.g. the student's data,
2
employees data, etc.

The data structures defines a


particular way of storing and
organizing data so it can be 3
used efficiently.
Why DS is important ?
Analysis of a problem to determine the basic operations that must
be supported. e.g inserting/deleting/searching.

A programmer having a poor knowledge of Data Structures


does not understand the importance and hence puts any data
structure which is easier.
Computer programs not just to solve problem but also efficient.

Select the data structure that best meets the requirements.

It helps to store data in logical manner.


1. Stores huge 4. Provides
data various
structures

Need of
2. Stores data in data 5. Static and
systematic way structures dynamic
formats

3. Retains logical 6. Better


relationship algorithms
Some important terms

Data Record File


Means a value or a Is a collection of A collection of
set of values data items related records
Data Structures (DS)

Primitive DS Non-Primitive DS

Integer
Character
Linear DS Non-Linear DS
Boolean
Integer trees
Stack Graphs
Queue Sets
Linked list Tables
❑ Primitive and Non-Primitive Data Type

Primitive Data structures are directly supported by the language;


any operation is directly performed in these data items.

Primitive data types are stored in stack memory.

Non-primitive data types are not defined by the programming


language, but are instead created by the programmer.

Non-Primitive data type are stored in heap memory.


Linear data structure Non linear data structure
Every item is related to its previous Every item is attached with many other
and next item items

Data is arranged in linear sequence Data is not arranged in sequence

Data items can be traversed in a


single run Data cannot be traversed in a single run

Implementation is easy Implementation is difficult

Ex : Array , Stack Ex : Tree , Graph


Operations on Data Structures
Searching-
find location of one or more data items
Traversing- that satisfies the given constraint. E.g.
access each data item exactly once. find all students who secured 100
E.g. Print the values of all the marks in Maths
elements in the array.
Deleting-
Inserting-
remove the data item from the given
add new data item in the set of data items.
given list of data items.
Merging-
Sorting-
combine two data sets.
arrange data items in specific
order- ascending or descending.
Operating Compiler
Systems Construction

Applications
of DS
Solve real
DBMS world problems
Efficiency of Programs
The programs are written to solve

01 problems, like searching a data


item, sorting data items, updating
data, etc.

02
The programs efficiency is
based on the execution time
and memory space utilization.

The efficiency of the program depends

03
on the data structure used to represent
data on which operations like search,
sort, insert, etc are performed.
❑ Types of Data Structures and its operations

Data structures and


operations

Linear Data Structures Non-linear Data Operations


Structures

Array Linked list Stack Queue Graph Tree

Insert Delete Traverse Search Sort


An array is a collection of data
elements of same data types.

The data elements are stored in


Linear DS- Array consecutive memory locations,
i.e. one after another

The elements can be accessed


by an index.
❑ Linear DS- Array
Example-
int marks[10]={10,20,30,45,60,70,88,99,33,11};
marks[0]=10, marks[1]=20…

Index 0 1 2 3 4 5 6 … 9
Marks[ 10 20 30 45 60 70 88 11
index]

Benefits?
Linked list is dynamic data structure in which
elements can be inserted and deleted anywhere
without shifting. In contract to fixed size in arrays,
linked list can allocate memory to data elements
during run time also.

Linear DS-Linked List

A linked list consists of nodes that are dynamically


created by allotting memory during run time.
❑ Linked list
▪ The node has two parts: data and pointer to next node. The nodes
are linked to each other as shown in the figure.

12 56 89 29

Advantages
Disadvantages
❑ Linear DS- Stack
• A stack is an ordered collection of data elements into which
1 elements can be inserted or deleted only from one end, called the
top of the stack as shown in figure.
• Known as Last in First out(LIFO) Data structure.
2

58 Top of stack
98
16

21
❑ Examples on Stack

22
❑ Linear DS-Queue

In contrast to stack, queue is opened at both end.


One end is always used to insert data
(front-enqueue) and the other is used to remove
data (rear-dequeue).

Queue follows First-In-First-Out


methodology(FIFO), i.e., the data item stored
first will be accessed first as shown in figure.
❑ Examples on Queue

24
A binary tree is another commonly used Nonlinear DS- Trees
data structure. It is organized like an
upside-down tree

A node, holds an item of data along


with a left pointer and a right pointer.

Useful for efficient search, e.g telephone


directory
Nonlinear DS- Use of Trees

01 A binary search tree is a good data structure to


use for searching sorted data.

02
The middle item from the list is stored in the root node,
with lesser items to the left and greater items to the
right.

03
A search begins at the root. The computer either find the
data, or moves left or right, depending on the value for
which you are searching

04 Each move down the tree cuts the remaining data


in half.
Nonlinear DS- Use of Trees

05 Items can be located very quickly in a tree.

06
Telephone directory assistance information is
stored in a tree, so that a name and phone number
can be found quickly.
Nonlinear DS- Graphs
Graph is a collection of vertices(nodes)
and edges that connect these vertices.

Unlike trees, in graph the nodes may


not be connected in hierarchy.

Used to represent a complex


relationships between components.

City map can be represented using


graph, where nodes represent cities and
edges represent road connectivity.
❑ Arrays
1. Write a program to perform following operations using arrays
• Traversal- print the name, age and marks of students.
• Search- search the students who secured 100 marks

• 2. Create an array of size 10. Write functions to -


• Insert a new data element in to array at a given position by shifting the
existing elements.
• Delete an existing element
• Sort the array elements
• Merge two arrays.

29
❑ Abstract Data Type
• Solving a problem involves processing data, this requires that we identify-

1. the collection of data items and


2. basic operations that must be performed on them.

• Such a collection of data items together with the operations on the data is
called an abstract data type (ADT).

• The word abstract refers to the fact that data and operations are defined
independent of its implementation.

• We just specify what can be done with the data, and not how it is done.
30
❑ Example of ADT
• Problem- Because of new government regulation, the university has to
keep records of all students currently receiving financial aid and submit
regular reports to government. Design the program for the same.

Data: Financial aid awards: string source, int amount


Operations:
Get_amount(): Access and return the amount stored
Display(): display on screen source and amount
Update_amount(): modify the amount

Data: Student_data: int id, string name, list of financial aid awarded <
source, amount>
Operation:
• Get_record(),Update_amount()
31
❑ Implementation of ADT
• An implementation of the ADT consists of storage structures to store
1 the data items and algorithms for the basic operations.

2 • For example:
We use structure in C to store student financial records and
algorithms are written for basic operations.

32
❑ Advantage of ADT
• Data abstraction: The idea of separating the definition of data
1 definition from implementation enables software designers to study
data types without being concerned about the details of its
implementation.
2 • Reuse: Programmers may reuse these data types and its operation
without worrying about its implementation.
• To manage complexity: To manage the complexity of problems and
3 the problem-solving process, computer scientists use abstractions to
allow them to focus on the “big picture” without getting lost in the
details.

33
❑ Reflection
1. Abstract data type represents –
1 A. Set of data types and operations defined on that data type.
B. The data types and operations implemented using
programming language
C. The data types and its values only

22. We can have several implementations of the same ADT


A. True
B. False

34
Software design –
Problems are often complex and ill-defined. Such
problem solving approaches
complex problems are solved by breaking the
problem into subproblems also known as
modularization.

The key advantages of modularization are


as follows:
▪It makes the complex algorithm simpler to
design and implement.
▪Each module can be designed independently.
❑ Problem analysis: Modularization

36
❑ Modularization

Outcome: ADT(Data and operations) are defined

37
❑ Solution design

•Once ADT is defined, next step is to identify storage


1 mechanisms and algorithms.

•Select the appropriate data structures and algorithms.


2

38
CARATERISTICS OF AN ALGORITHM

OUTPUT EFFECTIVENESS

INPUT DIFINITENESS FINITENESS


STEP IDENTIFYING STEP Identifying the

01 INPUTS
03 sequence of steps
to be processed

DEFINE STEP IDENTIFYING STEP


THE PROBLEM
02 OUTPUTS 04
Top down approach Bottom up approach
Breaking down a program into small The piercing together of small modules
modules when program is complex

Languages like C follows top down Languages such as C++ and JAVA follows
approach bottom up approach

Usually begins with high level design Usually begin with low level degin or
and ends with low level design or development and ends with high level
development design

Main function is written at the Main function is written at the end and
beginning and remaining functions remaining function/classes are defined
are defined below main above main
02
AVERAGE CASE
01 03
WORST CASE BEST CASE

CASE TO
ANALYSE
ALGORITHM
❑ Algorithm complexity

• Complexity is defined as the running time required to execute a


1 process and it depends on space as well as time

• There are two types of complexity


2 • Space complexity
• Time complexity

43
❑ Big-O ‘O’ notation

• Big O Notation in Data Structure is used to express algorithmic


1 complexity using algebraic terms.

• It describes the upper bound of an algorithm's runtime and


2 calculates the time and amount of memory needed to execute
the algorithm for an input value.

44
❑ Big-O ‘O’ notation

• LIMITATIONS
1
• ITS COMPLEXITY OF AN ALGORITHM ONLY CONSIDERS LARGE INPUT
• IT GIVES ONLY WORST CASE BOUND
• IT IGNORED CONSTANTS WHETHER IT IS CONSTANT OVERHEAD

45
ORDERS OF GROWTH

O(N) N
O(2 )

01 02 03 04
2
O(1) O(N)
THANKYOU

You might also like