Algorithm Analysis and Modeling

Slides:



Advertisements
Similar presentations
Lecture: Algorithmic complexity
Advertisements

CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Estimating Running Time Algorithm arrayMax executes 3n  1 primitive operations in the worst case Define a Time taken by the fastest primitive operation.
Analysys & Complexity of Algorithms Big Oh Notation.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
CSC401 – Analysis of Algorithms Lecture Notes 1 Introduction
Complexity Analysis (Part II)
CS 310 – Fall 2006 Pacific University CS310 Complexity Section 7.1 November 27, 2006.
Week 2 CS 361: Advanced Data Structures and Algorithms
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.
Analysis Tools Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
Asymptotic Analysis-Ch. 3
Algorithms Growth of Functions. Some Notation NNatural numbers RReal numbers N + Positive natural numbers R + Positive real numbers R * Non-negative real.
1 Dr. J. Michael Moore Data Structures and Algorithms CSCE 221 Adapted from slides provided with the textbook, Nancy Amato, and Scott Schaefer.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Introduction to Analysis of Algorithms CS342 S2004.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 2 Prepared by İnanç TAHRALI.
Chapter 2 Computational Complexity. Computational Complexity Compares growth of two functions Independent of constant multipliers and lower-order effects.
Algorithm Analysis (Big O)
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
Algorithms Lecture #05 Uzair Ishtiaq. Asymptotic Notation.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
Algorithm Analysis 1.
Analysis of Algorithms
Chapter 2 Algorithm Analysis
Analysis of algorithms and BIG-O
GC 211:Data Structures Week 2: Algorithm Analysis Tools
Recitation 13 Searching and Sorting.
GC 211:Data Structures Algorithm Analysis Tools
CSE 373: Data Structures and Algorithms Pep Talk; Algorithm Analysis
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Catie Baker Spring 2015.
Assignment 2 Tze calculations.
Analysis of Algorithms
Analysis of Algorithms
Data Structures and Algorithms
Lecture 3: Working with Data Structures
Lecture 2: Implementing ADTs
Data Structures and Algorithms
Lecture 3: Queues, Testing, and Working with Code
Data Structures and Algorithms
GC 211:Data Structures Algorithm Analysis Tools
Data Structures and Algorithms
Analysis of Algorithms
Analysis of Algorithms
Analysys & Complexity of Algorithms
Implementing Hash and AVL
Analysis of Algorithms
GC 211:Data Structures Algorithm Analysis Tools
CSE 2010: Algorithms and Data Structures Algorithms
Searching, Sorting, and Asymptotic Complexity
Searching, Sorting, and Asymptotic Complexity
CSE 373: Data Structures & Algorithms
Complexity Analysis Text is mainly from Chapter 2 by Drozdek.
Lecture 3: Maps and Iterators
Asymptotic complexity
8. Comparison of Algorithms
Lecture 14: Midterm Review
Lecture 4: Introduction to Code Analysis
Complexity Analysis (Part II)
Lecture 14: Midterm Review
Lecture 2: Stacks and Queues
Lecture 3: Code Analysis
Analysis of Algorithms
Analysis of Algorithms
Algorithm Analysis How can we demonstrate that one algorithm is superior to another without being misled by any of the following problems: Special cases.
Presentation transcript:

Algorithm Analysis and Modeling Data Structures and Algorithms CSE 373 SP 18 - Kasey Champion

Warm Up From Last Lecture: Imagine you have implemented a Map with the following functions: - put(key,value) - get(key) - size() What are some test cases you would use to check it works? Socrative: www.socrative.com Room Name: CSE373 Please enter your name as: Last, First CSE 373 SP 18 - Kasey Champion

Administrivia Due dates: - Partner form Thursday April 5th at 1:59pm - Homework #1 Friday April 6th at 11:59pm - Class Survey Friday April 6th at 11:59pm Notes: - Sign up for piazza! - Set up your development environment - Project 1 to go out Friday CSE 373 SP 18 - Kasey Champion

Asymptotic Analysis asymptotic analysis: how the runtime of an algorithm grows as the data set grows Approximations Basic operations take “constant” time Assigning a variable Accessing a field or array index Consecutive statements Some of time for each statement Function calls Time of function’s body Conditionals Time of condition + maximum time of branch code Loops Number of iterations x time for loop body CSE 373 SP 18 - Kasey Champion

Modeling Case Study Goal: return ‘true’ if a sorted array of ints contains duplicates Solution 1: compare each pair of elements public boolean hasDuplicate1(int[] array) { for (int i = 0; i < array.length; i++) { for (int j = 0; j < array.length; j++) { if (i != j && array[i] == array[j]) { return true; } return false; Solution 2: compare each consecutive pair of elements public boolean hasDuplicate2(int[] array) { for (int i = 0; i < array.length - 1; i++) { if (array[i] == array[i + 1]) { CSE 373 wi 18 – Michael Lee

Modeling Case Study: Solution 2 T(n) where n = array.length -> work inside out Solution 2: compare each consecutive pair of elements public boolean hasDuplicate2(int[] array) { for (int i = 0; i < array.length - 1; i++) { if (array[i] == array[i + 1]) { return true; } return false; T(n) = 5 (n-1) + 1 linear time complexity class O(n) +4 x n - 1 +1 If statement = 5 +1 CSE 373 wi 18 – Michael Lee

Modeling Case Study: Solution 1 Solution 1: compare each consecutive pair of elements public boolean hasDuplicate1(int[] array) { for (int i = 0; i < array.length; i++) { for (int j = 0; j < array.length; j++) { if (i != j && array[i] == array[j]) { return true; } return false; T(n) = 6 n2 + 1 quadratic time complexity class O(n2) x n x n +5 6n2 6n +1 6 +1 CSE 373 wi 18 – Michael Lee

Comparing Functions CSE 373 SP 18 - Kasey Champion

Function growth n n n and 4n look very different up close n and 4n look the same over time n2 doesn’t start off dominating the linear functions It eventually takes over… n2 eventually dominates n CSE 373 SP 18 - Kasey Champion

Function comparison: exercise f(n) = n ≤ g(n) = 5n + 3? f(n) = 5n + 3 ≤ g(n) = n? f(n) = 5n + 3 ≤ g(n) = 1? f(n) = 5n + 3 ≤ g(n) = n2? f(n) = n2 + 3n + 2 ≤ g(n) = n3? f(n) = n3 ≤ g(n) = n2 + 3n + 2 ? True – all linear functions are treated as equivalent True False True – quadratic will always dominate linear True False CSE 373 Wi 18 – Michael Lee

Definition: function domination A function f(n) is dominated by g(n) when… There exists two constants c > 0 and n0 > 0 Such that for all values of n ≥ n0 f(n) ≤ c * g(n) Example: Is f(n) = n dominated by g(n) = 5n + 3 ? c = 1 n0 = 1 Yes! Definition: Domination CSE 373 SP 18 - Kasey Champion

Exercise: Function Domination Demonstrate that 5n2 + 3n + 6 is dominated by n3 by finding a c and n0 that satisfy the definition of domination 5n2 + 3n + 6 ≤ 5n2 + 3n2 + 6n2 when n ≥ 1 5n2 + 3n2 + 6n2 = 14n2 5n2 + 3n + 6 ≤ 14n2 for n ≥ 1 14n2 ≤ c*n3 for c = ? n >= ? 14 𝑛 -> c = 14 & n >= 1 CSE 373 SP 18 - Kasey Champion

Definition: Big O If f(n) = n ≤ g(n) = 5n + 3 ≤ h(n) = 100n and h(n) = 100n ≤ g(n) = 5n + 3 ≤ f(n) n Really they are all the “same” O(f(n)) is the “family” or “set” of all functions that are dominated by f(n) Question: are O(n), O(5n + 3) and O(100n) all the same? True! By convention we pick simplest of the above -> O(n) ie “linear” Definition: Big O CSE 373 SP 18 - Kasey Champion

Definitions: Big Ω “f(n) is greater than or equal to g(n)” F(n) dominates g(n) when: There exists two constants such that c > 0 and n0 > 0 Such that for all values n >= n0 F(n) >= c * g(n) is true Ω(f(n)) is the family of all functions that dominates f(n) Definition: Big Ω CSE 373 SP 18 - Kasey Champion

∈ Element Of f(n) is dominated by g(n) Is that the same as “f(n) is contained inside O(g(n))” Yes! f(n) ∈ g(n) CSE 373 SP 18 - Kasey Champion

Examples 4n2 ∈ O(1) 4n2 ∈ Ω(1) false true 4n2 ∈ O(n) 4n2 ∈ Ω(n) Definition: Big O O(f(n)) is the “family” or “set” of all functions that are dominated by f(n) 4n2 ∈ O(1) false 4n2 ∈ O(n) 4n2 ∈ O(n2) true 4n2 ∈ O(n3) 4n2 ∈ O(n4) 4n2 ∈ Ω(1) true 4n2 ∈ Ω(n) 4n2 ∈ Ω(n2) 4n2 ∈ Ω(n3) false 4n2 ∈ Ω(n4) Definition: Big Ω Ω(f(n)) is the family of all functions that dominates f(n) CSE 373 SP 18 - Kasey Champion

Definitions: Big Θ We say f(n) ∈ Θ(g(n)) when both f(n) ∈ O(g(n)) and f(n) ∈ Ω (g(n)) are true Which is only when f(n) = g(n) Θ(f(n)) is the family of functions that are equivalent to f(n) Industry uses “Big Θ” and “Big O” interchangeably Definition: Big Θ CSE 373 SP 18 - Kasey Champion

CSE 373 SP 18 - Kasey Champion