“It ain’t no good if it ain’t snappy enough.” (Efficient Computations) COS 116, Spring 2010 Adam Finkelstein.

Slides:



Advertisements
Similar presentations
Growth-rate Functions
Advertisements

MATH 224 – Discrete Mathematics
Lower Bounds for Sorting, Searching and Selection
CSE Lecture 3 – Algorithms I
1 CS101 Introduction to Computing Lecture 17 Algorithms II.
Lecture 3: Parallel Algorithm Design
Algorithms (continued)
What is the computational cost of automating brilliance or serendipity? (Computational complexity & P vs NP) COS 116, Spring 2012 Adam Finkelstein.
CS50 SECTION: WEEK 3 Kenny Yu. Announcements  Watch Problem Set 3’s walkthrough online if you are having trouble.  Problem Set 1’s feedback have been.
Discrete Structure Li Tak Sing( 李德成 ) Lectures
Searching and Sorting Topics  Sequential Search on an Unordered File  Sequential Search on an Ordered File  Binary Search  Bubble Sort  Insertion.
CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis Tyler Robison Summer
CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 27: A Few Words on NP Dan Grossman Spring 2010.
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
CS107 Introduction to Computer Science
“It ain’t no good if it ain’t snappy enough.” (Efficient Computations) COS 116: 2/19/2008 Sanjeev Arora.
CS 1114: Sorting and selection (part one) Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
Wednesday, 11/13/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/13/02  QUESTIONS??  Today:  More on searching a list; binary search  Sorting a list;
Complexity (Running Time)
Algorithm Efficiency and Sorting
DAST 2005 Week 4 – Some Helpful Material Randomized Quick Sort & Lower bound & General remarks…
Elementary Data Structures and Algorithms
Chapter 3: The Efficiency of Algorithms
Solution methods for Discrete Optimization Problems.
CS107 Introduction to Computer Science Lecture 7, 8 An Introduction to Algorithms: Efficiency of algorithms.
Unit 1. Sorting and Divide and Conquer. Lecture 1 Introduction to Algorithm and Sorting.
Creating new worlds inside the computer COS 116, Spring 2012 Adam Finkelstein.
February 17, 2015Applied Discrete Mathematics Week 3: Algorithms 1 Double Summations Table 2 in 4 th Edition: Section th Edition: Section th.
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
Chapter 16: Searching, Sorting, and the vector Type.
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
DISCRETE MATHEMATICS I CHAPTER 11 Dr. Adam Anthony Spring 2011 Some material adapted from lecture notes provided by Dr. Chungsim Han and Dr. Sam Lomonaco.
“It ain’t no good if it ain’t snappy enough.” (Efficient Computations) COS 116, Spring 2012 Adam Finkelstein.
Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Analysis of Algorithms
Chapter 19: Searching and Sorting Algorithms
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
CSC 211 Data Structures Lecture 13
“It ain’t no good if it ain’t snappy enough.” (Efficient Computations) COS 116, Spring 2011 Sanjeev Arora.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 2.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
CSCI 51 Introduction to Programming March 12, 2009.
Recurrences David Kauchak cs161 Summer Administrative Algorithms graded on efficiency! Be specific about the run times (e.g. log bases) Reminder:
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
(c) , University of Washington18a-1 CSC 143 Java Searching and Recursion N&H Chapters 13, 17.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Algorithm Analysis (Big O)
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Searching Topics Sequential Search Binary Search.
General Computer Science for Engineers CISC 106 Lecture 06 James Atlas Computer and Information Sciences 06/24/2009.
Big O David Kauchak cs302 Spring Administrative Assignment 1: how’d it go? Assignment 2: out soon… Lab code.
0 Introduction to asymptotic complexity Search algorithms You are responsible for: Weiss, chapter 5, as follows: 5.1 What is algorithmic analysis? 5.2.
Sorting & Lower Bounds Jeff Edmonds York University COSC 3101 Lecture 5.
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
Chapter 16: Searching, Sorting, and the vector Type.
Chapter 16: Searching, Sorting, and the vector Type
Applied Discrete Mathematics Week 2: Functions and Sequences
Analysis of Algorithms
David Kauchak CS52 – Spring 2015
Searching CLRS, Sections 9.1 – 9.3.
Analyzing an Algorithm Computing the Order of Magnitude Big O Notation
Algorithmic complexity
Presentation transcript:

“It ain’t no good if it ain’t snappy enough.” (Efficient Computations) COS 116, Spring 2010 Adam Finkelstein

Administrative stuff Readings avail. from course web page Feedback form on course web page; fully anonymous. HW1 extended - due Tues 2/23 instead. Reminder: Lab 3 Wed 7:30 Friend 007.

In what ways (according to Brian Hayes) is the universe like a cellular automaton? What aspect(s) of the physical world are not represented well by a cellular automaton? Discussion Time

Question: How do we measure the “speed” of an algorithm? Ideally, should be independent of:  machine  technology

“Running time” of an algorithm Definition: the number of “elementary operations” performed by the algorithm Elementary operations: +, -, *, /, assignment, evaluation of conditionals (discussed also in pseudocode handout) “Speed” of computer: number of elementary steps it can perform per second ( Simplified definition)  Do not consider this in “running time” of algorithm; technology-dependent.

Example: Find Min n items, stored in array A Variables are i, best best  1 Do for i = 2 to n { if (A[ i ] < A[best]) then { best  i } }

Example: Find Min n items, stored in array A Variables are i, best best  1 Do for i = 2 to n { if (A[ i ] < A[best]) then { best  i } } How many operations executed before the loop?  A: 0 B: 1 C: 2 D: 3

Example: Find Min n items, stored in array A Variables are i, best best  1 Do for i = 2 to n { if (A[ i ] < A[best]) then { best  i } } How many operations per iteration of the loop?  A: 0 B: 1 C: 2 D: 3

Example: Find Min n items, stored in array A Variables are i, best best  1 Do for i = 2 to n { if (A[ i ] < A[best]) then { best  i } } How many times does the loop run?  A: n B: n+1 C: n-1 D: 2n “iterations”

Example: Find Min n items, stored in array A Variables are i, best best  1 Do for i = 2 to n { if (A[ i ] < A[best]) then { best  i } } Uses at most 2(n – 1) + 1 operations Initialization Number of iterations 1 assignment & 1 comparison = 2 operations per loop iteration } (roughly = 2n)

Discussion Time “20 Questions”: I have a number between 1 and a million in mind. Guess it by asking me yes/no questions, and keep the number of questions small. Question 1: “Is the number bigger than half a million?”No Question 2: “Is the number bigger than a quarter million?” Strategy: Each question halves the range of possible answers. No

Pseudocode: Guessing number from1 to n Lower  1 Upper  n Found  0 Do while (Found=0) { Guess  Round( (Lower + Upper)/2 ) If (Guess = True Number) { Found  1 Print(Guess) } If (Guess < True Number) { Lower  Guess } else { Upper  Guess} Binary Search How many times does the loop run??

Brief detour: Logarithms (CS view) log 2 n = K means 2 K-1 < n ≤ 2 K In words: K is the number of times you need to divide n by 2 in order to get a number ≤ 1 John Napier log 2 n n

“There are only 10 types of people in the world – those who know binary and those who don’t.” Next….

Binary search and binary representation of numbers Say we know 0 ≤ number < 2 K Is 2 K / 2 ≤ number < 2 K ? NoYes Is 2 K / 4 ≤ number < 2 K / 2? No Yes Is 2 K × 3/8 ≤ number < 2 K / 2? NoYes … … 02K2K

Binary representations (cont’d) In general, each number can be uniquely identified by a sequence of yes/no answers to these questions. Correspond to paths down this “tree”: Is 2 K / 2 ≤ number < 2 K ? No Yes Is 2 K / 4 ≤ number < 2 K / 2? No Yes Is 2 K / 8 ≤ number < 2 K / 4? NoYes … … Is 2 K × 3/8 ≤ number < 2 K / 2? NoYes … … …

Binary representation of n (the more standard definition) n = 2 k b k + 2 k-1 b k-1 + … + 2 b 2 + b 1 where the b’s are either 0 or 1) The binary representation of n is:  n  2 = b k b k – 1 … b 2 b 1

Efficiency of Selection Sort Do for i = 1 to n – 1 { Find cheapest bottle among those numbered i to n Swap that bottle and the i’th bottle. } For the i’th round, takes at most 2(n – i ) + 3 To figure out running time, need to figure out how to sum (n – i) for i = 1 to n – 1 …and then double the result. About 2(n – i) steps 3 steps

Gauss’s trick : Sum of (n – i) for i = 1 to n – 1 S = … + (n – 2) + (n – 1) +S = (n – 1) + (n – 2) + … S = n + n + … + n + n 2S = n(n – 1) So total time for selection sort is ≤ n(n – 1) + 3n n – 1 times (for large n, roughly = n 2 )

Running times encountered in this lecture n= 8n= 1024n= n= log 2 n n n2n Efficiency really makes a difference!

Efficiency of Effort: A lens on the world QWERTY keyboard “UPS Truck Driver’s Problem” (a.k.a. Traveling Salesman Problem or TSP) CAPTCHA’s Quantum computing [Jim Loy]

Can n particles do 2 n “operations” in a single step? Or is Quantum Mechanics not quite correct? SIAM J. Computing 26(5) 1997 Computational efficiency has a bearing on physical theories.