unit iii : introduction to data structures and analysis of algorithm 10/8/2015 2 objective : 1.to...

30

Upload: dale-butler

Post on 12-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/2015 2 Objective : 1.To understand primitive storage structures and types 2.To
Page 2: Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/2015 2 Objective : 1.To understand primitive storage structures and types 2.To

Unit III : Introduction To Data Structures

and Analysis Of Algorithm

04/21/232

Objective : 1.To understand primitive storage structures and types2.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.

Contents : Introduction to Data Structures: Concept of data, Data object, Data structure, Abstract Data Types, realization of ADT in 'C'. Concept of Primitive and non-primitive, 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.

Page 3: Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/2015 2 Objective : 1.To understand primitive storage structures and types 2.To

Contents

1.Basic concepts Data Data object Data structure Abstract data type

Page 4: Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/2015 2 Objective : 1.To understand primitive storage structures and types 2.To

Contents

2.Types Of Data Structure Primitive and Non-Primitive Linear and Non-Linear Static and Dynamic Persistent and Ephemeral

Page 5: Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/2015 2 Objective : 1.To understand primitive storage structures and types 2.To

Contents

3.Analysis of Algorithm Frequency Count Time and Space Complexity of Algorithm Asymptotic notation Best case, worst case and average case analysis

Page 6: Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/2015 2 Objective : 1.To understand primitive storage structures and types 2.To

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.

Page 7: Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/2015 2 Objective : 1.To understand primitive storage structures and types 2.To

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.

Page 8: Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/2015 2 Objective : 1.To understand primitive storage structures and types 2.To

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.

Page 9: Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/2015 2 Objective : 1.To understand primitive storage structures and types 2.To

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.

Page 10: Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/2015 2 Objective : 1.To understand primitive storage structures and types 2.To

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).

Page 11: Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/2015 2 Objective : 1.To understand primitive storage structures and types 2.To

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.

Page 12: Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/2015 2 Objective : 1.To understand primitive storage structures and types 2.To

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.

Page 13: Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/2015 2 Objective : 1.To understand primitive storage structures and types 2.To

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

Page 14: Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/2015 2 Objective : 1.To understand primitive storage structures and types 2.To

Classification of Data Structure In Non- Linear data structures this each element

can have more than one successor.

e.g.

Tree Graph

Page 15: Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/2015 2 Objective : 1.To understand primitive storage structures and types 2.To

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

Page 16: Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/2015 2 Objective : 1.To understand primitive storage structures and types 2.To

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.

Page 17: Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/2015 2 Objective : 1.To understand primitive storage structures and types 2.To

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

Page 18: Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/2015 2 Objective : 1.To understand primitive storage structures and types 2.To

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

Page 19: Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/2015 2 Objective : 1.To understand primitive storage structures and types 2.To

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.

Page 20: Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/2015 2 Objective : 1.To understand primitive storage structures and types 2.To

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.

Page 21: Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/2015 2 Objective : 1.To understand primitive storage structures and types 2.To

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 Frequency Total steps

float sum(float list[ ], int n) float tempsum = 0; int i; for(i=0; i <n; i++)

tempsum += list[i]; return tempsum;

0 0 0 0 0 0 1 1 1 0 0 0 1 n+1 n+1 1 n n 1 1 1 0 0 0

Total 2n+3

Page 22: Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/2015 2 Objective : 1.To understand primitive storage structures and types 2.To

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)

Page 23: Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/2015 2 Objective : 1.To understand primitive storage structures and types 2.To

Asymptotic notations:A formal way of representing functions in different

Classes.

3 Asymptotic notations:

1.Θ-notation

2.Ω notation

3.O-natation

Page 24: Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/2015 2 Objective : 1.To understand primitive storage structures and types 2.To

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.

Page 25: Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/2015 2 Objective : 1.To understand primitive storage structures and types 2.To

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

Page 26: Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/2015 2 Objective : 1.To understand primitive storage structures and types 2.To

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

Page 27: Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/2015 2 Objective : 1.To understand primitive storage structures and types 2.To

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

1<log 2 N <N < N log2 N <N 2<N 3<2N <3N

Order :

Page 28: Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/2015 2 Objective : 1.To understand primitive storage structures and types 2.To

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)

Page 29: Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/2015 2 Objective : 1.To understand primitive storage structures and types 2.To

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.

Page 30: Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/2015 2 Objective : 1.To understand primitive storage structures and types 2.To

UNIT-III

Thank You