Data Structure

40
Data Structures and Algorithms Lecture # 1 Book: Fundamentals of Data Structures in c++ Horwitz, Sahani, and Mehta

Transcript of Data Structure

Page 1: Data Structure

Data Structures and AlgorithmsLecture # 1

Book: Fundamentals of Data Structures in c++

Horwitz, Sahani, and Mehta

Page 2: Data Structure

Software Design Quality

• What is good design? - Is it efficient code?

- compact implementation? - Most maintainable?

. For most large, long life time software systems,maintenance cost normally exceeds development cost byfactors ranging from 2 to 3.

Page 3: Data Structure

Maintainable DesignMaintainable Design

• Cost of system changes is minimal

• Readily adaptable to modify existing functionality and enhance functionality

• Design is understandable

• Changes should be local in effect

• Design should be modular

Page 4: Data Structure

AbstractionAbstraction. Abstraction is a technique in which we construct a

model of an entity based upon its essentialCharacteristics while ignoring the inessential details.

. The principle of abstraction also helps in handling theinherent complexity of a system by allowing looking at its important external characteristic, and hiding itsinner complexity at the same time.

. Hiding the internal details is called encapsulation.

. Engineers of all fields, including computer science,have been practicing abstraction for masteringcomplexity.

Page 5: Data Structure

Types of data Abstraction

. Code and Data abstraction

– What is Data ?

- What is code ?

Page 6: Data Structure

Advantages of data Abstraction

• Simplification of software development

• Testing and Debugging

• Reusability

• Modification to representation of a data type

• etc

Page 7: Data Structure

• void selectionSort(int a[],int size){

int I,j,min,temp;for(i=0; i<size-1; i++){

min=i; for(j=i; i<size; j++)

{if(a[j]< a[min])

min=j;}temp=a[i];a[i]=a[min];

a[min]=temp;}

}

Page 8: Data Structure

int minimum(int a[],int from,int to) void swap(int &x, int &y){ {

int min=from; int temp=x;for(int i=from;i<=to;i++) x=y; if(a[i] < a[min]) min=i; y=temp;return min;

} }

void selectionSort(int a[],int size){

int i,j,min;for(i=0;i<size-1;i++){

min=minimum(a,I,size-1)swap(a[i],a[min]);

}}

Page 9: Data Structure

void selectionSort(int a[], int size)

{

int I,j,min,temp;

for(i=0;i<size-1;i++)

{

min=i; void selectionSort(int a[],int size)

for(j=i;j<size; j++) {

{ int i,j,min;

if(a[j]< a[min]) for(i=0;i<size-1;i++) {

min=j; min=minimum(a,i,size-1);

} swap(a[i],a[min]);

temp=a[i]; }

a[i]=a[min]; }

a[min]=temp;

}

}

Page 10: Data Structure

Data Abstraction and Abstract Data Types(ADT)

• A data type is a template for objects and a set of

operations that define the behavior of the objects (or

instances) of that type.• An Abstract data type (ADT) is a data type in which

the implementation details are hidden and the user is

concerned with the properties ( or behavior ) of that

type.• An ADT has two commponents:

- Interface – the behavior

- Implementation• Example:

-int,float

Page 11: Data Structure

Abstract Data Type

• The data structures used to

implement the data type can only

be accessed through the interface.• Any change in the implementation

does not change the user

programs as long as the interface

remains the same.• This is also known as data

encapsulation or data abstraction.

implementation

interface1 interfece2

interface3 interface4

Page 12: Data Structure

Abstraction Vs.Implementation

• X -> 01000001 01000010 01000011 00000000• X = ?

• If x is CString

-then x -> “ABC”• If x is integer

- then x -> 1094861568

Page 13: Data Structure

int main(){

int i, *pi;float f, *pf;

i = 1024;pi = &i;

pf = (float *) pi ;

f = *pf;cout << i << “ “ <<f<<“\n”;

f = i ;cout << i << “ “ <<f<<“\n”;

return 0;

}

Page 14: Data Structure

1024 1.43493e-042 1024 1024press any key to continue

Page 15: Data Structure

Abstraction Vs. ImplementationTwo dimensional array

1 5 3 6

3 2 38 64

22 76 82 99

0 106 345 54

User’s view (abstraction)

1 5 3 6 3 2 38 64 22 76 82 99 0 106 345 54

System’s view (implementation)

Page 16: Data Structure

ADTs and C++ Classes

• A class is used to define (and implement) an ADT

in C++.• A class consists of data and functions that operate

on that data.• A class in C++ has two parts – public and private

(let’s ignore the protected members for now).• The data and functions defined in the class are

called the members of the class.

Page 17: Data Structure

ADTs and C++ Classes

• Users of the class can only access and

manipulate the class state through the public

members of the class.• Private members can only be used by other

members of the class (let’s also ignore the friends for now).

• Data encapsulation is achieved by declaring

all data members of a class to be private.• The interface of the class is provided through

the use of public member functions.

Page 18: Data Structure

Data Structures

• The primary objective of programming is to efficiently

process the input to generate the desired output.• We can achieve this objective in an efficient and neat

style if the input data is organized in a way to help us

meet our goal.• Data Structures is nothing but ways and means of

organizing data so that it can be processed easily and

efficiently.• Data structures dictate the manner in which the data

can be processed. In other words, the choice of an

algorithm depends upon the underlying data

organization. ( What is an Algorithm ? )

Page 19: Data Structure

What is an Algorithm ?

• An algorithm is a well defined list of steps for solving a particular problem

• An algorithm manipulates the data in data structures in various ways, such as inserting a new element, searching for a particular item etc.

• An algorithm must satisfy the following criteria1) Input 2) output 3) Definiteness ( each

instruction should be clear and unambiguous) 4) Fitness (terminates after finite number of steps) 5) Effectiveness (each instruction must be feasible enough)

Page 20: Data Structure

Data Structure Operations

• Traversing• Searching• Inserting• Deleting• Sorting• Merging• Recursion• To perform operations on various data structures

we use algorithms.

Page 21: Data Structure

Types of Data Structures

• Premitive/Scalar : data types that can be manipulated as a single quantity or can be represented alone

• Structured/ Non-Premitive (Data type which is collection of other premitive or non-premitive data structures.– Can be further divided into

a) linear b) non-linear- Linear can be further split into a) physically linear b) logically linear

Page 22: Data Structure

problem: Determine if a key is present in a collection of 10 integers

Organization 1: Data are stored in 10 disjoint ( as opposed to

composite ) variables: A0, A2, A3,……,A9

Algorithmfound=false;

if (key = = A0 ) found = true;

else if (key = = A1 ) found = true;

else if (key = = A2 ) found = true;

else if (key = = A3 ) found = true;

else if (key = = A4 ) found = true;

else if (key = = A5 ) found = true;

else if (key = = A6 ) found = true;

else if (key = = A7 ) found = true;

else if (key = = A8 ) found = true;

else if (key = = A9) found = true;

Page 23: Data Structure

problem: Determine if a key is present in a collection of 10 integers

Organization 2: Data are stored in an array of 10 elements

Algorithmfound=false;

for (int i =0; i < 10; i ++) if ( A[i] == key) {

found = true;break;

}

• Average number of comparisons in both cases is the same soboth algorithms are equally efficient (or in efficient)

• Organization 2 is better because it yields an algorithms which ismore maintainable. For example, if the collection contains 100 elements. In general, number of elements could be N.

Page 24: Data Structure

problem: Determine if a key is present in a collection of 10 integers

Organization 3: Data are stored in an array A in ascending order

Algorithmfound=false;

while (( ! Found) && (low<= high))

{

mid = (low + high)/2;

if( A[mid]==key) found=true;

else if ( A[mid] > key) high = mid – 1;

else low = mid +1;

}

• Average number of comparisons ?• Order of “log(N)” as compared to N.

Page 25: Data Structure

found=false;

while (( ! Found) && (low<= high))

{

mid = (low + high)/2;

if( A[mid]==key) found=true;

else if ( A[mid] > key) high = mid – 1;

else low = mid +1;

}

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

2 3 5 7 10 12 15 22 28 29 32 47 48 50 55 73

Key=29

Page 26: Data Structure

Performance Comparison

• NADRA database: ~80,000 records

• Computer which can perform 10,000

comparisons per second- Linear Search: ~2.22 hours

- Binary Search: ~0.005 seconds

- Roughly 1.6 million times less

• Why?

Page 27: Data Structure

Performance Analysis

1. Does the program efficiently use primary and secondary storage?

2. Is the program’s running time acceptable forthe task?

3. Space Complexity:. The space complexity of a program is the measure of the amount of memory that it needs to run to

completion.4. Time Complexity:

. The time complexity of a program is the measure of the amount of computer time it needs to run to completion.

Page 28: Data Structure

Performance Estimation

• How to determine which algorithm is

better?

• We need some mechanism to predict

the performance without actually

executing the program.

• Mechanism should be independent of

the compiler and underlying hardware.

Page 29: Data Structure

Step Count

• Program Step: A program step is a

meaningful program segment.

• We can consider each statement as a

single step.a = 2;

a = 2 * b + c + 3 * c / d – e;

Page 30: Data Structure

Step Count

To count total number of steps, we must

determine:1. Step count for each statement –

steps/execution or s/e

2. Frequency of each statement

3. Total steps for each statement

4. Finally sum these counts to get the total step

count

Page 31: Data Structure

Example 1 – Summing of a list ofnumbers (Step count Table)

Statement S/e Frequency Total Steps

Float sum (float list[],int n) 0 0 0

{ 0 0 0

float temp=0; 1 1 1

int i ; 0 0 0

for (i=0;i<n; i++) 1 n + 1 n + 1

temp+=list[i] ; 1 n N

return temp; 1 1 1

} 0 0 0

Total 2n+3

Page 32: Data Structure

ProblemsDetermining the exact step count of a program can be a

Very difficult task

- inexactness of the definition of a step, exact step count is not very

useful for - comparative purposes.e.g. which one is better 45n+3 or 100n+10

- Frequency of execution

. How many steps are executed?

if (condition)

{

step1;step2step3;step4;step5;

}

else

step6;

We need some asymptotic notation as a measure of growth

Page 33: Data Structure

Big Oh (O)

Big Oh is defined as:

F(n)=O(g(n)) iff there exists positive constants c and n0

such that f(n)<= cg(n) for all values of n > = n0

• No matter what the value of c1, c2,c3 there will be an n

beyond which the program with complexity c3n will be faster than the one with complexity c1n2+c2n

Example: 1) 3n +3 = O(n) as 3n +3 <=4n for all n >=3

2) 10n2 + 4n + 2 = O(n2) as 10n2 + 4n +2 <=11n2

• An estimate of how will the running time grow as a function

of the problem size.

• There are infinitely many functions g for a given function f:

- N = O(N); N = O(N2) N = O(N3)

- Choose the smallest function g.

• Theorem:If f(n)=amnm+…..a1n+a0, then f(n)=O(nm)

-Choose the largest term in the polynomial

Page 34: Data Structure

----------------------n

growth

Page 35: Data Structure

Big Oh (O)

• Summing of list of numbers O(n)

• Matrix addition O(rows,cols)

• Searching a key in an array O(n)

• Binary Search O(n log n)

Page 36: Data Structure

Big Oh (O)

int search_min(int list[],int from, int to){

int I; int min=from;for ( i=from;I <= to; i++)

If (list[i] < list[min]) min=I;return min;

}// O(n)

Page 37: Data Structure

void swap(int &a, int &b)

{

int temp=a;

a=b;

b=temp;

}

// O (1)

Page 38: Data Structure

Big Oh (O)

void Selection_sort(int list[], int size)

{int I,j;for(i=0;i<size-1;i++){ //O(size) or O(n)

j= search_min(list, i+1 , size-1) //O(n).O(n)=O(n2)

swap (list[i], list[j]) //O(1).O(n)=O(n)

}//O(n)+O(n2)+O(n)+O(n)= O(n2)

}

Page 39: Data Structure

Big Oh (O)

• O(1) - constant

• O(log n) - logarithmic

• O(n) - linear

• O(n log n)- log linear

• O(n2) - quadratic

• O(n3) - cubic

• O(2n) - exponential

Page 40: Data Structure

Assignment# 1Last Submission Day : 07-09-2009 (Monday)

• Given the following code segment int x=0; for (int i= 1; i<=n; i++)

for (int j = 1; j<=I; j++) for (int k = 1; k<=j; k++)

x++;a) What will be the value of x in terms of n after the following code is

executed ?b) Give the time complexity of the above code in big Oh notation• Prove the following i) 5n2 – 6n = O(n2) ii) 6*2n + n2 = O (2n)