CSE332: Data Abstractions Section 2 HyeIn Kim Spring 2013.

Slides:



Advertisements
Similar presentations
Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
Advertisements

Nattee Niparnan. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.
CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis Tyler Robison Summer
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CSE 326: Data Structures Lecture #3 Analysis of Recursive Algorithms Alon Halevy Fall Quarter 2000.
Runtime Analysis CSC 172 SPRING 2002 LECTURE 9 RUNNING TIME A program or algorithm has running time T(n), where n is the measure of the size of the input.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 2 Elements of complexity analysis Performance and efficiency Motivation: analysis.
Tirgul 2 Asymptotic Analysis. Motivation: Suppose you want to evaluate two programs according to their run-time for inputs of size n. The first has run-time.
Analysis of Algorithms
1 Divide and Conquer Binary Search Mergesort Recurrence Relations CSE Lecture 4 – Algorithms II.
David Luebke 1 8/17/2015 CS 332: Algorithms Asymptotic Performance.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 1 Prepared by İnanç TAHRALI.
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Aaron Bauer Winter 2014.
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.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
Week 2 CS 361: Advanced Data Structures and Algorithms
Analysis Tools Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
Mathematics Review and Asymptotic Notation
Analyzing Recursive Algorithms A recursive algorithm can often be described by a recurrence equation that describes the overall runtime on a problem of.
Algorithm Analysis An algorithm is a clearly specified set of simple instructions to be followed to solve a problem. Three questions for algorithm analysis.
Recitation 11 Analysis of Algorithms and inductive proofs 1.
Analysis of Algorithms
2IL50 Data Structures Fall 2015 Lecture 2: Analysis of Algorithms.
CS 221 Analysis of Algorithms Instructor: Don McLaughlin.
10/25/20151 CS 3343: Analysis of Algorithms Lecture 6&7: Master theorem and substitution method.
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.
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
Introduction to Analysis of Algorithms CS342 S2004.
Algorithm Analysis Part of slides are borrowed from UST.
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.
David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Asymptotic Performance. Review: Asymptotic Performance Asymptotic performance: How does algorithm behave as the problem size gets very large? Running.
Solving Recurrences with the Substitution Method.
Spring 2015 Lecture 2: Analysis of Algorithms
Asymptotic Analysis CSE 331. Definition of Efficiency An algorithm is efficient if, when implemented, it runs quickly on real instances Implemented where?
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.
2IL50 Data Structures Spring 2016 Lecture 2: Analysis of Algorithms.
Big O David Kauchak cs302 Spring Administrative Assignment 1: how’d it go? Assignment 2: out soon… Lab code.
Kompleksitas Waktu Asimptotik CSG3F3 Lecture 5. 2 Intro to asymptotic f(n)=an 2 + bn + c RMB/CSG3F3.
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
Complexity of Algorithms Fundamental Data Structures and Algorithms Ananda Guna January 13, 2005.
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
Analysis of Algorithms and Inductive Proofs
GC 211:Data Structures Week 2: Algorithm Analysis Tools
Introduction to Algorithms
Growth of functions CSC317.
Instructor: Lilian de Greef Quarter: Summer 2017
CS 3343: Analysis of Algorithms
Data Structures and Algorithms
Asymptotes: Why? How to describe an algorithm’s running time?
CSC 413/513: Intro to Algorithms
Introduction to Algorithms Analysis
Advanced Analysis of Algorithms
Searching, Sorting, and Asymptotic Complexity
CSE 373, Copyright S. Tanimoto, 2002 Asymptotic Analysis -
CSE 373 Optional Section Led by Yuanwei, Luyi Apr
CSE 326: Data Structures Lecture #6 Asymptotic Analysis
At the end of this session, learner will be able to:
David Kauchak cs161 Summer 2009
CSE 373, Copyright S. Tanimoto, 2001 Asymptotic Analysis -
Advanced Analysis of Algorithms
Complexity Analysis (Part II)
Big O notation f = O(g(n)) c g(n)
Presentation transcript:

CSE332: Data Abstractions Section 2 HyeIn Kim Spring 2013

Section Agenda Bugs & Testing Induction Review Recurrence Relations Asymptotic Analysis Homework Tips & Questions

Bugs & Testing

Software Bugs - Error in a computer program - Causes program to behave in unexpected ways

Bugs & Testing Why Testing? Bugs can be costly - Cost points in homework - Can cost $$$ and even life (Therac-25) Interesting Bug References - List of bugs History’s / worst Bugs of the month

Bugs & Testing Reverse.java does not test your stack!! - Stack can still have lots of bugs when working perfectly with Reverse.java - Some extreme case in past quarter: it only worked with Reverse.java (Not a good stack!)

Bugs & Testing Tips for Testing - Make sure program meets the spec - Test if each method works independently - Test if methods work together - Test for edge cases

Bugs & Testing Make sure program meets the spec - What is wrong with this implementation? public class ListStack implements DStack { private LinkedList myStack; public ListStack() { myStack = new LinkedList (); } public void push(double d) { myStack.add(d); } … }

Bugs & Testing Test if each method works - Four public methods boolean isEmpty() True if no elements, false otherwise void push(E elem) Add element on top of stack E pop() Remove & return top element, exception when empty E peek() Return (but don’t remove) top element, exception when empty

Bugs & Testing Test if each method works Thorough commenting can help - Think about what each method is supposed to do - Check if the method actually does what you think it should do

Bugs & Testing Test if methods work together - Should work in any order stack.push(3.3) stack.isEmpty() stack.push(9.3) stack.peek() stack.pop() stack.push(100) stack.push(4343) …

Bugs & Testing Test for edge cases - Empty stack - Push after resizing - Anything else?

Bugs & Testing Testing tools: JUnit Testing - Not required for Project 1 - Required for Project 2 - Covered in section next week

Induction Review

Proof by Induction - Prove that the first statement in the infinite sequence of statements is true (Base case) - Prove that if any one statement in the infinite sequence of statements is true, then so is the next one. (Inductive case)

Induction Review Proof by Induction To prove statement P(n), - Base Case: Prove that P(1) is true - Inductive Case: Assuming P(k) is true, prove that P(k+1) is true

Recurrence Relations

Recursively defines a Sequence - Example: T(n) = T(n-1) + 3, T(1) = 5 ^ Has T(x) in definition Solving Recurrence Relation - Eliminate recursive part in definition = Find “Closed Form” - Example: T(n) = 3n + 2

Recurrence Relations Expansion Method example - Solve T(n) = T(n-1) + 2n – 1, T(1) = 1 T(n) = T(n-1) + 2n – 1 T(n-1) = T([n-1]-1) + 2[n-1] – 1 = T(n-2) + 2(n-1) – 1 T(n-2) = T([n-2]-1) + 2[n-2] – 1 = T(n-3) + 2(n-2) – 1

Recurrence Relations Expansion Method example T(n) = T(n-1) + 2n – 1 T(n-1) = T(n-2) + 2(n-1) – 1 T(n-2) = T(n-3) + 2(n-2) – 1 T(n) = [T(n-2) + 2(n-1) – 1] + 2n – 1 = T(n-2) + 2(n-1) + 2n – 2 T(n) = [T(n-3) + 2(n-2) – 1] + 2(n-1) + 2n – 2 = T(n-3) + 2(n-2) + 2(n-1) + 2n – 3

Recurrence Relations Expansion Method example T(n) = T(n-1) + 2n – 1 T(n) = T(n-2) + 2(n-1) + 2n – 2 T(n) = T(n-3) + 2(n-2) + 2(n-1) + 2n – 3 … T(n) = T(n-k) + [2(n-(k-1)) + … + 2(n-1) + 2n] – k = T(n-k) + [2(n-k+1) + … + 2(n-1) + 2n] – k

Recurrence Relations Expansion Method example T(n) = T(n-k) + [2(n-k+1) + … + 2(n-1) + 2n] – k When expanded all the way down, T(n-k) = T(1) n-k = 1, k = n-1 T(n) = T(n-[n-1]) + [2(n-[n-1]+1) + … + 2(n-1) + 2n] – [n-1] = T(1) + [2(2) + … + 2(n-1) + 2n] – n + 1

Recurrence Relations Expansion Method example T(n) = T(1) + [2(2) + … + 2(n-1) + 2n] – n + 1 = T(1) + 2[2 + … + (n-1) + n] – n + 1 = T(1) + 2[(n+1)(n/2) -1] – n + 1 = T(1) + (n+1)(n) - 2 – n + 1 = T(1) + (n 2 +n) – n - 1 = T(1) + n 2 – 1 = 1 + n 2 – 1 = n 2

Recurrence Relations Expansion Method example Check it! T(n) = T(n-1) + 2n – 1, T(1) = 1 T(n) = n 2 T(1) = 1 same as 1 2 T(2) = T(1) + 2(2) – 1 = 4 same as 2 2 T(3) = T(2) + 2(3) – 1 = 9 same as 3 2 T(4) = T(3) + 2(4) – 1 = 16 same as 4 2

Asymptotic Analysis

Describe Limiting behavior of F(n) - Characterize growth rate of F(n) - Use O(g(n)), Ω (g(n)), Θ (g(n)) for set of functions with asymptotic behavior , ,  &  to g(n) Upper Bound: O(n) f(n) ∈ O(g(n)) if and only if there exist positive constants c and n 0 such that f(n)  c*g(n) for all n 0  n log n ∈ O(n)

Asymptotic Analysis Lower Bound: Ω(n) f(n) ∈ Ω (g(n)) if and only if there exist positive constants c and n 0 such that c*g(n)  f(n) for all n 0  n Tight Bound: Θ(n) f(n) ∈ Θ (g(n)) if and only if f(n) ∈ Ω (g(n)) and f(n) ∈ O(g(n)) n ∈ Ω(log n) 5*log 10 n ∈ Θ(log n)

Asymptotic Analysis Ordering Growth rates (k = constant) - Ignore Low-Order terms & Coefficients O(k) constant O(log n) logarithmic Increasing O(n) linear Growth rate O(n k ) polynomial O(k n ) exponential (k > 1)

Asymptotic Analysis Ordering Growth rates

Asymptotic Analysis Ordering Growth rates - log k n ∈ O(n b ) if 1 < k & 0 < b - n k ∈ O(b n ) if 0 < k & 1 < b Ordering Example 2n n 2 n/ n/ n + log 8 n 23785n 1/ log 10 n + 1 n/300 n n/100 n n 1/2 log 10 n

Asymptotic Analysis Proof Example: f(n) ∈ O(g(n)) - Prove or disprove nlog n ∈ O (3n) nlog n ∈ O (3n) nlog n  c*(3n), for 0 < c && 0 < n 0  n (1/3)log n  c but as n → ∞, log n → ∞ Finite constant c always greater than log n cannot exist, no matter what n 0 we choose nlog n ∉ O (3n)

Homework Tips

Problem #1 - Use formula in the book (You don’t have to derive it by yourself) Problem #2 - Use following rules: 1. which means 2.

Homework Tips Problem #3 - f(n) x sec  t sec, solve for n Problem #4 <= Not in this HW - Remember that when you are proving P(k+1), you are assuming P(k) no matter how silly it is! - Find flaw in inductive reasoning

Homework Tips Problem #5 - Use definitions and show you can/cannot find the constant c Problem #6 - Analyze runtime of each loop & merge when appropriate - Practice finding exact runtime when you can - Think about maximum iteration of each loop