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

Similar documents
0.1 Welcome. 0.2 Insertion sort. Jessica Su (some portions copied from CLRS)

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

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

Chapter 2: Complexity Analysis

Asymptotic Analysis Spring 2018 Discussion 7: February 27, 2018

Introduction to Data Structure

Data Structures Lecture 8

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

Algorithms and Data Structures

CS302 Topic: Algorithm Analysis. Thursday, Sept. 22, 2005

Big-O-ology 1 CIS 675: Algorithms January 14, 2019

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

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

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

CS141: Intermediate Data Structures and Algorithms Analysis of Algorithms

CSE 373 APRIL 3 RD ALGORITHM ANALYSIS

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

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

Computer Science 210 Data Structures Siena College Fall Topic Notes: Complexity and Asymptotic Analysis

CS141: Intermediate Data Structures and Algorithms Analysis of Algorithms

4.4 Algorithm Design Technique: Randomization

UNIT 1 ANALYSIS OF ALGORITHMS

ASYMPTOTIC COMPLEXITY

Section 05: Solutions

LECTURE 9 Data Structures: A systematic way of organizing and accessing data. --No single data structure works well for ALL purposes.

CS240 Fall Mike Lam, Professor. Algorithm Analysis

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

STA141C: Big Data & High Performance Statistical Computing

How fast is an algorithm?

Choice of C++ as Language

Analysis of Algorithms

Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types

CS:3330 (22c:31) Algorithms

Introduction to the Analysis of Algorithms. Algorithm

The Limits of Sorting Divide-and-Conquer Comparison Sorts II

Table of Contents. Course Minutiae. Course Overview Algorithm Design Strategies Algorithm Correctness Asymptotic Analysis 2 / 32

Complexity Analysis of an Algorithm

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

RUNNING TIME ANALYSIS. Problem Solving with Computers-II

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

CSCI 136 Data Structures & Advanced Programming. Lecture 7 Spring 2018 Bill and Jon

STA141C: Big Data & High Performance Statistical Computing

Algorithm Analysis. Applied Algorithmics COMP526. Algorithm Analysis. Algorithm Analysis via experiments

ASYMPTOTIC COMPLEXITY

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

DATA STRUCTURES AND ALGORITHMS

CSI33 Data Structures

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

INTRODUCTION. Analysis: Determination of time and space requirements of the algorithm

Test 1 SOLUTIONS. June 10, Answer each question in the space provided or on the back of a page with an indication of where to find the answer.

Big-O-ology. Jim Royer January 16, 2019 CIS 675. CIS 675 Big-O-ology 1/ 19

Algorithm Analysis. Algorithm Efficiency Best, Worst, Average Cases Asymptotic Analysis (Big Oh) Space Complexity

Data Structures and Algorithms CSE 465

CS S-02 Algorithm Analysis 1

Algorithm Analysis. Part I. Tyler Moore. Lecture 3. CSE 3353, SMU, Dallas, TX

Section 05: Solutions

Complexity of Algorithms. Andreas Klappenecker

Scientific Computing. Algorithm Analysis

Complexity of Algorithms

IT 4043 Data Structures and Algorithms

Assignment 1 (concept): Solutions

Data Structure Lecture#5: Algorithm Analysis (Chapter 3) U Kang Seoul National University

CSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees. Linda Shapiro Spring 2016

Another Sorting Algorithm

CS240 Fall Mike Lam, Professor. Algorithm Analysis

Quiz 1 Practice Problems

Lecture Notes for Chapter 2: Getting Started

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK

Lecture 2: Algorithms, Complexity and Data Structures. (Reading: AM&O Chapter 3)

Elementary maths for GMT. Algorithm analysis Part I

csci 210: Data Structures Program Analysis

IE 495 Lecture 3. Septermber 5, 2000

Analysis of Algorithm. Chapter 2

Theory and Algorithms Introduction: insertion sort, merge sort

Ceng 111 Fall 2015 Week 12b

Internal Sorting by Comparison

Overview of Data. 1. Array 2. Linked List 3. Stack 4. Queue

An algorithm is a sequence of instructions that one must perform in order to solve a wellformulated

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

CMPSCI 187: Programming With Data Structures. Lecture 5: Analysis of Algorithms Overview 16 September 2011

Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES

Math 501 Solutions to Homework Assignment 10

What is an algorithm?

Lecture 5: Running Time Evaluation

Agenda. The worst algorithm in the history of humanity. Asymptotic notations: Big-O, Big-Omega, Theta. An iterative solution

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

Data Structures and Algorithms Key to Homework 1

Midterm solutions. n f 3 (n) = 3

Overview. CSE 101: Design and Analysis of Algorithms Lecture 1

DATA STRUCTURES AND ALGORITHMS

1. Attempt any three of the following: 15

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

CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist

Solutions. (a) Claim: A d-ary tree of height h has at most 1 + d +...

Data Structures and Algorithms

Introduction to Algorithms 6.046J/18.401J

Jalil Boudjadar, Tommy Färnqvist. IDA, Linköping University

Introduction to Algorithms and Complexity Theory

Final Exam in Algorithms and Data Structures 1 (1DL210)

How do we compare algorithms meaningfully? (i.e.) the same algorithm will 1) run at different speeds 2) require different amounts of space

Transcription:

Why study algorithms? CS 561, Lecture 1 Jared Saia University of New Mexico Seven years of College down the toilet - John Belushi in Animal House Q: Can I get a programming job without knowing something about algorithms and data structures? A: Yes, but do you really want to be programming GUIs your entire life? 2 Today s Outline Why study algorithms? (II) Administrative Info Asymptotic Analysis Almost all big companies want programmers with knowledge of algorithms: Microsoft, Google, Oracle, IBM, Yahoo, Sandia, Los Alamos, etc. In most programming job interviews, they will ask you several questions about algorithms and/or data structures Your knowledge of algorithms will set you apart from the large masses of interviewees who know only how to program If you want to start your own company, you should know that many startups are successful because they ve found better algorithms for solving a problem (e.g. Google, Akamai, etc.) 1 3

Why Study Algorithms? (III) Solution You ll improve your research skills in almost any area You ll write better, faster code You ll learn to think more abstractly and mathematically It s one of the most challenging and interesting area of CS! Ideas on how to solve this problem?? What if we allowed multiple iterations? 4 6 A Real Job Interview Question Naive Algorithm The following is a question commonly asked in job interviews in 2002 (thanks to Maksim Noy, see the career center link from the dept web page for the full compilation of questions): You are given an array with integers between 1 and 1,000,000. All integers between 1 and 1,000,000 are in the array at least once, and one of those integers is in the array twice Q: Can you determine which integer is in the array twice? Can you do it while iterating through the array only once? Create a new array of ints between 1 and 1,000,000, which we ll use to count the occurences of each number. Initialize all entries to 0 Go through the input array and each time a number is seen, update its count in the new array Go through the count array and see which number occurs twice. Return this number 5 7

Naive Algorithm Analysis A better Algorithm Q: How long will this algorithm take? A: We iterate through the numbers 1 to 1,000,000 three times! Note that we also use up a lot of space with the extra array This is wasteful of time and space, particularly as the input array gets very large (e.g. it might be a huge data stream) Q: Can we do better? Iterate through the input array, summing up all the numbers, let S be this sum Let x = S (1,000,000 + 1)1,000,000/2 Return x 8 10 Ideas for a better Algorithm Analysis Note that n i=1 i = (n + 1)n/2 Let S be the sum of the input array Let x be the value of the repeated number Then S = (1,000,000 + 1)1,000,000/2 + x Thus x = S (1,000,000 + 1)1,000,000/2 This algorithm takes iterates through the input array just once It uses up essentially no extra space It is at least three times faster than the naive algorithm Further, if the input array is so large that it won t fit in memory, this is the only algorithm which will work! These time and space bounds are the best possible 9 11

Take Away Random-access machine model Designing good algorithms matters! Not always this easy to improve an algorithm However, with some thought and work, you can almost always get a better algorithm than the naive approach We will use RAM model of computation in this class All instructions operate in serial All basic operations (e.g. add, multiply, compare, read, store, etc.) take unit time All atomic data (chars, ints, doubles, pointers, etc.) take unit space 12 14 How to analyze an algorithm? Worst Case Analysis There are several resource bounds we could be concerned about: time, space, communication bandwidth, logic gates, etc. However, we are usually most concerned about time Recall that algorithms are independent of programming languages and machine types Q: So how do we measure resource bounds of algorithms We ll generally be pessimistic when we evaluate resource bounds We ll evaluate the run time of the algorithm on the worst possible input sequence Amazingly, in most cases, we ll still be able to get pretty good bounds Justification: The average case is often about as bad as the worst case. 13 15

Example Analysis Algorithm 2 Consider the problem discussed last tuesday about finding a redundant element in an array Let s consider the more general problem, where the numbers are 1 to n instead of 1 to 1,000,000 Iterate through the input array, summing up all the numbers, let S be this sum Let x = S (n + 1)n/2 Return x 16 18 Algorithm 1 Example Analysis: Time Create a new count array of ints of size n, which we ll use to count the occurences of each number. Initialize all entries to 0 Go through the input array and each time a number is seen, update its count in the count array As soon as a number is seen in the input array which has already been counted once, return this number Worst case: Algorithm 1 does 5 n operations (n inits to 0 in count array, n reads of input array, n reads of count array (to see if value is 1), n increments, and n stores into count array) Worst case: Algorithm 2 does 2 n + 4 operations (n reads of input array, n additions to value S, 4 computations to determine x given S) 17 19

Example Analysis: Space Asymptotic analysis? Worst Case: Algorithm 1 uses n additional units of space to store the count array Worst Case: Algorithm 2 uses 2 additional units of space A tool for analyzing time and space usage of algorithms Assumes input size is a variable, say n, and gives time and space bounds as a function of n Ignores multiplicative and additive constants Concerned only with the rate of growth E.g. Treats run times of n, 10,000 n + 2000, and.5n + 2 all the same (We use the term O(n) to refer to all of them) 20 22 A Simpler Analysis What is Asymptotic Analysis?(II) Analysis above can be tedious for more complicated algorithms In many cases, we don t care about constants. 5n is about the same as 2n + 4 which is about the same as an + b for any constants a and b However we do still care about the difference in space: n is very different from 2 Asymptotic analysis is the solution to removing the tedium but ensuring good analysis Informally, O notation is the leading (i.e. quickest growing) term of a formula with the coefficient stripped off O is sort of a relaxed version of E.g. n is O(n) and n is also O(n 2 ) By convention, we use the smallest possible O value i.e. we say n is O(n) rather than n is O(n 2 ) 21 23

More Examples Formal Defn of Big-O E.g. n, 10,000n 2000, and.5n + 2 are all O(n) n + log n, n n are O(n) n 2 + n + log n, 10n 2 + n n are O(n 2 ) nlog n + 10n is O(nlog n) 10 log 2 n is O(log 2 n) n n + nlog n + 10n is O(n n) 10,000, 2 50 and 4 are O(1) A function f(n) is O(g(n)) if there exist positive constants c and n 0 such that f(n) cg(n) for all n n 0 24 26 More Examples Example Algorithm 1 and 2 both take time O(n) Algorithm 1 uses O(n) extra space But, Algorithm 2 uses O(1) extra space Let s show that f(n) = 10n+100 is O(g(n)) where g(n) = n We need to give constants c and n 0 such that f(n) cg(n) for all n n 0 In other words, we need constants c and n 0 such that 10n+ 100 cn for all n n 0 25 27

Example Relatives of big-o We can solve for appropriate constants: 10n + 100 cn (1) 10 + 100/n c (2) So if n > 1, then c should be greater than 110. In other words, for all n > 1, 10n + 100 110n So 10n + 100 is O(n) The following are relatives of big-o: O Θ = Ω o < ω > 28 30 Questions Relatives of big-o Express the following in O notation n 3 /1000 100n 2 100n + 3 log n + 100 10 log 2 n + 100 n i=1 i When would you use each of these? Examples: O This algorithm is O(n 2 ) (i.e. worst case is Θ(n 2 )) Θ = This algorithm is Θ(n) (best and worst case are Θ(n)) Ω Any comparison-based algorithm for sorting is Ω(nlog n) o < Can you write an algorithm for sorting that is o(n 2 )? ω > This algorithm is not linear, it can take time ω(n) 29 31

Rule of Thumb Problems Let f(n), g(n) be two functions of n Let f 1 (n), be the fastest growing term of f(n), stripped of its coefficient. Let g 1 (n), be the fastest growing term of g(n), stripped of its coefficient. Then we can say: If f 1 (n) g 1 (n) then f(n) = O(g(n)) If f 1 (n) g 1 (n) then f(n) = Ω(g(n)) If f 1 (n) = g 1 (n) then f(n) = Θ(g(n)) If f 1 (n) < g 1 (n) then f(n) = o(g(n)) If f 1 (n) > g 1 (n) then f(n) = ω(g(n)) True or False? (Justify your answer) n 3 + 4 is ω(n 2 ) nlog n 3 is Θ(nlog n) log 3 5n 2 is Θ(log n) 10 10 n 2 + n is Θ(n) nlog n is Ω(n) n 3 + 4 is o(n 4 ) 32 34 More Examples Formal Defns The following are all true statements: O(g(n)) = {f(n) : there exist positive constants c and n 0 such that 0 f(n) cg(n) for all n n 0 } n i=1 i 2 is O(n 3 ), Ω(n 3 ) and Θ(n 3 ) log n is o( n) log n is o(log 2 n) 10,000n 2 + 25n is Θ(n 2 ) Θ(g(n)) = {f(n) : there exist positive constants c 1, c 2, and n 0 such that 0 c 1 g(n) f(n) c 2 g(n) for all n n 0 } Ω(g(n)) = {f(n) : there exist positive constants c and n 0 such that 0 cg(n) f(n) for all n n 0 } 33 35

Formal Defns (II) In-Class Exercise Show that for f(n) = n + 100 and g(n) = (1/2)n 2, that f(n) Θ(g(n)) o(g(n)) = {f(n) : for any positive constant c > 0 there exists n 0 > 0 such that 0 f(n) < cg(n) for all n n 0 } What statement would be true if f(n) = Θ(g(n))? Show that this statement can not be true. ω(g(n)) = {f(n) : for any positive constant c > 0 there exists n 0 > 0 such that 0 cg(n) < f(n) for all n n 0 } 36 38 Another Example Todo Let f(n) = 10log 2 n + log n, g(n) = log 2 n. Let s show that f(n) = Θ(g(n)). We want positive constants c 1, c 2 and n 0 such that 0 c 1 g(n) f(n) c 2 g(n) for all n n 0 0 c 1 log 2 n 10log 2 n + log n c 2 log 2 n Dividing by log 2 n, we get: 0 c 1 10 + 1/log n c 2 If we choose c 1 = 1, c 2 = 11 and n 0 = 2, then the above inequality will hold for all n n 0 Read Syllabus Visit the class web page: www.cs.unm.edu/~saia/561/ Sign up for the class mailing list (cs561) Read Chapter 3 and 4 in the text 37 39