0% found this document useful (0 votes)
14 views70 pages

DS-I - Introduction To Data Structure - 24 Oct 2018

The document provides an introduction to data structures and algorithms, covering fundamental concepts such as data types, abstract data types (ADT), and various data structures including linear and non-linear types. It also discusses algorithm design tools like pseudocode and flowcharts, as well as the analysis of algorithms in terms of time and space complexity. Key topics include the definitions of data objects, the classification of data types, and the importance of algorithm efficiency.

Uploaded by

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

DS-I - Introduction To Data Structure - 24 Oct 2018

The document provides an introduction to data structures and algorithms, covering fundamental concepts such as data types, abstract data types (ADT), and various data structures including linear and non-linear types. It also discusses algorithm design tools like pseudocode and flowcharts, as well as the analysis of algorithms in terms of time and space complexity. Key topics include the definitions of data objects, the classification of data types, and the importance of algorithm efficiency.

Uploaded by

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

Introduction to Data Structures

 Data, Data objects, Data Types

 Abstract Data types (ADT) and Data Structure

 Types of data structure

 Introduction to Algorithms

 Algorithm Design Tools: Pseudo code and flowchart

 Analysis of Algorithms- Space complexity, Time complexity, Asymptotic notations

DATA STRUCTURE -I UNIT-I 1


Data, Data Objects and Data Types
Computer Science is study of data

1) Machines that hold data

2) Languages for describing data manipulations

3) Foundations which describe what kinds of refined data can be produced


from raw data

4) Structures of refining data

DATA STRUCTURE -I UNIT-I 2


Data, Data Objects and Data Types
Data is of two types

 Atomic Data
It consist of single piece of information. It cannot be divided into other meaningful
pieces of data. e.g Name of Person, Name of Book

Composite Data
It can be divided into subfields that have meaning.
e.g. Address, Telephone number

DATA STRUCTURE -I UNIT-I 3


Data, Data Objects and Data Types
Data Objects Roll_ Number

Data object is referring to set of elements say D.


For Example: Data Object integers refers to D={0,±1,±2, Name
…………………}

Percentage
Data Object represents an object having a data.
For Example:
If student is one object then it will consist of different data like roll no,
name, percentage , address etc.

DATA STRUCTURE -I UNIT-I


4
Data, Data Objects and Data Types
Data Types
A Data type is a term which refers to the kinds of data that variables may hold in a
programming languages.
For Example: In C programming languages, the data types are integer, float, character etc.
Data type is a way to classify various types of data such as integer, string, etc. which
determines the values that can be used with the corresponding type of data, the type of
operations that can be performed on the corresponding type of data.
There are two data types −
 Built-in Data Type
 Derived Data Type

5
DATA STRUCTURE -I UNIT-I
Data, Data Objects and Data Types

Built-in Data Type Derived Data Type


Those data types for which a language has These data types are normally built by the combination of
built-in support are known as Built-in Data primary or built-in data types and associated operations
types. on them.

For example, most of the languages For example −


provide the following built-in data types.
• List
• Array
◦ Integers • Stack
• Queue
◦ Boolean (true, false)
◦ Floating (Decimal numbers)
◦ Character and Strings

DATA STRUCTURE -I UNIT-I 6


Abstract Data Type(ADT) and Data Structure

 Abstract Data Type Abstract Data Type consist of

 Concern about what can be done not ❑Declaration of Data


how it can be done
❑Declaration of Operations
❑Encapsulation of data and operations

7
DATA STRUCTURE -I UNIT-I
Abstract data types
An abstract data type is a type with associated operations, but whose representation is
hidden.

• The calculator explains it very well.

• One can use it different ways by giving various


values and perform operations.

• But, mechanism how the operation is done is not


shown.

• This process of hiding the information is called as


Abstraction.

8
Abstract Data Types (ADT)

 An ADT is composed of

 A collection of data
 A set of operations on that data

 Specifications of an ADT indicate


 What the ADT operations do, not how to implement them

 Implementation of an ADT
 Includes choosing a particular data structure

9
Data Structure
DATA STRUCTURE:- Structural representation of data items in primary
memory to do storage & retrieval operations efficiently.
Data may be organized in many different ways,
The logical or mathematical model of a particular organization of data is
called data structure.
Data objects + Relationship among the instances
Data Structure Study concerns with data object representation and
functions implementation
Data Structure
Types of Data Structures
Linear data structure:
The data structure where data items are organized sequentially or linearly where data
elements attached one after another is called linear data structure. It has unique
predecessor and Successor.

Ex: Arrays, Linked Lists

Non-Linear data structure:


The data structure where data items are not organized sequentially is called non linear
data structure. It don’t have unique predecessor or Successor.
In other words, A data elements of the non linear data structure could be connected to
more than one elements to reflect a special relationship among them.
Ex: Trees, Graphs

DATA STRUCTURE -I UNIT-I 12


Types of Data Structures
Linear Data Structure Non Linear Data Structure

13
Types of Data Structures

Persistent vs. Ephemeral


Persistent :
• Retain their previous state and modification can be done by performing certain
operations on it.
• A persistent data structure is a data structure that always preserves the previous version
of itself when it is modified

Ephermal :
• Can not retain their previous state

14
Types of Data Structures

Persistent Data Structure

15
Introduction to Algorithms
Algorithm
◦ Solution to a problem that is independent of any programming language.
◦ Sequence of steps required to solve the problem
◦ Algorithm is a finite set of instructions that if followed, accomplishes a particular task
◦ All algorithms must satisfy the following criteria:
❑Input: Zero or more Quantities are externally supplied
❑Output: At least one quantity is produced
❑Definiteness: Each instruction is clear and unambiguous
❑Finiteness: if we trace out the instructions of an algorithm then for all cases the algorithm
terminates after a finite number of steps.
❑Effectiveness: Every instruction must be very basic so that it can be carried out in principle by a
person using pencil and paper.

DATA STRUCTURE -I UNIT-I 16


Introduction to Algorithms
Program vs Algorithm

● A program is a written out set of statements in a language that can be


executed by the machine.

● An algorithm is simply an idea or a solution to a problem that is often


procedurally written.

17
Introduction to Algorithms

Example : Finding the largest integer among five integers

18
Introduction to Algorithms
Defining actions in Find Largest algorithm

19
Introduction to Algorithms
Find Largest refined

20
Introduction to Algorithms

21
Introduction to Algorithms
Three constructs

22
Algorithm Design Tools
➢Pseudo Code

❑is an artificial and informal language that helps programmers develop algorithms.
❑Uses English-like phrases with some Visual Basic terms to outline the program

➢Flowchart

❑Graphical representation of an algorithm.


❑Graphically depicts the logical steps to carry out a task and shows how the steps relate
to each other.

DATA STRUCTURE -I UNIT-I 23


Algorithm Design Tools
Flowchart
Example 1: Print 1 to 20:

Algorithm
Step 1: Initialize X as 0,

Step 2: Increment X by 1,

Step 3: Print X,

Step 4: If X is less than 20 then go back to step 2.

24
Pseudo Code
Algorithm SORT(A, n)
Pseudocode is an informal high-level description {
of the operating principle of a computer program or for (i =1;i<n; i++)
other algorithm. {
j=i;
for (k = j+1;k<n; k++)
It uses the structural conventions of a normal {
programming language, but is intended for human if A[k] < A[j]
reading rather than machine reading. j=k;
}
t = A[i];
A[i]= A[j];
A[j]=t
}
}

DATA STRUCTURE -I UNIT-I 25


Pseudo Code
Examples Examples
Algorithm grade_count()
Algorithm grade_assignment( ) {
{ total=0;
grade_counter =1;
if (student_grade >= 60)
print "passed“; while (grade_counter<10)
else {
read next grade;
print "failed" ; total=total + grade;
grade_counter=grade_counter + 1;
}
}
class_average=total/10;
print class_average.;
}

DATA STRUCTURE -I UNIT-I 26


Pseudo Code
Some Keywords That Should be Used And Additional Points:

 Algorithm Keyword is used

 Curly brackets are used instead of begin-end

 Directly programming syntaxes are used

 Easy to convert into program

 Semicolons used

DATA STRUCTURE -I UNIT-I


27
Pseudo Code
Some Keywords That Should be Used And Additional Points:

❑Words such as set, reset, increment, compute, calculate, add, sum, multiply, ... print, display,
input, output, edit, test , etc. with careful indentation tend to foster desirable pseudocode.

❑Also, using words such as Set and Initialize, when assigning values to variables is also
desirable.

DATA STRUCTURE -I UNIT-I


28
Pseudo Code
Formatting and Conventions in Pseudo code

❑INDENTATION in pseudocode should be identical to its implementation in a


programming language.

❑Use curly brackets for indentation

❑No flower boxes (discussed ahead) in your pseudocode.

❑Do not include data declarations in your pseudocode.

❑But do cite variables that are initialized as part of their declarations. E.g. "initialize
count to zero" is a good entry.

DATA STRUCTURE -I UNIT-I 29


Pseudo Code
Calls to Functions should appear as: Functions called with addresses should be
written as:
Call FunctionName (arguments: field1,
field2, etc.) Call FunctionName (arguments: pointer to
fieldn, pointer to field1, etc.)
Function headers containing pointers should
Returns in functions should appear as: be indicated as:
Return (field1) FunctionName (parameters: pointer to
field1, pointer to field2, ...)

Function headers should appear as: Returns in functions where a pointer is


returned:
FunctionName (parameters: field1, Return (pointer to fieldn)
field2, etc. )

DATA STRUCTURE -I UNIT-I 30


Pseudo Code
➢Advantages and Disadvantages

Pseudocode Disadvantages
 It’s not visual
 There is no accepted standard, so it varies widely from company to
company
Pseudocode Advantages
 Can be done easily on a word processor
 Easily modified
 Implements structured concepts well

DATA STRUCTURE -I UNIT-I 31


Flow Chart Symbols
Flowchart Symbol Explanation

Flow lines are indicated by straight lines with


optional arrows to show direction of data flow.

An ellipse uses the name of the module at the


start. The end is indicated by the word end or
stop.
Start/Stop/End
Processing block such as calculations,
opening and closing files

Input to or output from the computer

Decision symbol. one entrance and two and only


two exits

DATA STRUCTURE -I UNIT-I 32


Drawing the Flowcharts
Flowchart Symbol Explanation
Process of module. Having one entrance
and one exit

Loop within counter. The counter starts with


A s A and incremented by s until the counter is
B greater than B

On-page connector. Connects sections


on same page
Off Page Connectors

DATA STRUCTURE -I UNIT-I 33


Flow Charts
Flowchart Disadvantages

 Hard to modify
 Need special software

Flowchart Advantages

 Standardized: all pretty much agree on the symbols and their meaning
 Visual

DATA STRUCTURE -I UNIT-I 34


Algorithms and Flowcharts
Algorithm Flowcharts

Control Module Control

1.Repeat Read
Process Read
Process Calc Calc
Process Print
Until
Print
NoMoreEmployee False
Until
noMoreEm
ployee
2.End
True

End

DATA STRUCTURE -I UNIT-I 35


Algorithms and Flowcharts
Algorithm Flowcharts

Read Module Read

1. Read Hours,
2. Read
PayRate Read
Hours,
PayRate
3.Exit

Exit

DATA STRUCTURE -I UNIT-I 36


Algorithms and Flowcharts
Algorithm Flowcharts

Calc Module Calc

1. GrossPay=
HoursWorked
*PayRate
GrossPay=
HoursWorked*PayRate
2.Exit

Exit

DATA STRUCTURE -I UNIT-I 37


Algorithms and Flowcharts
Algorithm Flowcharts

Print Module Print

1. Print Pay

2.Exit Print GrossPay

Exit

DATA STRUCTURE -I UNIT-I 38


Algorithms and Flowcharts
Read

Read
Hours,Pay
Rate

Control Exit Calc

Read
GrossPay=
Calc Hours*PayRate

Print
Exit
False
Until
Print
noMoreEm
ployee

True Print
GrossP
End ay

Exit

DATA STRUCTURE -I UNIT-I 39


Analysis of Algorithms

• Finding Efficiency of an algorithm in terms of

 Time Complexity

 Space Complexity

DATA STRUCTURE -I UNIT-I 40


Analysis of Algorithms

• What is time complexity

– Finding amount of time required for executing set of instructions or functions


– It is represented in terms of frequency count
– Frequency count is number of time every instruction of a code is to be executed.

• What is space complexity


– Finding amount of memory space the program is going to consume.
– It is calculated in terms of variables used in program.

DATA STRUCTURE -I UNIT-I

41
complexity
It is the time taken and space required for an algorithm for its
completion
It decides the quality of algorithm
◦ Two types
◦ Space complexity
◦ Time complexity
Space complexity
Amount of space required by an algorithm
It is the sum of fixed part and variable part
Fixed part : it depends on the characteristics of i/p and o/p
Variable part : instance characteristics
Time complexity
Sum of compile time and execution time
Time complexity is amount of time required by algorithm for
completion of problem .
There are three cases
◦ Best case
◦ Worst case
◦ Average case
Best case
If an algorithm takes an minimum amount of time to run to completion
for specific set of input
Eg. While searching a particular element by using sequential search we
get desired element at first place itself .
Worst case
If an algorithm takes an Maximum amount of time to run to completion
for specific set of input
Eg. While searching a particular element by using sequential search we
get desired element at end.
Average case : the average time taken by an algorithm to run to
completion is called average case.
◦ This classification gives complexity information about behavior of algorithm
at particular instance .
Running Time
•The running time depends on the input: an already
sorted sequence is easier to sort.

• Parameterize the running time by the size of the


input, since short sequences are easier to sort than
long ones.

• Generally, we seek upper bounds on the running


time, because everybody likes a guarantee.
Time complexity
It can be found by
◦ Brute force method
◦ Step count method
◦ Asymptotic notation
Asymptotic Notation

The notation we use to describe the asymptotic running


time of an algorithm are defined in terms of functions
whose domains are the set of natural numbers

N 0, 1, 2, ...
Asymptotic
Notation
There are 5 types
Big-oh Notation (O- notation)
Omega Notation (Ω - notation)
Theta Notation (Θ - notation)
Little –oh Notation (o- notation)
Little –omega Notation (ω- notation)
O-notation
For function g(n), we define O(g(n)),
big-O of n, as the set:

O(g(n)) = {f(n) :
∃ positive constants c and n0,
such that ∀n ≥ n , 0

we have 0 ≤ f(n) ≤ cg(n) }


Intuitively: Set of all functions whose rate of growth
is the same as or lower than that of g(n).

g(n) is an asymptotic upper bound for f(n).

DATA STRUCTURE -I UNIT-I 51


Ω -notation
For function g(n), we define Ω(g(n)),
big-Omega of n, as the set:

Ω(g(n)) = {f(n) :
∃ positive constants c and n0,
such that ∀n ≥ n , 0

we have 0 ≤ cg(n) ≤ f(n)}


Intuitively: Set of all functions whose rate of
growth is the same as or higher than that of
g(n).

g(n) is an asymptotic lower bound for f(n).

DATA STRUCTURE -I UNIT-I 52


Θ-notation
For function g(n), we define Θ(g(n)),
big-Theta of n, as the set:
Θ(g(n)) = {f(n) :
∃ positive constants c1, c2, and n0,
such that ∀n ≥ n , 0

we have 0 ≤ c1g(n) ≤ f(n) ≤ c2g(n)

}
Intuitively: Set of all functions that
have the same rate of growth as g(n).

g(n) is an asymptotically tight bound for f(n).

DATA STRUCTURE -I UNIT-I 53


Analysis of Algorithms

• Algorithm analysis is done in following three cases

– Best Case
The amount of time a program might be expected to take on best possible input
data
– Worst Case
The amount of time a program might be expected to take on typical(or average)
input data
– Average case
The amount of time a program would take on worst possible input configuration.
Example: Sorting Algorithms DATA STRUCTURE -I UNIT-I 54
FAQ

What are the types of data structures?


Which are types of data types?
What is an ADT?
What is frequency count?
Define Big-O, Omega and Theta notations.
What are the types of problems?
What is frequency count? Explain its relation with time complexity.
Define Algorithm, Flowchart and Pseudo code.

DATA STRUCTURE -I UNIT-I 55


Practice Assignments
1.Write a pseudo code and draw flowchart to input any alphabet and
check whether it is vowel or consonant.
2.Write a pseudo code to check whether a number is even or odd
3.Write a pseudo code to check whether a year is leap year or not.
4.Write a pseudo code to check whether a number is negative, positive or
zero
5.Write a pseudo code to input basic salary of an employee and calculate
its Gross salary according to following:
Basic Salary <= 10000 : HRA = 20%, DA = 80%
Basic Salary <= 20000 : HRA = 25%, DA = 90%
Basic Salary > 20000 : HRA = 30%, DA = 95%

DATA STRUCTURE -I UNIT-I 56


DATA STRUCTURE -I UNIT-I 57
DATA STRUCTURE -I UNIT-I 58
DATA STRUCTURE -I UNIT-I 59
DATA STRUCTURE -I UNIT-I 60
DATA STRUCTURE -I UNIT-I 61
DATA STRUCTURE -I UNIT-I 62
DATA STRUCTURE -I UNIT-I 63
DATA STRUCTURE -I UNIT-I 64
DATA STRUCTURE -I UNIT-I 65
Practice Problems
Q.1 Determine frequency count of following statements? Analyze time
complexity of the following code:
i) for (i=1;i<=n;i++)
for (j=1;j<=m;j++)
sum=sum+i;
ii) i=n;
while(i>=1)
{
i--;
}

DATA STRUCTURE -I UNIT-I 66


Practice Problems

Problems on frequency count & time


complexity
double IterPow(double X,int N)
{
for(i=1;i<=n;i++)
double Result=1;
{
while(N>0)
For(j=1;j<=n;j++)
{
{
Result=Result* X
C[j][j]=0;
N--;
For(k=1;k<=n;k++)
}
C[i][j]=c[i][j]+a[i][k]*b[k][j];
return result;
}
}

DATA STRUCTURE -I UNIT-I 67


Practice Problems

Q.3 What is the frequency count of a statement? Analyze time


complexity of following code?

for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
for(k=1;k<=p;k++)
Sum=sum+i

DATA STRUCTURE -I UNIT-I 68


Takeaway

 Data Structures plays major role in problem solving.

 Pseudo code and flowcharts are the tools used to represent the solution of a

problem in effective way.

 Analysis of algorithms is done in terms of time complexity and space

complexity.

69
DATA STRUCTURE -I UNIT-I
References

❑Horowitz,Sahani,”Fundamentals of Data Struyctures”, Galgotia Publication

❑Gilberg, Forozen,”Data Structures :A Pseudo Code Approach with C”

❑Maureen Sprankle,”Problem Solving and Programming Concepts”

70
DATA STRUCTURE -I UNIT-I

You might also like