Data Structures by R.S. Chang, Dept. CSIE, NDHU1 1 Chapter 4 Complexity Analysis 4.1 Computational and Asymptotic Complexity The same problem can frequently.

Slides:



Advertisements
Similar presentations
BY Lecturer: Aisha Dawood. The notations we use to describe the asymptotic running time of an algorithm are defined in terms of functions whose domains.
Advertisements

Algorithms Algorithm: what is it ?. Algorithms Algorithm: what is it ? Some representative problems : - Interval Scheduling.
Logistics HW due next week Midterm also next week on Tuesday
Algorithm Complexity Level II, Term ii CSE – 243 Md. Monjur-ul-hasan
Intro to Algorithm analysis
Data Structues and Algorithms Algorithms growth evaluation.
Analysis of Algorithms CS 477/677
Program Efficiency & Complexity Analysis
MATH 224 – Discrete Mathematics
Data Structures by R.S. Chang, Dept. CSIE, NDHU1 Chapter 2 Algorithm Design Human Problems Result Input Data Structures Processing Output Data Structures.
CSE115/ENGR160 Discrete Mathematics 03/01/12
The Efficiency of Algorithms Chapter 4 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Lecture3: Algorithm Analysis Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Asymptotic Notation (O, Ω,  ) s Describes the behavior of the time or space complexity for large instance characteristics s Common asymptotic functions.
Chapter 3 Growth of Functions
CSCE 411 Design and Analysis of Algorithms Andreas Klappenecker TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAAA.
CS2420: Lecture 4 Vladimir Kulyukin Computer Science Department Utah State University.
The Efficiency of Algorithms
DATA STRUCTURES AND ALGORITHMS Lecture Notes 1 Prepared by İnanç TAHRALI.
CSE 5311 DESIGN AND ANALYSIS OF ALGORITHMS. Definitions of Algorithm A mathematical relation between an observed quantity and a variable used in a step-by-step.
Algorithm analysis and design Introduction to Algorithms week1
Program Performance & Asymptotic Notations CSE, POSTECH.
CSC 201 Analysis and Design of Algorithms Lecture 03: Introduction to a CSC 201 Analysis and Design of Algorithms Lecture 03: Introduction to a lgorithms.
1 Computer Algorithms Lecture 3 Asymptotic Notation Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
Complexity Analysis Chapter 1.
Analysis of Algorithms
Asymptotic Analysis-Ch. 3
Complexity A decidable problem is computationally solvable. But what resources are needed to solve the problem? –How much time will it require? –How much.
Complexity of Algorithms
Algorithms Growth of Functions. Some Notation NNatural numbers RReal numbers N + Positive natural numbers R + Positive real numbers R * Non-negative real.
Asymptotic Notation (O, Ω, )
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Introduction to Programming (in C++) Complexity Analysis of Algorithms
Algorithm Analysis Part 2 Complexity Analysis. Introduction Algorithm Analysis measures the efficiency of an algorithm, or its implementation as a program,
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Time Complexity of Algorithms (Asymptotic Notations)
Big-O. Algorithm Analysis Exact analysis: produce a function f(n) measuring how many basic steps are needed for a given inputs n On any input of size.
COP 3530 Spring2012 Data Structures & Algorithms Discussion Session Week 5.
Asymptotic Notations By Er. Devdutt Baresary. Introduction In mathematics, computer science, and related fields, big O notation describes the limiting.
David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance.
Asymptotic Performance. Review: Asymptotic Performance Asymptotic performance: How does algorithm behave as the problem size gets very large? Running.
–1 –5–4–3–2– Describe the continuity of the graph. Warm UP:
Properties of Logarithms. Basic Properties All logarithmic functions have certain properties. The basic properties are:
Algorithm Complexity L. Grewe 1. Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them?
4.2 Area Definition of Sigma Notation = 14.
Algorithms Lecture #05 Uzair Ishtiaq. Asymptotic Notation.
Computational complexity The same problem can frequently be solved with different algorithms which differ in efficiency. Computational complexity is a.
DR. Gatot F. Hertono, MSc. Design and Analysis of ALGORITHM (Session 2)
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
Asymptotic Bounds The Differences Between (Big-O, Omega and Theta) Properties.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Complexity of Algorithms Fundamental Data Structures and Algorithms Ananda Guna January 13, 2005.
1 Potential for Parallel Computation Chapter 2 – Part 2 Jordan & Alaghband.
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
Data Structures & Algorithm CS-102 Lecture 12 Asymptotic Analysis Lecturer: Syeda Nazia Ashraf 1.
Mathematical Foundations (Growth Functions) Neelima Gupta Department of Computer Science University of Delhi people.du.ac.in/~ngupta.
Asymptotic Analysis.
Introduction to Algorithms
Analysis of Algorithms
Courtsey & Copyright: DESIGN AND ANALYSIS OF ALGORITHMS Courtsey & Copyright:
DATA STRUCTURES Introduction: Basic Concepts and Notations
Time Complexity Analysis Neil Tang 01/19/2010
Algorithm Analysis Neil Tang 01/22/2008
Asymptotic Analysis.
Advanced Analysis of Algorithms
Asst. Dr.Surasak Mungsing
Complexity Analysis Text is mainly from Chapter 2 by Drozdek.
Advanced Analysis of Algorithms
Estimating Algorithm Performance
An Upper Bound g(n) is an upper bound on f(n). C++ Review EECE 352.
Presentation transcript:

Data Structures by R.S. Chang, Dept. CSIE, NDHU1 1 Chapter 4 Complexity Analysis 4.1 Computational and Asymptotic Complexity The same problem can frequently be solved with different algorithms which differ in efficiency. To compare the efficiency of algorithms, a measure of the degree of difficulty of an algorithm call computational complexity was developed. Computational complexity indicates how much effort is needed to apply an algorithm or how costly it is.

Data Structures by R.S. Chang, Dept. CSIE, NDHU2 4.1 Computational and Asymptotic Complexity Chapter 4 Complexity Analysis Time complexity: running time required (more important) Space complexity: memories required Time depends on: the algorithm, the machine used, the programming language used, the compiler used, the environment, the programming skill, etc.

Data Structures by R.S. Chang, Dept. CSIE, NDHU3 4.1 Computational and Asymptotic Complexity Chapter 4 Complexity Analysis To evaluate an algorithm’s efficiency, logical time units that express a relationship between the size n of an input and the amount of time t required to process the input should be used. t=f(n) Example: : 輸入資料量增加一倍,執行時間增加一倍 : 輸入資料量增加一倍,執行時間增加一

Data Structures by R.S. Chang, Dept. CSIE, NDHU4 The resulting function gives only an approximate measure of efficiency. However, this approximation is sufficiently close, especially for a function which processes large quantities of data. 4.1 Computational and Asymptotic Complexity Chapter 4 Complexity Analysis The measure of efficiency is called asymptotic complexity and is used when disregarding certain terms of a function to express the efficiency of an algorithm.( 只留函數中最重要 的部分 )

Data Structures by R.S. Chang, Dept. CSIE, NDHU5 4.1 Computational and Asymptotic Complexity Chapter 4 Complexity Analysis Example: Only the n 2 term is important when n becomes large. Therefore, we say f(n)=O(n 2 )

Data Structures by R.S. Chang, Dept. CSIE, NDHU6 4.2 Big-O Notation Chapter 4 Complexity Analysis Definition: Given two positive-valued functions f and g, f(n) is O(g(n)) if there exist positive numbers c and N such that cg(n)  f(n) for all n  N. Example:

Data Structures by R.S. Chang, Dept. CSIE, NDHU7 4.2 Big-O Notation Chapter 4 Complexity Analysis Problem with the big-O notation: It states only that there must exist certain c and N, but it does not give any hint how to calculate these constants. There can be infinitely many functions g for a given function f. For example, f(n)=O(n 2 )=O(n 3 )=O(n 4 )=... We want the lowest upper bound.

Data Structures by R.S. Chang, Dept. CSIE, NDHU8 4.3 Properties of Big-O Notation Chapter 4 Complexity Analysis Fact 1. (transitivity) If f(n) is O(g(n)) and g(n) is O(h(n)), then f(n) is O(h(n)). (i.e., f(n)=O(g(n))=O(O(h(n)))=O(h(n)). Fact 2. If f(n) is O(h(n)) and g(n) is O(h(n)) then f(n)+g(n) is O(h(n)). Fact 3. The function of an k is O(n k ). Fact 4. The function n k is O(n k+j ) for any positive j. Fact 5. If f(n)=cg(n) then f(n)=O(g(n)). Fact 6. log a n=O(log b n) for any numbers a and b greater than 1. Fact 7. log a n is O(log 2 n) for any positive a.

Data Structures by R.S. Chang, Dept. CSIE, NDHU9 4.4  (omega) and  (theta) Notations Chapter 4 Complexity Analysis Definition 2. The function f is  (g(n)) if there exist positive numbers c and N such that f(n)  cg(n) for all n  N. We want the greatest lower bound. f(n) is  (g(n)) iff (if and only if) g(n) is O(f(n)).

Data Structures by R.S. Chang, Dept. CSIE, NDHU  (omega) and  (theta) Notations Chapter 4 Complexity Analysis Definition 3. f(n) is  (g(n)) if there exist positive numbers c 1, c 2, and N such that c 2 g(n)  f(n)  c 1 g(n) for all n  N. f(n) is  (g(n)) iff f(n) is O(g(n)) and f(n) is  (g(n)).

Data Structures by R.S. Chang, Dept. CSIE, NDHU Possible Problems Chapter 4 Complexity Analysis Big-O notation is the asymptotic complexity function. Please don’t forget there are concealed constants. For example: 10 8 n is O(n) and 10n 2 is O(n 2 ). However, for 10 7  n, 10n 2 is better.

Data Structures by R.S. Chang, Dept. CSIE, NDHU Examples of Complexities Chapter 4 Complexity Analysis

Data Structures by R.S. Chang, Dept. CSIE, NDHU Examples of Complexities Chapter 4 Complexity Analysis

Data Structures by R.S. Chang, Dept. CSIE, NDHU Finding Asymptotic Complexity: Examples Chapter 4 Complexity Analysis Example 1: for (i=sum=0; i<n; i++) sum += a[i]; O(n) Example 2: for (i=0; i<n; i++) { for (j=1 sum=a[0]; j<=i; j++) sum += a[j]; printf(“sum for subarray 0 through %d is %d\n”,i,sum); }

Data Structures by R.S. Chang, Dept. CSIE, NDHU Finding Asymptotic Complexity: Examples Chapter 4 Complexity Analysis Example 3: for (i=4; i<n; i++) { for (j=i-3, sum=a[i-4]; j<=i; j++) sum += a[j]; printf(“sum for subarray %d through %d is %d\n”,i-4,i,sum); } O(n): 課本的計算式有誤

Data Structures by R.S. Chang, Dept. CSIE, NDHU16 Example 4: Determine the longest increasing subarray. For (I=0, length=1; I<n-1; I++) { for (i1=i2=k=I; k<n-1 && a[k]<a[k+1]; k++,i2++); if (length <i2-i1+1) length=i2-i1+1; } 4.7 Finding Asymptotic Complexity: Examples Chapter 4 Complexity Analysis Worst case: O(n 2 ) Best case: O(n) Average case: ???

Data Structures by R.S. Chang, Dept. CSIE, NDHU17 Chapter 4 Complexity Analysis Exercise: 2 and 5 in Page 56.