“It ain’t no good if it ain’t snappy enough.” (Efficient Computations) COS 116, Spring 2012 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.
Algorithms (continued)
Divide-and-Conquer Recursive in structure –Divide the problem into several smaller sub-problems that are similar to the original but smaller in size –Conquer.
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.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Wednesday, 11/25/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/25/02  QUESTIONS??  Today:  More on sorting. Advanced sorting algorithms.  Complexity:
CS107 Introduction to Computer Science
Complexity Analysis (Part I)
“It ain’t no good if it ain’t snappy enough.” (Efficient Computations) COS 116: 2/19/2008 Sanjeev Arora.
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;
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.
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?
Week 2 CS 361: Advanced Data Structures and Algorithms
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.
“It ain’t no good if it ain’t snappy enough.” (Efficient Computations) COS 116, Spring 2010 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
Lecture 4. RAM Model, Space and Time Complexity
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
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
“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.
1 Algorithms CS 202 Epp section ??? Aaron Bloomfield.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Sorting.
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.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 5: Algorithms.
(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)
CS 150: Analysis of Algorithms. Goals for this Unit Begin a focus on data structures and algorithms Understand the nature of the performance of algorithms.
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.
1 Algorithms Searching and Sorting Algorithm Efficiency.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
Chapter 16: Searching, Sorting, and the vector Type
Applied Discrete Mathematics Week 2: Functions and Sequences
Analysis of Algorithms
Sorting by Tammy Bailey
David Kauchak CS52 – Spring 2015
Presentation transcript:

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

Today’s focus: efficiency in computation “If it is worth doing, it is worth doing well, and fast.” Recall: our model of “computation”: pseudocode

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 operations 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)

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

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

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

“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 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.