Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures Unit-1 Engineered for Tomorrow CSE, MVJCE.

Similar presentations


Presentation on theme: "Data Structures Unit-1 Engineered for Tomorrow CSE, MVJCE."— Presentation transcript:

1 Data Structures Unit-1 Engineered for Tomorrow CSE, MVJCE

2 CSC2110 - Data Structures/Algorithms
Subject Name: Data Structures With C Subject Code: 10CS35 Prepared By: Alpana, Chandana, Sandhya, Sushma Department : CSE Date : 30/8/2014 CSC Data Structures/Algorithms

3 CSC2110 - Data Structures/Algorithms
Unit 1 Basic Concepts 1.1 Pointers and Dynamic Memory Allocation 1.2 Algorithm Specification 1.3 Data Abstraction 1.4 Performance Analysis 1.5 Performance Measurement CSC Data Structures/Algorithms

4 Pointers Pointers Powerful feature of the C++ language
One of the most difficult to master Essential for construction of interesting data structures

5 Addresses and Pointers
C++ allows two ways of accessing variables Name (C++ keeps track of the address of the first location allocated to the variable) Address/Pointer Symbol & gets the address of the variable that follows it Addresses/Pointers can be displayed by the cout statement Addresses displayed in HEXADECIMAL

6 Example Output: #include <iostream.h> 56.47 value void main( ) {
int data = 100; float value = 56.47; cout << data << &data << endl; cout << value << &value << endl; } Output: 100 FFF4 56.47 FFF0 56.47 100 FFF1 FFF0 FFF2 FFF3 FFF4 FFF5 FFF6 value data

7 CSC2110 - Data Structures/Algorithms
Engineered for Tomorrow Pointer Variables The pointer data type A data type for containing an address rather than a data value Integral, similar to int Size is the number of bytes in which the target computer stores a memory address Provides indirect access to values CSC Data Structures/Algorithms

8 Declaration of Pointer Variables
Engineered for Tomorrow Declaration of Pointer Variables A pointer variable is declared by: dataType *pointerVarName; The pointer variable pointerVarName is used to point to a value of type dataType The * before the pointerVarName indicates that this is a pointer variable, not a regular variable The * is not a part of the pointer variable name CSC Data Structures/Algorithms

9 Declaration of Pointer Variables (Cont ..)
Engineered for Tomorrow Declaration of Pointer Variables (Cont ..) Example int *ptr1; float *ptr2; ptr1 is a pointer to an int value i.e., it can have the address of the memory location (or the first of more than one memory locations) allocated to an int value ptr2 is a pointer to a float value i.e., it can have the address of the memory location (or the first of more than one memory locations) allocated to a float value

10 Declaration of Pointer Variables (Cont ..)
Engineered for Tomorrow Declaration of Pointer Variables (Cont ..) Whitespace doesn’t matter and each of the following will declare ptr as a pointer (to a float) variable and data as a float variable float *ptr, data; float* ptr, data; float (*ptr), data; float data, *ptr;

11 Assignment of Pointer Variables
Engineered for Tomorrow Assignment of Pointer Variables A pointer variable has to be assigned a valid memory address before it can be used in the program Example: float data = 50.8; float *ptr; ptr = &data; This will assign the address of the memory location allocated for the floating point variable data to the pointer variable ptr. This is OK, since the variable data has already been allocated some memory space having a valid address CSC Data Structures/Algorithms

12 Assignment of Pointer Variables (Cont ..)
Engineered for Tomorrow Assignment of Pointer Variables (Cont ..) float data = 50.8; float *ptr; ptr = &data; FFF0 FFF1 FFF2 FFF3 data FFF4 50.8 FFF5 FFF6 CSC Data Structures/Algorithms

13 Assignment of Pointer Variables (Cont ..)
Engineered for Tomorrow Assignment of Pointer Variables (Cont ..) float data = 50.8; float *ptr; ptr = &data; ptr FFF0 FFF1 FFF2 FFF3 data FFF4 50.8 FFF5 FFF6

14 Assignment of Pointer Variables (Cont ..)
Engineered for Tomorrow Assignment of Pointer Variables (Cont ..) float data = 50.8; float *ptr; ptr = &data; ptr FFF0 FFF4 FFF1 FFF2 FFF3 data FFF4 50.8 FFF5 FFF6

15 Assignment of Pointer Variables (Cont ..)
Engineered for Tomorrow Assignment of Pointer Variables (Cont ..) Don’t try to assign a specific integer value to a pointer variable since it can be disastrous float *ptr; ptr = 120; You cannot assign the address of one type of variable to a pointer variable of another type even though they are both integrals int data = 50; float *ptr; ptr = &data;

16 Initializing pointers
Engineered for Tomorrow Initializing pointers A pointer can be initialized during declaration by assigning it the address of an existing variable float data = 50.8; float *ptr = &data; If a pointer is not initialized during declaration, it is wise to give it a NULL (0) value int *ip = 0; float *fp = NULL;

17 CSC2110 - Data Structures/Algorithms
Engineered for Tomorrow The NULL pointer The NULL pointer is a valid address for any data type. But NULL is not memory address 0. It is an error to dereference a pointer whose value is NULL. Such an error may cause your program to crash, or behave erratically. It is the programmer’s job to check for this. CSC Data Structures/Algorithms

18 Engineered for Tomorrow
Dereferencing Dereferencing – Using a pointer variable to access the value stored at the location pointed by the variable Provide indirect access to values and also called indirection Done by using the dereferencing operator * in front of a pointer variable Unary operator Highest precedence

19 Dereferencing (Cont ..) Example: float data = 50.8; float *ptr;
Engineered for Tomorrow Dereferencing (Cont ..) Example: float data = 50.8; float *ptr; ptr = &data; cout << *ptr; Once the pointer variable ptr has been declared, *ptr represents the value pointed to by ptr (or the value located at the address specified by ptr) and may be treated like any other variable of float type

20 Engineered for Tomorrow
Dereferencing (Cont ..) The dereferencing operator * can also be used in assignments. *ptr = 200; Make sure that ptr has been properly initialized

21 Dereferencing Example
Engineered for Tomorrow Dereferencing Example #include <iostream.h> void main() { float data = 50.8; float *ptr; ptr = &data; cout << ptr << *ptr << endl; *ptr = 27.4; cout << *ptr << endl; cout << data << endl; } Output: ptr FFF0 FFF4 FFF1 FFF2 FFF3 data FFF4 50.8 FFF5 FFF6

22 Dereferencing Example (Cont ..)
Engineered for Tomorrow Dereferencing Example (Cont ..) #include <iostream.h> void main() { float data = 50.8; float *ptr; ptr = &data; cout << ptr << *ptr << endl; *ptr = 27.4; cout << *ptr << endl; cout << data << endl; } Output: FFF ptr FFF0 FFF4 FFF1 FFF2 FFF3 data FFF4 50.8 FFF5 FFF6 CSC Data Structures/Algorithms

23 Dereferencing Example (Cont ..)
Engineered for Tomorrow Dereferencing Example (Cont ..) #include <iostream.h> void main() { float data = 50.8; float *ptr; ptr = &data; cout << ptr << *ptr << endl; *ptr = 27.4; cout << *ptr << endl; cout << data << endl; } Output: ptr FFF0 FFF4 FFF1 FFF2 FFF3 data FFF4 27.4 FFF5 FFF6 CSC Data Structures/Algorithms

24 Dereferencing Example (Cont ..)
Engineered for Tomorrow Dereferencing Example (Cont ..) #include <iostream.h> void main() { float data = 50.8; float *ptr; ptr = &data; cout << ptr << *ptr << endl; *ptr = 27.4; cout << *ptr << endl; cout << data << endl; } Output: 27.4 ptr FFF0 FFF4 FFF1 FFF2 FFF3 data FFF4 27.4 FFF5 FFF6 CSC Data Structures/Algorithms

25 Dereferencing Example (Cont ..)
Engineered for Tomorrow Dereferencing Example (Cont ..) #include <iostream.h> void main() { float data = 50.8; float *ptr; ptr = &data; cout << ptr << *ptr << endl; *ptr = 27.4; cout << *ptr << endl; cout << data << endl; } Output: 27.4 ptr FFF0 FFF4 FFF1 FFF2 FFF3 data FFF4 27.4 FFF5 FFF6 CSC Data Structures/Algorithms

26 Operations on Pointer Variables
Engineered for Tomorrow Operations on Pointer Variables Assignment – the value of one pointer variable can be assigned to another pointer variable of the same type Relational operations - two pointer variables of the same type can be compared for equality, and so on Some limited arithmetic operations integer values can be added to and subtracted from a pointer variable value of one pointer variable can be subtracted from another pointer variable CSC Data Structures/Algorithms

27 Engineered for Tomorrow
Pointers to arrays A pointer variable can be used to access the elements of an array of the same type. int gradeList[8] = {92,85,75,88,79,54,34,96}; int *myGrades = gradeList; cout << gradeList[1]; cout << *myGrades; cout << *(myGrades + 2); cout << myGrades[3]; Note that the array name gradeList acts like the pointer variable myGrades.

28 Dynamic Memory Allocation
Engineered for Tomorrow Dynamic Memory Allocation

29 Engineered for Tomorrow
Types of Program Data Static Data: Memory allocation exists throughout execution of program Automatic Data: Automatically created at function entry, resides in activation frame of the function, and is destroyed when returning from function Dynamic Data: Explicitly allocated and deallocated during program execution by C++ instructions written by programmer

30 Engineered for Tomorrow
Allocation of Memory Static Allocation: Allocation of memory space at compile time. Dynamic Allocation: Allocation of memory space at run time.

31 Dynamic memory allocation
Engineered for Tomorrow Dynamic memory allocation Dynamic allocation is useful when arrays need to be created whose extent is not known until run time complex structures of unknown size and/or shape need to be constructed as the program runs objects need to be created and the constructor arguments are not known until run time

32 Dynamic memory allocation
Engineered for Tomorrow Dynamic memory allocation Pointers need to be used for dynamic allocation of memory Use the operator new to dynamically allocate space Use the operator delete to later free this space

33 Engineered for Tomorrow
The new operator If memory is available, the new operator allocates memory space for the requested object/array, and returns a pointer to (address of) the memory allocated. If sufficient memory is not available, the new operator returns NULL. The dynamically allocated object/array exists until the delete operator destroys it.

34 Engineered for Tomorrow
The delete operator The delete operator deallocates the object or array currently pointed to by the pointer which was previously allocated at run-time by the new operator. the freed memory space is returned to Heap the pointer is then considered unassigned If the value of the pointer is NULL there is no effect.

35 Example int *ptr; ptr ptr = new int; *ptr = 22;
Engineered for Tomorrow Example int *ptr; ptr = new int; *ptr = 22; cout << *ptr << endl; delete ptr; ptr = NULL; ptr FDE0 FDE1 FDE2 FDE3 0EC4 0EC5 0EC6 0EC7

36 Example (Cont ..) int *ptr; ptr 0EC4 ptr = new int; *ptr = 22;
Engineered for Tomorrow Example (Cont ..) int *ptr; ptr = new int; *ptr = 22; cout << *ptr << endl; delete ptr; ptr = NULL; ptr FDE0 0EC4 FDE1 FDE2 FDE3 0EC4 0EC5 0EC6 0EC7

37 Example (Cont ..) int *ptr; ptr 0EC4 ptr = new int; *ptr = 22;
Engineered for Tomorrow Example (Cont ..) int *ptr; ptr = new int; *ptr = 22; cout << *ptr << endl; delete ptr; ptr = NULL; ptr FDE0 0EC4 FDE1 FDE2 FDE3 0EC4 22 0EC5 0EC6 0EC7

38 Example (Cont ..) 22 int *ptr; ptr 0EC4 ptr = new int; *ptr = 22;
Engineered for Tomorrow Example (Cont ..) int *ptr; ptr = new int; *ptr = 22; cout << *ptr << endl; delete ptr; ptr = NULL; ptr FDE0 0EC4 FDE1 FDE2 FDE3 0EC4 22 Output: 22 0EC5 0EC6 0EC7

39 Example (Cont ..) int *ptr; ptr ? ptr = new int; *ptr = 22;
Engineered for Tomorrow Example (Cont ..) int *ptr; ptr = new int; *ptr = 22; cout << *ptr << endl; delete ptr; ptr = NULL; ptr FDE0 ? FDE1 FDE2 FDE3 0EC4 0EC5 0EC6 0EC7

40 Example (Cont ..) int *ptr; ptr ptr = new int; *ptr = 22;
Engineered for Tomorrow Example (Cont ..) int *ptr; ptr = new int; *ptr = 22; cout << *ptr << endl; delete ptr; ptr = NULL; ptr FDE0 FDE1 FDE2 FDE3 0EC4 0EC5 0EC6 0EC7

41 Dynamic allocation and deallocation of arrays
Engineered for Tomorrow Dynamic allocation and deallocation of arrays Use the [IntExp] on the new statement to create an array of objects instead of a single instance. On the delete statement use [] to indicate that an array of objects is to be deallocated.

42 Example of dynamic array allocation
Engineered for Tomorrow Example of dynamic array allocation int* grades = NULL; int numberOfGrades; cout << "Enter the number of grades: "; cin >> numberOfGrades; grades = new int[numberOfGrades]; for (int i = 0; i < numberOfGrades; i++) cin >> grades[i]; for (int j = 0; j < numberOfGrades; j++) cout << grades[j] << " "; delete [] grades; grades = NULL;

43 Dynamic allocation of 2D arrays
Engineered for Tomorrow Dynamic allocation of 2D arrays A two dimensional array is really an array of arrays (rows). To dynamically declare a two dimensional array of int type, you need to declare a pointer to a pointer as: int **matrix;

44 Dynamic allocation of 2D arrays (Cont ..)
Engineered for Tomorrow Dynamic allocation of 2D arrays (Cont ..) To allocate space for the 2D array with r rows and c columns: You first allocate the array of pointers which will point to the arrays (rows) matrix = new int*[r]; This creates space for r addresses; each being a pointer to an int. Then you need to allocate the space for the 1D arrays themselves, each with a size of c for(i=0; i<r; i++) matrix[i] = new int[c];

45 Dynamic allocation of 2D arrays (Cont ..)
Engineered for Tomorrow Dynamic allocation of 2D arrays (Cont ..) The elements of the array matrix now can be accessed by the matrix[i][j] notation Keep in mind, the entire array is not in contiguous space (unlike a static 2D array) The elements of each row are in contiguous space, but the rows themselves are not. matrix[i][j+1] is after matrix[i][j] in memory, but matrix[i][0] may be before or after matrix[i+1][0] in memory

46 Engineered for Tomorrow
Example // create a 2D array dynamically int rows, columns, i, j; int **matrix; cin >> rows >> columns; matrix = new int*[rows]; for(i=0; i<rows; i++) matrix[i] = new int[columns]; // deallocate the array for(i=0; i<rows; i++) delete [] matrix[i]; delete [] matrix;

47 Passing pointers to a function
Engineered for Tomorrow Passing pointers to a function

48 Pointers as arguments to functions
Engineered for Tomorrow Pointers as arguments to functions Pointers can be passed to functions just like other types. Just as with any other argument, verify that the number and type of arguments in function invocation match the prototype (and function header).

49 Example of pointer arguments
Engineered for Tomorrow Example of pointer arguments void Swap(int *p1, int *p2); void main () { int x, y; cin >> x >> y; cout << x << " " << y << endl; Swap(&x,&y); // passes addresses of x and y explicitly } void Swap(int *p1, int *p2) int temp = *p1; *p1 = *p2; *p2 = temp;

50 Example of reference arguments
Engineered for Tomorrow Example of reference arguments void Swap(int &a, int &b); void main () { int x, y; cin >> x >> y; cout << x << " " << y << endl; Swap(x,y); // passes addresses of x and y implicitly } void Swap(int &a, int &b) int temp = a; a = b; b = temp;

51 More example void main () { int r, s = 5, t = 6; int *tp = &t;
Engineered for Tomorrow More example void main () { int r, s = 5, t = 6; int *tp = &t; r = MyFunction(tp,s); r = MyFunction(&t,s); r = MyFunction(&s,*tp); } int MyFunction(int *p, int i) *p = 3; i = 4; return i; first invocation assigns t a value of 3 and does nothing to s since passed by value second invocation assigns t a value of 3 and again nothing to s Third invocation assigns t a value of 3 and second assignment makes copy of t =4 but again has no effect on t. Point out that pointers can be passed by value or reference as well, and must be passed by reference if the value of the address itself is to be changed.

52 Memory leaks and Dangling Pointers
Engineered for Tomorrow Memory leaks and Dangling Pointers

53 Engineered for Tomorrow
Memory leaks When you dynamically create objects, you can access them through the pointer which is assigned by the new operator Reassigning a pointer without deleting the memory it pointed to previously is called a memory leak It results in loss of available memory space

54 Memory leak example ptr1 int *ptr1 = new int; int *ptr2 = new int;
Engineered for Tomorrow Memory leak example ptr1 8 5 ptr2 int *ptr1 = new int; int *ptr2 = new int; *ptr1 = 8; *ptr2 = 5; ptr2 = ptr1; ptr1 8 5 ptr2 How to avoid?

55 Engineered for Tomorrow
Inaccessible object An inaccessible object is an unnamed object that was created by operator new and which a programmer has left without a pointer to it. It is a logical error and causes memory leaks.

56 Engineered for Tomorrow
Dangling Pointer It is a pointer that points to dynamic memory that has been deallocated. The result of dereferencing a dangling pointer is unpredictable.

57 Dangling Pointer example
Engineered for Tomorrow Dangling Pointer example ptr1 8 ptr2 int *ptr1 = new int; int *ptr2; *ptr1 = 8; ptr2 = ptr1; delete ptr1; ptr1 ptr2 How to avoid?

58 Engineered for Tomorrow
Pointers to objects Any type that can be used to declare a variable/object can also have a pointer type. Consider the following class: class Rational { private: int numerator; int denominator; public: Rational(int n, int d); void Display(); };

59 Pointers to objects (Cont..)
Engineered for Tomorrow Pointers to objects (Cont..) Rational *rp = NULL; Rational r(3,4); rp = &r; rp FFF0 FFF1 FFF2 FFF3 FFF4 FFF5 FFF6 FFF7 FFF8 FFF9 FFFA FFFB FFFC FFFD

60 Pointers to objects (Cont..)
Engineered for Tomorrow Pointers to objects (Cont..) Rational *rp = NULL; Rational r(3,4); rp = &r; rp FFF0 FFF1 FFF2 FFF3 FFF4 3 FFF5 FFF6 r FFF7 numerator = 3 denominator = 4 FFF8 4 FFF9 FFFA FFFB FFFC FFFD

61 Pointers to objects (Cont..)
Engineered for Tomorrow Pointers to objects (Cont..) Rational *rp = NULL; Rational r(3,4); rp = &r; rp FFF0 FFF4 FFF1 FFF2 FFF3 FFF4 3 FFF5 FFF6 r FFF7 numerator = 3 denominator = 4 FFF8 4 FFF9 FFFA FFFB FFFC FFFD

62 Pointers to objects (Cont..)
Engineered for Tomorrow Pointers to objects (Cont..) If rp is a pointer to an object, then two notations can be used to reference the instance/object rp points to. Using the de-referencing operator * (*rp).Display(); Using the member access operator -> rp -> Display();

63 Dynamic Allocation of a Class Object
Engineered for Tomorrow Dynamic Allocation of a Class Object Consider the Rational class defined before Rational *rp; int a, b; cin >> a >> b; rp = new Rational(a,b); (*rp).Display(); // rp->Display(); delete rp; rp = NULL;

64 Analysis of Algorithms
Efficiency: Running time Space used Efficiency as a function of input size: Number of data elements (numbers, points) A number of bits in an input number

65 The RAM model Very important to choose the level of detail.
Instructions (each taking constant time): Arithmetic (add, subtract, multiply, etc.) Data movement (assign) Control (branch, subroutine call, return) Data types – integers and floats

66 Analysis of Insertion Sort
Time to compute the running time as a function of the input size for j¬2 to n do key¬A[j] Insert A[j] into the sorted sequence A[1..j-1] i¬j-1 while i>0 and A[i]>key do A[i+1]¬A[i] i-- A[i+1]:=key cost c1 c2 c3 c4 c5 c6 c7 times n n-1 n-1 n-1 n-1

67 Best/Worst/Average Case
Best case: elements already sorted ® tj=1, running time = f(n), i.e., linear time. Worst case: elements are sorted in inverse order ® tj=j, running time = f(n2), i.e., quadratic time Average case: tj=j/2, running time = f(n2), i.e., quadratic time

68 Best/Worst/Average Case (2)
For a specific size of input n, investigate running times for different input instances: 6n 5n 4n 3n 2n 1n

69 Best/Worst/Average Case (3)
For inputs of all sizes: worst-case average-case 6n 5n best-case Running time 4n 3n 2n 1n ….. Input instance size

70 Best/Worst/Average Case (4)
Worst case is usually used: It is an upper-bound and in certain application domains (e.g., air traffic control, surgery) knowing the worst-case time complexity is of crucial importance For some algorithms worst case occurs fairly often The average case is often as bad as the worst case Finding the average case can be very difficult

71 That’s it? Is insertion sort the best approach to sorting?
Alternative strategy based on divide and conquer MergeSort sorting the numbers <4, 1, 3, 9> is split into sorting <4, 1> and <3, 9> and merging the results Running time f(n log n)

72 Example 2: Searching OUTPUT INPUT j a1, a2, a3,….,an; q
an index of the found number or NIL INPUT sequence of numbers (database) a single number (query) j a1, a2, a3,….,an; q 2 ; 5 ; 9 NIL

73 Searching (2) Worst-case running time: f(n), average-case: f(n/2)
INPUT: A[1..n] – an array of integers, q – an integer. OUTPUT: an index j such that A[j] = q. NIL, if "j (1£j£n): A[j] ¹ q j¬1 while j £ n and A[j] ¹ q do j++ if j £ n then return j else return NIL Worst-case running time: f(n), average-case: f(n/2) We can’t do better. This is a lower bound for the problem of searching in an arbitrary sequence.

74 Example 3: Searching OUTPUT INPUT j a1, a2, a3,….,an; q
sorted non-descending sequence of numbers (database) a single number (query) OUTPUT an index of the found number or NIL j a1, a2, a3,….,an; q 2 ; 5 ; 9 NIL

75 Binary search Idea: Divide and conquer, one of the key design techniques INPUT: A[1..n] – a sorted (non-decreasing) array of integers, q – an integer. OUTPUT: an index j such that A[j] = q. NIL, if "j (1£j£n): A[j] ¹ q left¬1 right¬n do j¬(left+right)/2 if A[j]=q then return j else if A[j]>q then right¬j-1 else left=j+1 while left<=right return NIL

76 Binary search – analysis
How many times the loop is executed: With each execution the difference between left and right is cult in half Initially the difference is n The loop stops when the difference becomes 0 How many times do you have to cut n in half to get 1? lg n

77 The Goals of this Course
The main things that we will try to learn in this course: To be able to think “algorithmically”, to get the spirit of how algorithms are designed To get to know a toolbox of classical algorithms To learn a number of algorithm design techniques (such as divide-and-conquer) To learn reason (in a formal way) about the efficiency and the correctness of algorithms

78 The Concept of Abstraction
An abstraction is a view or representation of an entity that includes only the most significant attributes The concept of abstraction is fundamental in programming (and computer science) Nearly all programming languages support process abstraction with subprograms Nearly all programming languages designed since 1980 support data abstraction 1-78 78

79 Introduction to Data Abstraction
An abstract data type is a user-defined data type that satisfies the following two conditions: The representation of, and operations on, objects of the type are defined in a single syntactic unit The representation of objects of the type is hidden from the program units that use these objects, so the only operations possible are those provided in the type's definition 1-79 79

80 Advantages of Data Abstraction
Advantage of the first condition Program organization, modifiability (everything associated with a data structure is together), and separate compilation Advantage the second condition Reliability--by hiding the data representations, user code cannot directly access objects of the type or depend on the representation, allowing the representation to be changed without affecting user code 1-80 80

81 Language Requirements for ADTs
A syntactic unit in which to encapsulate the type definition A method of making type names and subprogram headers visible to clients, while hiding actual definitions Some primitive operations must be built into the language processor 1-81 81

82 Classification of ADT operations
Creator (constructor) GroupUsers(String userIDs[ ]) Producer addUser(String userID) Mutator setUser (String ) Observer isMember (String userID)

83 Algorithm Specification
1.2.1 Introduction An algorithm is a finite set of instructions that accomplishes a particular task. Criteria input: zero or more quantities that are externally supplied output: at least one quantity is produced definiteness: clear and unambiguous finiteness: terminate after a finite number of steps effectiveness: instruction is basic enough to be carried out A program does not have to satisfy the finiteness criteria.

84 Algorithm Specification
Representation A natural language, like English or Chinese. A graphic, like flowcharts. A computer language, like C. Algorithms + Data structures = Programs [Niklus Wirth] Sequential search vs. Binary search

85 Algorithm Specification
Example 1.1 [Selection sort]: From those integers that are currently unsorted, find the smallest and place it next in the sorted list. i [0] [1] [2] [3] [4]

86 Program 1.3 contains a complete program which you may run on your computer

87 Algorithm Specification
Example 1.2 [Binary search]: [0] [1] [2] [3] [4] [5] [6] left right middle list[middle] : searchnum < > == > < > Searching a sorted list while (there are more integers to check) { middle = (left + right) / 2; if (searchnum < list[middle]) right = middle - 1; else if (searchnum == list[middle]) return middle; else left = middle + 1; }

88 int binsearch(int list[], int searchnum, int left, int right) { /
int binsearch(int list[], int searchnum, int left, int right) { /* search list[0] <= list[1] <= … <= list[n-1] for searchnum. Return its position if found. Otherwise return -1 */ int middle; while (left <= right) { middle = (left + right)/2; switch (COMPARE(list[middle], searchnum)) { case -1: left = middle + 1; break; case 0 : return middle; case 1 : right = middle – 1; } return -1;

89 Algorithm Specification
Recursive algorithms Beginning programmer view a function as something that is invoked (called) by another function It executes its code and then returns control to the calling function.

90 Algorithm Specification
This perspective ignores the fact that functions can call themselves (direct recursion). They may call other functions that invoke the calling function again (indirect recursion). extremely powerful frequently allow us to express an otherwise complex process in very clear term We should express a recursive algorithm when the problem itself is defined recursively.

91 Algorithm Specification
Example 1.3 [Binary search]:

92 Example 1.4 [Permutations]:
lv0 perm: i=0, n=2 abc lv0 SWAP: i=0, j=0 abc lv1 perm: i=1, n=2 abc lv1 SWAP: i=1, j=1 abc lv2 perm: i=2, n=2 abc print: abc lv1 SWAP: i=1, j=2 abc lv2 perm: i=2, n=2 acb print: acb lv1 SWAP: i=1, j=2 acb lv0 SWAP: i=0, j=1 abc lv1 perm: i=1, n=2 bac lv1 SWAP: i=1, j=1 bac lv2 perm: i=2, n=2 bac print: bac lv1 SWAP: i=1, j=2 bac lv2 perm: i=2, n=2 bca print: bca lv1 SWAP: i=1, j=2 bca lv0 SWAP: i=0, j=1 bac lv0 SWAP: i=0, j=2 abc lv1 perm: i=1, n=2 cba lv1 SWAP: i=1, j=1 cba lv2 perm: i=2, n=2 cba print: cba lv1 SWAP: i=1, j=2 cba lv2 perm: i=2, n=2 cab print: cab lv1 SWAP: i=1, j=2 cab lv0 SWAP: i=0, j=2 cba Example 1.4 [Permutations]:

93 Data abstraction Data Type A data type is a collection of objects and a set of operations that act on those objects. For example, the data type int consists of the objects {0, +1, -1, +2, -2, …, INT_MAX, INT_MIN} and the operations +, -, *, /, and %. The data types of C The basic data types: char, int, float and double The group data types: array and struct The pointer data type The user-defined types

94 Data abstraction Abstract Data Type
An abstract data type (ADT) is a data type that is organized in such a way that the specification of the objects and the operations on the objects is separated from the representation of the objects and the implementation of the operations. We know what is does, but not necessarily how it will do it.

95 Data abstraction Specification vs. Implementation
An ADT is implementation independent Operation specification function name the types of arguments the type of the results The functions of a data type can be classify into several categories: creator / constructor transformers observers / reporters

96 Data abstraction Example 1.5 [Abstract data type Natural_Number]
::= is defined as

97 Performance analysis Criteria Is it correct? Is it readable? …
Performance Analysis (machine independent) space complexity: storage requirement time complexity: computing time Performance Measurement (machine dependent)

98 Performance analysis Space Complexity: S(P)=C+SP(I)
Fixed Space Requirements (C) Independent of the characteristics of the inputs and outputs instruction space space for simple variables, fixed-size structured variable, constants Variable Space Requirements (SP(I)) depend on the instance characteristic I number, size, values of inputs and outputs associated with I recursive stack space, formal parameters, local variables, return address

99 Performance analysis Examples: Example 1.6: In program 1.9, Sabc(I)=0.
Example 1.7: In program 1.10, Ssum(I)=Ssum(n)=0. Recall: pass the address of the first element of the array & pass by value

100 Performance analysis Ssum(I)=Ssum(n)=6n
Example 1.8: Program 1.11 is a recursive function for addition. Figure 1.1 shows the number of bytes required for one recursive call. Ssum(I)=Ssum(n)=6n

101 Performance analysis Time Complexity: T(P)=C+TP(I)
The time, T(P), taken by a program, P, is the sum of its compile time C and its run (or execution) time, TP(I) Fixed time requirements Compile time (C), independent of instance characteristics Variable time requirements Run (execution) time TP TP(n)=caADD(n)+csSUB(n)+clLDA(n)+cstSTA(n)

102 Performance analysis A program step is a syntactically or semantically meaningful program segment whose execution time is independent of the instance characteristics. Example (Regard as the same unit machine independent) abc = a + b + b * c + (a + b - c) / (a + b) + 4.0 abc = a + b + c Methods to compute the step count Introduce variable count into programs Tabular method Determine the total number of steps contributed by each statement step per execution  frequency add up the contribution of all statements

103 Performance analysis 2n + 3 steps
Iterative summing of a list of numbers *Program 1.12: Program 1.10 with count statements (p.23) float sum(float list[ ], int n) { float tempsum = 0; count++; /* for assignment */ int i; for (i = 0; i < n; i++) { count++; /*for the for loop */ tempsum += list[i]; count++; /* for assignment */ } count++; /* last execution of for */ count++; /* for return */ return tempsum; } 2n + 3 steps

104 Iterative function to sum a list of numbers
Performance analysis Tabular Method *Figure 1.2: Step count table for Program 1.10 (p.26) Iterative function to sum a list of numbers steps/execution

105 Performance analysis Recursive summing of a list of numbers 2n+2 steps
*Program 1.14: Program 1.11 with count statements added (p.24) float rsum(float list[ ], int n) { count++; /*for if conditional */ if (n) { count++; /* for return and rsum invocation*/ return rsum(list, n-1) + list[n-1]; } count++; return list[0]; } 2n+2 steps

106 Performance analysis 1.4.3 Asymptotic notation (O, , )
Complexity of c1n2+c2n and c3n for sufficiently large of value, c3n is faster than c1n2+c2n for small values of n, either could be faster c1=1, c2=2, c3=100 --> c1n2+c2n  c3n for n  98 c1=1, c2=2, c3= > c1n2+c2n  c3n for n  998 break even point no matter what the values of c1, c2, and c3, the n beyond which c3n is always faster than c1n2+c2n

107 Performance analysis Definition: [Big “oh’’] Definition: [Omega]
f(n) = O(g(n)) iff there exist positive constants c and n0 such that f(n)  cg(n) for all n, n  n0. Definition: [Omega] f(n) = (g(n)) (read as “f of n is omega of g of n”) iff there exist positive constants c and n0 such that f(n)  cg(n) for all n, n  n0. Definition: [Theta] f(n) = (g(n)) (read as “f of n is theta of g of n”) iff there exist positive constants c1, c2, and n0 such that c1g(n)  f(n)  c2g(n) for all n, n  n0.

108 Performance analysis Theorem 1.2: Theorem 1.3: Theorem 1.4:
If f(n) = amnm+…+a1n+a0, then f(n) = O(nm). Theorem 1.3: If f(n) = amnm+…+a1n+a0 and am > 0, then f(n) = (nm). Theorem 1.4: If f(n) = amnm+…+a1n+a0 and am > 0, then f(n) = (nm).

109 Performance analysis Examples f(n) = 3n+2 f(n) = 10n2+4n+2
3n + 2 <= 4n, for all n >= 2, 3n + 2 =  (n) 3n + 2 >= 3n, for all n >= 1, 3n + 2 =  (n) 3n <= 3n + 2 <= 4n, for all n >= 2,  3n + 2 =  (n) f(n) = 10n2+4n+2 10n2+4n+2 <= 11n2, for all n >= 5,  10n2+4n+2 =  (n2) 10n2+4n+2 >= n2, for all n >= 1,  10n2+4n+2 =  (n2) n2 <= 10n2+4n+2 <= 11n2, for all n >= 5,  10n2+4n+2 =  (n2) 100n+6=O(n) /* 100n+6101n for n10 */ 10n2+4n+2=O(n2) /* 10n2+4n+211n2 for n5 */ 6*2n+n2=O(2n) /* 6*2n+n2 7*2n for n4 */

110 Performance analysis Practical complexity
To get a feel for how the various functions grow with n, you are advised to study Figures 1.7 and 1.8 very closely.

111 Performance analysis

112 Performance analysis Figure 1.9 gives the time needed by a 1 billion instructions per second computer to execute a program of complexity f(n) instructions.

113 Performance measurement
Although performance analysis gives us a powerful tool for assessing an algorithm’s space and time complexity, at some point we also must consider how the algorithm executes on our machine. This consideration moves us from the realm of analysis to that of measurement.

114 Performance measurement
Example [Worst case performance of the selection function]: The tests were conducted on an IBM compatible PC with an cpu, an numeric coprocessor, and a turbo accelerator. We use Broland’s Turbo C compiler.

115 Performance measurement

116 Thank You..


Download ppt "Data Structures Unit-1 Engineered for Tomorrow CSE, MVJCE."

Similar presentations


Ads by Google