CS 61B Data Structures and Programming Methodology July 10, 2008 David Sun.

Slides:



Advertisements
Similar presentations
 O: order of magnitude  Look at the loops and to see whether the loops are nested. ◦ One single loop: O(n) ◦ A nested loop: O(n 2 ) ◦ A nested loop.
Advertisements

Lecture: Algorithmic complexity
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Practice Quiz Question
HST 952 Computing for Biomedical Scientists Lecture 10.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
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.
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
Introduction to Analysis of Algorithms
Complexity Analysis (Part I)
CS 307 Fundamentals of Computer Science 1 Asymptotic Analysis of Algorithms (how to evaluate your programming power tools) based on presentation material.
Analysis of Algorithms. Time and space To analyze an algorithm means: –developing a formula for predicting how fast an algorithm is, based on the size.
Cmpt-225 Algorithm Efficiency.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
CS2336: Computer Science II
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 1 Prepared by İnanç TAHRALI.
Algorithm Analysis (Big O)
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Aaron Bauer Winter 2014.
COMP s1 Computing 2 Complexity
Analysis of Performance
Asymptotic Notations Iterative Algorithms and their analysis
1 Chapter 2 Program Performance – Part 2. 2 Step Counts Instead of accounting for the time spent on chosen operations, the step-count method accounts.
Program Efficiency and Complexity
Algorithm Analysis & Complexity We saw that a linear search used n comparisons in the worst case (for an array of size n) and binary search had logn comparisons.
Program Performance & Asymptotic Notations CSE, POSTECH.
Week 2 CS 361: Advanced Data Structures and Algorithms
1 ©2008 DEEDS Group Introduction to Computer Science 2 - SS 08 Asymptotic Complexity Introduction in Computer Science 2 Asymptotic Complexity DEEDS Group.
Data Structures and Algorithms Lecture 5 and 6 Instructor: Quratulain Date: 15 th and 18 th September, 2009 Faculty of Computer Science, IBA.
Algorithm Analysis. Algorithm An algorithm is a clearly specified set of instructions which, when followed, solves a problem. recipes directions for putting.
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
Iterative Algorithm Analysis & Asymptotic Notations
Algorithm Analysis An algorithm is a clearly specified set of simple instructions to be followed to solve a problem. Three questions for algorithm analysis.
Complexity Analysis Chapter 1.
Analysis of Algorithms
CS 221 Analysis of Algorithms Instructor: Don McLaughlin.
CSCI 3160 Design and Analysis of Algorithms Tutorial 1
Asymptotic Analysis-Ch. 3
Algorithms Growth of Functions. Some Notation NNatural numbers RReal numbers N + Positive natural numbers R + Positive real numbers R * Non-negative real.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
The Time Complexity of an Algorithm Specifies how the running time depends on the size of the input. CSE 3101Z Design and Analysis of Algorithms.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Algorithm Analysis O Ω.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Lauren Milne Summer 2015.
CSC – 332 Data Structures Generics Analysis of Algorithms Dr. Curry Guinn.
Algorithmic Analysis Charl du Plessis and Robert Ketteringham.
1 Asymptotic Notations Iterative Algorithms and their analysis Asymptotic Notations –Big O,  Notations Review of Discrete Math –Summations –Logarithms.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Algorithm Analysis (Big O)
27-Jan-16 Analysis of Algorithms. 2 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based.
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.
Algorithm Complexity L. Grewe 1. Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them?
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
DS.A.1 Algorithm Analysis Chapter 2 Overview Definitions of Big-Oh and Other Notations Common Functions and Growth Rates Simple Model of Computation Worst.
A Introduction to Computing II Lecture 5: Complexity of Algorithms Fall Session 2000.
Big O David Kauchak cs302 Spring Administrative Assignment 1: how’d it go? Assignment 2: out soon… Lab code.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
Introduction to Analysis of Algorithms
Introduction to Algorithms
CS 3343: Analysis of Algorithms
Efficiency (Chapter 2).
CS 61B Data Structures and Programming Methodology
CS 201 Fundamental Structures of Computer Science
Presentation transcript:

CS 61B Data Structures and Programming Methodology July 10, 2008 David Sun

So far… We’ve been mainly looking at – the syntax of the Java language. – key ideas in object-oriented programming: objects, inheritance, polymorphism, and dynamic binding, access privileges. – mechanisms provided in Java for organization of abstractions: abstracted classes, interfaces, packages. – Head First Java Chapter

Now... We are going to continue our voyage with Java, but with a different focus. We are going to examine – a range of canonical data structures. – algorithms often associated with these data structures. – the focus is on the efficiency and cost of these algorithms and data structures.

Measuring Cost

What Does Cost Mean? Cost can mean – Development costs: How much engineering time? When delivered? – Costs of failure: How robust? How safe? – Operational cost (for programs, time to run, space requirements). Is this program fast enough? Depends on: – What purpose. – What input data.

Cost Measures (Time) – Wall-clock or execution time – You can do this at home: time java FindPrimes 1000 – Advantages: easy to measure, meaning is obvious. Appropriate where time is critical (real-time systems, e.g.). – Disadvantages: applies only to specific data set, compiler, machine, etc. Number of times certain statements are executed: – Advantages: more general (not sensitive to speed of machine). – Disadvantages: still applies only to specific data sets. Symbolic execution times: – Formulas for execution times or statement counts in terms of input size. – Advantages: applies to all inputs, makes scaling clear. – Disadvantage: practical formula must be approximate, may tell very little about actual time.

Example An algorithm for processing a retail store's inventory takes: – 10,000 ms to read the initial inventory from disk, and then – 10 ms to process each transaction – Processing n transactions takes (10, * n) ms. – 10 * n is more important if n is very large.

Asymptotic Cost The constant coefficients can change: – If we had a faster computer or, – Use a different language or compiler. The constant factors can get smaller as technology improves. We want to express the speed of an algorithm independently of a specific implementation on a specific machine. We examine the cost of the algorithms for large input sets i.e. the asymptotic cost.

Big-Oh Specify bounding from above: – Big-Oh says how slowly code might run as its input size grows. Let n be the size of a program's input. Let T(n) be a function that equals to the algorithm's running time, given an input of size n. Let f(n) be another function. We say that if and only if whenever n is big, and for some constant c.

Consider the cost function: T(n) = 10, * n, Let's try out f(n) = n. We can choose c as large as we want : Example As these functions extend forever to the right (infinity), their lines will never cross again. – For any n bigger than 1000, T(n) ≤ c * f(n).

Definition O(f(n)) is the set of all functions T(n) that satisfy: – There exist positive constants c and N such that, for all In the example above: c = 20, and N = 1000.

T(n) ≤ 2f(n) whenever, n ≥ 1 So: T(n) is in O(f(n)) Notice T(n) > f(n) everywhere. 2f(n) f(n) T(n)

Examples If T(n) = 1,000,000 * n, T(n) is in O(n). – Let f(n) = n, set c = 1,000,000, N = 1: T(n) = 1,000,000 * n ≤ c * f(n) – Big-Oh notation doesn't care about (most) constant factors. – Generally leave constants out; it's unnecessary to write O(2n). If T(n) = n, T(n) is in O(n 3 ). – Let f(n) = n 3, set c = 1, N = 1: T(n) = n ≤ n 3 = f(n) – Big-Oh notation can be misleading. Just because an algorithm's running time is in O(n 3 ) doesn't mean it's slow; it might also be in O(n).

More Examples If T(n) = n 3 + n 2 + n, then T(n) is in O(n 3 ). – Let T(n) = n 3, set c = 3, N = 1. T(n) = n 3 + n 2 + n ≤ 3 * n 3 = c * f(n) – Big-Oh notation is usually used only to indicate the dominating term in the function. The other terms become insignificant when n is really big.

Important Big-Oh Sets FunctionCommon Name O(1)Constant O(log n)Logarithmic O( log 2 n)Log-squared O( )Root-n O(n)Linear O(n log n)n log n O(n 2 )Quadratic O(n 3 )Cubic O(n 4 )Quartic O(2 n )Exponential O(e n )Bigger exponential Subset of

Example /** Find position of X in list Return -1 if not found */ class List {... int find (Object X) { int c = 0; ListNode cur = head; for (; cur != null; cur = cur.next, c++) { if (cur.item.equals(X)) return c; } return -1; } Choose representative operation: number of. equals tests. If N is length of the list, then loop does at most N tests: worst-case time is N tests. Worst-case time is O(N);

Caveats About Constants n 2 is in O(n) – Justification: if we choose c = n, we get n 2 ≤ n 2. – c must be a constant; it cannot depend on n. WRONG!

Caveats About Constants e 3n is in O(e n ) because the constant factor 3 don't matter. 10 n is in O(2 n ) because the constant factor 10 don't matter. Big-Oh notation doesn't care about most constant factors. But… Constant factor in an exponent is not the same as a constant factor in front of a term. – e 3n is bigger than e n by a factor of e 2n – 10 n is bigger than 2 n by a factor of 5 n. WRONG!

Caveats About Constants Problem actual size does matter in practice. Example: – An algorithm runs in time T(n) = n log n, and another algorithm runs in time U(n) = 100 * n – Big-Oh notation suggests you should use U(n), because T(n) dominates U(n) asymptotically. – In practice, U(n) is only faster than T(n) if your input size is greater than current estimates of the number of subatomic particles in the universe. – For U(n) ≤ T(n), 100 ≤ log n, ≤ n

Omega Big-Oh is an upper bound, it says, "Your algorithm is at least this good." Omega gives us a lower bound, it says, "Your algorithm is at least this bad." Omega is the reverse of Big-Oh: – If T(n) is in O(f(n)), f(n) is in Ω(T(n)).

Examples 2n is in Ω(n) because n is in O(2n). n 2 is in Ω(n) because n is in O(n 2 ). n 2 is in Ω(3n 2 + n log n) because 3n 2 + n log n is in O(n 2 ).

Definition Ω(f(n)) is the set of all functions T(n) that satisfy: – There exist positive constants d and N such that, for all n ≥ N, T(n) ≥ d * f(n)

T(n) ≥ 0.5f(n) whenever n ≥ 1 So T(n) is in Ω(f(n)) Notice T(x) < f(n) everywhere. f(n) 0.5 * f(n) T(n)

Theta If we have: T(n) is in O(f(n)) and is also in Ω(g(n)) then T(n) is effectively sandwiched between c * f(n) and d * g(n). When f(n) = g(n) we say that T(n) is in Ѳ(g(n)). But how can a function be sandwiched between f(n) and f(n)? – we choose different constants (c and d) for the upper bound and lower bound.

Let c = 1, d = 0.5, c*f(n) ≥ T(n) ≥ 0.5f(n) whenever x ≥ 1 So f‘(x) is in Ѳ(g(n)) c * f(n) d * f(n) T(n)

Interpreting Choice of O, Ω, or Ѳ is independent of whether we're talking about worst-case running time, best-case running time, average-case running time, memory use, or some other function. "Big-Oh" is NOT a synonym for "worst-case running time," and Omega is not a synonym for "best-case running time."

Analysis Example 1 Problem #1: Given a set of p points, find the pair closest to each other. Algorithm #1: Calculate the distance between each pair; return the minimum. double minDistance = point[0].distance(point[1]); /* Visit a pair (i, j) of points. */ for (int i = 0; i < numPoints; i++) { /* We require that j > i so that each pair is visited only once. */ for (int j = i + 1; j < numPoints; j++) { double thisDistance = point[i].distance(point[j]); if (thisDistance < minDistance) { minDistance = thisDistance; } There are p (p - 1) / 2 pairs, and each pair takes constant time to examine. Therefore, worst- and best-case running times are in Ѳ(p 2 ).

Analysis Example 2 Problem #2: Smooshing an array called "ints" to remove consecutive duplicates Algorithm #2: int i = 0, j = 0; while (i < ints.length) { ints[j] = ints[i]; do { i++; } while ((i < ints.length) && (ints[i] == ints[j])); j++; } The outer loop can iterate up to ints.length times, and so can the inner loop. But the index "i" advances on every iteration of the inner loop. It can't advance more than ints.length times before both loops end. So the worst-case running time of this algorithm is Theta(p) time.

Analysis Example 3 /** True iff X is a substring of S */ boolean occurs (String S, String X) { if (S.equals (X)) return true; if (S.length () <= X.length () return false; return occurs (S.substring (1), X) || occurs (S.substring (0, S.length ()-1), X); }