Lec 1 Ds


Published on

Published in: Business, Technology
  • Be the first to comment

  • Be the first to like this

Lec 1 Ds

  1. 1. Data Structures and Algorithms Lecture # 1 Book: Fundamentals of Data Structures in c++ Horwitz, Sahani, and Mehta
  2. 2. Software Design Quality <ul><li>What is good design? </li></ul><ul><li>- Is it efficient code? </li></ul><ul><li> - compact implementation? </li></ul><ul><li> - Most maintainable? </li></ul><ul><li> . For most large, long life time software systems, </li></ul><ul><li>maintenance cost normally exceeds development cost by </li></ul><ul><li>factors ranging from 2 to 3. </li></ul><ul><li> </li></ul>
  3. 3. Maintainable Design <ul><li>Cost of system changes is minimal </li></ul><ul><li>Readily adaptable to modify existing functionality and enhance functionality </li></ul><ul><li>Design is understandable </li></ul><ul><li>Changes should be local in effect </li></ul><ul><li>Design should be modular </li></ul>
  4. 4. Abstraction <ul><ul><li>. Abstraction is a technique in which we construct a </li></ul></ul><ul><ul><li>model of an entity based upon its essential </li></ul></ul><ul><ul><li>Characteristics while ignoring the inessential details. </li></ul></ul><ul><ul><li>. The principle of abstraction also helps in handling the </li></ul></ul><ul><ul><li>inherent complexity of a system by allowing looking </li></ul></ul><ul><ul><li>at its important external characteristic, and hiding its </li></ul></ul><ul><ul><li>inner complexity at the same time. </li></ul></ul><ul><ul><li>. Hiding the internal details is called encapsulation. </li></ul></ul><ul><ul><li>. Engineers of all fields, including computer science, </li></ul></ul><ul><ul><li>have been practicing abstraction for mastering </li></ul></ul><ul><ul><li>complexity. </li></ul></ul>
  5. 5. Types of data Abstraction <ul><ul><li>. Code and Data abstraction </li></ul></ul><ul><ul><li>What is Data ? </li></ul></ul><ul><li>- What is code ? </li></ul>
  6. 6. Advantages of data Abstraction <ul><li>Simplification of software development </li></ul><ul><li>Testing and Debugging </li></ul><ul><li>Reusability </li></ul><ul><li>Modification to representation of a data type </li></ul><ul><li>etc </li></ul>
  7. 7. <ul><li>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; } } </li></ul>
  8. 8. <ul><li>int minimum(int a[],int from,int to) void swap(int &x, int &y) </li></ul><ul><li>{ { </li></ul><ul><li>int min=from; int temp=x; </li></ul><ul><li>for(int i=from;i<=to;i++) x=y; </li></ul><ul><li> if(a[i] < a[min]) min=i; y=temp; </li></ul><ul><li>return min; </li></ul><ul><li>} } </li></ul><ul><li>void selectionSort(int a[],int size) </li></ul><ul><li>{ </li></ul><ul><li>int i,j,min; </li></ul><ul><li>for(i=0;i<size-1;i++){ </li></ul><ul><li>min=minimum(a,I,size-1) </li></ul><ul><li>swap(a[i],a[min]); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  9. 9. <ul><li>void selectionSort(int a[], int size) </li></ul><ul><li>{ </li></ul><ul><li>int I,j,min,temp; </li></ul><ul><li>for(i=0;i<size-1;i++) </li></ul><ul><li>{ </li></ul><ul><li>min=i; void selectionSort(int a[],int size) </li></ul><ul><li>for(j=i;j<size; j++) { </li></ul><ul><li>{ int i,j,min; </li></ul><ul><li> if(a[j]< a[min]) for(i=0;i<size-1;i++) { </li></ul><ul><li>min=j; min=minimum(a,i,size-1); </li></ul><ul><li>} swap(a[i],a[min]); </li></ul><ul><li>temp=a[i]; } </li></ul><ul><li>a[i]=a[min]; } </li></ul><ul><li>a[min]=temp; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  10. 10. Data Abstraction and Abstract Data Types(ADT) <ul><li>A data type is a template for objects and a set of </li></ul><ul><li>operations that define the behavior of the objects (or </li></ul><ul><li>instances) of that type. </li></ul><ul><li>An Abstract data type (ADT) is a data type in which </li></ul><ul><li>the implementation details are hidden and the user is </li></ul><ul><li>concerned with the properties ( or behavior ) of that </li></ul><ul><li>type. </li></ul><ul><li>An ADT has two commponents: </li></ul><ul><li> - Interface – the behavior </li></ul><ul><li> - Implementation </li></ul><ul><li>Example: </li></ul><ul><li>-int,float </li></ul>
  11. 11. Abstract Data Type <ul><li>The data structures used to </li></ul><ul><li>implement the data type can only </li></ul><ul><li>be accessed through the interface. </li></ul><ul><li>Any change in the implementation </li></ul><ul><li>does not change the user </li></ul><ul><li>programs as long as the interface </li></ul><ul><li>remains the same. </li></ul><ul><li>This is also known as data </li></ul><ul><li>encapsulation or data abstraction. </li></ul>implementation interface1 interfece2 interface3 interface4
  12. 12. Abstraction Vs. Implementation <ul><li>X -> 01000001 01000010 01000011 00000000 </li></ul><ul><li>X = ? </li></ul><ul><li>If x is CString </li></ul><ul><li>-then x -> “ABC” </li></ul><ul><li>If x is integer </li></ul><ul><li>- then x -> 1094861568 </li></ul>
  13. 13. <ul><li>int main() </li></ul><ul><li>{ </li></ul><ul><li>int i, *pi; </li></ul><ul><li>float f, *pf; </li></ul><ul><li>i = 1024; </li></ul><ul><li>pi = &i; </li></ul><ul><li>pf = (float *) pi ; </li></ul><ul><li>f = *pf; </li></ul><ul><li>cout << i << “ “ <<f<<“ ”; </li></ul><ul><li>f = i ; </li></ul><ul><li>cout << i << “ “ <<f<<“ ”; </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul>
  14. 14. <ul><li>1024 1.43493e-042 </li></ul><ul><li> 1024 1024 </li></ul><ul><li>press any key to continue </li></ul>
  15. 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. 16. ADTs and C++ Classes <ul><li>A class is used to define (and implement) an ADT </li></ul><ul><li>in C++. </li></ul><ul><li>A class consists of data and functions that operate </li></ul><ul><li>on that data. </li></ul><ul><li>A class in C++ has two parts – public and private </li></ul><ul><li>(let’s ignore the protected members for now). </li></ul><ul><li>The data and functions defined in the class are </li></ul><ul><li>called the members of the class. </li></ul>
  17. 17. ADTs and C++ Classes <ul><li>Users of the class can only access and </li></ul><ul><li>manipulate the class state through the public </li></ul><ul><li>members of the class. </li></ul><ul><li>Private members can only be used by other </li></ul><ul><li>members of the class (let’s also ignore the friends for now). </li></ul><ul><li>Data encapsulation is achieved by declaring </li></ul><ul><li>all data members of a class to be private. </li></ul><ul><li>The interface of the class is provided through </li></ul><ul><li>the use of public member functions. </li></ul>
  18. 18. Data Structures <ul><li>The primary objective of programming is to efficiently </li></ul><ul><li>process the input to generate the desired output. </li></ul><ul><li>We can achieve this objective in an efficient and neat </li></ul><ul><li>style if the input data is organized in a way to help us </li></ul><ul><li>meet our goal. </li></ul><ul><li>Data Structures is nothing but ways and means of </li></ul><ul><li>organizing data so that it can be processed easily and </li></ul><ul><li>efficiently. </li></ul><ul><li>Data structures dictate the manner in which the data </li></ul><ul><li>can be processed. In other words, the choice of an </li></ul><ul><li>algorithm depends upon the underlying data </li></ul><ul><li>organization. ( What is an Algorithm ? ) </li></ul>
  19. 19. What is an Algorithm ? <ul><li>An algorithm is a well defined list of steps for solving a particular problem </li></ul><ul><li>An algorithm manipulates the data in data structures in various ways, such as inserting a new element, searching for a particular item etc. </li></ul><ul><li>An algorithm must satisfy the following criteria </li></ul><ul><li>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) </li></ul>
  20. 20. Data Structure Operations <ul><li>Traversing </li></ul><ul><li>Searching </li></ul><ul><li>Inserting </li></ul><ul><li>Deleting </li></ul><ul><li>Sorting </li></ul><ul><li>Merging </li></ul><ul><li>Recursion </li></ul><ul><li>To perform operations on various data structures we use algorithms. </li></ul>
  21. 21. Types of Data Structures <ul><li>Premitive/Scalar : data types that can be manipulated as a single quantity or can be represented alone </li></ul><ul><li>Structured/ Non-Premitive (Data type which is collection of other premitive or non-premitive data structures. </li></ul><ul><ul><li>Can be further divided into </li></ul></ul><ul><li>a) linear b) non-linear </li></ul><ul><li>- Linear can be further split into </li></ul><ul><li>a) physically linear b) logically linear </li></ul>
  22. 22. <ul><li>problem: Determine if a key is present in a collection of 10 integers </li></ul><ul><li> Organization 1: Data are stored in 10 disjoint ( as opposed to </li></ul><ul><li> composite ) variables: A0, A2, A3,……,A9 </li></ul><ul><li>Algorithm </li></ul><ul><li>found=false; </li></ul><ul><li>if (key = = A0 ) found = true; </li></ul><ul><li>else if (key = = A1 ) found = true; </li></ul><ul><li>else if (key = = A2 ) found = true; </li></ul><ul><li>else if (key = = A3 ) found = true; </li></ul><ul><li>else if (key = = A4 ) found = true; </li></ul><ul><li>else if (key = = A5 ) found = true; </li></ul><ul><li>else if (key = = A6 ) found = true; </li></ul><ul><li>else if (key = = A7 ) found = true; </li></ul><ul><li>else if (key = = A8 ) found = true; </li></ul><ul><li>else if (key = = A9) found = true; </li></ul>
  23. 23. <ul><li>problem: Determine if a key is present in a collection of 10 integers </li></ul><ul><li> Organization 2: Data are stored in an array of 10 elements </li></ul><ul><li>Algorithm </li></ul><ul><li>found=false; </li></ul><ul><li>for (int i =0; i < 10; i ++) </li></ul><ul><li> if ( A[i] == key) </li></ul><ul><li> { </li></ul><ul><li>found = true; </li></ul><ul><li>break; </li></ul><ul><li> } </li></ul><ul><li>Average number of comparisons in both cases is the same so </li></ul><ul><li>both algorithms are equally efficient (or in efficient) </li></ul><ul><li>Organization 2 is better because it yields an algorithms which is </li></ul><ul><li>more maintainable. For example, if the collection contains 100 elements. In general, number of elements could be N. </li></ul>
  24. 24. <ul><li>problem: Determine if a key is present in a collection of 10 integers </li></ul><ul><li> Organization 3: Data are stored in an array A in ascending order </li></ul><ul><li>Algorithm </li></ul><ul><li>found=false; </li></ul><ul><li>while (( ! Found) && (low<= high)) </li></ul><ul><li> { </li></ul><ul><li>mid = (low + high)/2; </li></ul><ul><li>if( A[mid]==key) found=true; </li></ul><ul><li>else if ( A[mid] > key) high = mid – 1; </li></ul><ul><li>else low = mid +1; </li></ul><ul><li> } </li></ul><ul><li>Average number of comparisons ? </li></ul><ul><li>Order of “log(N)” as compared to N. </li></ul>
  25. 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. 26. Performance Comparison <ul><li>NADRA database: ~80,000 records </li></ul><ul><li>Computer which can perform 10,000 </li></ul><ul><li>comparisons per second </li></ul><ul><li>- Linear Search: ~2.22 hours </li></ul><ul><li>- Binary Search: ~0.005 seconds </li></ul><ul><li>- Roughly 1.6 million times less </li></ul><ul><li>Why? </li></ul>
  27. 27. Performance Analysis <ul><li>Does the program efficiently use primary and </li></ul><ul><li>secondary storage? </li></ul><ul><li>Is the program’s running time acceptable for </li></ul><ul><li>the task? </li></ul><ul><li>Space Complexity: </li></ul><ul><li>. The space complexity of a program is the measure </li></ul><ul><li> of the amount of memory that it needs to run to </li></ul><ul><li>completion. </li></ul><ul><li>Time Complexity: </li></ul><ul><li>. The time complexity of a program is the measure of </li></ul><ul><li> the amount of computer time it needs to run to </li></ul><ul><li> completion. </li></ul>
  28. 28. Performance Estimation <ul><li>How to determine which algorithm is </li></ul><ul><li>better? </li></ul><ul><li>We need some mechanism to predict </li></ul><ul><li>the performance without actually </li></ul><ul><li>executing the program. </li></ul><ul><li>Mechanism should be independent of </li></ul><ul><li>the compiler and underlying hardware. </li></ul>
  29. 29. Step Count <ul><li>Program Step: A program step is a </li></ul><ul><li>meaningful program segment. </li></ul><ul><li>We can consider each statement as a </li></ul><ul><li>single step. </li></ul><ul><li>a = 2; </li></ul><ul><li>a = 2 * b + c + 3 * c / d – e; </li></ul>
  30. 30. Step Count <ul><li>To count total number of steps, we must </li></ul><ul><li>determine: </li></ul><ul><li>1. Step count for each statement – </li></ul><ul><li> steps/execution or s/e </li></ul><ul><li>2. Frequency of each statement </li></ul><ul><li>3. Total steps for each statement </li></ul><ul><li>4. Finally sum these counts to get the total step </li></ul><ul><li>count </li></ul>
  31. 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. 32. Problems <ul><li>Determining the exact step count of a program can be a </li></ul><ul><li>Very difficult task </li></ul><ul><li>- inexactness of the definition of a step, exact step count is not very </li></ul><ul><li> useful for - comparative purposes.e.g. which one is better 45n+3 or 100n+10 </li></ul><ul><li>- Frequency of execution </li></ul><ul><li>. How many steps are executed? </li></ul><ul><li>if (condition) </li></ul><ul><li>{ </li></ul><ul><li>step1;step2step3;step4;step5; </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>step6; </li></ul>We need some asymptotic notation as a measure of growth
  33. 33. Big Oh (O) <ul><li>Big Oh is defined as: </li></ul><ul><li>F(n)=O(g(n)) iff there exists positive constants c and n 0 </li></ul><ul><li>such that f(n)<= cg(n) for all values of n > = n 0 </li></ul><ul><li>No matter what the value of c 1 , c 2 ,c 3 there will be an n </li></ul><ul><li>beyond which the program with complexity c 3 n will be faster than the one with complexity c 1 n2+c 2 n </li></ul><ul><li>Example: 1) 3n +3 = O(n) as 3n +3 <=4n for all n >=3 </li></ul><ul><li>2) 10n 2 + 4n + 2 = O(n 2 ) as 10n 2 + 4n +2 <=11n 2 </li></ul><ul><li>An estimate of how will the running time grow as a function </li></ul><ul><li>of the problem size. </li></ul><ul><li>There are infinitely many functions g for a given function f: </li></ul><ul><li>- N = O(N); N = O(N 2 ) N = O(N 3 ) </li></ul><ul><li>- Choose the smallest function g. </li></ul><ul><li>Theorem:If f(n)=a m n m +…..a 1 n+a 0 , then f(n)=O(n m ) </li></ul><ul><li>-Choose the largest term in the polynomial </li></ul>
  34. 34. ----------------------  n growth
  35. 35. Big Oh (O) <ul><li>Summing of list of numbers O(n) </li></ul><ul><li>Matrix addition O(rows,cols) </li></ul><ul><li>Searching a key in an array O(n) </li></ul><ul><li>Binary Search O(n log n) </li></ul>
  36. 36. Big Oh (O) <ul><li>int search_min(int list[],int from, int to) </li></ul><ul><li>{ </li></ul><ul><li>int I; int min=from; </li></ul><ul><li>for ( i=from;I <= to; i++) </li></ul><ul><li>If (list[i] < list[min]) min=I; </li></ul><ul><li>return min; </li></ul><ul><li>} </li></ul><ul><li>// O(n) </li></ul>
  37. 37. <ul><li>void swap(int &a, int &b) </li></ul><ul><li>{ </li></ul><ul><li>int temp=a; </li></ul><ul><li>a=b; </li></ul><ul><li>b=temp; </li></ul><ul><li>} </li></ul><ul><li>// O (1) </li></ul>
  38. 38. Big Oh (O) <ul><li>void Selection_sort(int list[], int size) </li></ul><ul><li>{ </li></ul><ul><li>int I,j; </li></ul><ul><li>for(i=0;i<size-1;i++) </li></ul><ul><li>{ //O(size) or O(n) </li></ul><ul><li>j= search_min(list, i+1 , size-1) </li></ul><ul><li> //O(n).O(n)=O(n 2 ) </li></ul><ul><li>swap (list[i], list[j]) </li></ul><ul><li> //O(1).O(n)=O(n) </li></ul><ul><li>} </li></ul><ul><li>//O(n)+O(n 2 )+O(n)+O(n)= O(n 2 ) </li></ul><ul><li>} </li></ul>
  39. 39. Big Oh (O) <ul><li>O(1) - constant </li></ul><ul><li>O(log n) - logarithmic </li></ul><ul><li>O(n) - linear </li></ul><ul><li>O(n log n) - log linear </li></ul><ul><li>O(n 2 ) - quadratic </li></ul><ul><li>O(n 3 ) - cubic </li></ul><ul><li>O(2 n ) - exponential </li></ul>
  40. 40. Assignment# 1 Last Submission Day : 07-09-2009 (Monday) <ul><li>Given the following code segment </li></ul><ul><li>int x=0; </li></ul><ul><li>for (int i= 1; i<=n; i++) </li></ul><ul><li>for (int j = 1; j<=I; j++) </li></ul><ul><li> for (int k = 1; k<=j; k++) </li></ul><ul><li>x++; </li></ul><ul><li>What will be the value of x in terms of n after the following code is executed ? </li></ul><ul><li>Give the time complexity of the above code in big Oh notation </li></ul><ul><li>Prove the following </li></ul><ul><li>i) 5n 2 – 6n = O(n 2 ) </li></ul><ul><li>ii) 6*2 n + n 2 = O (2 n ) </li></ul>