CSE 326: Data Structures Lecture #4 Alon Halevy Spring Quarter 2001.

Slides:



Advertisements
Similar presentations
Slide: 1 Interra Induction Training Data Structure ADT & Complexity.
Advertisements

Chapter 7. Binary Search Trees
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Introduction to Algorithms Quicksort
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Algorithmic Complexity Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CSE 326 Analyzing Recursion David Kaplan Dept of Computer Science & Engineering Autumn 2001.
CPSC 411, Fall 2008: Set 6 1 CPSC 411 Design and Analysis of Algorithms Set 6: Amortized Analysis Prof. Jennifer Welch Fall 2008.
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.
1 CSE 326: Data Structures Program Analysis Lecture 3: Friday, Jan 8, 2003.
CSE 326: Data Structures Lecture #2 Analysis of Algorithms Alon Halevy Fall Quarter 2000.
Andreas Klappenecker [based on the slides of Prof. Welch]
Tirgul 4 Order Statistics Heaps minimum/maximum Selection Overview
CSE 326: Data Structures Lecture #5 Alon Halevy Spring Quarter 2001.
1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003.
CSE 326: Data Structures Introduction & Part One: Complexity Henry Kautz Autumn Quarter 2002.
CSE 373 Data Structures Lecture 15
Algorithm Cost Algorithm Complexity. Algorithm Cost.
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Aaron Bauer Winter 2014.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
Ajinkya Nene, Lynbrook CS Club. 04/21/2014. Definition Allows you to figure the worst-case bound for the performance of an algorithm (most useful for.
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
1 Lecture 16: Lists and vectors Binary search, Sorting.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
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.
Analyzing algorithms & Asymptotic Notation BIO/CS 471 – Algorithms for Bioinformatics.
Analysis of Algorithms CS 477/677
Fundamentals CSE 373 Data Structures Lecture 5. 12/26/03Fundamentals - Lecture 52 Mathematical Background Today, we will review: ›Logs and exponents ›Series.
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Aaron Bauer Winter 2014.
1 CSE 326: Data Structures: Hash Tables Lecture 12: Monday, Feb 3, 2003.
CSC 211 Data Structures Lecture 13
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
CSE 326 Linear ADTs: Lists, Stacks and Queues David Kaplan Dept of Computer Science & Engineering Autumn 2001.
1 CSE 326 Data Structures: Complexity Lecture 2: Wednesday, Jan 8, 2003.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Nicki Dell Spring 2014.
CSE373: Data Structures & Algorithms Lecture 10: Implementing Union-Find Dan Grossman Fall 2013.
CSCE 411H Design and Analysis of Algorithms Set 6: Amortized Analysis Prof. Evdokia Nikolova* Spring 2013 CSCE 411H, Spring 2013: Set 6 1 * Slides adapted.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
Week 13 - Friday.  What did we talk about last time?  Sorting  Insertion sort  Merge sort  Started quicksort.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
CSE 326: Data Structures Lecture #3 Asymptotic Analysis Steve Wolfman Winter Quarter 2000.
CSE 326: Data Structures Class #4 Analysis of Algorithms III Analysis of Recursive Algorithms Henry Kautz Winter 2002.
Amortized Analysis and Heaps Intro David Kauchak cs302 Spring 2013.
CSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees Lauren Milne Summer 2015.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
CSE 589 Applied Algorithms Spring 1999 Prim’s Algorithm for MST Load Balance Spanning Tree Hamiltonian Path.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
CSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees Linda Shapiro Winter 2015.
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
Andreas Klappenecker [partially based on the slides of Prof. Welch]
COSC160: Data Structures Linked Lists
Lecture 18: Uniformity Testing Monotonicity Testing
CSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees Catie Baker Spring 2015.
Algorithm design and Analysis
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Linda Shapiro Spring 2016.
Data Structures and Algorithms
CSE 326: Data Structures Lists
CSE 373 Data Structures Lecture 5
CSE373: Data Structures & Algorithms Implementing Union-Find
Presentation transcript:

CSE 326: Data Structures Lecture #4 Alon Halevy Spring Quarter 2001

Agenda Today: –Finish complexity issues. –Linked links (Read Ch 3; skip “radix sort”)

Direct Proof of Recursive Fibonacci Recursive Fibonacci: int Fib(n) if (n == 0 or n == 1) return 1 else return Fib(n - 1) + Fib(n - 2) Lower bound analysis T(0), T(1) >= b T(n) >= T(n - 1) + T(n - 2) + c if n > 1 Analysis let  be (1 +  5)/2 which satisfies  2 =  + 1 show by induction on n that T(n) >= b  n - 1

Direct Proof Continued Basis: T(0)  b > b  -1 and T(1)  b = b  0 Inductive step: Assume T(m)  b  m - 1 for all m < n T(n)  T(n - 1) + T(n - 2) + c  b  n-2 + b  n-3 + c  b  n-3 (  + 1) + c = b  n-3  2 + c  b  n-1

Fibonacci Call Tree

Learning from Analysis To avoid recursive calls –store all basis values in a table –each time you calculate an answer, store it in the table –before performing any calculation for a value n check if a valid answer for n is in the table if so, return it Memoization –a form of dynamic programming How much time does memoized version take?

Kinds of Analysis So far we have considered worst case analysis We may want to know how an algorithm performs “on average” Several distinct senses of “on average” –amortized average time per operation over a sequence of operations –average case average time over a random distribution of inputs –expected case average time for a randomized algorithm over different random seeds for any input

Amortized Analysis Consider any sequence of operations applied to a data structure –your worst enemy could choose the sequence! Some operations may be fast, others slow Goal: show that the average time per operation is still good

Stack ADT Stack operations –push –pop –is_empty Stack property: if x is on the stack before y is pushed, then x will be popped after y is popped What is biggest problem with an array implementation? A BCDEFBCDEF E D C B A F

Stretchy Stack Implementation int data[]; int maxsize; int top; Push(e){ if (top == maxsize){ temp = new int[2*maxsize]; copy data into temp; deallocate data; data = temp; } else { data[++top] = e; } Best case Push = O( ) Worst case Push = O( )

Stretchy Stack Amortized Analysis Consider sequence of n operations push(3); push(19); push(2); … What is the max number of stretches? What is the total time? –let’s say a regular push takes time a, and stretching an array containing k elements takes time kb, for some constants a and b. Amortized = (an+b(2n-1))/n = a+2b-(1/n)= O(1) log n

Average Case Analysis Attempt to capture the notion of “typical” performance Imagine inputs are drawn from some random distribution –Ideally this distribution is a mathematical model of the real world –In practice usually is much more simple – e.g., a uniform random distribution

Example: Find a Red Card Input: a deck of n cards, half red and half black Algorithm: turn over cards (from top of deck) one at a time until a red card is found. How many cards will be turned over? –Best case = –Worst case = –Average case: over all possible inputs (ways of shuffling deck)

Summary Asymptotic Analysis – scaling with size of input Upper bound O, Lower bound  O(1) or O(log n) great O(2 n ) almost never okay Worst case most important – strong guarantee Other kinds of analysis sometimes useful: –amortized –average case

List ADT List properties –A i precedes A i+1 for 1  i < n –A i succeeds A i-1 for 1 < i  n –Size 0 list is defined to be the empty list Key operations –Find(item) = position –Find_Kth(integer) = item –Insert(item, position) –Delete(position) –Next(position) = position What are some possible data structures? ( A 1 A 2 … A n-1 A n ) length = n

Implementations of Linked Lists abc  (a b c) (optional header) L W1ISES Array: Linked list: HAY Can we apply binary search to an array representation?

Linked List vs. Array Find(item) = position Find_Kth(integer)=item Find_Kth(1)=item Insert(item, position) Insert(item) Delete(position) Next(position) = position linked listarraysorted array

Tradeoffs For what kinds of applications is a linked list best? Examples for an unsorted array? Examples for a sorted array?

Implementing in C++ Create separate classes for –Node –List (contains a pointer to the first node) –List Iterator (specifies a position in a list; basically, just a pointer to a node) Pro: syntactically distinguishes uses of node pointers Con: a lot of verbage! Also, is a position in a list really distinct from a list? abc  (a b c) (optional header) L