CS10: The Beauty and Joy of Computing Lecture #7: Algorithm Complexity TA Jon Kotker (2010-09-27) LEDs + Math = Art Leo Villareal combines modern LED control.

Slides:



Advertisements
Similar presentations
Introduction to Computer Science Theory
Advertisements

CSE Lecture 3 – Algorithms I
SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY Lecture 9 CS2110 – Spring 2015 We may not cover all this material.
Efficiency of Algorithms Csci 107 Lecture 6-7. Topics –Data cleanup algorithms Copy-over, shuffle-left, converging pointers –Efficiency of data cleanup.
Practice Quiz Question
 The amount of time it takes a computer to solve a particular problem depends on:  The hardware capabilities of the computer  The efficiency of the.
CSC 331: Algorithm Analysis Divide-and-Conquer Algorithms.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
CS10 The Beauty and Joy of Computing Lecture #7 Algorithmic Complexity One million Wi-Fi devices isn’t cool. You know what’s cool? A Billion.
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
1 CSE1301 Computer Programming Lecture 31: List Processing (Search)
CS107 Introduction to Computer Science
Efficiency of Algorithms February 19th. Today Binary search –Algorithm and analysis Order-of-magnitude analysis of algorithm efficiency –Review Sorting.
© 2006 Pearson Addison-Wesley. All rights reserved6-1 More on Recursion.
Cmpt-225 Algorithm Efficiency.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Efficiency of Algorithms Csci 107 Lecture 8. Last time –Data cleanup algorithms and analysis –  (1),  (n),  (n 2 ) Today –Binary search and analysis.
Efficiency of Algorithms February 11th. Efficiency of an algorithm worst case efficiency is the maximum number of steps that an algorithm can take for.
 Last lesson  Arrays for implementing collection classes  Performance analysis (review)  Today  Performance analysis  Logarithm.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Analysis of Algorithm.
Elementary Data Structures and Algorithms
CS107 Introduction to Computer Science Lecture 7, 8 An Introduction to Algorithms: Efficiency of algorithms.
1 Section 2.3 Complexity of Algorithms. 2 Computational Complexity Measure of algorithm efficiency in terms of: –Time: how long it takes computer to solve.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
COMP s1 Computing 2 Complexity
Asymptotic Notations Iterative Algorithms and their analysis
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Searching – Linear and Binary Searches. Comparing Algorithms Should we use Program 1 or Program 2? Is Program 1 “fast”? “Fast enough”? P1P2.
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
{ CS203 Lecture 7 John Hurley Cal State LA. 2 Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Analysis of Algorithms
Efficiency of Algorithms Csci 107 Lecture 7. Last time –Data cleanup algorithms and analysis –  (1),  (n),  (n 2 ) Today –Binary search and analysis.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
Big Oh Algorithms are compared to each other by expressing their efficiency in big-oh notation Big O notation is used in Computer Science to describe the.
Growth of Functions. Asymptotic Analysis for Algorithms T(n) = the maximum number of steps taken by an algorithm for any input of size n (worst-case runtime)
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
Analysis of algorithms Analysis of algorithms is the branch of computer science that studies the performance of algorithms, especially their run time.
Algorithm Analysis (Algorithm Complexity). Correctness is Not Enough It isn’t sufficient that our algorithms perform the required tasks. We want them.
CSC 211 Data Structures Lecture 13
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
3.3 Complexity of Algorithms
Sorting: Implementation Fundamental Data Structures and Algorithms Klaus Sutner February 24, 2004.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Scalability for Search Scaling means how a system must grow if resources or work grows –Scalability is the ability of a system, network, or process, to.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Searching Topics Sequential Search Binary Search.
Searching & Sorting. Algorithms Step by step recipe to do a task…
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
The Beauty and Joy of Computing Lecture #7 Algorithms II minors-delete-activity/story?id= /
The Beauty and Joy of Computing Lecture #7 Algorithmic Complexity “Data scientists at Yahoo are using prediction markets – along with polls, sentiment.
1 Algorithms Searching and Sorting Algorithm Efficiency.
LECTURE 9 CS203. Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search) and sorting (selection sort.
CMPT 438 Algorithms.
19 Searching and Sorting.
Yahoo predicts contest already!
Searching – Linear and Binary Searches
Algorithmic Efficency
Lecture 14 Searching and Sorting Richard Gesick.
Lecture 11 Searching and Sorting Richard Gesick.
CS 201 Fundamental Structures of Computer Science
Recurrences (Method 4) Alexandra Stefan.
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:

CS10: The Beauty and Joy of Computing Lecture #7: Algorithm Complexity TA Jon Kotker ( ) LEDs + Math = Art Leo Villareal combines modern LED control systems to produce contemporary modern art. The exhibit is on display at the San Jose Museum of Art _ html

BIG IDEA Many ways to do the same thing = many algorithms to accomplish the same task. Example: Distributing candy! Example: Searching through a list of numbers to find a specific number. ◦ Linear search (list is unsorted): Go through the list number by number and check if each number is The One. ◦ Binary search (list is sorted): Look at the middle of the list. If it is not The One, break the list into two smaller halves and ignore the irrelevant half. ◦ Any other algorithms? UC Berkeley CS10 "The Beauty and Joy of Computing" : Algorithm Complexity2

MAKING A DECISION How do we decide which algorithm to use? Which is easier to implement? Which takes less time? Which uses up less space (memory)? Which gives a more precise answer? Which of the above questions even matter? UC Berkeley CS10 "The Beauty and Joy of Computing" : Algorithm Complexity3

WHAT DO YOU THINK? Which of the factors below is most important in making a choice between two algorithms? A. Which is easier to implement? B. Which takes less time? C. Which uses up less space (memory)? D. Which gives a more precise answer? E. I don’t know / I don’t have an opinion. UC Berkeley CS10 "The Beauty and Joy of Computing" : Algorithm Complexity4

RUNTIME ANALYSIS One commonly used criterion in making a decision is runtime – how much time does the algorithm take to run and finish its task? Computers are most useful for large inputs, so find the runtime of the algorithm on large inputs. How do we do that? UC Berkeley CS10 "The Beauty and Joy of Computing" : Algorithm Complexity5

RUNTIME ANALYSIS Time the algorithm with a stopwatch! But… Different computers will have different runtimes.  Same computer can have different runtime on the same input.  Need to implement the algorithm first so that we can run it. o_O; Solution: Need to somehow abstract the computer away from the algorithm. UC Berkeley CS10 "The Beauty and Joy of Computing" : Algorithm Complexity6

RUNTIME ANALYSIS Idea: Do not focus on how long the algorithm takes on one input. Instead, focus on how the worst-case runtime of the algorithm scales as we scale the input. Why? Abstracts the computer out. A good algorithm should work well, no matter what the computer = a good recipe should produce a great dish, no matter what the kitchen. UC Berkeley CS10 "The Beauty and Joy of Computing" : Algorithm Complexity7

RUNTIME ANALYSIS Idea: Do not focus on how long the algorithm takes on one input. Instead, focus on how the worst-case runtime of the algorithm scales as we scale the input. Why? Computers are mainly used for large sets of data. The runtime of an algorithm should scale “reasonably” as we make the dataset even larger, or else we need to improve/discard that algorithm. UC Berkeley CS10 "The Beauty and Joy of Computing" : Algorithm Complexity8

A LOT OF APPROXIMATION AHEAD! UC Berkeley CS10 "The Beauty and Joy of Computing" : Algorithm Complexity9 Dangerous for Math majors. Courtesy

ARITHMETIC OPERATIONS Key Idea: As the input scales, arithmetic operations take approximately the same time. Arithmetic operations are constant-time operations. Another Key Idea: We only care about how the runtime of the block scales as the input scales! UC Berkeley CS10 "The Beauty and Joy of Computing" : Algorithm Complexity10

LAUNDRY It Must Be Done UC Berkeley CS10 "The Beauty and Joy of Computing" : Algorithm Complexity11

WASHING LOADS UC Berkeley CS10 "The Beauty and Joy of Computing" : Algorithm Complexity12 If each load takes about the same time to launder, how does the runtime scale as the number of loads doubles? Triples?

WASHING LOADS UC Berkeley CS10 "The Beauty and Joy of Computing" : Algorithm Complexity13 Key Idea: The runtime of the algorithm scales by the same amount as the size of its input scales. Doing laundry is a linear-time operation in the number of loads.

FINDING CLOTHES TO WEAR UC Berkeley CS10 "The Beauty and Joy of Computing" : Algorithm Complexity14 How does the worst-case total time to find a good pair scale as the number of shirts and pants doubles? 1. Stays the same. 2. Doubles. 3. Halves. 4. Quadruples. 5. Buh? Hint: For each shirt that I own, how many pants do I have to match against it?

FINDING CLOTHES TO WEAR UC Berkeley CS10 "The Beauty and Joy of Computing" : Algorithm Complexity15 Key Idea: If I have 3 shirts and pants, there are 9 different combinations that I can try, because for each shirt, I can try on 3 pants to match with it. Double it: If I have 6 shirts and pants, there are 36 different combinations that I can try. If I double the number of shirts and pants that I have, then the number of different combinations that I can try quadruples.

FINDING CLOTHES TO WEAR UC Berkeley CS10 "The Beauty and Joy of Computing" : Algorithm Complexity16 Key Idea: The runtime of the algorithm scales by the square of the amount that the input scales by. Finding a good pair of clothes to wear is a quadratic-time algorithm in the number of shirts and pants.

LAUNDRY It Has Been Done UC Berkeley CS10 "The Beauty and Joy of Computing" : Algorithm Complexity17

RUNTIME ANALYSIS UC Berkeley CS10 "The Beauty and Joy of Computing" : Algorithm Complexity18 What is the runtime of this script? 1. Constant in num. 2. Linear in num. 3. Quadratic in num. 4. Buh?

RUNTIME ANALYSIS UC Berkeley CS10 "The Beauty and Joy of Computing" : Algorithm Complexity19 What is the runtime of this script? 1. Constant in num. 2. Linear in num. 3. Quadratic in num. 4. Buh?

RUNTIME ANALYSIS UC Berkeley CS10 "The Beauty and Joy of Computing" : Algorithm Complexity20 What is the runtime of this script? 1. Constant in num. 2. Linear in num. 3. Quadratic in num. 4. Buh?

RUNTIME ANALYSIS UC Berkeley CS10 "The Beauty and Joy of Computing" : Algorithm Complexity21 What is the runtime of this script? 1. Constant in num. 2. Linear in num. 3. Quadratic in num. 4. Buh?

IT’S ALL APPROXIMATE! UC Berkeley CS10 "The Beauty and Joy of Computing" : Algorithm Complexity22 Which is better: a linear-time algorithm or a quadratic-time algorithm? As the input size increases, the quadratic-time algorithm takes so much more time than the linear-time algorithm that the linear-time algorithm is negligible in comparison. Input Size (N) Linear (msec) C10C100C1000C10000C Quadratic (msec) C100C10000C10 6 C10 8 C

IT’S ALL APPROXIMATE! UC Berkeley CS10 "The Beauty and Joy of Computing" : Algorithm Complexity23 Which is better: a linear-time algorithm or a quadratic-time algorithm? Since we only consider large sized inputs, expressions like N 2 – N or N 2 + N are considered approximately equal to N and thus quadratic-time; the linear-time part is ignored. Input Size (N) Linear (msec) C10C100C1000C10000C Quadratic (msec) C100C10000C10 6 C10 8 C

RUNTIME ANALYSIS (EXTRA) UC Berkeley CS10 "The Beauty and Joy of Computing" : Algorithm Complexity24 What is the runtime of this algorithm to find the factorial of a number? 1. Constant in the number. 2. Linear in the number. 3. Quadratic in the number. 4. Buh?