1 CSE 326 Data Structures: Complexity Lecture 2: Wednesday, Jan 8, 2003.

Slides:



Advertisements
Similar presentations
Time Analysis Since the time it takes to execute an algorithm usually depends on the size of the input, we express the algorithm's time complexity as a.
Advertisements

Analysis of Algorithms
Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.
CSE 373 Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III.
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
CPSC 320: Intermediate Algorithm Design & Analysis Asymptotic Notation: (O, Ω, Θ, o, ω) Steve Wolfman 1.
CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis Tyler Robison Summer
Chapter 3 Growth of Functions
The Growth of Functions
CSE 326 Asymptotic Analysis David Kaplan Dept of Computer Science & Engineering Autumn 2001.
CSE 326: Data Structures Lecture #3 Analysis of Recursive Algorithms Alon Halevy Fall Quarter 2000.
CSE 326: Data Structures Introduction 1Data Structures - Introduction.
Analyzing algorithms & Asymptotic Notation BIO/CS 471 – Algorithms for Bioinformatics.
© 2006 Pearson Addison-Wesley. All rights reserved6-1 More on Recursion.
CSE 326: Data Structures Lecture #2 Analysis of Algorithms Alon Halevy Fall Quarter 2000.
25 June 2015Comp 122, Spring 2004 Asymptotic Notation, Review of Functions & Summations.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (1) Asymptotic Complexity 10/28/2008 Yang Song.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
CSE 326: Data Structures Introduction & Part One: Complexity Henry Kautz Autumn Quarter 2002.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 1 Prepared by İnanç TAHRALI.
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Aaron Bauer Winter 2014.
SIGCSE Tradeoffs, intuition analysis, understanding big-Oh aka O-notation Owen Astrachan
Asymptotic Notations Iterative Algorithms and their analysis
CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea Algorithm Analysis II Reading: Weiss, chap. 2.
Mathematics Review and Asymptotic Notation
CS 3343: Analysis of Algorithms
Iterative Algorithm Analysis & Asymptotic Notations
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Course Review Midterm.
Analysis of Algorithms
1 COMP3040 Tutorial 1 Analysis of algorithms. 2 Outline Motivation Analysis of algorithms Examples Practice questions.
Analyzing algorithms & Asymptotic Notation BIO/CS 471 – Algorithms for Bioinformatics.
Fundamentals CSE 373 Data Structures Lecture 5. 12/26/03Fundamentals - Lecture 52 Mathematical Background Today, we will review: ›Logs and exponents ›Series.
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
Lecture 3 Analysis of Algorithms, Part II. Plan for today Finish Big Oh, more motivation and examples, do some limit calculations. Little Oh, Theta notation.
Growth of Functions. 2 Analysis of Bubble Sort 3 Time Analysis – Best Case The array is already sorted – no swap operations are required.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
1 Asymptotic Notations Iterative Algorithms and their analysis Asymptotic Notations –Big O,  Notations Review of Discrete Math –Summations –Logarithms.
Asymptotics and Recurrence Equations Prepared by John Reif, Ph.D. Analysis of Algorithms.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
CSE 326: Data Structures Asymptotic Analysis Hannah Tang and Brian Tjaden Summer Quarter 2002.
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.
CSE 326: Data Structures Lecture #3 Asymptotic Analysis Steve Wolfman Winter Quarter 2000.
ALGORITHM ANALYSIS Analysis of Resource consumption by processor –Time –Memory –Number of processors –Power Proof that the algorithm works correctly Debasis.
Complexity of Algorithms Fundamental Data Structures and Algorithms Ananda Guna January 13, 2005.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
COMP108 Algorithmic Foundations Algorithm efficiency
Chapter 3: Growth of Functions
CS 3343: Analysis of Algorithms
Analysis of Algorithms
Chapter 3: Growth of Functions
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
Introduction to Algorithms Analysis
CS 3343: Analysis of Algorithms
Defining Efficiency Asymptotic Complexity - how running time scales as function of size of input Two problems: What is the “input size” ? How do we express.
DS.A.1 Algorithm Analysis Chapter 2 Overview
CSE 373 Data Structures Lecture 5
Algorithm Analysis, Asymptotic notations CISC4080 CIS, Fordham Univ.
At the end of this session, learner will be able to:
Discrete Mathematics 7th edition, 2009
Intro to Theory of Computation
Presentation transcript:

1 CSE 326 Data Structures: Complexity Lecture 2: Wednesday, Jan 8, 2003

2 Overview of the Quarter Complexity and analysis of algorithms List-like data structures Search trees Priority queues Hash tables Sorting Disjoint sets Graph algorithms Algorithm design Advanced topics Midterm around here

3 Complexity and analysis of algorithms Weiss Chapters 1 and 2 Additional material –Cormen,Leiserson,Rivest – on reserve at Eng. library –Graphical analysis –Amortized analysis

4 Program Analysis Correctness –Testing –Proofs of correctness Efficiency –Asymptotic complexity - how running times scales as function of size of input

5 Proving Programs Correct Often takes the form of an inductive proof Example: summing an array int sum(int v[], int n) { if (n==0) return 0; else return v[n-1]+sum(v,n-1); } int sum(int v[], int n) { if (n==0) return 0; else return v[n-1]+sum(v,n-1); } What are the parts of an inductive proof?

6 Inductive Proof of Correctness int sum(int v[], int n) { if (n==0) return 0; else return v[n-1]+sum(v,n-1); } int sum(int v[], int n) { if (n==0) return 0; else return v[n-1]+sum(v,n-1); } Theorem: sum(v,n) correctly returns sum of 1 st n elements of array v for any n. Basis Step: Program is correct for n=0; returns 0. Inductive Hypothesis (n=k): Assume sum(v,k) returns sum of first k elements of v. Inductive Step (n=k+1): sum(v,k+1) returns v[k]+sum(v,k), which is the same of the first k+1 elements of v.

7 Inductive Proof of Correctness int find(int x, int v[], int n) { int left = -1; int right = n; while (left+1 < right) { int m = (left+right) / 2; if (x == v[m]) return m; if (x < v[m]) right = m; else left = m; } return –1; /* not found */ } int find(int x, int v[], int n) { int left = -1; int right = n; while (left+1 < right) { int m = (left+right) / 2; if (x == v[m]) return m; if (x < v[m]) right = m; else left = m; } return –1; /* not found */ } Binary search in a sorted array: v[0]  v[1] ...  v[n-1] Given x, find m s.t. x=v[m] Exercise 1: proof that it is correct Exercise 2: compute m, k s.t. v[m-1] < x = v[m] = v[m+1] =... = v[k] < v[k+1]

8 Proof by Contradiction Assume negation of goal, show this leads to a contradiction Example: there is no program that solves the “halting problem” –Determines if any other program runs forever or not Alan Turing, 1937

9 Does NonConformist(NonConformist) halt? Yes? That means HALT(NonConformist) = “never halts” No? That means HALT(NonConformist) = “halts” Program NonConformist (Program P) If ( HALT(P) = “never halts” ) Then Halt Else Do While (1 > 0) Print “Hello!” End While End If End Program Contradiction!

10 Defining Efficiency Asymptotic Complexity - how running time scales as function of size of input Two problems: –What is the “input size” ? –How do we express the running time ? (The Big-O notation)

11 Input Size Usually: length (in characters) of input Sometimes: value of input (if it is a number) Which inputs? –Worst case: tells us how good an algorithm works –Best case: tells us how bad an algorithm works –Average case: useful in practice, but there are technical problems here (next)

12 Input Size Average Case Analysis Assume inputs are randomly distributed according to some “realistic” distribution  Compute expected running time Drawbacks –Often hard to define realistic random distributions –Usually hard to perform math

13 Input Size Recall the function: find(x, v, n) Input size: n (the length of the array) T(n) = “running time for size n ” But T(n) needs clarification: –Worst case T(n): it runs in at most T(n) time for any x,v –Best case T(n): it takes at least T(n) time for any x,v –Average case T(n): average time over all v and x

14 Input Size Amortized Analysis Instead of a single input, consider a sequence of inputs: –This is interesting when the running time on some input depends on the result of processing previous inputs Worst case analysis over the sequence of inputs Determine average running time on this sequence Will illustrate in the next lecture

15 Definition of Order Notation Upper bound:T(n) = O(f(n))Big-O Exist constants c and n’ such that T(n)  c f(n)for all n  n’ Lower bound:T(n) =  (g(n))Omega Exist constants c and n’ such that T(n)  c g(n)for all n  n’ Tight bound: T(n) =  (f(n))Theta When both hold: T(n) = O(f(n)) T(n) =  (f(n)) Other notations: o(f),  (f) - see Cormen et al.

16 Example: Upper Bound

17 Using a Different Pair of Constants

18 Example: Lower Bound

19 Conventions of Order Notation

20 Which Function Dominates? f(n) = n 3 + 2n 2 n 0.1 n + 100n 0.1 5n 5 n n / log n g(n) = 100n log n 2n + 10 log n n! 1000n 15 3n 7 + 7n Question to class: is f = O(g) ? Is g = O(f) ?

21 Race I f(n)= n 3 +2n 2 g(n)=100n vs.

22 Race II n 0.1 log n vs.

23 Race III n + 100n 0.1 2n + 10 log n vs.

24 Race IV 5n 5 n! vs.

25 Race V n n / n 15 vs.

26 Race VI 8 2log(n) 3n 7 + 7n vs.

27 Eliminate low order terms Eliminate constant coefficients

28 Common Names constant:O(1) logarithmic:O(log n) linear:O(n) log-linear:O(n log n) quadratic:O(n 2 ) exponential:O(c n ) (c is a constant > 1) hyperexponential: (a tower of n exponentials Other names: superlinear:O(n c )(c is a constant > 1) polynomial:O(n c )(c is a constant > 0) Fastest Growth Slowest Growth

29 Sums and Recurrences Often the function f(n) is not explicit but expressed as: A sum, or A recurrence Need to obtain analytical formula first

30 Sums

31 More Sums Sometimes sums are easiest computed with integrals:

32 Recurrences f(n) = 2f(n-1) + 1, f(0) = T Telescoping  f(n)+1 = 2(f(n-1)+1) f(n-1)+1 = 2(f(n-2)+1)  2 f(n-2)+1 = 2(f(n-3)+1)  f(1) + 1 = 2(f(0) + 1)  2 n-1  f(n)+1 = 2 n (f(0)+1) = 2 n (T+1)  f(n) = 2 n (T+1) - 1

33 Recurrences Fibonacci: f(n) = f(n-1)+f(n-2), f(0)=f(1)=1  try f(n) = A c n What is c ? A c n = A c n-1 + A c n-2 c 2 – c – 1 = 0 Constants A, B can be determined from f(0), f(1) – not interesting for us for the Big O notation

34 Recurrences f(n) = f(n/2) + 1, f(1) = T Telescoping: f(n) = f(n/2) + 1 f(n/2) = f(n/4) f(2) = f(1) + 1 = T + 1  f(n) = T + log n = O(log n)