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

Slides:



Advertisements
Similar presentations
Chapter 1 – Basic Concepts
Advertisements

1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
CHAPTER 11 Space and Time Complexity in Chapter 1 All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
C++ for Engineers and Scientists Third Edition
CHAPTER 1 BASIC CONCEPT All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed “Fundamentals of Data Structures.
What is Program  A Set of Instructions  Data Structures + Algorithms  Data Structure = A Container stores Data  Algoirthm = Logic + Control.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Program Performance & Asymptotic Notations CSE, POSTECH.
C. – C. Yao Data Structure. C. – C. Yao Chap 1 Basic Concepts.
Chapter 1 1. Overview: System life cycle Requirements Analysis Bottom-up Top-down Design Data objects (abstract data type) and operations (algorithm)
Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/ Objective : 1.To understand primitive storage structures and types 2.To.
CS Data Structures Chapter 1 Basic Concepts.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
Pointers OVERVIEW.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Pointers and Dynamic Memory Allocation. Declaring a pointer.
Data Structure Introduction.
Review 1 List Data Structure List operations List Implementation Array Linked List.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Software Learning Resource Service Platform BASIC CONCEPT CHAPTER 1 BASIC CONCEPT 1.
Program Performance 황승원 Fall 2010 CSE, POSTECH. Publishing Hwang’s Algorithm Hwang’s took only 0.1 sec for DATASET1 in her PC while Dijkstra’s took 0.2.
Pointers and Dynamic Memory Allocation. Declaring a pointer.
September 10, 2001 Algorithms and Data Structures Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CSC Pointers Powerful feature of the C++ language One of the most difficult to master Essential for construction of interesting data structures.
SPACE COMPLEXITY & TIME COMPLEXITY 1. ALGORITHMS COMPLEXITY 2.
Pointers and Dynamic Memory Allocation
Object Lifetime and Pointers
Dynamic Storage Allocation
EGR 2261 Unit 11 Pointers and Dynamic Variables
Analysis of Algorithms
Introduction Algorithms Order Analysis of Algorithm
What is an Algorithm? Algorithm Specification.
Data Structures (1/2) 內容:Basic Concepts, Arrays and Structures, Stacks and Queues, Lists, Trees, Graphs, Sorting, Hashing, Heap Structures, and Search.
Data Structures Interview / VIVA Questions and Answers
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
8 Pointers.
Object Oriented Programming COP3330 / CGS5409
Algorithm design and Analysis
Chapter 15 Pointers, Dynamic Data, and Reference Types
7 Arrays.
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
Objective of This Course
Pointers, Dynamic Data, and Reference Types
Algorithm Efficiency Chapter 10.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Classes and Objects.
Analysis of Algorithms
Objectives You should be able to describe: Addresses and Pointers
7 Arrays.
Introduction to Data Structure
Algorithms and Data Structures
Data Structures and Algorithms Introduction to Pointers
Math/CSE 1019N: Discrete Mathematics for Computer Science Winter 2007
CHAPTER 1 BASIC CONCEPT All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed “Fundamentals of Data Structures.
Pointers, Dynamic Data, and Reference Types
Data structures & Algorithm Strategies
Analysis of Algorithms
September 10, 2001 Algorithms and Data Structures Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Algorithms and Data Structures
Presentation transcript:

Data Structures Unit-1 Engineered for Tomorrow CSE, MVJCE

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 CSC2110 - Data Structures/Algorithms

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 CSC2110 - Data Structures/Algorithms

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

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

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

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 CSC2110 - Data Structures/Algorithms

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 CSC2110 - Data Structures/Algorithms

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

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;

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 CSC2110 - Data Structures/Algorithms

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 CSC2110 - Data Structures/Algorithms

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

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

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;

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;

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. CSC2110 - Data Structures/Algorithms

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

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

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

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

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: FFF4 50.80 ptr FFF0 FFF4 FFF1 FFF2 FFF3 data FFF4 50.8 FFF5 FFF6 CSC2110 - Data Structures/Algorithms

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 CSC2110 - Data Structures/Algorithms

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 CSC2110 - Data Structures/Algorithms

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 CSC2110 - Data Structures/Algorithms

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 CSC2110 - Data Structures/Algorithms

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.

Dynamic Memory Allocation Engineered for Tomorrow Dynamic Memory Allocation

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

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

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

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

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.

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.

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

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

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

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

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

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

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.

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;

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;

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

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

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;

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

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

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;

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;

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.

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

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

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?

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.

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.

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?

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(); };

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

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

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

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

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;

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

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

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

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

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

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

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

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)

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 2 5 4 10 7; 5 2 5 4 10 7; 9 NIL

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.

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 2 4 5 7 10; 5 2 4 5 7 10; 9 NIL

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

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

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

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

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

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

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

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

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.

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

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] - 30 10 50 40 20 0 10 30 50 40 20 1 10 20 40 50 30 2 10 20 30 40 50 3 10 20 30 40 50

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

Algorithm Specification Example 1.2 [Binary search]: [0] [1] [2] [3] [4] [5] [6] 8 14 26 30 43 50 52 left right middle list[middle] : searchnum 0 6 3 30 < 43 4 6 5 50 > 43 4 4 4 43 == 43 0 6 3 30 > 18 0 2 1 14 < 18 2 2 2 26 > 18 2 1 - 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; }

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;

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.

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.

Algorithm Specification Example 1.3 [Binary search]:

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]:

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

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.

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

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

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)

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

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

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

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)

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

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

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

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

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=1000 --> 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

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.

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

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 */

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.

Performance analysis

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.

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.

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

Performance measurement

Thank You..