Lecture 4: Introduction to Code Analysis

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

CSE Lecture 3 – Algorithms I
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Analysis of Algorithm.
Elementary Data Structures and Algorithms
Abstract Data Types (ADTs) Data Structures The Java Collections API
CSE 143 Lecture 5 Binary search; complexity reading: slides created by Marty Stepp and Hélène Martin
Building Java Programs Chapter 13 Searching reading: 13.3.
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.
Data Structures and Algorithms Lecture 5 and 6 Instructor: Quratulain Date: 15 th and 18 th September, 2009 Faculty of Computer Science, IBA.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
Week 12 - Wednesday.  What did we talk about last time?  Asymptotic notation.
Binary search and complexity reading:
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Building Java Programs Chapter 13 Lecture 13-1: binary search and complexity reading:
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Linda Shapiro Winter 2015.
Building Java Programs Chapter 12: Recursive public/private pairs Chapter 13: Searching reading: 13.3.
Searching and Sorting Searching algorithms with simple arrays
Algorithm Analysis 1.
Chapter 2 Algorithm Analysis
Sort & Search Algorithms
Analysis of Algorithms
Algorithmic Efficency
Introduction to Search Algorithms
Recitation 13 Searching and Sorting.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Catie Baker Spring 2015.
Introduction to Algorithms
Algorithm Analysis CSE 2011 Winter September 2018.
Algorithm Analysis and Modeling
Data Structures and Algorithms
Lecture 3: Working with Data Structures
Lecture 2: Implementing ADTs
Lecture 2: Implementing ADTs
Algorithm Analysis (not included in any exams!)
Building Java Programs
Lecture 14: binary search and complexity reading:
CSE 143 Lecture 5 Binary search; complexity reading:
Algorithm design and Analysis
Data Structures and Algorithms
CSc 110, Spring 2017 Lecture 39: searching.
Lecture 3: Queues, Testing, and Working with Code
CSE 143 Lecture 5 More Stacks and Queues; Complexity (Big-Oh)
Lecture 15: binary search reading:
Lecture 5: complexity reading:
CS 201 Fundamental Structures of Computer Science
Implementing Hash and AVL
CSE 143 Lecture 8 More Stacks and Queues; Complexity (Big-Oh)
Analyzing an Algorithm Computing the Order of Magnitude Big O Notation
Building Java Programs
Searching, Sorting, and Asymptotic Complexity
Searching, Sorting, and Asymptotic Complexity
CSE 373: Data Structures & Algorithms
Building Java Programs
Search,Sort,Recursion.
CSE 143 Lecture 8 More Stacks and Queues; Complexity (Big-Oh)
Lecture 2: Stacks and Queues
Lecture 3: Maps and Iterators
8. Comparison of Algorithms
Building Java Programs
IST311 - CIS265/506 Cleveland State University – Prof. Victor Matos
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
Lecture 3: Maps and Iterators
Lecture 2: Stacks and Queues
Lecture 3: Code Analysis
Module 8 – Searching & Sorting Algorithms
Lecture 16: Midterm recap + Heaps ii
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:

Lecture 4: Introduction to Code Analysis CSE 373: Data Structures and Algorithms CSE 373 19 sp - Kasey Champion

Warm Up 5 Minutes Read through the code on the worksheet given Come up with a test case for each of the described test categories Expected Behavior Forbidden Input Empty/Null Boundary/Edge Scale add(1) add(null) Add into empty list Add enough values to trigger internal array double and copy Add 1000 times in a row Socrative: www.socrative.com Room Name: CSE373 Please enter your name as: Last, First CSE 373 SP 18 - Kasey Champion

Administrivia - Fill out HW 2 Partner form Posted on class webpage at top Due TONIGHT Monday 4/8 by 11:59pm - Fill out Student Background Survey, on website announcements - Read Pair Programming Doc (on readings for Wednesday on calendar) CSE 373 SP 18 - Kasey Champion

Algorithm Analysis CSE 373 SP 18 - Kasey Champion

Code Analysis How do we compare two pieces of code? Time needed to run Memory used Number of network calls Amount of data saved to disk Specialized vs generalized Code reusability Security CSE 373 SP 18 - Kasey Champion

Comparing Algorithms with Mathematical Models Consider overall trends as inputs increase Computers are fast, small inputs don’t differentiate Must consider what happens with large inputs Identify trends without investing in testing Model performance across multiple possible scenarios Worst case - what is the most expensive or least performant an operation can be Average case – what functions are most likely to come up? Best case – if we understand the ideal conditions can increase the likelihood of those conditions? CSE 373 SP 19 - Kasey Champion

Review: Sequential Search sequential search: Locates a target value in a collection by examining each element sequentially Example: Searching the array below for the value 42: index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 20 22 25 30 36 42 50 56 68 85 92 103 i public int search(int[] a, int val) { for (int i = 0; i < a.length; i++) { if (a[i] == val) { return i; } return –1; How many elements will be examined? What is the best case? What is the worst case? What is the average case? element found at index 0, 1 item examined, O(1) f(n) = n element found at index 16 or not found, all elements examined, O(n) most elements examined, O(n) CSE 373 SP 19 – Kasey Champion (thanks to zoraH Fung)

Review: Binary Search binary search: Locates a target value in a sorted array or list by successively eliminating half of the array from consideration. Example: Searching the array below for the value 42: index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 20 22 25 30 36 42 50 56 68 85 92 103 min mid max public static void binarySearch(int[] a, int val){   …    while (first <= last){         if (arr[mid] < key ){            first = mid + 1;            } else if ( arr[mid] == key ){            return mid;         } else{            last = mid - 1;         }         mid = (first + last)/2;      }   return -1;   }   How many elements will be examined? What is the best case? What is the worst case? What is the average case? element found at index 8, 1 item examined, O(1) element found at index 9 or not found, ½ elements examined, O(?) CSE 373 SP 19 – Kasey Champion (thanks to zoraH Fung)

Analyzing Binary Search 𝑛 2 𝑘 =1 What is the pattern? At each iteration, we eliminate half of the remaining elements How long does it take to finish? 1st iteration – N/2 elements remain 2nd iteration – N/4 elements remain Kth iteration - N/2k elements remain Finishes when -> multiply right side by 2K N = 2K -> isolate K exponent with logarithm log2N = k 𝑛 2 𝑘 =1 index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 20 22 25 30 36 42 50 56 68 85 92 103 CSE 373 SP 18 - Kasey Champion

Asymptotic Analysis asymptotic analysis – the process of mathematically representing runtime of a algorithm in relation to the number of inputs and how that relationship changes as the number of inputs grow Two step process Model – reduce code run time to a mathematical relationship with number of inputs Analyze – compare runtime/input relationship across multiple algorithms CSE 373 SP 18 - Kasey Champion

Code Modeling CSE 373 SP 18 - Kasey Champion

Code Modeling code modeling – the process of mathematically representing how many operations a piece of code will run in relation to the number of inputs n Examples: Sequential search Binary search 𝑓 𝑛 =𝑛 𝑓 𝑛 =𝑙𝑜𝑔2𝑛 What counts as an “operation”? Basic operations Adding ints or doubles Variable assignment Variable update Return statement Accessing array index or object field Consecutive statements Sum time of each statement Assume all operations run in equivalent time Function calls Count runtime of function body Conditionals Time of test + worst case scenario branch Loops Number of iterations of loop body x runtime of 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 Goal: produce mathematical function representing runtime where n = array.length 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; linear -> O(n) 𝑓 𝑛 loop = (n – 1)(body) +4 +1 If statement = 5 +1 Approach -> start with basic operations, work inside out for control structures Each basic operation = +1 Conditionals = worst case test operations + branch Loop = iterations (loop body) 𝑓 𝑛 =5 𝑛−1 +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; quadratic -> O(n2) x n x n +5 6n2 6n +1 6 +1 Approach -> start with basic operations, work inside out for control structures Each basic operation = +1 Conditionals = worst case test operations + branch Loop = iterations (loop body) 𝑓 𝑛 =5 𝑛−1 +1 CSE 373 wi 18 – Michael Lee

Your turn! 5 Minutes 𝑓 𝑘 = 𝑘 𝑘+2 5 Write the specific mathematical code model for the following code and indicate the big o runtime. public void foobar (int k) { int j = 0; while (j < k) { for (int i = 0; i < k; i++) { System.out.println(“Hello world”); } j = j + 5; +1 +k/5 (body) 𝑓 𝑘 = 𝑘 𝑘+2 5 +k(body) +1 quadratic -> O(k^2) +2 Approach -> start with basic operations, work inside out for control structures Each basic operation = +1 Conditionals = worst case test operations + branch Loop = iterations (loop body) CSE 373 SP 18 - Kasey Champion