SlideShare a Scribd company logo
Data Structures and Algorithms Lecture # 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 by factors ranging from 2 to 3.
Maintainable 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
Abstraction . Abstraction is a technique in which we construct a  model of an entity based upon its essential Characteristics while ignoring the inessential details. . The principle of abstraction also helps in handling the inherent complexity of a system by allowing looking  at its important external characteristic, and hiding its inner complexity at the same time. . Hiding the internal details is called encapsulation. . Engineers of all fields, including computer science, have been practicing abstraction for mastering complexity.
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  1024 press any key to continue
Abstraction Vs. Implementation Two dimensional array User’s view (abstraction) System’s view (implementation) 54 345 106 0 99 82 76 22 64 38 2 3 6 3 5 1 54 345 106 0 99 82 76 22 64 38 2 3 6 3 5 1
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 criteria 1) 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 Algorithm found=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 Algorithm found=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 Algorithm 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;   } 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;   } Key=29 73 55 50 48 47 32 29 28 22 15 12 10 7 5 3 2 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
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 Does the program efficiently use primary and  secondary storage? Is the program’s running time acceptable for the task? Space Complexity: . The space complexity of a program is the measure   of the amount of memory that it needs to run to completion. 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 of numbers (Step count Table) 2n+3 Total 0 0 0 } 1 1 1 return temp; N n 1 temp+=list[i] ; n + 1 n + 1 1 for (i=0;i<n; i++)  0 0 0 int i ; 1 1 1 float temp=0; 0 0 0 { 0 0 0 Float sum (float list[],int n) Total Steps Frequency S/e Statement
Problems Determining 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 n 0 such that f(n)<= cg(n) for all values of n > = n 0 No matter what the value of c 1 , c 2 ,c 3  there will be an n beyond which the program with complexity c 3 n will be faster than the one with complexity c 1 n2+c 2 n Example: 1)  3n +3  = O(n) as 3n +3 <=4n for all n >=3  2)  10n 2  + 4n + 2 = O(n 2 ) as 10n 2  + 4n +2 <=11n 2 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(N 2 ) N = O(N 3 ) - Choose the smallest function g. Theorem:If f(n)=a m n m +…..a 1 n+a 0 , then f(n)=O(n m ) -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(n 2 ) swap (list[i], list[j])   //O(1).O(n)=O(n) } //O(n)+O(n 2 )+O(n)+O(n)= O(n 2 ) }
Big Oh (O) O(1) - constant O(log n) - logarithmic O(n) - linear O(n log n) - log linear O(n 2 ) - quadratic O(n 3 ) -  cubic O(2 n ) - exponential
Assignment# 1 Last 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++; What will be the value of x in terms of n after the following code is executed ? Give the time complexity of the above code in big Oh notation Prove the following i)  5n 2  – 6n = O(n 2 )  ii)  6*2 n  + n 2  = O (2 n )

More Related Content

PPTX
1. Fundamental Concept - Data Structures using C++ by Varsha Patil
PPTX
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
PPTX
3. Stack - Data Structures using C++ by Varsha Patil
PPTX
4. Recursion - Data Structures using C++ by Varsha Patil
PPT
Stacks in algorithems & data structure
PPTX
5. Queue - Data Structures using C++ by Varsha Patil
PPTX
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
PPTX
7. Tree - Data Structures using C++ by Varsha Patil
1. Fundamental Concept - Data Structures using C++ by Varsha Patil
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
3. Stack - Data Structures using C++ by Varsha Patil
4. Recursion - Data Structures using C++ by Varsha Patil
Stacks in algorithems & data structure
5. Queue - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
7. Tree - Data Structures using C++ by Varsha Patil

What's hot (20)

PPTX
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
PPTX
10. Search Tree - Data Structures using C++ by Varsha Patil
PPTX
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
PPTX
Data Structure
PPTX
6. Linked list - Data Structures using C++ by Varsha Patil
PPTX
8. Graph - Data Structures using C++ by Varsha Patil
PDF
Lecture-05-DSA
PDF
Data structure using c++
PPTX
14. Files - Data Structures using C++ by Varsha Patil
PDF
Ii pu cs practical viva voce questions
PPTX
Data Structure and Algorithms
PDF
Introduction to Data Structure
PPT
Data Structure In C#
PDF
UNIT I LINEAR DATA STRUCTURES – LIST
PPTX
Mca ii dfs u-1 introduction to data structure
PPTX
Bsc cs ii dfs u-1 introduction to data structure
PPTX
Datastructures using c++
PPT
Introduction of data structure
PDF
Elementary data structure
PDF
Data Structures
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
10. Search Tree - Data Structures using C++ by Varsha Patil
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
Data Structure
6. Linked list - Data Structures using C++ by Varsha Patil
8. Graph - Data Structures using C++ by Varsha Patil
Lecture-05-DSA
Data structure using c++
14. Files - Data Structures using C++ by Varsha Patil
Ii pu cs practical viva voce questions
Data Structure and Algorithms
Introduction to Data Structure
Data Structure In C#
UNIT I LINEAR DATA STRUCTURES – LIST
Mca ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structure
Datastructures using c++
Introduction of data structure
Elementary data structure
Data Structures
Ad

Viewers also liked (19)

PDF
Fundamentals of Data Structures in C++ - Ellis Horowitz, Sartaj Sahni
PPT
stacks in algorithems and data structure
PPT
Lec5
PPTX
Data structures and Big O notation
PPTX
Constructors and destructors
DOCX
Advanced data structures using c++ 3
PPTX
Structured query language functions
PPTX
Classes and objects1
PPTX
Structured query language constraints
PPS
PDF
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
PDF
Working with Cookies in NodeJS
PPTX
Stacks in c++
PPTX
Data types in c++
PPT
C++ data types
PPT
Kuliah1 Struktur Data V1.0
PPTX
Data file handling in c++
PPTX
Big o notation
Fundamentals of Data Structures in C++ - Ellis Horowitz, Sartaj Sahni
stacks in algorithems and data structure
Lec5
Data structures and Big O notation
Constructors and destructors
Advanced data structures using c++ 3
Structured query language functions
Classes and objects1
Structured query language constraints
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
Working with Cookies in NodeJS
Stacks in c++
Data types in c++
C++ data types
Kuliah1 Struktur Data V1.0
Data file handling in c++
Big o notation
Ad

Similar to Data Structure (20)

PPTX
Introduction to Data Structure and algorithm.pptx
PPTX
Ds12 140715025807-phpapp02
PPTX
Data structures using C
PPTX
Chapter 1 - Introduction to data structure.pptx
PPTX
VCE Unit 01 (2).pptx
PPT
Introduction to Algorithms
DOCX
3rd-Sem_CSE_Data-Structures and Applications.docx
PPT
Data structure and algorithm first chapter
PPTX
Bca ii dfs u-1 introduction to data structure
PPT
Data Structures and Algorithm Analysis
PDF
Data structure and Alogorithm analysis unit one
PPT
Chapter 1 & 2 - Introduction dhjgsdkjfsaf.ppt
PDF
DATA STRUCTURE AND ALGORITHM FULL NOTES
PPTX
9 big o-notation
PPTX
Chapter 1 Data structure _Algorithms.pptx
PDF
Introduction to data structure and algorithm
PPTX
Introduction to data structures and its types
DOCX
PPTX
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Introduction to Data Structure and algorithm.pptx
Ds12 140715025807-phpapp02
Data structures using C
Chapter 1 - Introduction to data structure.pptx
VCE Unit 01 (2).pptx
Introduction to Algorithms
3rd-Sem_CSE_Data-Structures and Applications.docx
Data structure and algorithm first chapter
Bca ii dfs u-1 introduction to data structure
Data Structures and Algorithm Analysis
Data structure and Alogorithm analysis unit one
Chapter 1 & 2 - Introduction dhjgsdkjfsaf.ppt
DATA STRUCTURE AND ALGORITHM FULL NOTES
9 big o-notation
Chapter 1 Data structure _Algorithms.pptx
Introduction to data structure and algorithm
Introduction to data structures and its types
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx

Recently uploaded (20)

PPTX
master seminar digital applications in india
PPTX
Cell Types and Its function , kingdom of life
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
Computing-Curriculum for Schools in Ghana
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PPTX
UNIT III MENTAL HEALTH NURSING ASSESSMENT
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Lesson notes of climatology university.
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
RMMM.pdf make it easy to upload and study
master seminar digital applications in india
Cell Types and Its function , kingdom of life
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
Complications of Minimal Access Surgery at WLH
Computing-Curriculum for Schools in Ghana
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Final Presentation General Medicine 03-08-2024.pptx
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
LDMMIA Reiki Yoga Finals Review Spring Summer
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
UNIT III MENTAL HEALTH NURSING ASSESSMENT
Weekly quiz Compilation Jan -July 25.pdf
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Microbial disease of the cardiovascular and lymphatic systems
Lesson notes of climatology university.
Supply Chain Operations Speaking Notes -ICLT Program
Final Presentation General Medicine 03-08-2024.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
Microbial diseases, their pathogenesis and prophylaxis
RMMM.pdf make it easy to upload and study

Data Structure

  • 1. Data Structures and Algorithms Lecture # 1 Book: Fundamentals of Data Structures in c++ Horwitz, Sahani, and Mehta
  • 2. 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 by factors ranging from 2 to 3.
  • 3. Maintainable 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
  • 4. Abstraction . Abstraction is a technique in which we construct a model of an entity based upon its essential Characteristics while ignoring the inessential details. . The principle of abstraction also helps in handling the inherent complexity of a system by allowing looking at its important external characteristic, and hiding its inner complexity at the same time. . Hiding the internal details is called encapsulation. . Engineers of all fields, including computer science, have been practicing abstraction for mastering complexity.
  • 5. Types of data Abstraction . Code and Data abstraction What is Data ? - What is code ?
  • 6. Advantages of data Abstraction Simplification of software development Testing and Debugging Reusability Modification to representation of a data type etc
  • 7. 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; } }
  • 8. 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]); } }
  • 9. 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; } }
  • 10. 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
  • 11. 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
  • 12. Abstraction Vs. Implementation X -> 01000001 01000010 01000011 00000000 X = ? If x is CString -then x -> “ABC” If x is integer - then x -> 1094861568
  • 13. 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; }
  • 14. 1024 1.43493e-042 1024 1024 press any key to continue
  • 15. Abstraction Vs. Implementation Two dimensional array User’s view (abstraction) System’s view (implementation) 54 345 106 0 99 82 76 22 64 38 2 3 6 3 5 1 54 345 106 0 99 82 76 22 64 38 2 3 6 3 5 1
  • 16. 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.
  • 17. 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.
  • 18. 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 ? )
  • 19. 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 criteria 1) 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)
  • 20. Data Structure Operations Traversing Searching Inserting Deleting Sorting Merging Recursion To perform operations on various data structures we use algorithms.
  • 21. 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
  • 22. 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 Algorithm found=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;
  • 23. problem: Determine if a key is present in a collection of 10 integers Organization 2: Data are stored in an array of 10 elements Algorithm found=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.
  • 24. 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 Algorithm 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; } Average number of comparisons ? Order of “log(N)” as compared to N.
  • 25. 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; } Key=29 73 55 50 48 47 32 29 28 22 15 12 10 7 5 3 2 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
  • 26. 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?
  • 27. Performance Analysis Does the program efficiently use primary and secondary storage? Is the program’s running time acceptable for the task? Space Complexity: . The space complexity of a program is the measure of the amount of memory that it needs to run to completion. Time Complexity: . The time complexity of a program is the measure of the amount of computer time it needs to run to completion.
  • 28. 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.
  • 29. 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;
  • 30. 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
  • 31. Example 1 – Summing of a list of numbers (Step count Table) 2n+3 Total 0 0 0 } 1 1 1 return temp; N n 1 temp+=list[i] ; n + 1 n + 1 1 for (i=0;i<n; i++) 0 0 0 int i ; 1 1 1 float temp=0; 0 0 0 { 0 0 0 Float sum (float list[],int n) Total Steps Frequency S/e Statement
  • 32. Problems Determining 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
  • 33. Big Oh (O) Big Oh is defined as: F(n)=O(g(n)) iff there exists positive constants c and n 0 such that f(n)<= cg(n) for all values of n > = n 0 No matter what the value of c 1 , c 2 ,c 3 there will be an n beyond which the program with complexity c 3 n will be faster than the one with complexity c 1 n2+c 2 n Example: 1) 3n +3 = O(n) as 3n +3 <=4n for all n >=3 2) 10n 2 + 4n + 2 = O(n 2 ) as 10n 2 + 4n +2 <=11n 2 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(N 2 ) N = O(N 3 ) - Choose the smallest function g. Theorem:If f(n)=a m n m +…..a 1 n+a 0 , then f(n)=O(n m ) -Choose the largest term in the polynomial
  • 35. 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)
  • 36. 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)
  • 37. void swap(int &a, int &b) { int temp=a; a=b; b=temp; } // O (1)
  • 38. 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(n 2 ) swap (list[i], list[j]) //O(1).O(n)=O(n) } //O(n)+O(n 2 )+O(n)+O(n)= O(n 2 ) }
  • 39. Big Oh (O) O(1) - constant O(log n) - logarithmic O(n) - linear O(n log n) - log linear O(n 2 ) - quadratic O(n 3 ) - cubic O(2 n ) - exponential
  • 40. Assignment# 1 Last 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++; What will be the value of x in terms of n after the following code is executed ? Give the time complexity of the above code in big Oh notation Prove the following i) 5n 2 – 6n = O(n 2 ) ii) 6*2 n + n 2 = O (2 n )