Uploaded by Shrawani Dongre

1 Intro DSU

advertisement
PIMPRI CHINCHWAD EDUCATION TRUST’S
PIMPRI CHINCHWAD POLYTECHNIC
NBA Accredited Institute
I.S.O. Certified Organization, Approved by A.I.C.T.E.
Affiliated to M.S.B.T.E. Mumbai
Address : Pimpri Chinchwad Polytechnic
Sector No. 26, Pradhikaran, Nigdi,
Pune - 411 044
Phone: 020 - 2765 4156 / 2765 8797
Fax: 27654156
Email: principal@pcpolytechnic.com
Website: www.pcpolytechnic.com
Pimpri Chinchwad Education Trust’s
Pimpri Chinchwad Polytechnic
Sector No. 26, Pradhikaran, Nigdi, Pune – 411044
(NBA Accredited / An ISO 9001:2015 Certified)
Phone: 020-27654156 / 27658797 Fax: 27654156
Website : www.pcpolytechnic.com Email: principal@pcpolytechnic.com
Department : Computer Engineering
Course : Data Structures Using ‘C’
Course Code : 22317
Course Outcomes (COs) :
a.
Perform basic operations on arrays
b.
Apply different searching and sorting techniques.
c.
Implement basic operations on stack and queue using array
representation.
d.
Implement basic operations on Linked List.
e.
Implement program to create and traverse tree to solve problems.
Pimpri Chinchwad Polytechnic, Computer Department
Course : Data Structures Using ‘C’
Course Code : 22317
Pimpri Chinchwad Polytechnic, Computer Department
Data Structures Using ‘C’ (22317)
Suggested Learning Resources :
01
Data Structures using ‘C’
02
Data Structures using ‘C’
03
Data Structures with ‘C’ (SIE)
(Schaum’s Outline Series)
04
Practical ‘C’ Programming
05
Data Structures
Pimpri Chinchwad Polytechnic, Computer Department
●
Balgurusamy, E.
●
McGraw Hill Education, New Delhi
●
ISRD Group
●
McGraw Hill Education, New Delhi
●
Lipschutz
●
McGraw Hill Education, New Delhi
●
Steve Qualline
●
O’Reilly Media
●
Dr. Rajendra Kawale
●
Devraj Publicatiions
Data Structures Using ‘C’ (22317)
Suggested Learning Websites:
❏
http://nptel.ac.in/courses/106102064/1
❏
www.oopweb.com/algorithms
❏
www.studytonight.com/data-structures/
❏
www.cs.utexas.edu/users
❏
liscs.wssu.edu
❏
http://www.academictutorials.com/data-structure/
❏
http://www.sitesbay.com/data-structure/c-data-structure
❏
http://www.indiabix.com
❏
http://www.khanacademy.org/
Pimpri Chinchwad Polytechnic, Computer Department
Data Structures Using ‘C’ (22317)
Unit : I
Introduction to Data Structures
1.Introduction:
Concept and need of DS, Abstract Data Type
2. Types of Data Structures:
(i) Linear Data Structures (ii) Non-Linear Data Structures
3. Algorithm Complexity:
(i)Time (ii)Space
4. Operations on Data Structures:
(i) Traversing, (ii) Searching, (iii) Insertion, (iv) Deletion, (v) Sorting
Pimpri Chinchwad Polytechnic, Computer Department
Data Structures Using ‘C’ (22317)
Data Structures :
Pimpri Chinchwad Polytechnic, Computer Department
Data Structures Using ‘C’ (22317)
Data Structures :
Data :
Data is nothing but a collection of numbers, alphabets and symbols combined to represent
“Information”. [In computers - Data is simply value assigned to the variables.]
Ex. Numeric data, Character data, Audio, Video, Graphics etc.
Data Structures :
Data Structure is a way of collecting and organizing data in such a way that we can perform
operations on these data in an effective way.
[ Anything that can store data can be called as a data structure, hence Integer, Float, Boolean,
Char etc, all are data structures. They are known as Primitive Data Structures.
Then we also have some complex Data Structures, which are used to store large and connected
data , for ex, Array, Stack, Queue, Linked List, Graph and Tree. These are known as Abstract Data Types
which are Non-Primitive Data Structures. ]
Pimpri Chinchwad Polytechnic, Computer Department
Data Structures Using ‘C’ (22317)
Abstract Data Types (ADT) :
What is ADT :
❏
An ADT is a type with associated operations, but whose representation is hidden.
❏
The programmer may have to create & implement his own data type to suit the program’s
requirements using built-in data types.
❏
The triplet {D,F,A} is also called as the ADT since it is conceptual specification of the data
structure.
D - set of Domains which denotes the data objects.
F - set of Functions or operations to be carried out on data objects.
A - Axioms means describes the properties & rules of the operations.
Pimpri Chinchwad Polytechnic, Computer Department
Data Structures Using ‘C’ (22317)
Abstract Data Types (ADT) :
Advantages :
❏
Astraction : The ADT hides the implementation so that the programs can simply use the data
structure & its operations without knowing about how it works. So even if the implementation of
specific operation is changed it will not affect the program any way.
❏
Encapsulation : The data object, operation & the rules are grouped together into a single logical
unit. This helps in arranging the program in a better manner.
❏
Generalization : The ADT is a general specification of data structure. The implementation is left
with the programmers. Ex. ADT for array can be used create integer arrays, char arrays, structure
array, pointer array, object array etc.
❏
Reliability : Since the data object and related operations are “tied” together or encapsulated,
they can be stored elsewhere and only used in the program. Hence the program will not be able to
modify the data structure.
Pimpri Chinchwad Polytechnic, Computer Department
Data Structures Using ‘C’ (22317)
Need and Advantages of Data Structures :
Need :
❏
❏
❏
❏
To achieve “Proper and Systematic” arrangement of data.
Variety of operations (insert, delete, search, sort, merge, traverse etc.) can be performed using as
few resources as possible.
Speed up execution and save time and Efficient use of memory space.
Data Structures are implemented by a programming language by the data types and the
references and operations provide by that particular language
Advantages :
❏
❏
❏
❏
❏
Structured data makes it easier to access & manipulate the information as compared to raw or
unstructured data.
A variety of operations can be performed on structured data.
Related data can be stored together in the required format.
The logical relationship between the data elements can be properly represented using data
structures.
Better algorithms can be used on organized data. This improves program efficiency.
Pimpri Chinchwad Polytechnic, Computer Department
Data Structures Using ‘C’ (22317)
Ex. : Each data structure has their own unique properties that make it well suited to give a
certain view of the data.
Name
Position
Aaron
Charles
George
Jack
Janet
John
Kim
Larry
Martha
Patricia
Rick
Sarah
Susan
Thomas
Zack
Manager
VP
Employee
Employee
VP
President
Manager
Manager
Employee
Employee
Secretary
VP
Manager
Employee
Employee
Dia. 1
Pimpri Chinchwad Polytechnic, Computer Department
Dia. 2
These two diagrams are examples of the different data
structures. Your data is organized into a list, in one of the
data structures which dia. 1. The employees name can be
stored in alphabetical order so that we can locate the
employee's record very quickly.
For showing the relationships between employees this
structure is not very useful. The tree structure is a much
better suited for this purpose which is Dia. 2.
Source of information : http://www.academictutorials.com/data-structure/data-structure-introduction.asp
Data Structures Using ‘C’ (22317)
Classification of Data Structures :
Pimpri Chinchwad Polytechnic, Computer Department
Data Structures Using ‘C’ (22317)
Types of Data Structures :
Source of information: https://www.geeksforgeeks.org/difference-between-linear-and-non-linear-data-structures/
Pimpri Chinchwad Polytechnic, Computer Department
Data Structures Using ‘C’ (22317)
Types of Data Structures :
S.NO
LINEAR DATA STRUCTURE
NON-LINEAR DATA STRUCTURE
1.
In a linear data structure, data elements are arranged
in a linear order where each and every elements are
attached to its previous and next adjacent.
In a non-linear data structure, data elements are
attached in hierarchically manner.
2.
In linear data structure, single level is involved.
Whereas in non-linear data structure, multiple levels
are involved.
3.
Its implementation is easy in comparison to non-linear
data structure.
While its implementation is complex in comparison
to linear data structure.
4.
In linear data structure, data elements can be
traversed in a single run only.
While in non-linear data structure, data elements
can’t be traversed in a single run only.
5.
In a linear data structure, memory is not utilized in an
efficient way.
While in a non-linear data structure, memory is
utilized in an efficient way.
6.
Its examples are: array, stack, queue, linked list, etc.
While its examples are: trees and graphs.
7.
Applications of linear data structures are mainly in
application software development.
Applications of non-linear data structures are in
Artificial Intelligence and image processing.
Source of information: https://www.geeksforgeeks.org/difference-between-linear-and-non-linear-data-structures/
Pimpri Chinchwad Polytechnic, Computer Department
Data Structures Using ‘C’ (22317)
Algorithm Complexity :
❏
Algorithm :
❏
Algorithm is a step-by-step procedure, which defines a set of instructions to be executed in a certain
order to get the desired output.
❏
Algorithms are generally created independent of underlying languages,
❏
i.e. an algorithm can be implemented in more than one programming language.
❏
Characteristics of Algorithm :
❏
Unambiguous − Algorithm should be clear and unambiguous. Each of its steps (or phases), and their
inputs/outputs should be clear and must lead to only one meaning.
❏
Input − An algorithm should have 0 or more well-defined inputs.
❏
Output − An algorithm should have 1 or more well-defined outputs, and should match the desired o/p.
❏
Finiteness − Algorithms must terminate after a finite number of steps.
❏
Feasibility − Should be feasible with the available resources.
❏
Independent − An algorithm should have step-by-step directions, which should be independent of any
programming code.
Pimpri Chinchwad Polytechnic, Computer Department
Data Structures Using ‘C’ (22317)
Algorithm Complexity :
Step 1 − START
Step 2 − declare three integers a, b & c
Step 3 − define values of a & b
Step 4 − add values of a & b
Step 5 − store output of step 4 to c
Step 6 − print c
Step 7 − STOP
Step 1 − START ADD
Step 2 − get values of a & b
Step 3 − c ← a + b
Step 4 − display c
Step 5 − STOP
Step 1 − START ADD
Step 2 − get values of a & b
Step 3 − display ← a + b
Step 4 − STOP
Hence, many solution algorithms can be derived for a given
problem. The next step is to analyze those proposed solution
algorithms and implement the best suitable solution.
Source of Diagram and information:
https://www.tutorialspoint.com/data_structures_algorithms/algorithms_basics.htm
Pimpri Chinchwad Polytechnic, Computer Department
Data Structures Using ‘C’ (22317)
Algorithm Complexity :
❏
Algorithm should be easy to understand, write and debug.
❏
Algorithm should also make efficient use of resources like CPU, memory etc.
❏
Analysis of algorithm basically focuses on two things :
❏
Space Complexity
❏
Time Complexity
Pimpri Chinchwad Polytechnic, Computer Department
Data Structures Using ‘C’ (22317)
Algorithm Complexity :
❏
Space Complexity :
❏
It is an amount of memory required by the algorithm to run to the completion.
❏
The space needed by an algorithm is the sum of the following two components:
❏
1. Fixed Part : It includes instruction space for simple variables, constants etc.
❏
2. Variable Part : It consists of space needed by variables whose size depends upon the particular
problem being solved, referenced variables and the stack space required for recursion on particular
instance of variables. e.g. Additional space required where function uses recursion.
❏
Space requirement S(P) − for an Algorithm P is : S(P) = C + Sp where
❏
C − is constant & represents fixed part.
❏
Sp − is variable part.
❏
Ex.
void main( )
{ int a,b;
float c;
int arr[5];
}
Pimpri Chinchwad Polytechnic, Computer Department
18 bytes
Also explore more on Space Complexity here:
https://www.studytonight.com/data-structures/space-complexity-of-algorithms
Data Structures Using ‘C’ (22317)
Algorithm Complexity :
❏
Time Complexity : [Denoted by Big ‘O’ notation]
❏
It is an amount of time required by the algorithm to run to the completion.
❏
While calculating time complexity, we develop frequency count for all key statements (active statements)
which are important.
If sum of frequency count generates a polynomial, then generally largest from that polynomial is
considered.
Ex. f(N) = N4 + 2ON3 + N + 40 =>
f(N) = N4
=> denoted as f(N) = O(N4)
❏
❏
Algorithm A:a=a+1
f(N) = O(1)
Algorithm B:for i=1 to N loop
a=a+1
f(N) = O(N)
Algorithm C:for i=1 to N loop
for j=1 to N loop
a=a+1
f(N) = O(N2)
Pimpri Chinchwad Polytechnic, Computer Department
Algorithm D:for i=1 to N loop
a=a+1
for i=1 to N loop
for j=1 to N loop
a=a+1
f(N) = N +N2
= O(N2)
Also explore more on Time Complexity here:
https://www.studytonight.com/data-structures/time-complexity-of-algorithms
Data Structures Using ‘C’ (22317)
Algorithm Analysis :
❏
We can analyse an algorithm as it depends on the number of input data. There are basically 3
cases.
❏
Best Case :
❏
It is the measure of the minimum time an algorithm will require for an input of size N.
❏
Worst Case :
❏
It is the measure of the maximum time an algorithm will require for an input of size N.
❏
Average Case :
❏
The average time that an algorithm will require to execute for an input of size N.
❏
Generally case step count is average number of steps executed.
Pimpri Chinchwad Polytechnic, Computer Department
Data Structures Using ‘C’ (22317)
Algorithm Design Approaches :
❏
Top-Down Approach :
❏
A top-down approach starts with identifying major components of system or program
decomposing them into their lower level components & iterating until desired level of module
complexity is achieved . In this we start with topmost module & incrementally add modules
that is calls.
❏
Bottom-Up Approach :
❏
A bottom-up approach starts with designing most basic or primitive component & proceeds to
higher level components. Starting from very bottom, operations that provide layer of
abstraction are implemented.
Pimpri Chinchwad Polytechnic, Computer Department
Data Structures Using ‘C’ (22317)
Operations on data structures :
❏
Traversing- It is used to access each data item exactly once so that it can be processed.
❏
Searching- It is used to find out the location of the data item if it exists in the given
collection of data items.
❏
Inserting- It is used to add a new data item in the given collection of data items.
❏
Deleting- It is used to delete an existing data item from the given collection of data items.
❏
Sorting- It is used to arrange the data items in some order i.e. in ascending or descending
order in case of numerical data and in dictionary order in case of alphanumeric data.
❏
Merging- It is used to combine the data items of two sorted files into single file in the
sorted form.
Pimpri Chinchwad Polytechnic, Computer Department
Data Structures Using ‘C’ (22317)
Download