Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 2. Types of Complexities.

Slides:



Advertisements
Similar presentations
Discrete Structures CISC 2315
Advertisements

Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
MAT 4 – Kompleks Funktionsteori MATEMATIK 4 INDUKTION OG REKURSION MM 1.5 MM 1.5: Kompleksitet Topics: Computational complexity Big O notation Complexity.
CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis Tyler Robison Summer
Cmpt-225 Algorithm Efficiency.
25 June 2015Comp 122, Spring 2004 Asymptotic Notation, Review of Functions & Summations.
Algorithmic Complexity 2 Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
 Last lesson  Arrays for implementing collection classes  Performance analysis (review)  Today  Performance analysis  Logarithm.
Chapter 2: Algorithm Analysis Big-Oh and Other Notations in Algorithm Analysis Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm.
Data Structures and Algorithms1 Basics -- 2 From: Data Structures and Their Algorithms, by Harry R. Lewis and Larry Denenberg (Harvard University: Harper.
CS 310 – Fall 2006 Pacific University CS310 Complexity Section 7.1 November 27, 2006.
CS-2852 Data Structures LECTURE 3A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Asymptotic Growth Rates Themes –Analyzing the cost of programs –Ignoring constants and Big-Oh –Recurrence Relations & Sums –Divide and Conquer Examples.
CS 461 – Nov. 21 Sections 7.1 – 7.2 Measuring complexity Dividing decidable languages into complexity classes. Algorithm complexity depends on what kind.
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.
Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure.
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Algorithm Analysis & Complexity We saw that a linear search used n comparisons in the worst case (for an array of size n) and binary search had logn comparisons.
Mathematics Review and Asymptotic Notation
CSC 201 Analysis and Design of Algorithms Lecture 04: CSC 201 Analysis and Design of Algorithms Lecture 04: Time complexity analysis in form of Big-Oh.
CS 3343: Analysis of Algorithms
Complexity Analysis Chapter 1.
Analysis of Algorithms
CS 221 Analysis of Algorithms Instructor: Don McLaughlin.
Asymptotic Analysis-Ch. 3
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
MS 101: Algorithms Instructor Neelima Gupta
Zeinab EidAlgorithm Analysis1 Chapter 4 Analysis Tools.
Asymptotic Growth Rates  Themes  Analyzing the cost of programs  Ignoring constants and Big-Oh  Recurrence Relations & Sums  Divide and Conquer 
Asymptotic Analysis (based on slides used at UMBC)
Introduction to Analysis of Algorithms CS342 S2004.
Time Complexity of Algorithms
Algorithmic Analysis Charl du Plessis and Robert Ketteringham.
General rules: Find big-O f(n) = k = O(1) f(n) = a k n k + a k-1 n k a 1 n 1 + a 0 = O(n k ) Other functions, try to find the dominant term according.
Chapter 2 Computational Complexity. Computational Complexity Compares growth of two functions Independent of constant multipliers and lower-order effects.
Big-Oh Notation. Agenda  What is Big-Oh Notation?  Example  Guidelines  Theorems.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 1. Complexity Bounds.
Lecture aid 1 Devon M. Simmonds University of North Carolina, Wilmington CSC231 Data Structures.
13 February 2016 Asymptotic Notation, Review of Functions & Summations.
CISC220 Spring 2010 James Atlas Lecture 07: Big O Notation.
Big O David Kauchak cs302 Spring Administrative Assignment 1: how’d it go? Assignment 2: out soon… Lab code.
Computability Sort homework. Formal definitions of time complexity. Big 0. Homework: Exercises. Searching. Shuffling.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 3. Time Complexity Calculations.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 3. Introduction to the Analysis of Algorithms.
Complexity of Algorithms Fundamental Data Structures and Algorithms Ananda Guna January 13, 2005.
Mathematical Foundations (Growth Functions) Neelima Gupta Department of Computer Science University of Delhi people.du.ac.in/~ngupta.
Chapter 2 Algorithm Analysis
Theoretical analysis of time efficiency
Chapter 3: Growth of Functions
David Kauchak CS52 – Spring 2015
Chapter 3: Growth of Functions
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
BIG-OH AND OTHER NOTATIONS IN ALGORITHM ANALYSIS
Chapter 2: Fundamentals of the Analysis of Algorithm Efficiency
Defining Efficiency Asymptotic Complexity - how running time scales as function of size of input Two problems: What is the “input size” ? How do we express.
Advanced Analysis of Algorithms
Chapter 2.
Time Complexity Lecture 14 Sec 10.4 Thu, Feb 22, 2007.
Time Complexity Lecture 15 Mon, Feb 27, 2006.
At the end of this session, learner will be able to:
David Kauchak cs161 Summer 2009
Advanced Analysis of Algorithms
Complexity Analysis (Part II)
Estimating Algorithm Performance
Algorithm Course Dr. Aref Rashad
CMPT 225 Lecture 6 – Review of Complexity Analysis using the Big O notation + Comparing List ADT class implementations.
Presentation transcript:

Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 2. Types of Complexities

Prof. Amr Goneid, AUC2 Types of Complexities

Prof. Amr Goneid, AUC3 Types of Complexities Rules for Upper Bound Comparing Complexities Types of Complexities

Prof. Amr Goneid, AUC4 1. Rules for Upper Bound If (k) is a constant, then: O(k)  O(n) O(k f(n)) = O(f(n)) T(n) n O(n) O(k) O(kn)

Prof. Amr Goneid, AUC5 Rules for Big O O(f(n)) + O(g(n)) = max(O(f(n)),O(g(n))) e.g. f(n) = 2n = O(n), g(n) = 0.1 n 3 = O(n 3 ) T(n) = max(O(n), O(n 3 )) = O(n 3 ) 2n 0.1 n 3 O(n 3 )

Prof. Amr Goneid, AUC6 Rules for Big O O(f(n)) * O(g(n)) = O(f(n) * g(n)) e.g. f(n) = n, g(n) = n 2 T(n) = n * n 2 = O(n 3 ) O(n 2 ) Repeat n times O(n 3 )

Prof. Amr Goneid, AUC7 Rules for Big O Logarithmic Complexity < Linear Complexity O(log n)  O(n) Claim: for all n ≥ 1, log n ≤ n. Prove by induction on n.

Prof. Amr Goneid, AUC8 Rules for Big O For a polynomial of degree m, Prove! O(n m-1 )  O(n m ) follows from above

Prof. Amr Goneid, AUC9 Summary of Rules for Big-O RuleExample For constant k, O(k) < O(n) O(7) = O(1) < O(n) For constant k, O(kf) = O(f) O(2n) = O(n) O(|f|+|g|) = O(|f|) + O(|g|) = Max (O(|f|), O(|g|) O(6n 2 +n)=O(6n 2 )+O(n) = O(n 2 ) Nesting of loop O(g) within a loop O(f) gives O(f*g) O(n 4 *n 2 )=O(n 6 ) O(n m-1 ) < O(n m )O(n 2 ) < O(n 3 ) O((log n) k )  O(n) O(log n) < O(n)

Prof. Amr Goneid, AUC10 2. Comparing Complexities Dominance: If lim (n->  ) f(n)/g(n) =  then f(n) dominates (i.e. grows faster), but if lim (n->  ) f(n)/g(n) = 0 then g(n) dominates. In the latter case, we say that f(n) = o(g(n)) little oh

Prof. Amr Goneid, AUC11 Comparing Complexities Examples: if a > b then n a dominates n b Why? n 2 dominates (3n+2) Why? n 2 dominates (n log n) Why?

Prof. Amr Goneid, AUC12 Using l’Hopital’s Rule (1696) Example: Show that f(n) = n (k+α) + n k (log n) 2 and g(n) = k n (k+α) grow at the same rate.

Prof. Amr Goneid, AUC13 Comparing Complexities Which grows faster: f(n) = n 3 or g(n) = n log n f(n) = n org(n) = log n f(n) = 2 n+1 org(n) = 2 n f(n) = 2 n org(n) = 2 2n

Prof. Amr Goneid, AUC14 Exercises Which function has smaller complexity ? f = 100 n 4 g = n 5 f = log(log n 3 )g = log n f = n 2 g = n log n f = 50 n 5 + n 2 + ng = n 5 f = e n g = n!

Prof. Amr Goneid, AUC15 3. Types of Complexities Constant Complexity T(n) = constant independent of (n) Runs in constant amount of time  O(1) Example: cout << a[0][0]

Prof. Amr Goneid, AUC16 Types of Complexities Logarithmic Complexity Log 2 n=m is equivalent to n=2 m Reduces the problem to half  O(log 2 n) Example: Binary Search T(n) = O(log 2 n) Much faster than Linear Search which has T(n) = O(n)

Prof. Amr Goneid, AUC17 Linear vs Logarithmic Complexities n T(n) O(log 2 n) O(n)

Prof. Amr Goneid, AUC18 Types of Complexities Polynomial Complexity T(n)=a m n m +…+ a 2 n 2 + a 1 n 1 + a 0 If m=1, then O(a 1 n+a 0 )  O(n) If m > 1, then  O(n m ) as n m dominates

Prof. Amr Goneid, AUC19 Polynomials Complexities O(n) O(n 2 ) O(n 3 ) n Log T(n)

Prof. Amr Goneid, AUC20 Types of Complexities Exponential Example: List all the subsets of a set of n elements {a,b,c} {a,b,c}, {a,b},{a,c},{b,c},{a},{b},{c},{} Number of operations T(n) = O(2 n ) Exponential expansion of the problem  O(a n ) where a is a constant greater than 1

Prof. Amr Goneid, AUC21 Exponential Vs Polynomial Log T(n) n O(n 3 ) O(n) O(2 n )

Prof. Amr Goneid, AUC22 Types of Complexities Factorial time Algorithms Example: Traveling salesperson problem (TSP): Find the best route to take in visiting n cities away from home. What are the number of possible routes? For 3 cities: (A,B,C)

Prof. Amr Goneid, AUC23 Possible routes in a TSP –Number of operations = 3!=6, Hence T(n) = n! –Expansion of the problem  O(n!)

Prof. Amr Goneid, AUC24 Exponential Vs Factorial Log T(n) n O(2 n ) O(n!) O(n n )

Prof. Amr Goneid, AUC25 Execution Time Example Example: For the exponential algorithm of listing all subsets of a given set, assume the set size to be of 1024 elements Number of operations is about 1.8* If we can list a subset every nanosecond the process will take 5.7 * yr!!!

Prof. Amr Goneid, AUC26 P and NP – Times P (Polynomial) Times: O(1), O(log n), O(log n) 2, O(n), O(n log n), O(n 2 ), O(n 3 ), …. NP (Non-Polynomial) Times: O(2 n ), O(e n ), O(n!), O(n n ), …..

Prof. Amr Goneid, AUC27 P and NP – Times Polynomial time is GOOD Try to reduce the polynomial power NP (e.g. Exponential) Time is BAD