Fundamentals CSE 373 Data Structures Lecture 5. 12/26/03Fundamentals - Lecture 52 Mathematical Background Today, we will review: ›Logs and exponents ›Series.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

Intro to Analysis of Algorithms. Algorithm “A sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
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.
COMPSCI 102 Introduction to Discrete Mathematics.
Asymptotic Growth Rates Themes –Analyzing the cost of programs –Ignoring constants and Big-Oh –Recurrence Relations & Sums –Divide and Conquer Examples.
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.
Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Lecture 2 Computational Complexity
Mathematics Review and Asymptotic Notation
Iterative Algorithm Analysis & Asymptotic Notations
Analysis of Algorithms
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.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Mathematical Background and Linked Lists. 2 Iterative Algorithm for Sum Find the sum of the first n integers stored in an array v : sum (v[], n) temp_sum.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Lauren Milne Summer 2015.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Nicki Dell Spring 2014.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Asymptotic Growth Rates  Themes  Analyzing the cost of programs  Ignoring constants and Big-Oh  Recurrence Relations & Sums  Divide and Conquer 
CSC – 332 Data Structures Generics Analysis of Algorithms Dr. Curry Guinn.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 2 Prepared by İnanç TAHRALI.
Algorithm Analysis (Big O)
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
CSE 373: Data Structures and Algorithms Lecture 4: Math Review/Asymptotic Analysis II 1.
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.
Intro to Analysis of Algorithms. Algorithm “A sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any.
CSE 326: Data Structures Class #4 Analysis of Algorithms III Analysis of Recursive Algorithms Henry Kautz Winter 2002.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Linda Shapiro Winter 2015.
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
Algorithm Analysis 1.
CMPT 438 Algorithms.
CSE373: Data Structures and Algorithms Lecture 2: Math Review; Algorithm Analysis Kevin Quinn Fall 2015.
Theoretical analysis of time efficiency
Analysis of Algorithms
Analysis of Algorithms
Introduction to Algorithms
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Catie Baker Spring 2015.
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency
Algorithm design and Analysis
Introduction to Algorithms Analysis
CSE373: Data Structures and Algorithms Lecture 2: Math Review; Algorithm Analysis Dan Grossman Fall 2013.
Comparing Algorithms Unit 1.2.
CS 201 Fundamental Structures of Computer Science
Analysis of Algorithms
DS.A.1 Algorithm Analysis Chapter 2 Overview
Chapter 2.
Programming and Data Structure
CSE 2010: Algorithms and Data Structures Algorithms
CSE 373 Data Structures Lecture 5
Searching, Sorting, and Asymptotic Complexity
CSE 373: Data Structures & Algorithms
Mathematical Background 2
IST311 - CIS265/506 Cleveland State University – Prof. Victor Matos
At the end of this session, learner will be able to:
Discrete Mathematics 7th edition, 2009
CS210- Lecture 2 Jun 2, 2005 Announcements Questions
Estimating Algorithm Performance
Analysis of Algorithms
Algorithm Course Dr. Aref Rashad
Presentation transcript:

Fundamentals CSE 373 Data Structures Lecture 5

12/26/03Fundamentals - Lecture 52 Mathematical Background Today, we will review: ›Logs and exponents ›Series ›Recursion ›Motivation for Algorithm Analysis

12/26/03Fundamentals - Lecture 53 Powers of 2 Many of the numbers we use in Computer Science are powers of 2 Binary numbers (base 2) are easily represented in digital computers ›each "bit" is a 0 or a 1 ›2 0 =1, 2 1 =2, 2 2 =4, 2 3 =8, 2 4 =16,…, 2 10 =1024 (1K) ›, an n-bit wide field can hold 2 n positive integers: 0  k  2 n

12/26/03Fundamentals - Lecture 54 Unsigned binary numbers For unsigned numbers in a fixed width field ›the minimum value is 0 ›the maximum value is 2 n -1, where n is the number of bits in the field ›The value is Each bit position represents a power of 2 with a i = 0 or a i = 1

12/26/03Fundamentals - Lecture 55 Logs and exponents Definition: log 2 x = y means x = 2 y ›8 = 2 3, so log 2 8 = 3 ›65536= 2 16, so log = 16 Notice that log 2 x tells you how many bits are needed to hold x values ›8 bits holds 256 numbers: 0 to = 0 to 255 ›log = 8

x, 2 x and log 2 x x = 0:.1:4 y = 2.^x plot(x,y,'r') hold on plot(y,x,'g') plot(y,y,'b') x y

2 x and log 2 x x = 0:10 y = 2.^x plot(x,y,'r') hold on plot(y,x,'g') plot(y,y,'b') x y

12/26/03Fundamentals - Lecture 58 Floor and Ceiling Floor function: the largest integer < X Ceiling function: the smallest integer > X

12/26/03Fundamentals - Lecture 59 Facts about Floor and Ceiling

12/26/03Fundamentals - Lecture 510 Properties of logs (of the mathematical kind) We will assume logs to base 2 unless specified otherwise log AB = log A + log B ›A=2 log 2 A and B=2 log 2 B ›AB = 2 log 2 A 2 log 2 B = 2 log 2 A+log 2 B ›so log 2 AB = log 2 A + log 2 B ›[note: log AB  log Alog B]

12/26/03Fundamentals - Lecture 511 Other log properties log A/B = log A – log B log (A B ) = B log A log log X 0 ›log log X = Y means ›log X grows slower than X called a “sub-linear” function

12/26/03Fundamentals - Lecture 512 A log is a log is a log Any base x log is equivalent to base 2 log within a constant factor substitution x = B by def. of logs log B x

12/26/03Fundamentals - Lecture 513 Arithmetic Series The sum is ›S(1) = 1 ›S(2) = 1+2 = 3 ›S(3) = = 6 Why is this formula useful when you analyze algorithms?

12/26/03Fundamentals - Lecture 514 Algorithm Analysis Consider the following program segment: x:= 0; for i = 1 to N do for j = 1 to i do x := x + 1; What is the value of x at the end?

12/26/03Fundamentals - Lecture 515 Analyzing the Loop Total number of times x is incremented is the number of “instructions” executed = You’ve just analyzed the program! ›Running time of the program is proportional to N(N+1)/2 for all N ›O(N 2 )

12/26/03Fundamentals - Lecture 516 Analyzing Mergesort Mergesort(p : node pointer) : node pointer { Case { p = null : return p; //no elements p.next = null : return p; //one element else d : duo pointer; // duo has two fields first,second d := Split(p); return Merge(Mergesort(d.first),Mergesort(d.second)); }

12/26/03Fundamentals - Lecture 517 Mergesort Analysis Upper Bound n = 2 k, k = log n

12/26/03Fundamentals - Lecture 518 Recursion Used Badly Classic example: Fibonacci numbers F n 0,1, 1, 2, 3, 5, 8, 13, 21, … ›F 0 = 0, F 1 = 1 (Base Cases) ›Rest are sum of preceding two F n = F n-1 + F n-2 (n > 1) Leonardo Pisano Fibonacci ( )

12/26/03Fundamentals - Lecture 519 Recursive Procedure for Fibonacci Numbers fib(n : integer): integer { Case { n < 0 : return 0; n = 1 : return 1; else : return fib(n-1) + fib(n-2); } Easy to write: looks like the definition of F n But, can you spot the big problem?

12/26/03Fundamentals - Lecture 520 Recursive Calls of Fibonacci Procedure Re-computes fib(N-i) multiple times!

12/26/03Fundamentals - Lecture 521 Fibonacci Analysis Lower Bound It can be shown by induction that T(n) >  n-2 where

12/26/03Fundamentals - Lecture 522 Iterative Algorithm for Fibonacci Numbers fib_iter(n : integer): integer { fib0, fib1, fibresult, i : integer; fib0 := 0; fib1 := 1; case {_ n < 0 : fibresult := 0; n = 1 : fibresult := 1; else : for i = 2 to n do { fibresult := fib0 + fib1; fib0 := fib1; fib1 := fibresult; } return fibresult; }

12/26/03Fundamentals - Lecture 523 Recursion Summary Recursion may simplify programming, but beware of generating large numbers of calls ›Function calls can be expensive in terms of time and space Be sure to get the base case(s) correct! Each step must get you closer to the base case

12/26/03Fundamentals - Lecture 524 Motivation for Algorithm Analysis Suppose you are given two algorithms A and B for solving a problem The running times T A (N) and T B (N) of A and B as a function of input size N are given TATA TBTB Run Time Input Size N Which is better?

12/26/03Fundamentals - Lecture 525 More Motivation For large N, the running time of A and B is: Now which algorithm would you choose? Run Time Input Size N T A (N) = 50N T B (N) = N 2

12/26/03Fundamentals - Lecture 526 Asymptotic Behavior The “asymptotic” performance as N  , regardless of what happens for small input sizes N, is generally most important Performance for small input sizes may matter in practice, if you are sure that small N will be common forever We will compare algorithms based on how they scale for large values of N

12/26/03Fundamentals - Lecture 527 Order Notation (one more time) Mainly used to express upper bounds on time of algorithms. “n” is the size of the input. T(n) = O(f(n)) if there are constants c and n 0 such that T(n) n 0. ›10000n + 10 n log 2 n = O(n log n) › n 2  O(n log n) Order notation ignores constant factors and low order terms.

12/26/03Fundamentals - Lecture 528 Why Order Notation Program performance may vary by a constant factor depending on the compiler and the computer used. In asymptotic performance (n  ) the low order terms are negligible.

12/26/03Fundamentals - Lecture 529 Some Basic Time Bounds Logarithmic time is O(log n) Linear time is O(n) Quadratic time is 0(n 2 ) Cubic time is O(n 3 ) Polynomial time is O(n k ) for some k. Exponential time is O(c n ) for some c > 1.

12/26/03Fundamentals - Lecture 530 Kinds of Analysis Asymptotic – uses order notation, ignores constant factors and low order terms. Upper bound vs. lower bound Worst case – time bound valid for all inputs of length n. Average case – time bound valid on average – requires a distribution of inputs. Amortized – worst case time averaged over a sequence of operations. Others – best case, common case (80%-20%) etc.