Lec 1 Ds

Preview:

Citation preview

Data Structures and AlgorithmsLecture # 1

Book: Fundamentals of Data Structures in c++

Horwitz, Sahani, and Mehta

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.

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

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.

Types of data Abstraction

. Code and Data abstraction

– What is Data ?

- What is code ?

Advantages of data Abstraction

• Simplification of software development

• Testing and Debugging

• Reusability• Modification to representation of a data

type

• etc

• 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;}

}

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]);

}}

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;

}

}

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

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

Abstraction Vs.Implementation

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

• If x is CString

-then x -> “ABC”

• If x is integer

- then x -> 1094861568

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;

}

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

Abstraction Vs. ImplementationTwo dimensional array

543451060

99827622

643823

6351

User’s view (abstraction)

54

3451060998276226438236351

System’s view (implementation)

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.

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.

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

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)

Data Structure Operations

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

we use algorithms.

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

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;

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 so

both algorithms are equally efficient (or in efficient)• Organization 2 is better because it yields an algorithms which is

more maintainable. For example, if the collection contains 100 elements. In general, number of elements could be N.

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.

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;

}

7355504847322928221512107532

1514131211109876543210

Key=29

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?

Performance Analysis

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

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

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

completion.9. Time Complexity:

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

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.

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;

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

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

2n+3 Total

000}

111 return temp;

Nn1 temp+=list[i] ;

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

000 int i ;

111 float temp=0;

000{

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

Total StepsFrequencyS/eStatement

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

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

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

growth

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)

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)

void swap(int &a, int &b)

{

int temp=a;

a=b;

b=temp;

}

// O (1)

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)

}

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

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++;g) What will be the value of x in terms of n after the following code is

executed ?h) 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)