Topics to becovered
• What is Data Structure?
• Different types of Data Structure
• Operations on Data Structure
• Algorithm
• Analysis Terms
• Python Specific Data Structures-List, Tuple,Set,
Dictionary
• Array
3.
What is DataStructure?
• Computer is an electronic machine which is
used for data processing and manipulation.
• In order to make computer work we need to
know,
Representation of data in computer.
Accessing of data.
How to solve problem step by step.
• For doing this task we use data structure.
4.
What is DataStructure?
Data and Information
• Data is raw collection of fact, figure, number, symbol etc.
• Data is not significant without proper arrangement and
interpretation.
• Collection of data which describe some meaningful action or work
is known as information.
• For example weather condition of each day is data but when it
monitoring regularly, analysing and prepare prediction chart is
information.
DATA INFORMATION
PROCESS
DATA
5.
What is DataStructure?
Employee Number
Last Name
First Name
Address
City
PinCode
One Record
Field
Employee
Number
Last Name First Name Address City Pin Code
101 Xyz Abc Near xyz… Rajkot 360001
Field Record
6.
What is DataStructure?
Fundamental Definitions
• Cells: A cell is a memory location, will used to store elements of data
items. It can be referred as single bit, byte or group of bytes.
• Record: It is a collection of information about particular item.
• Field: A field is the smallest piece of information that can be referred
by programming language.
Example: A Record with multiple fields, this type of number of records contains in data file.
• Key: To search for a record within a data file, you must designate one
of the record’s field as a key.
• For above example of employee, you choose an employee number as
key field to search a specific record of a employee within data file.
• Search Key: Every record has a key. The key have you are looking for in
search is called search key.
7.
What is DataStructure?
• Data structure is a systematic way of organizing and accessing data.
OR
• Logical or mathematical model of organizing data is called data structure.
OR
• Data structure is a particular way of storing and organizing data in a
computer so that it can be used efficiently.
• Data structure study covers the following points:
Amount of memory require to store.
Amount of time require to process.
Representation of data in memory.
Operations performed on that data.
8.
Different types ofData Structure
Data Structure
Linear Non-Linear
Stack Array
Queue Linked List
Trees Graphs
9.
Linear data structures:
•Linear data structures are those data structure in
which data items are arranged in a linear
sequence by physically or logically or both the
ways.
• Ex: array, linked list.
Array Linked list
10.
• Array −It is a sequential arrangement of data
elements paired with the index of the data
element.
• Linked List − Each data element contains a link
to another element along with the data present
in it.
• Stack − It is a data structure which follows only
to specific order of operation. LIFO(last in First
Out)
• Queue − It is similar to Stack but the order of
operation is only FIFO(First In First Out).
11.
Non linear datastructures:
• Non linear data structures are those data
structure in which Elements of this data structure
are stored /accessed in a non-linear order.
• Ex: graph, tree.
Tree Graph
12.
• Tree: Elementsstores elements hierarchically.
Parent-child relationship
• Graph: A graph is a non-linear data structure
that has a finite number of vertices and edges,
and these edges are used to connect the
vertices
13.
Operations on DataStructure
Operations on data structures:
Insertion: Adding a new data element into respective data structure at
particular position.
Update: It will update any elements value into respective data structure
at particular position.
Deletion: Removing a data element from respective data structure at
particular position.
Traversal: It will visit each element in respective data structure at once.
Searching: Finding the location for specific data.
Sorting: It will arrange (sort)( in ascending or descending order) each
element in respective data structure.
Merging: It will merge (combine) data of two or more data structure into
one.
Split: it will divide data structure into two or more parts.
14.
Algorithm and it’sfeatures
Algorithm:
• Algorithm is defined as step follows to solve particular problem
which is written in common language which non professionals
can understand.
• Algorithm computer program computer programming
(process) programming language
• Properties of algorithm:
Input– Zero or more quantities are externally supplied.
Output– At least one quantity is produced.
Finiteness – Terminates after finite step.
Definiteness – Proper execution of steps without ambiguity.
Effectiveness – It must be effective enough to solve in finite time with
given resources.
15.
Analysis Terms
• Thecomplexity of an algorithm is a function
describing the efficiency of the algorithm in
terms of the amount of data the algorithm
must process.
• There are two main complexity measures of
the efficiency of an algorithm:
1. Time Complexity
2. Space complexity
16.
Time Complexity
• Thetime complexity of an algorithm quantifies
the amount of time taken by an algorithm to
run as a function of the length of the input.
17.
Time Complexity
Best casetime complexity: - Minimum time required for program execution.
• The case occurs, when sorting techniques have Minimum number of
comparisons occurs.
• So sorting can achieved in less time and execution time of program is
increased.
Average case time complexity: - Average time required for program execution.
• The case occurs, when sorting techniques have Average number of
comparisons occurs.
Worst case time complexity:- Maximum time required for program execution.
• The case occurs, when sorting techniques have Maximum number of
comparisons occurs.
• So sorting can achieved in more time and execution time of program is
decreased.
18.
Space complexity
• Spacecomplexity is the number of memory cells which
an algorithm needs.
• It is directly dependent or proportional to the amount
of input that the algorithm takes.
• To calculate space complexity, all you have to do is
calculate the space taken up by the variables in an
algorithm. The lesser space, the faster the algorithm
executes.
• The better the time complexity of an algorithm is, the
faster the algorithm will carry out his work in practice.
19.
Big ‘O’ Notation
•
Thenotation Ο(n) is the formal way to express the upper bound of an
algorithm's running time.
20.
Common Asymptotic Notations
constant− Ο(1)
logarithmic − Ο(log n)
linear − Ο(n)
n log n − Ο(n log n)
quadratic − Ο(n2
)
cubic − Ο(n3
)
polynomial − nΟ(1)
exponential − 2Ο(n)
21.
Example
• Linear timeO(n)
• def ex1( n ):
total = 0
for i in range( n ) :
total += i
return total
• Quadratic Time O(n^2)
• def ex3( n ):
count = 0
for i in range( n ) :
for j in range( n ) :
count += 1
return count
• Logarithmic time O(log n)
• def ex6( n ):
count = 0
i = n
while i >= 1 :
count += 1
i = i // 2
return count
22.
Python Specific DataStructures-List, Tuple, Set,
Dictionary
• String
• List
• Set
• Tuple
• Dictionary
23.
Array in Python
•Array is a container which can hold a fix number of items and
these items should be of the same type.
• ordered collection of elements
• In Python, arrays are mutable.
• Create an array
Step1: import array module
import array as arr
Step2: create array object
variable_name = arr.array(typecode,[elements])
24.
• Below isa typecode table, with the different
typecodes that can be used with the different
data types when defining Python arrays
Typecode Value
b Represents signed integer of size 1 byte
B Represents unsigned integer of size 1 byte
'u' 2 byte
i Represents signed integer of size 2 bytes
I Represents unsigned integer of size 2 bytes
l Represents signed long of size 4 bytes
L Represents unsigned long of size 4 bytes
f Represents floating point of size 4 bytes
d Represents floating point of size 8 bytes
25.
Array
Overview of variousArray operations
• Insertion: Adding a new data element into the array.
• Deletion: Removing a data element from the array.
• Traversal: Accessing each data element once in such a way
that all the data elements are processed.
• Searching: Finding the location for specific data in the array.
• Sorting: Arranging data element in logical (in ascending or
descending) order.
• Merging/concatenation: Combining the two array element
into single array.
• Slicing, Repetition,
26.
Create an array
fromarray import *
array1 = array('i', [10,20,30,40,50])
for x in array1:
print(x)
o/p:
10
20
30
40
50
Insertion Operation –insert, append, extend
from array import *
array1 = array('i', [10,20,30,40,50])
array1.insert(1,60)
for x in array1:
print(x)
Output
10
60
20
30
40
50
29.
Deletion Operation –pop, remove, del keyword
from array import *
array1 = array('i', [10,20,30,40,50])
array1.remove(40)
for x in array1:
print(x)
Output
10
20
30
50
30.
Search Operation
from arrayimport *
array1 = array('i', [10,20,30,40,50])
print (array1.index(40))
If the value is not present in the array then the program returns an error (ValueError:
array.index(x): x not in array).
Output
3
31.
Update Operation
from arrayimport *
array1 = array('i', [10,20,30,40,50])
array1[2] = 80
for x in array1:
print(x)
Output
10
20
80
40
50
If the index is not present in the array then the program returns an error
IndexError: array assignment index out of range
32.
Using numpy
• NumPystands for numeric python which is a
python package for the computation and
processing of the multidimensional and single
dimensional array elements.
• NumPy is a Python library used for working with
arrays.
• numeric computations with a high speed.
• Install numpy module – pip install numpy
• In Python we have lists that serve the purpose of
arrays, but they are slow to process.(50x faster)
Matrix multiplication
• Multiplicationof matrix is an operation which
produces a single matrix by taking two matrices
as input and multiplying rows of the first matrix
to the column of the second matrix.
• Note that we have to ensure that the number of
rows in the first matrix should be equal to the
number of columns in the second matrix.
Arrays vs List
ListArray
List is used to store items that usually
consist of elements of multiple data
types.
An array is also a vital component that
store several items of the same data
type.
It consumes a larger memory. It consumes less memory than a list.
The lists are the build-in data structure
so we don't need to import it.
We need to import the array before
work with the array.
Cannot directly handle arithmetic
operations
Can directly handle arithmetic
operations
Preferred for shorter sequence of data
items
Preferred for longer sequence of data
items
Example:
List1=[1,2,3]
Example:
import array as arr
A=arr.array(‘i’,[1,2,3])
Can use numpy module