22C:21 Problem 2 (Set 1) Solution outline.

Slides:



Advertisements
Similar presentations
Process Flow Thinking 1. Overview Process flow is about how the product or service is made. Some measures we will want to study include: Throughput time,
Advertisements

Topic 14 Searching and Simple Sorts "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The.
David Luebke 1 5/4/2015 CS 332: Algorithms Dynamic Programming Greedy Algorithms.
CHAPTER 14 Long-Term Liabilities ……..…………………………………………………………... Covenants on Long-Term Debt  Liquidity and solvency requirements Current cash debt coverage.
CSE332: Data Abstractions Lecture 21: Amortized Analysis Dan Grossman Spring 2010.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Nested for loops Cont’d. 2 Drawing complex figures Use nested for loops to produce the following output. Why draw ASCII art? –Real graphics require a.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
CMPT 225 ADT List using Dynamic arrays A dynamic data structure is one that changes size, as needed, as items are inserted or removed The Java ArrayList.
Long-term financing. Review item  When a firm creates value through a financial transaction, who gets the increase?
CS 307 Fundamentals of Computer Science 1 Lists and ArrayList many slides taken from Mike Scott, UT Austin.
Long-term financing. Review item  When a firm creates value through a financial transaction, who gets the increase?
Long-term financing. Review item  When a firm creates value through a financial transaction, who gets the increase?
CSE332: Data Abstractions Lecture 26: Amortized Analysis Tyler Robison Summer
Recursion Examples Fundamentals of CS Case 1: Code /* Recursion: Case 1 */ #include void count (int index); main () { count (0); getchar(); } void count.
Copyright © 2015 Pearson Education, Inc. publishing as Prentice Hall 14-1.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-3: Loop Figures and Constants reading: self-checks: 27 exercises:
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
CMSC 341 Binomial Queues and Fibonacci Heaps. Basic Heap Operations OpBinary Heap Leftist Heap Binomial Queue Fibonacci Heap insertO(lgN) deleteMinO(lgN)
CSE332: Data Abstractions Lecture 25: Amortized Analysis Sandra B. Fan Winter
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
CSE373: Data Structures & Algorithms Lecture 8: Amortized Analysis Dan Grossman Fall 2013.
David Luebke 1 2/26/2016 CS 332: Algorithms Dynamic Programming.
CSE 326: Data Structures Lecture #6 Putting Our Heaps Together Steve Wolfman Winter Quarter 2000.
Lab 2. Prepare: 1.Create a folder named lab2 inside the workspace. 2.Download RecordDB.java and Record.java from the lab document. 3.Change the file name.
Prof. U V THETE Dept. of Computer Science YMA
Amortized Analysis of Rehashing
May 17th – Comparison Sorts
Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008
Hash Tables 6/13/2018 Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 Cuckoo Hashing.
Chapter 2: Balance Sheet
UNIT III TREES.
Lecture 16 Amortized Analysis
Statement of Cash Flows
6.11 Function Call Stack and Activation Records
Lab 10 - Quicksort.
Topic 14 Searching and Simple Sorts
Wednesday, April 11, 2018 Announcements… For Today…
Describing algorithms in pseudo code
Accounting BBI2O.
Accounting Basics Dr. Shete N.P Dept of Commerce
Building Java Programs
Discrete Mathematics CMP-101 Lecture 12 Sorting, Bubble Sort, Insertion Sort, Greedy Algorithms Abdul Hameed
Tim Ehrlich Growing Arrays in C.
CMSC 341 Splay Trees.
Unit 4 The Accounting Cycle for a Merchandising Corporation
CS200: Algorithms Analysis
Topic 14 Searching and Simple Sorts
Building Java Programs
Cost of benefits to an employer
When a function is called...
Analysis of Algorithms
CS 332: Algorithms Amortized Analysis Continued
Building Java Programs
Amortized Analysis and Heaps Intro
Chapter 18 Recursion.
Financial Statements, Taxes, and Cash Flow
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Accounting Basics Rania A. Azmi rania. a.
Building Java Programs
CMSC 341 Splay Trees.
CSCE 3110 Data Structures & Algorithm Analysis
Lecture 20 Hashing Amortized Analysis
DATA STRUCTURES-COLLISION TECHNIQUES
Discrete Mathematics CS 2610
Collision Resolution: Open Addressing Extendible Hashing
Lecture-Hashing.
Presentation transcript:

22C:21 Problem 2 (Set 1) Solution outline

Original INSERT code public void insert (Record rec) { // Check for overflow if (numRecords == maxRecords) { return; // Doing nothing }

Modified INSERT code public void insert (Record rec) { // Check for overflow. Double when necessary. if (numRecords == maxRecords) { maxRecords *= 2; Record tempList[] = new Record[maxRecords]; System.arraycopy(recordList,0,tempList,0,recordList.length); recordList = tempList; }

Output --------------------------- Capacity:2 0 3 Record inserted at position 0: 3 1 22 Record inserted at position 1: 22 Capacity:4 1 13 2 22 Record inserted at position 1: 13 (and so on)

Amortized Analysis Average running time per operation over a sequence of worst-case operations.

am·or·tize     [am-er-tahyz, uh-mawr-tahyz] –verb (used with object), -tized, -tiz·ing. 1.Finance. a. to liquidate or extinguish (a mortgage, debt, or other obligation), esp. by periodic payments to the creditor or to a sinking fund. b. to write off a cost of (an asset) gradually. 2.Old English Law. to convey to a corporation or church group; alienate in mortmain.

Basic idea Knowledge of which sequence of operations is possible. Data structures that have states that persists between operations. Worst-case operation can alter the state in a way that worst-case doesn’t occur again for a long time. Thus amortizing the cost!

Some intuition … so on Now t = 4, Total cost $2t = $8

Formal analysis Consider DynamicRecordDB with N slots and n records. INSERT operations doubles the size before adding another item if n = N. Any operation that doesn’t involve doubling takes O(1) time unit – say, at most 1 seconds. Resizing takes 2n seconds.

Analysis (contd.) We start from empty list and perform i INSERT operations. So, n = i and N is the smallest power of 2 ≥ i. Total seconds for all the resizing operations = 2 + 4 + 8 + … +N/4 + N/2 + N = 2N – 2. In reference to the code: n = numRecords, N = maxRecords. We start with N = 2. Then N becomes 4 and finally 8.

Analysis (almost done!) Total seconds for i INSERTs = i + 2N – 2 Now, N ≤ 2n = 2i. So the i INSERTs take O(5i – 2) or O(i) time. This is worst case! So, on average, each INSERT takes O(i)/i = O(1) time. This is the amortized running time of insertion.

Bottom line(s) Amortized analysis is a way of proving that even if an operation is occasionally expensive, its cost is made up for by other, cheaper occurrences of the same operation.