Algorithm Design and Time Analysis

Similar documents
Notes slides from before lecture. CSE 21, Winter 2017, Section A00. Lecture 4 Notes. Class URL:

Time Analysis of Sorting and Searching Algorithms

Notes slides from before lecture. CSE 21, Winter 2017, Section A00. Lecture 3 Notes. Class URL:

Elementary maths for GMT. Algorithm analysis Part II

CSE 146. Asymptotic Analysis Interview Question of the Day Homework 1 & Project 1 Work Session

CSE 373 APRIL 3 RD ALGORITHM ANALYSIS

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

Sorting Pearson Education, Inc. All rights reserved.

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

Randomized Algorithms: Element Distinctness

Data Structures and Algorithms Key to Homework 1

Algorithm. Algorithm Analysis. Algorithm. Algorithm. Analyzing Sorting Algorithms (Insertion Sort) Analyzing Algorithms 8/31/2017

Encoding/Decoding and Lower Bound for Sorting

Algorithms and Data Structures

Why study algorithms? CS 561, Lecture 1. Today s Outline. Why study algorithms? (II)

Chapter 2: Complexity Analysis

17 February Given an algorithm, compute its running time in terms of O, Ω, and Θ (if any). Usually the big-oh running time is enough.

CS 61B Asymptotic Analysis Fall 2018

CSC148 Week 9. Larry Zhang

Binomial Coefficient Identities and Encoding/Decoding

[ 11.2, 11.3, 11.4] Analysis of Algorithms. Complexity of Algorithms. 400 lecture note # Overview

Outline Purpose How to analyze algorithms Examples. Algorithm Analysis. Seth Long. January 15, 2010

Randomized Algorithms: Selection

asymptotic growth rate or order compare two functions, but ignore constant factors, small inputs

The Running Time of Programs

Introduction to Data Structure

Math 4242 Polynomial Time algorithms, IndependentSet problem

CS60003 Algorithm Design and Analysis, Autumn

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

Today s Outline. CSE 326: Data Structures Asymptotic Analysis. Analyzing Algorithms. Analyzing Algorithms: Why Bother? Hannah Takes a Break

ASYMPTOTIC COMPLEXITY

Data Structures and Algorithms. Course slides: String Matching, Algorithms growth evaluation

Class Note #02. [Overall Information] [During the Lecture]

Computational Geometry

Comparison of x with an entry in the array

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

CS2223: Algorithms D- Term, Homework I. Teams: To be done individually. Due date: 03/27/2015 (1:50 PM) Submission: Electronic submission only

CSE 332 Spring 2014: Midterm Exam (closed book, closed notes, no calculators)

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48

Recall from Last Time: Big-Oh Notation

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

What is an algorithm?

Lecture 5: Running Time Evaluation

Searching Algorithms/Time Analysis

Introduction to Data Management CSE 344

Intro. Speed V Growth

More Algorithm Analysis

Asymptotic Analysis Spring 2018 Discussion 7: February 27, 2018

Dijkstra's Algorithm

Introduction to Data Management CSE 344

You should know the first sum above. The rest will be given if you ever need them. However, you should remember that,, and.

CS S-02 Algorithm Analysis 1

CPSC W2: Quiz 2 Sample Solutions

CS126 Final Exam Review

Recitation 9. Prelim Review

CS302 Topic: Algorithm Analysis #2. Thursday, Sept. 21, 2006

RUNNING TIME ANALYSIS. Problem Solving with Computers-II

DESIGN AND ANALYSIS OF ALGORITHMS. Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES

Computer Science Foundation Exam

CS 261 Data Structures. Big-Oh Analysis: A Review

(Refer Slide Time: 1:27)

CSE 332: Data Abstractions Lecture 15: Topological Sort / Graph Traversals. Ruth Anderson Winter 2013

Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES

CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING

COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 7 Complexity and Orders of Growth TODAY PROBLEM SOLVING: ALGORITHMS COMPARISON OF ALGORITHMS 7/10/2012

CSS 443: Mid Term Exam


COSC 311: ALGORITHMS HW1: SORTING

CS 206 Introduction to Computer Science II

Announcements. Subqueries. Lecture Goals. 1. Subqueries in SELECT. Database Systems CSE 414. HW1 is due today 11pm. WQ1 is due tomorrow 11pm

Algorithm Analysis. Big Oh

MITOCW watch?v=yarwp7tntl4

Sorting Algorithms. CSE21 Winter 2017, Day 2 (B00), Day 1-2 (A00) January 11, 2017

Data Structures and Algorithms CSE 465

CSE 101, Winter Discussion Section Week 1. January 8 - January 15

Math 501 Solutions to Homework Assignment 10

Algorithm. Lecture3: Algorithm Analysis. Empirical Analysis. Algorithm Performance

introduction to Programming in C Department of Computer Science and Engineering Lecture No. #40 Recursion Linear Recursion

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri

10/5/2016. Comparing Algorithms. Analyzing Code ( worst case ) Example. Analyzing Code. Binary Search. Linear Search

CSE Winter 2015 Quiz 1 Solutions

CSE wi: Practice Midterm

Analysis of Algorithms & Big-O. CS16: Introduction to Algorithms & Data Structures Spring 2018

5/17/17. Announcements. Review: Transactions. Outline. Review: TXNs in SQL. Review: ACID. Database Systems CSE 414.

CS240 Fall Mike Lam, Professor. Algorithm Analysis

Notes slides from before lecture. CSE 21, Winter 2017, Section A00. Lecture 10 Notes. Class URL:

Adam Blank Lecture 2 Winter 2017 CSE 332. Data Structures and Parallelism

Assignment 1 (concept): Solutions

Database Systems CSE 414

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

CS 231 Data Structures and Algorithms Fall Algorithm Analysis Lecture 16 October 10, Prof. Zadia Codabux

Algorithm Analysis. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

English Bible for the Deaf

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

O(n): printing a list of n items to the screen, looking at each item once.

IT 4043 Data Structures and Algorithms

Big O & ArrayList Fall 2018 Margaret Reid-Miller

Remember to also pactice: Homework, quizzes, class examples, slides, reading materials.

Transcription:

Algorithm Design and Time Analysis CSE21 Winter 2017, Day 6 (B00), Day 4 (A00) January 23, 2017 http://vlsicad.ucsd.edu/courses/cse21-w17

Today s Plan Analyzing algorithms that solve other problems (besides sorting and searching) Designing better algorithms pre-processing re-use of computation

Summing Triples: WHAT Given a list of real numbers a 1, a 2,..., a n look for three indices, i, j, k (each between 1 and n) such that a i + a j = a k Does the list 3,6,5,7,8 have a summing triple? A. Yes: 1,2,3 B. Yes: 1,3,5 C. No

Summing Triples: WHAT Given a list of real numbers a 1, a 2,..., a n look for three indices, i, j, k (each between 1 and n) such that a i + a j = a k Design an algorithm to look for summing triples

Summing Triples: HOW (1) What's the best-case runtime of this algorithm? A. O(1) B. O(n) C. O(n 2 ) D. O(n 3 ) E. None of the above

Summing Triples: HOW (1) Describe all best-case inputs?

Summing Triples: HOW (1) What's the worst-case runtime of this algorithm? A. O(1) B. O(n) C. O(n 2 ) D. O(n 3 ) E. None of the above

Summing Triples: HOW (1) Can we do better? How?

Summing Triples: HOW (2) Eliminate redundancy

Summing Triples: HOW (2) Eliminate redundancy What's the worst-case runtime of this algorithm? A. O(1) B. O(n) C. O(n 2 ) D. O(n 3 ) E. None of the above

Summing Triples: HOW (2) Eliminate redundancy Hmmmm Can we do better? How?

Reframing what we did: Summing Triples: HOW (2) For each candidate sum a i +a j, do linear search to find it Improvements??

Summing Triples: HOW (2) For each candidate sum a i +a j, do linear search to find it We have a faster search than linear search!

Summing Triples: HOW (3) For each candidate sum a i +a j, Worst-case runtime? A. O(n 3 ) B. O(n 2 ) C. O(n 2 log n) D. O(n log n) do binary search to find it

Summing Triples: HOW (3) For each candidate sum a i +a j, do binary search to find it Something is wrong!

Summing Triples: HOW (3) For each candidate sum a i +a j, do binary search to find it Does this algorithm really work?

Summing Triples: HOW (4) Preprocessing step This algorithm works! How long does it take? aka SortedSumTriples

Summing Triples: HOW (4) O(n 2 ) O(n 2 log n) SumTriples4 worst-case complexity is max of these: O(n 2 log n)

Summing Triples: HOW (4) O(n 2 ) O(n 2 log n) Max of these: O(n 2 log n) SumTriples4 does better than O(n 3 ). Using a faster sort won't help overall. Fastest known algorithm: O(n 2 )

Tight? To know that we've actually made improvements, need to make sure our original analysis was not overly pessimistic. A tight bound for runtime is a function g(n) so that the runtime is in Big-O: upper bound. Big-Ω: lower bound.

Summing Triples: WHEN (1) What's a lower bound on the worst-case runtime of this algorithm? A. B. C. D. E. None of the above

Summing Triples: WHEN (1) Strategy: work from the inside out

Summing Triples: WHEN (2) What's a lower bound on the worst-case runtime of this algorithm? A. B. C. D. E. None of the above

Summing Triples: WHEN (2) For at least n/2 values of i (1 n/2), we do inner for loop (k) at least n/2 times, each taking n steps

Summing Triples: WHEN (2) Observe: in both these examples, the product rule for calculating the nested loop runtime gave us tight upper bounds is that always the case?

When is the product rule for nested loops tight? Nested code: If Guard Condition is O(1) and body of the loop has runtime O(T 2 ) in the worst case and run at most O(T 1 ) iterations, then runtime is O(T 1 T 2 ) But what if many t k are much better than the worst case?

Intersecting sorted lists: WHAT Given two sorted lists a 1, a 2,..., a n and b 1, b 2,..., b n determine if there are indices i,j such that a i = b j Design an algorithm to look for indices of intersection

Intersecting sorted lists: HOW Given two sorted lists a 1, a 2,..., a n and b 1, b 2,..., b n determine if there are indices i,j such that a i = b j High-level description: Use linear search to see if b 1 is anywhere in first list, using early abort Since b 2 >b 1, start the search for b 2 where the search for b 1 left off And in general, start the search for b j where the search for b j-1 left off

Intersecting sorted lists: HOW

Intersecting sorted lists: WHY To practice: trace examples & generalize argument for correctness

Intersecting sorted lists: WHEN Using product rule O(n) O(1)

Intersecting sorted lists: WHEN Using product rule O(n) Total: O(n 2 )

Intersecting sorted lists: WHEN More careful analysis Every time the while loop condition is true, i is incremented. If i ever reaches n+1, the program terminates (returns)

More careful analysis Intersecting sorted lists: WHEN This executes O(n) times total (across all iterations of for loop)

More careful analysis Intersecting sorted lists: WHEN This executes O(n) times total (across all iterations of for loop) Total: O(n) Be careful: product rule isn't always tight!

Announcements HW2 Due tomorrow! (Tues 1/24, 11:59PM) Practice with Order Notation on Khan Academy e.g., https://www.khanacademy.org/computing/computerscience/algorithms/asymptotic-notation/a/big-o-notation 1-1 Signup Is available!