1 i206: Lecture 6: Math Review, Begin Analysis of Algorithms Marti Hearst Spring 2012.

Slides:



Advertisements
Similar presentations
Estimating Running Time Algorithm arrayMax executes 3n  1 primitive operations in the worst case Define a Time taken by the fastest primitive operation.
Advertisements

CSC401 – Analysis of Algorithms Lecture Notes 1 Introduction
© 2004 Goodrich, Tamassia 1 Lecture 01 Algorithm Analysis Topics Basic concepts Theoretical Analysis Concept of big-oh Choose lower order algorithms Relatives.
Analysis of Algorithms Algorithm Input Output. Analysis of Algorithms2 Outline and Reading Running time (§1.1) Pseudo-code (§1.1) Counting primitive operations.
Software Design Analysis of Algorithms i206 Fall 2010 John Chuang Some slides adapted from Glenn Brookshear, Marti Hearst, or Goodrich & Tamassia.
Analysis of Algorithms1 CS5302 Data Structures and Algorithms Lecturer: Lusheng Wang Office: Y6416 Phone:
Analysis of Algorithms (Chapter 4)
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
Analysis of Algorithms
Analysis of Algorithms (pt 2) (Chapter 4) COMP53 Oct 3, 2007.
Fall 2006CSC311: Data Structures1 Chapter 4 Analysis Tools Objectives –Experiment analysis of algorithms and limitations –Theoretical Analysis of algorithms.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 10: Math Review, Intro to Analysis of Algorithms.
The Seven Functions. Analysis of Algorithms. Simple Justification Techniques. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer Goodrich,
Analysis of Algorithms1 CS5302 Data Structures and Algorithms Lecturer: Lusheng Wang Office: Y6416 Phone:
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 11: Analysis of Algorithms, cont.
Analysis of Performance
COMPSCI 102 Introduction to Discrete Mathematics.
CS2210 Data Structures and Algorithms Lecture 2:
Analysis of Algorithms Algorithm Input Output © 2014 Goodrich, Tamassia, Goldwasser1Analysis of Algorithms Presentation for use with the textbook Data.
Analysis of Algorithms Lecture 2
Analysis Tools Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
Analysis of Algorithms
Analysis of Algorithms1 The Goal of the Course Design “good” data structures and algorithms Data structure is a systematic way of organizing and accessing.
Mathematics Review and Asymptotic Notation
Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity)
Data Structures Lecture 8 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
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.
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Algorithm Analysis (Algorithm Complexity). Correctness is Not Enough It isn’t sufficient that our algorithms perform the required tasks. We want them.
1 Dr. J. Michael Moore Data Structures and Algorithms CSCE 221 Adapted from slides provided with the textbook, Nancy Amato, and Scott Schaefer.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
CMSC 341 Asymptotic Analysis. 2 Complexity How many resources will it take to solve a problem of a given size? –time –space Expressed as a function of.
Introduction to Analysis of Algorithms CS342 S2004.
Analysis of Algorithms Algorithm Input Output © 2010 Goodrich, Tamassia1Analysis of Algorithms.
Analysis of algorithms. What are we going to learn? Need to say that some algorithms are “better” than others Criteria for evaluation Structure of programs.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
Announcement We will have a 10 minutes Quiz on Feb. 4 at the end of the lecture. The quiz is about Big O notation. The weight of this quiz is 3% (please.
Complexity of Algorithms Fundamental Data Structures and Algorithms Ananda Guna January 13, 2005.
LECTURE 2 : fundamentals of analysis of algorithm efficiency Introduction to design and analysis algorithm 1.
1 COMP9024: Data Structures and Algorithms Week Two: Analysis of Algorithms Hui Wu Session 2, 2014
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
Analysis of Algorithms Algorithm Input Output © 2014 Goodrich, Tamassia, Goldwasser1Analysis of Algorithms Presentation for use with the textbook Data.
Analysis of Algorithms
Analysis of Algorithms
COMP9024: Data Structures and Algorithms
COMP9024: Data Structures and Algorithms
COMP108 Algorithmic Foundations Algorithm efficiency
Introduction to Algorithms
Analysis of Algorithms
COMP9024: Data Structures and Algorithms
Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
i206: Lecture 8: Sorting and Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
Advanced Analysis of Algorithms
Analysis of Algorithms
Algorithm Analysis Bina Ramamurthy CSE116A,B.
Introduction to Discrete Mathematics
Performance Evaluation
CS210- Lecture 2 Jun 2, 2005 Announcements Questions
Analysis of Algorithms
Analysis of Algorithms
Presentation transcript:

1 i206: Lecture 6: Math Review, Begin Analysis of Algorithms Marti Hearst Spring 2012

2 Examples of Social Algorithms?

3 Which Algorithm is Better? What do we mean by “ Better ” ? Sorting algorithms –Bubble sort –Insertion sort –Shell sort –Merge sort –Heapsort –Quicksort –Radix sort –… Search algorithms –Linear search –Binary search –Breadth first search –Depth first search –…

4 Analysis of Algorithms Characterizing the running times of algorithms and data structure operations Secondarily, characterizing the space usage of algorithms and data structures

5 Why Analysis of Algorithms? To find out –How long an algorithm takes to run –How to compare different algorithms This is done at a very abstract level This can be done before code is written Alternative: Performance analysis –Actually time each operation as the program is running Specific to the machine and the implementation of the algorithm –Can only be done after code is written

6 Running Time In general, running time increases with input size Running time also affected by: –Hardware environment (processor, clock rate, memory, disk, etc.) –Software environment (operating system, programming language, compiler, interpreter, etc.) (n)

7 Intuitions with Playing Cards

8 Functions, Graphs of Functions Function: a rule that –Coverts inputs to outputs in a well-defined way. –This conversion process is often called mapping. –Input space called the domain –Output space called the range

9 Functions, Graphs of Functions Function: a rule that –Coverts inputs to outputs in a well-defined way. –This conversion process is often called mapping. –Input space called the domain –Output space called the range Examples –Mapping of speed of bicycling to calories burned Domain: values for speed Range: values for calories –Mapping of people to names Domain: people Range: Strings

10 Example: How many calories does bicycling burn? Miles/Hour vs. KiloCalories/Minute For a 150 lb rider. Green: riding on a gravel road with a mountain bike Blue: paved road riding a touring bicycle Red: racing bicyclist. From Whitt, F.R. & D. G. Wilson Bicycling Science (second edition).

11 Functions and Graphs of Functions Notation  Many different kinds  f(x) is read “ f of x ”  This means a function called f takes x as an argument  f(x) = y  This means the function f takes x as input and produces y as output.  The rule for the mapping is hidden by the notation.

12 Here f(x) = 7x A point on this graph can be called (x,y) or (x, f(x))

13 A straight line is defined by the function y = ax + b a and b are constants x is variable Here y = 7x + 0

14 Exponents and Logarithms Exponents: shorthand for multiplication Logarithms: shorthand for exponents 2 3 = 8 can be expressed as log 2 8 = 3 or we can say that the 'log with base 2 of 8' = 3. How we use these? –Difficult computational problems grow exponentially –Logarithms are useful for “ squashing ” them

15 Logarithm lets you “grab the exponent”

16 The exponential function f with base a is denoted by and x is any real number. Note how much more “ quickly the graph grows ” than the linear graph of f(x) = x Example: If the base is 2 and x = 4, the function value f(4) will equal 16. A corresponding point on the graph would be (4, 16).

17

18 Logarithmic functions are the inverse of exponential functions. If (4, 16) is a point on the graph of an exponential function, then (16, 4) would be the corresponding point on the graph of the inverse logarithmic function.

19 Illustration by Jacob Nielsen Zipf Distribution (linear and log scale)

20 Other Functions Quadratic function: This is a graph of: (-2,0) (2,0)

21 Summation Notation

22 Summation Notation

23 Intuition behind n(n+1)/2 A visual proof that n = n(n+1)/2 Count the number of dots in T(n) but, instead of summing the numbers 1, 2, 3, … up to n we will find the total using only one multiplication and one division!

Intuition behind n(n+1)/2 A visual proof that n = n(n+1)/2 Notice that we get a rectangle which is has the same number of rows (4) but has one extra column (5) so the rectangle is 4 by 5 it therefore contains 4x5=20 balls but we took two copies of T(4) to get this so we must have 20/2 = 10 balls in T(4), which we can easily check.

25 Intuition behind n(n+1)/2 A visual proof that n = n(n+1)/2

26 Intuition behind n(n+1)/2 T(n) = n = n(n + 1)/2 The same proof using algebra:

27 Summation Notation and Code

28 Iterative form vs. Closed Form

29 The Seven Common Functions Constant Linear Quadratic Cubic Exponential Logarithmic n-log-n Polynomial

30 Function Pecking Order In increasing order Adapted from Goodrich & Tamassia

31 More Plots

32 Definition of Big-Oh A running time is O(g(n)) if there exist constants n 0 > 0 and c > 0 such that for all problem sizes n > n 0, the running time for a problem of size n is at most c(g(n)). In other words, c(g(n)) is an upper bound on the running time for sufficiently large n. c g(n) Example: the function f(n)=8n–2 is O(n) The running time of algorithm arrayMax is O(n)

33 The Crossover Point Adapted from One function starts out faster for small values of n. But for n > n0, the other function is always faster.

34 More formally Let f(n) and g(n) be functions mapping nonnegative integers to real numbers. f(n) is  (g(n)) if there exist positive constants n0 and c such that for all n>=n0, f(n) <= c*g(n) Other ways to say this: f(n) is order g(n) f(n) is big-Oh of g(n) f(n) is Oh of g(n) f(n)  O(g(n)) (set notation)

35 Comparing Running Times Adapted from Goodrich & Tamassia

36 Analysis Example: Phonebook Given: –A physical phone book Organized in alphabetical order –A name you want to look up –An algorithm in which you search through the book sequentially, from first page to last –What is the order of: The best case running time? The worst case running time? The average case running time? –What is: A better algorithm? The worst case running time for this algorithm?

37 Analysis Example (Phonebook) This better algorithm is called Binary Search What is its running time? –First you look in the middle of n elements –Then you look in the middle of n/2 = ½*n elements –Then you look in the middle of ½ * ½*n elements –…–… –Continue until there is only 1 element left –Say you did this m times: ½ * ½ * ½* …*n –Then the number of repetitions is the smallest integer m such that

38 Analyzing Binary Search –In the worst case, the number of repetitions is the smallest integer m such that –We can rewrite this as follows: Multiply both sides by Take the log of both sides Since m is the worst case time, the algorithm is O(logn)

39 Binary Search … for Chocolate!