Download Unit III - WordPress.com

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Unit III :
Introduction To Data Structures and
Analysis Of Algorithm
Contents :
Introduction to Data Structures: Concept of data, Data object, Data structure,
Abstract Data Types, realization of ADT in 'C'. Concept of Primitive and nonprimitive, linear and Non-linear, static and dynamic, persistent and ephemeral
data structures. Analysis of algorithm: frequency count and its importance in
analysis of an algorithm, Time complexity & Space complexity of an algorithm,
Big 'O', 'Ω' and 'Θ' notations, Best, Worst and Average case analysis of an
algorithm.and basic operations on file, functions used for text and binary file
handling in C.
Objective :
1. To understand primitive storage structures and types
2. To understand the concept of abstraction.
3. To understand how good or bad the algorithm is.
4. To understand the parameter to measure the goodness of
algorithm.
4/29/2017
2
Contents
1.Basic concepts

Data

Data object

Data structure

Abstract data type
Contents
2.Types Of Data Structure

Primitive and Non-Primitive

Linear and Non-Linear

Static and Dynamic

Persistent and Ephemeral
Contents
3.Analysis of Algorithm

Frequency Count

Time and Space Complexity of Algorithm

Asymptotic notation

Best case, worst case and average case analysis
Basic Concepts
Data :
Information in raw or unorganized form is called as
data.(alphabets , numbers, Symbols).
Types of data
Atomic: consist of single piece of information.
It can't be divided further into other meaningful pieces. e.g.
Word “ Array” if we divide this word into separate
characters original meaning will get lost.
Basic Concepts
Composite Data: It can be divided into different meaningful
pieces/components.
Example: “Address SKNCOE,Vadgaon,Pune-41”
Even if we divide it into words every word has some meaning
associated with it.
Basic Concepts
Data Type : It specifies which type of value a variable can
hold.
It has:
•
Set of data
•
Operations that can be performed on data
e.g. Integer is a data type .It consists of
•
Whole numbers in some defined range.
•
Operations like add, subtract, multiply, divide etc. are allowed on
integers.
Data Structure:
It is combination of elements in which each element
is either a data type or another data structure with
defined rules.
In simple words data structure is a scheme for organizing data in
the memory of a computer.
Some of the more commonly used data structures - lists, arrays,
stacks, queues, heaps, trees, and graphs.
Basic Concepts
Abstract Data Type(ADT):
Abstracting means extracting some essential
Information which we are interested in and ignoring
other part.
So while implementing any Abstract Data Type we hide
implementation details from user so user is only extracting the
information like what the data type can do and he is ignoring
how it is done(implementation).
Basic Concepts
Understanding use of ADT:
Consider an application where we want to simulate waiting line
at a bank To determine how many tellers are needed to serve
Customers efficiently.
For this application we need to simulate “Queue”. As no such
data type is available in C, we have to write code for Queue
ourselves.
Instead of writing a code for specific application ,
We can implement queue as an ADT.
Basic Concepts
Understanding use of ADT (contd.):
i.e.
1. Implement queue using any data structure like array or linked
list.
2. Define operations like insertion deletion etc.
3. These implementation details are kept hidden from the user and
It is made available to all users which are implementing similar
applications.
Classification Of Data Structures
1.Linear and Non-Linear data structure:
In Linear data structures elements form a single sequence.

Every element can have at the max one successor
and one predecessor.
e.g.
Linked List
Array
Classification of Data Structure

In Non- Linear data structures this each element
can have more than one successor.
e.g.
Tree
Graph
Classification of Data Structure
2.Static and Dynamic Data Structure:

In Static data structure memory allocation is done for the
elements at complile time.
e.g. Array
int A[10]; // Fix 20 bytes are alloted for storing
// 20 elements at compile time

In Dynamic Data Structures memory allocation is
done at run time(dynamically)
e.g. Linked List
Classification Of Data Structure
3.Persistent and Ephemeral Data Structure

A persistent data structure is one that when modifies preserve
previous version of itself.
Such data structures are immutable(unchangeable)
as their operations do-not update structure in place.
e.g. In purely functional languages data structures are persistent

In Ephemeral data structure only one version is available at a
time. After an update operation the structure which existed before
the update is lost.
e.g. In Imperative languages most data structures are ephemeral.
Analysis of Algorithm
An algorithm is a set of instruction to be followed
to solve a problem.
There can be more than one solution (algorithm )to
solve a problem.
Before choosing a specific algorithm we have to
analyse efficiency of all algorithms by focusing on two issues.
1.Space Complexity
2.Time Complexity
Algorithm Analysis
Space Complexity:
The space complexity of a program is the amount of memory
that it needs to run to completion.
It Depends upon:
1. Fixed space: C
Includes the instructions, variables, and constants
Independent of the size of problem
2. Variable space: V
Includes dynamic allocation, functions' recursion
Total space of any program
S(P)= C+ V
Algorithm Analysis
e.g.
Algorithm SUM()
// To Sum the values in an array A
Sum=0
Enter value for N
Repeat for i=1 to N
Sum = sum + A[i]
Stop
since A must be large enough to hold the N elements to be summed, size of A i.e. N varies.
space needed by sum, I, and N is independent of problem size.
We can write,
S(P) = 3 + N.
3 for N,I and sum and N can vary for every run.
Algorithm Analysis
Time Complexity:
Now a days since space available is larger and
Larger time complexity is more important issue than
Space complexity.

It is total time required by an algorithm to run a program.

Each operation in an algorithm has a cost.
e.g. count=count+1 takes some constant time.
Analysis of Algorithm
Frequency count:
It means how many times certain instruction
executed in a piece of code.
Following example clears the concept:
Statement
s/e
float sum(float list[ ], int n) 0
{
0
float tempsum = 0;
1
int i;
0
for(i=0; i <n; i++)
1
tempsum += list[i];
1
return tempsum;
1
}
0
Total
Frequency
0
0
1
0
n+1
n
1
0
Total steps
0
0
1
0
n+1
n
1
0
2n+3
Analysis of Algorithm

Time taken by algorithm to execute on different
Machines are not same due to parameters like speed
So for defining time complexity of algorithm instead
Of writing exact frequency count we express it in a order of input
size.
e.g. All algorithms having complexity functions like
2n+3 , 4n+1, n are in same class .
Means their time complexity is in proportion of n.
Same for the functions like n3+4n2 + 20 ,
5n3+6n2 + 2, 2n3 +3 (They all are in same class)
Asymptotic notations:
A formal way of representing functions in different
Classes.
3 Asymptotic notations:
1.Θ-notation
2.Ω notation
3.O-natation
Algorithm analysis
Θ-notation :
Definition:
Considering two non negative functions f and g,
Θ(g) : {f | where f is non negative function such that
there exist constants c1,c2,n1 such that
c1g(n) <= f(n) <= c2 g(n) for all n>=n1 }
e.g. To prove 5n3+6n2 + 2 belongs to Θ
We can write 5n3 <=5n3+6n2 + 2<=5n3+6n3 + 2n3
i.e. 5n3 <=5n3+6n2 + 2< =13n3
Considering this in a form c1g(n) <= f(n) <= c2 g(n)
we can say that Here c1=5 , c2=13 and n>=1=n1.
Algorithm Analysis
O-notation :
Definition:
Considering two non negative functions f and g,
O(g):{f | where f is non negative function such that
there exist constants c2,n1 such that
f(n)>=c2g(n) for all n>=n1 }
Algorithm Analysis
O-notation :
Definition:
Considering two non negative functions f and g,
O(g):{f | where f is non negative function such that
there exist constants c2,n1 such that
f(n)>=c2g(n) for all n>=n1 }
Algorithm Analysis
Special classes of algorithm:
1.Logarithmic : O(logn)
2.Linear
: O(n)
3.Quadratic
: O(n2)
4.Polynomal : O(nk) ,k>=1
5.Exponential : O(an) , n>1
Order :
2
3
N
1<log 2 N <N < N log2 N <N <N <2 <3
N
Algorithm Analysis
O notation :denotes upper bound on complexity.
Ω-notation :denotes lower bound on complexity.
Θ-notation :denotes exact bound on complexity.
e.g.
1.we can write Time complexity of linear search is O(n) where n is total
no of elements in the list. Because to check for numbers in worst case
we have to search whole list.
2.We can write Time complexity of an algorithm for finding maximum no
from array of n elements is Ω(n) because minimum n-1 comparisons
should be done.
3.We can represent time complexity of finding maximum number from
an array as Θ(n) as well because we know upper bound for that
algorithm too. It is O(n)
Algorithm Analysis



The worst-case complexity of the algorithm: is the function
defined by the maximum number of steps taken on any instance
of size n.
The best-case complexity of the algorithm :is the function
defined by the minimum number of steps taken on any instance
of size n.
Finally, the average-case complexity of the algorithm is the
function defined by the average number of steps taken on any
instance of size n.
UNIT-III
Thank You