C. – C. Yao Data Structure. C. – C. Yao Chap 1 Basic Concepts.

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
Lecture3: Algorithm Analysis Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Chapter 1 – Basic Concepts
1 Data Structures Performance Analysis. 2 Fundamental Concepts Some fundamental concepts that you should know: –Dynamic memory allocation. –Recursion.
1 ICS 353 Design and Analysis of Algorithms Spring Semester (062) King Fahd University of Petroleum & Minerals Information & Computer Science.
Introduction to Analysis of Algorithms
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.
© Love Ekenberg The Algorithm Concept, Big O Notation, and Program Verification Love Ekenberg.
Data Structures Performance Analysis.
Chapter 1.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
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.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
Instructor Neelima Gupta
1 Chapter 2 Program Performance – Part 2. 2 Step Counts Instead of accounting for the time spent on chosen operations, the step-count method accounts.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
Program Performance & Asymptotic Notations CSE, POSTECH.
Chapter 1 basic Concepts
For Wednesday Read Weiss chapter 3, sections 1-5. This should be largely review. If you’re struggling with the C++ aspects, you may refer to Savitch, chapter.
1 R. Johnsonbaugh, Discrete Mathematics Chapter 4 Algorithms.
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.
Discrete Structures Lecture 11: Algorithms Miss, Yanyan,Ji United International College Thanks to Professor Michael Hvidsten.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Chapter 1 1. Overview: System life cycle Requirements Analysis Bottom-up Top-down Design Data objects (abstract data type) and operations (algorithm)
Lecture 2 Computational Complexity
CS Data Structures Chapter 1 Basic Concepts.
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
SNU IDB Lab. Ch3. Asymptotic Notation © copyright 2006 SNU IDB Lab.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data.
Complexity of Algorithms
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Data Structure Introduction.
System Life Cycles Requirements Analysis Design Refinement and Coding Verification –Correction proofs –Testing –Error Removal.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of the problem.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
Software Learning Resource Service Platform BASIC CONCEPT CHAPTER 1 BASIC CONCEPT 1.
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
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.
1 Chapter 2 Program Performance. 2 Concepts Memory and time complexity of a program Measuring the time complexity using the operation count and step count.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Ch03-Algorithms 1. Algorithms What is an algorithm? An algorithm is a finite set of precise instructions for performing a computation or for solving a.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Algorithm Complexity Analysis (Chapter 10.4) Dr. Yingwu Zhu.
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
SPACE COMPLEXITY & TIME COMPLEXITY 1. ALGORITHMS COMPLEXITY 2.
Algorithm Analysis 1.
Chapter 2 Algorithm Analysis
Analysis of Algorithms
Analysis of Algorithms
What is an Algorithm? Algorithm Specification.
Growth of functions CSC317.
DATA STRUCTURES Introduction: Basic Concepts and Notations
CS 3343: Analysis of Algorithms
CH1. BASIC CONCEPTS.
Algorithm design and Analysis
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.
Algorithm Efficiency Chapter 10.
Data Structures-CSE207.
ICS 353: Design and Analysis of Algorithms
At the end of this session, learner will be able to:
Discrete Mathematics 7th edition, 2009
Data structures & Algorithm Strategies
Presentation transcript:

C. – C. Yao Data Structure

C. – C. Yao Chap 1 Basic Concepts

C. – C. Yao Algorithm Definition: An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition, all algorithms must satisfy the following criteria: 1)Input. Zero more quantities are externally supplied. 2)Output. At least one quantity is produced. 3)Definiteness. Each instruction is clear and unambiguous. 4)Finiteness. If we trace out the instructions of an algorithm, then for all cases, the algorithm terminates after a finite number of steps. 5)Effectiveness. Every instruction must be basic enough to be carried out, in principle, by a person using only pencil and paper. It is not enough that each operation be definite as in 3) it also must be feasible.

C. – C. Yao Example: Selection Sort Suppose we must devise a program that sorts a collection of n ≥ 1 integers. From those integers that are currently unsorted, find the smallest and place it next in the sorted list. Problem in the above statement –Does not describe where and how the integers are initially sorted. –Does not indicate where to place the result.

C. – C. Yao C++ Program for Selection Sort void sort (int *a, const int n) // sort the n integers a[0] to a[n-1] into non-decreasing order for (int i = 0; i < n; i++) { int j = i; // find smallest integer in a[i] to a[n-1] for (int k = i + 1; k < n; k++) if (a[k] < a[j]) j = k; // interchange int temp = a[i]; a[i] = a[j]; a[j] = temp; }

C. – C. Yao Selection Sort (Cont.) Theorem 1.1: sort(a, n) correctly sorts a set of n ≥ 1 integers; the result remains in a[0] … a[n-1] such that a[0] ≤ a[1] ≤ … ≤ a[n–1].

C. – C. Yao Example:Binary Search Assume that we have n ≥ 1 distinct integers that are already sorted and stored in the array a[0] … a[n-1]. Our task is to determine if the integer x is present and if so to return j such that x = a[j]; otherwise return -1. By making use of the fact that the set is sorted, we conceive the following efficient method: Let left and right, respectively, denote the left and right ends of the list to be searched. Initially, left = 0 and right = n – 1. Let middle = (left + right) / 2 be the middle position in the list. If we compare a[middle] with x, we obtain one of the three results: (1) x < a[middle]. In this case, if x is present, it must be in the positions between 0 and middle – 1. Therefore, we set right to middle – 1. (2) x == a[middle]. In this case, we return middle. (3) x > a[middle]. In this case, if x is present, it must be in the positions between middle+1 and n-1. So, we set left to middle+1.

C. – C. Yao Algorithm for Binary Search int BinarySearch (int *a, const int x, const int n) // Search the sorted array a[0], …, a[n-1] for x { for (initialize left and right; while there are more elements;) { let middle be the middle element; switch (compare (x, a[middle])) { case ‘>’: set left to middle+1; break; case ‘<‘: set right to middle -1; break; case ‘=‘: found x; } // end of switch } // end of for not found; } // end of BinarySearch

C. – C. Yao C++ Program for Binary Search char compare (int x, int y) { if (x > y) return ‘>’; else if ( x < y) return ‘<‘; else return ‘=‘; } // end of compare

C. – C. Yao Algorithm for Binary Search (Cont.) int BinarySearch (int *a, const int x, const int n) // Search the sorted array a[0], …, a[n-1] for x { for (int left = 0, right = n - 1; left <= right;) { int middle = (left + right) / 2; switch (compare (x, a[middle])) { case ‘>’: left = middle+1; break; // x > a[middle] case ‘<‘: right = middle -1; break; // x < a[middle] case ‘=‘: return middle; // x == a[middle] } // end of switch } // end of for return -1; } // end of BinarySearch

C. – C. Yao Recursive Algorithms int BinarySearch (int *a, const int x, const int left, const int right) { if (left <= right) { int middle = (left + right) / 2; if (x < a[middle]) return BinarySearch(a,x,left,middle-1); else if (x < a[middle]) return BinarySearch(a,x,left,middle-1); return middle; } // end if return -1; } // end of BinarySearch

C. – C. Yao Recursive Algorithms(cont.) Recursive program: int main() { int n=10; printf( “ %d ”, rfib(n)); } int rfib(int n) { if (n==1 || n==2) return 1; return rfib(n  1)+rfib(n  2); }

C. – C. Yao Performance Analysis Space Complexity: The space complexity of a program is the amount of memory it needs to run to completion. Time Complexity: The time complexity of a program is the amount of computer time it needs to run to completion.

C. – C. Yao Space Complexity A fixed part that is independent of the characteristics of the inputs and outputs. This part typically includes the instruction space, space for simple varialbes and fixed-size component variables, space for constants, etc. A variable part that consists of the space needed by component variables whose size is dependent on the particular problem instance being solved, the space needed by referenced variables, and the recursion stack space. The space requirement S(P) of any program P is written as S(P) = c +Sp where c is a constant

C. – C. Yao Time Complexity The time, T(P), taken by a program P is the sum of the compile time and the run (or execution) time. The compile time does not depend on the instance characteristics. We focus on the run time of a program, denoted by tp (instance characteristics).

C. – C. Yao Time Complexity in C++ General statements in a C++ program Step count –Comments 0 –Declarative statements0 –Expressions and assignment statements1 –Iteration statementsN –Switch statementN –If-else statementN –Function invocation1 or N –Memory management statements1 or N –Function statements0 –Jump statements1 or N

C. – C. Yao Time Complexity (Cont.) Note that a step count does not necessarily reflect the complexity of the statement. Step per execution (s/e): The s/e of a statement is the amount by which count changes as a result of the execution of that statement.

C. – C. Yao Time Complexity Iterative Example float sum (float *a, const int n) { float s = 0; for (int i = 0; i < n; i++) s += a[i]; return; }

C. – C. Yao Step Count of Iterative Example float sum (float *a, const int n) { float s = 0; count++;// count is global for (int i = 0; i < n; i++) { count++;// for for s += a[i]; count++;// for assignment } count++; // for last time of for count++;// for return return; }

C. – C. Yao Step Count of Iterative Example (Simplified) void sum (float *a, const int n) { for (int i = 0; i < n; i++) count += 2; count +=3; } If initially count = 0, then the total of steps is 2n + 3.

C. – C. Yao Time Complexity of Recursive Example float rsum (float *a, const int n) { if (n <= 0) return 0; else return (rsum(a, n–1) + a[n-1]); }

C. – C. Yao Step Count of Recursive Example float rsum (float *a, const int n) { count++;// for if conditional if (n <= 0) { count++;// for return return 0; } else { count++;// for return return (rsum(a, n–1) + a[n-1]); } Assume t rsum (0) = 2 t rsum (n) = 2 + t rsum (n-1) = t rsum (n-2) = 2*2 + t rsum (n-2) = 2n + t rsum (0) = 2n + 2

C. – C. Yao Matrix Addition Example line void add (matrix a, matrix b, matrix c, int m, int n) 1 { 2 for (int i = 0; i < m; i++) 3 for (int j = 0; j < n; j++) 4 c[i][j] = a[i][j] + b[i][j]; 5 }

C. – C. Yao Step Count of Matrix Addition Example void add (matrix a, matrix b, matrix c, int m, int n) { for (int i = 0; i < m; i++) { count++;// for for i for (int j = 0; j < n; j++) { count++;// for for j c[i][j] = a[i][j] + b[i][j]; count++;// for assigment } count++;// for last time of for j } count++;// for last time of for i }

C. – C. Yao Step Count of Matrix Addition Example (Simplified) line void add (matrix a, matrix b, matrix c, int m, int n) { for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) count += 2; count+2; } count++; }

C. – C. Yao Step Table of Matrix Addition Example Lines/eFrequencyTotal steps m+1 31m(n+1)mn+m 41mn 5010 Total number of steps2mn+2m+1

C. – C. Yao Fibonacci Numbers The Fibonacci sequence of numbers starts as 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, … Each new term is obtained by taking the sum of the two previous terms. If we call the first term of the sequence F 0 then F 0 = 0, F 1 = 1, and in general F n = F n-1 + F n-2, n ≥ 2.

C. – C. Yao C++ Program of Fibonacci Numbers 1void fibonacci (int n) 2 // compute the Fibonacci number F n 3 { 4if (n <= 1) cout << n << endl;// F 0 = 0, and F 1 = 1 5else {// compute F n 6 int fn; int fnm2 = 0; int fnm1 = 1; 7 for (int i = 2; i <= n; i++) 8 { 9 fn = fnm1 + fnm2; 10 fnm2 = fnm1; 11 fnm1 = fn; 12 } // end of for 13 cout << fn << endl; 14} // end of else 15} // end of fibonacci

C. – C. Yao Step Count of Fibonacci Program Two cases: –n = 0 or n = 1 Line 4 regarded as two lines: 4(a) and 4(b), total step count in this case is 2 –n > 1 Line 4(a), 6, and 13 are each executed once Line 7 gets executed n times. Lines 8 – 12 get executed n-1 times each. Line 6 has s/e of 2 and the remaining lines have an s/e of 1. Total steps for the case n > 1 is 4n + 1

C. – C. Yao Asymptotic Notation Determining step counts help us to compare the time complexities of two programs and to predict the growth in run time as the instance characteristics change. But determining exact step counts could be very difficult. Since the notion of a step count is itself inexact, it may be worth the effort to compute the exact step counts. Definition [Big “ oh ” ]: f(n) = O(g(n)) iff there exist positive constants c and n 0 such that f(n) ≤ cg(n) for all n, n ≥ n 0

C. – C. Yao Examples of Asymptotic Notation 3n + 2 = O(n) 3n + 2 ≤ 4n for all n ≥ 3 100n + 6 = O(n) 100n + 6 ≤ 101n for all n ≥ 10 10n 2 + 4n + 2 = O(n 2 ) 10n 2 + 4n + 2 ≤ 11n 2 for all n ≥ 5

C. – C. Yao Asymptotic Notation (Cont.) Theorem 1.2: If f(n) = a m n m + … + a 1 n + a 0, then f(n) = O(n m ). Proof: for n ≥ 1 So, f(n) = O(n m )

C. – C. Yao Asymptotic Notation (Cont.) Definition: [Omega] f(n) = Ω(g(n)) iff there exist positive constants c and n 0 such that f(n) ≥ cg(n) for all n, n ≥ n 0. Example: –3n + 2 = Ω(n) –100n + 6 = Ω(n) –10n 2 + 4n + 2 =Ω(n 2 )

C. – C. Yao Asymptotic Notation (Cont.) Definition: f(n) = Θ(g(n)) iff there exist positive constants c 1, c 2, and n 0 such that c 1 g(n) ≤ f(n) ≤ c 2 g(n) for all n, n ≥ n 0.

C. – C. Yao Asymptotic Notation (Cont.) Theorem 1.3: If f(n) = a m n m + … + a 1 n + a 0 and a m > 0, then f(n) = Ω(n m ). Theorem 1.4: If f(n) = a m n m + … + a 1 n + a 0 and a m > 0, then f(n) = Θ(n m ).

C. – C. Yao Practical Complexities If a program P has complexities Θ(n) and program Q has complexities Θ( n 2 ), then, in general, we can assume program P is faster than Q for a sufficient large n. However, caution needs to be used on the assertion of “sufficiently large”.

C. – C. Yao Function Values log nnn log nn2n2 n3n3 2n2n