Chapter 1 1. Overview: System life cycle Requirements Analysis Bottom-up Top-down Design Data objects (abstract data type) and operations (algorithm)

Slides:



Advertisements
Similar presentations
CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
Advertisements

Analysys & Complexity of Algorithms Big Oh Notation.
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.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
1 Data Structures A program solves a problem. A program solves a problem. A solution consists of: A solution consists of:  a way to organize the data.
Data Structures Performance Analysis.
Lecture 4 Sept 4 Goals: chapter 1 (completion) 1-d array examples Selection sorting Insertion sorting Max subsequence sum Algorithm analysis (Chapter 2)
Chapter 1.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
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,
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.
Prof. Hsin-Mu (Michael) Tsai ( 蔡欣穆 ) Department of Computer Science and Information Engineering National Taiwan University BASICS.
Program Performance & Asymptotic Notations CSE, POSTECH.
Chapter 1 basic Concepts
C. – C. Yao Data Structure. C. – C. Yao Chap 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.
Data Structures Chapter 1: Basic Concepts 1-1. bit: basic unit of information. data type: interpretation of a bit pattern. e.g integer 65 ASCII.
Discrete Structures Lecture 11: Algorithms Miss, Yanyan,Ji United International College Thanks to Professor Michael Hvidsten.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Basic Concepts 2014, Fall Pusan National University Ki-Joune Li.
Data Structures and Algorithms Lecture 5 and 6 Instructor: Quratulain Date: 15 th and 18 th September, 2009 Faculty of Computer Science, IBA.
Iterative Algorithm Analysis & Asymptotic Notations
Data Structures in C Networking Laboratory Chap. 1-1 Copyright(c) 2000, Sungkyunkwan University Chapter #1: BASIC CONCEPTS Fundamentals of Data Structures.
CS Data Structures Chapter 1 Basic Concepts.
Analysis of Algorithms
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.
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.
1 ELEC 206 Chapter 3 Control Structures 5-Step Problem Solving Methodology 1. State the problem clearly. 2. Describe the input and output. 3. Work a.
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.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Algorithm Analysis (Big O)
Software Learning Resource Service Platform BASIC CONCEPT CHAPTER 1 BASIC CONCEPT 1.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
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.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Basic Concepts 2011, Fall Pusan National University Ki-Joune Li.
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.
2017, Fall Pusan National University Ki-Joune Li
Chapter 2 Algorithm Analysis
Data Structures 1st Week
Chapter 1: Basic Concepts
Analysis of Algorithms
What is an Algorithm? Algorithm Specification.
Data Structure 김용성 Data Structure in C.
Engineering Problem Solving with C++, Etter/Ingber
CH1. BASIC CONCEPTS.
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.
2018, Fall Pusan National University Ki-Joune Li
Data Structures-CSE207.
Revision of C++.
ICS 353: Design and Analysis of Algorithms
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.
Data structures & Algorithm Strategies
2019, Fall Pusan National University Ki-Joune Li
Presentation transcript:

Chapter 1 1

Overview: System life cycle Requirements Analysis Bottom-up Top-down Design Data objects (abstract data type) and operations (algorithm) Refinement and coding Verification Correctness proofs Testing Error removal 2

Data abstraction and encapsulation Data Encapsulation or Information Hiding is the concealing of the implementation details of a data object from the outside world Data Abstraction is the separation between the specification of a data object and its implementation A data type is a collection of objects and a set of operations that act on those objects An abstract data type (ADT) is a data type that is organized in such a way that the specification of the objects, and the specification of the operations on the objects is separated from the representation of the objects and the implementation of the operations 3

Example 1.1 [Abstract data type NaturalNumber] ADT NaturalNumber is objects: an ordered subrange of the integers starting at zero and ending at the maximum integer (MAXINT) on the computer. functions: for all x, y ∈ NatureNumber; TRUE, FALSE ∈ Boolean and where +, -, <, and == are the usual integer operations. Zero ( ):NaturalNumber ::= 0 IsZero(x):Boolean ::= if (x == 0) IsZero = true; else IsZero = false Add(x, y):NaturalNumber ::= if ((x+y) <= MAXINT) Add = x+y; else Add = MAXINT Equal(x,y):Boolean ::= if (x == y) Equal = TRUE; else Equal = FALSE Successor(x):NaturalNumber ::= if (x == MAXINT) Successor = x; else Successor = x+1 Subtract(x,y):NaturalNumber ::= if (x<y) Subtract = 0; else Subtract = x-y end NaturalNumber 4

Algorithm specification An algorithm is a finite set of instructions that accomplishes a particular task Criteria Input Output Definiteness: clear and unambiguous Finiteness: terminate after a finite number of steps Effectiveness: instruction is basic enough to be carried out, in principle, by a person using only pencil and paper 5

Example 1.2 [Selection sort] From those integers that are currently unsorted, find the smallest and place it next in the sorted list Smallest? for ( i=0; i<n; i++) { examine list[i] to list[n-1] and suppose that smallest integer is list[min] interchange list[i] & list[min] } 6

Selection sort void SelectionSort ( int *a, const int n) { // Sort the n integers a[0] to a[n-1] into nondecreasing 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; swap(a[i], a[j]); } 7

Example Input: (a[0]~a[5]) Iteration 1 Scan from a[0] to a[5] The smallest one is 6 (a[3]) Swap a[3] and a[0]

9 Reference: J.L.

Example 1.3 [Binary search] int BinarySearch (int *a, const int x, const int n) { // Search the sorted array a[0], …, a[n-1] for x Initialize left and right; while ( there are more elements ) { Let middle be the middle element; if (x < a[middle]) set right to middle-1 ; else if (x > a[middle]) set left to middle+1 ; else return middle ; } Not found; } 10

C++ function for binary search int BinarySearch (int *a, const int x, const int n) {// Search the sorted array a[0], …, a[n-1] for x int left = 0, right = n-1 ; while (left <= right) { // there are more elements int middle = (left + right)/2; if (x < a[middle]) right=middle-1 ; else if (x > a[middle]) left = middle+1 ; else return middle ; } // end of while return -1; // not found } 11

Example Input: Search for 7 Search for 16 12

Search for 7 13 Reference: J.L.

Search for Reference: J.L.

Recursive algorithms Recursion is usually used to solve a problem in a “divided-and-conquer” manner Direct recursion Functions that call themselves Indirect recursion Functions that call other functions that invoke calling function again Eg. C(n,m) = n!/[m!(n-m)!] C(n,m)=C(n-1,m)+C(n-1,m-1) Boundary condition for recursion 15

Example 1: Summation sum(1, n)=sum(1, n-1)+n sum(1, 1)=1 int sum(int n) { if (n==1) return (1); else return(sum(n-1)+n); } 16

Example 2: Factorial n!=n×(n-1)! fact(n)=n×fact(n-1) 0!=1 int fact(int n) { if ( n== 0) return (1); else return (n*fact(n-1)); } 17

Example 3: Multiplication a×b=a×(b-1)+a a×1=a int mult(int a, int b) { if ( b==1) return (a); else return(mult(a,b-1)+a); } 18

Example 1.4 [Recursive binary search] int BinarySearch(int *a, const int x, const int left, const int right) { // Search the sorted array a[left], …, a[right] for x 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, middle+1, right) ; else return middle ; } // end of if return -1 ; // not found } 19

Example 1.5 [Permutation generator] Permutation of {a, b, c}: (a, b, c), (a, c, b), (b, a, c), (b, c, a), (c, a, b), (c, b, a) Permutation of {a, b, c, d}: a + Perm{b, c, d} b + Perm{a, c, d} c + Perm{a, b, d} d + Perm{a, b, c} 20

Recursive permutation generator void Permutations (char *a, const int k, const int m) { // Generate all permutations of a[k], …, a[m]. if (k = = m) // output permutation { for (int i =0; i <=m; i++) cout << a[i] << “ “ ; cout << endl ; } else // a [k : m] has more than one permutation. Generate these recursively. for (i = k ; i <= m ; i++) { swap(a[k], a[i]); Permutations(a, k+1, m) ; swap(a[k], a[i]) ; } 21

Performance analysis and measurement Does it do what we want it to do? Does it work correctly according to the original specifications of the task? Is there documentation that describes how to use it and how it works? Are functions created in such a way that they perform logical subfunctions? Is the code readable? 22

Complexity Space complexity Amount of memory Time complexity Amount of computing time 23

Space complexity S(P) = c + S p (instance characteristics) P: the program c: constant (instruction, simple variables, constants) S p (instance characteristics): depends on characteristics of the instance 24

Example 1.8 float Abc(float a, float b, float c) { return a+b+b*c+(a+b-c)/(a+b)+4.0; } S p (instance characteristics)=0 25

Example 1.9 float Sum (float *a, const int n) { float s = 0; for(int i = 0; i <n ; i++) s += a[i] ; return s; } S sum (n)=0 26

Example 1.10 float Rsum(float *a, const int n) { if (n <= 0) return 0 ; else return (Rsum (a, n-1) + a[n-1]) ; } 4(n+1) 27

Time complexity T(P) = c + t p (instance characteristics) P: the program – c: compile time – t p (instance characteristics): program execution time 28

Program steps Comments: 0 Declarative statements: 0 Expressions and assignment statements: Most expressions have a step count of one Exceptions: contain function calls 29

Iteration statements for( ; ; ): one, unless the counts attributable to,, or are a function of the instance characteristics while do : do...while : Switch statement switch( ){// cost case cond1: case cond2: … default: }// plus all preceding conditions 30

If-else statement if( ) ; else ; // corresponding to,, Function invocation: one, unless the invocation involves pass-by-value parameters whose size depends on the instance characteristics Memory management statements: 1 Function statements: 0 (function invocation) Jump statements: continue, break, goto, return: 1 return : 31

Methods to compute the step count Tabular method Determine the total number of steps contributed by each statement steps per execution (i.e., s/e) × frequency Add up the contribution of all statements 32

Example 1.11 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 s ; } 2n+3 steps 33

Example 1.12 float Rsum (float *a, const int n) { count++ ; // for if condition if (n <= 0) { count++ ; // for return return 0; } else { count++ ; // for return return (Rsum (a, n-1) + a [n - 1]) ; } t Rsum (n) =2+t Rsum (n-1) =2+2+t Rsum (n-2) =2x2+t Rsum (n-2) … =2n+t Rsum (0) =2n+2 34

Tabular method 35 lines/efrequency total steps n+1 n n+1 n 1 0 Total number of steps 2n + 3 float Sum (float *a, const int n) { float s = 0; for(int i = 0; i <n ; i++) s += a[i] ; return s; }

Tabular method float Rsum(float *a, const int n) { if (n <= 0) return 0 ; else return (Rsum (a, n-1) + a[n-1]) ; } 36 lines/e frequency n = 0 n > 0 total steps n = 0 n > 0 1 2(a) 2(b) t Rsum (n-1) t Rsum (n-1) 0 Total number of steps22 + t Rsum (n-1)

Time complexity Cases Worst case Best case Average case Worst case and average case analysis is much more useful in practice 37

Time complexity Difficult to determine the exact step counts What a step stands for is inexact e.g. x = y and x = y + z + (x/y) + (x*y*z-x/z) Exact step count is not useful for comparison Step count doesn’t tell how much time step takes Just consider the growth in run time as the instance characteristics change 38

Big “oh” f(n)=O(g(n)) if and only if there exist positive constants c and n 0 such that f(n)≤cg(n) for all n, n  n 0 39 Reference: J.L.

Example n+2 =O(n) 3n+2≤4n for all n≥2 10n 2 +4n+2=O(n 2 ) 10n 2 +4n+2≤11n 2 for all n≥10 3n+2 = O(n 2 ) 3n+2≤n 2 for all n≥4 40

Omega f(n)=  (g(n)) if and only if there exist positive constants c and n 0 such that f(n)  cg(n) for all n, n  n 0 41 Reference: J.L.

Example n+3=Ω(n) 3n+3≥3n for all n≥1 6*2 n +n 2 =Ω(2 n ) 6*2 n +n 2 ≥2 n for all n≥1 3n+3=Ω(1) 3n+3≥3 for all n≥1 42

Theta f(n)=  (g(n)) if and only if 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 43 Reference: J.L.

Example n+2= Θ(n) 3n≤3n+2≤4n, for all n≥2 10n 2 +4n+2= Θ(n 2 ) 10n 2 ≤10n 2 +4n+2≤11n 2, for all n≥5 44

Some rules If T 1 (n)=O(f(n)) and T 2 (n)=O(g(n)), then T 1 (n)+T 2 (n) = max ( O(f(n)), O(g(n)) ) T 1 (n)×T 2 (n) = O( f(n)×g(n) ) If T(n) is a polynomial of degree k, then T(n)= Θ(n k ) 45

Example: Sum float Sum (float *a, const int n) { float s = 0; for(int i = 0; i <n ; i++) s += a[i] ; return s; } lines/efrequencytotal steps n+1 n 1 -  (0)  (1)  (n)  (1)  (0) t Sum (n) =  (max 1  i  6 {g(n)}) =  (n) 46

Example: Rsum float Rsum(float *a, const int n) { if (n <= 0) return 0 ; else return (Rsum (a, n-1) + a[n-1]) ; } 47 lines/efrequency n = 0 n > 0 total steps n = 0 n > 0 1 2(a) 2(b) t Rsum (n-1)  (0) 1  (1) 1  (0) 0  (2+t Rsum (n-1)) 0  (0) t Rsum (n) = 2  (2 + t Rsum (n-1))

Plot of function values 48