Lecture 7. SchemeList, finish up; Universal Hashing introduction

Similar documents
Solution: It may be helpful to list out exactly what is in each of these events:

1 Probability Review. CS 124 Section #8 Hashing, Skip Lists 3/20/17. Expectation (weighted average): the expectation of a random quantity X is:

Discrete Mathematics and Probability Theory Fall 2013 Midterm #2

1KOd17RMoURxjn2 CSE 20 DISCRETE MATH Fall

Skip Lists S 3 S 2 S 1. Skip Lists Goodrich, Tamassia

COT 3100 Spring 2010 Midterm 2

F28PL1 Programming Languages. Lecture 14: Standard ML 4

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

User-defined Functions. Conditional Expressions in Scheme

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

UCSD CSE 21, Spring 2014 [Section B00] Mathematics for Algorithm and System Analysis

Fall Lecture 3 September 4. Stephen Brookes

Midterm Exam 2B Answer key

Lecture Notes for Chapter 2: Getting Started

CSC 1351: Quiz 6: Sort and Search

Randomized Algorithms, Hash Functions

Structure and Interpretation of Computer Programs

function [A,V,a] = spherical cap(r,h) % Returns the area, volume and radius of a spherical cap % cut from a sphere of radius r, and cap height h

Mini-ML. CS 502 Lecture 2 8/28/08

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

CS Lecture 6: Map and Fold. Prof. Clarkson Fall Today s music: Selections from the soundtrack to 2001: A Space Odyssey

CS 321 Programming Languages

Discrete Mathematics Course Review 3

Map and Fold. Prof. Clarkson Fall Today s music: Selections from the soundtrack to 2001: A Space Odyssey

Lecture 15 Binary Search Trees

Graph Contraction. Graph Contraction CSE341T/CSE549T 10/20/2014. Lecture 14

(a) (4 pts) Prove that if a and b are rational, then ab is rational. Since a and b are rational they can be written as the ratio of integers a 1

CS302 Data Structures using C++

Functional Programming. Pure Functional Programming

CS1800 Discrete Structures Final Version B

CS 61B Discussion 5: Inheritance II Fall 2014

4.2 Function definitions the basics

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Sorting. There exist sorting algorithms which have shown to be more efficient in practice.

Denotational Semantics

CIS192 Python Programming

Problem Set 4 Solutions

Lecture 6: Sequential Sorting

Abstract Data Types. CS 234, Fall Types, Data Types Abstraction Abstract Data Types Preconditions, Postconditions ADT Examples

CMSC 330, Fall 2013, Practice Problems 3

CHAPTER 9 HASH TABLES, MAPS, AND SKIP LISTS

TAIL RECURSION, SCOPE, AND PROJECT 4 11

CS Lecture 6: Map and Fold. Prof. Clarkson Spring Today s music: Selections from the soundtrack to 2001: A Space Odyssey

Induction and Semantics in Dafny

Structure and Interpretation of Computer Programs

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

Analyze the obvious algorithm, 5 points Here is the most obvious algorithm for this problem: (LastLargerElement[A[1..n]:

Structure and Interpretation of Computer Programs

Lecture 3.4: Recursive Algorithms

Solutions to Problem Set 1

Statistical Computing

Agenda. We ve done Growth of functions Asymptotic Notations (O, o,,!, ) Now Binary search as an example Recurrence relations, solving them

Fall 2017 December 4, Scheme. Instructions

Lecture 9: Sorting Algorithms

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Fall 2017 December 4, 2017

Fall 2018 Lecture 1

Algorithms and Data Structures (INF1) Lecture 8/15 Hua Lu

Function definitions. CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Example, extended. Some gotchas. Zach Tatlock Winter 2018

CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Zach Tatlock Winter 2018

Overview. Probabilistic Programming. Dijkstra s guarded command language: Syntax. Elementary pgcl ingredients. Lecture #4: Probabilistic GCL

INTERPRETERS 8. 1 Calculator COMPUTER SCIENCE 61A. November 3, 2016

Lecture 16. Reading: Weiss Ch. 5 CSE 100, UCSD: LEC 16. Page 1 of 40

Booleans (aka Truth Values) Programming Languages and Compilers (CS 421) Booleans and Short-Circuit Evaluation. Tuples as Values.

CSE332 Summer 2010: Midterm Exam Sample Solutions

CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Dan Grossman Fall 2011

Chapter 10 Recursion

CS 139 Practice Midterm Questions #2

Structure and Interpretation of Computer Programs

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

;;; Determines if e is a primitive by looking it up in the primitive environment. ;;; Define indentation and output routines for the output for

University of Illinois at Urbana-Champaign Department of Computer Science. Final Examination

Lecture 15 Notes Binary Search Trees

1. Find the type and value of each of the following expressions. Warning: several of these expressions will result in an error.

CS 261: Data Structures. Skip Lists

PostFix. PostFix command semanccs (except exec) PostFix Syntax. A PostFix Interpreter in Racket. CS251 Programming Languages Fall 2017, Lyn Turbak

Denotational Semantics

Intro to Algorithms. Professor Kevin Gold

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Lecture 15: The subspace topology, Closed sets

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides

Working with recursion. From definition to template. Readings: HtDP, sections 11, 12, 13 (Intermezzo 2).

Copyright 2000, Kevin Wayne 1

Section 05: Solutions

List Ranking. Chapter 4

HO #13 Fall 2015 Gary Chan. Hashing (N:12)

CS 342 Lecture 8 Data Abstraction By: Hridesh Rajan

List Ranking. Chapter 4

Recursion I and II. Discrete Structures (CS 173) Madhusudan Parthasarathy, University of Illinois 1

Lecture Notes on Binary Search Trees

CS 106B Lecture 26: Esoteric Data Structures: Skip Lists and Bloom Filters

Lecture 16: Binary Search Trees

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

Lecture - 8A: Subbasis of Topology

Lists. Michael P. Fourman. February 2, 2010

Lecture 3: Recursion; Structural Induction

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

Priority Queue ADT. Revised based on textbook author s notes.

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

Transcription:

Lecture 7. SchemeList, finish up; Universal Hashing introduction CS 16 February 24, 2010 1 / 15

foldleft #!/usr/bin/python def foldleft(func, slist, init): foldleft: (<a> * <b> -> <a>) * (<b> SchemeList) * <a> -> <a> Consumes: a function of two parameters, of types <a> and <b>, returning type <a> a SchemeList whose entries are all of type <b>, and a value of type <a> Produces: a value Purpose: apply func over slist left to right, at first with init as the left arg, and subsequently with the previous result as the left arg. if slist.isempty(): return init else: val = func(init, slist.first()) return foldleft(func, slist.rest(), val) 2 / 15

foldleft, applied #!/usr/bin/python def length(slist) : foldl(lambda x, y: x + 1, slist, 0) def contains(slist, item) : foldl(lambda x, y: x (y == item), slist, False) def list_max(slist) : <fill in here to test your understanding> def odd_count(slist) : <fill in here to test your understanding> 3 / 15

Analysis of foldleft If the list has n items, and the function being folded takes time T... then the total time is O(nT ), Reason: let F (n) be the time taken by foldleft on a list of size n Then F (0) = C and F (n) K + T + F (n 1) for some constants C and K. Use induction to conclude F (n) MnT for some constant M. n.b.: it s possible that T may depend on n! 4 / 15

reverse def reverse(slist): reverse: SchemeList -> SchemeList Consumes: a SchemeList Produces: a SchemeList Purpose: to reverse slist if slist.isempty(): return slist else: r = reverse(slist.rest()) return append(r, slist.first()) def append(slist, item): append: SchemeList * any -> SchemeList Consumes: a SchemeList, a value Produces: a SchemeList Purpose: add item to the end of slist if slist.isempty(): return SchemeList.makeCons(item, slist) else: a = append(slist.rest(), item) return SchemeList.makeCons(slist.first(), a) 5 / 15

reverse Let A(n) be running time of append on list of length n By usual analysis, A(n) cn, i.e., it s O(n n) Let R(n) be running time of reverse on list of length n Then R(0) = C and R(n) K + A(n 1) + R(n 1) for n > 1 Plug-n-chug to see that R(n) nk + c((n 1) + (n 2) +... + 2 + 1) for n > 1. Then use induction to prove that (omitted) Conclude that R(n) is O(n n 2 ) 6 / 15

fast reverse #!/usr/bin/python def fast_reverse(slist): fast_reverse: SchemeList -> SchemeList Consumes: a SchemeList Produces: a SchemeList Purpose: to reverse slist return fr_helper(slist, SchemeList.empty()) def fr_helper(slist, partial): Consumes: two SchemeLists Produces: a SchemeList Purpose: helper function to fast_reverse if slist.isempty(): return partial else: return fr_helper(slist.rest(), SchemeList.makeCons(slist.first(), partia 7 / 15

Elegant,. high-level fast reverse #!/usr/bin/python def fast_reverse(slist): fast_reverse: SchemeList -> SchemeList Consumes: a SchemeList Produces: a SchemeList Purpose: to reverse slist return foldleft(schemelist.makecons, slist, SchemeList.empty()) Notice that all recursion is hidden in the folding! 8 / 15

Hashing Goal: to implement the Dictionary ADT so that insert, find, delete all are O(n 1) Application: maintain a table of about 250 IP addresses (each of which consists of four bytes, as in 128.148.37.3; the individual numbers are between 0 and 255). Could implement Dictionary as a SchemeList of (key, value) pairs. insert is O(n 1) find and delete are O(n n) this is OK for very small dictionaries 9 / 15

Hashing IP addresses Idea 1: make an array where the index is a 32-bit integer corresponding to the four bytes of IP address. VERY large, very empty. Not good. Idea 2: make a table of 256 entries, and use the last byte of the IP address as index Problem: collisions. Solution: Each table entry is a SchemeList! Problem: if all IP addresses fall in one bucket, then find is O(n n) If we use any small table, this is bound to happen in some bad case We ll show a way to guarantee that such bad performance is extremely unlikely. Read pages 33-36 of Dasgupta et al. 10 / 15

Introduction to Probability Goal: to make a mathematical model of everyday reasoning about how likely things are Clever idea: replace the idea of a single coin-flip with a set containing two possible outcomes, and the probability of each. Reason about this set! 11 / 15

Probability Space A probability space is a finite set S, together with... a function p : S R, satisfying: P 0 p(s) 1 for every s S s S p(s) = 1 Example: S 1 = {H, T } and p(h) = 0.5 and P(T ) = 0.5. This is a model of a fair coin toss. 12 / 15

Probability Space, II Example2: S 2 = {1, 2, 3, 4, 5, 6} and p(i) = 1/6 for i = 1, 2,..., 6. This is a model of a fair roll of a die. 13 / 15

Event An event is a subset E of a probability space S. For the coin-toss example, possible events are {}, {H},{T } and {H, T }. These correspond to The coin doesn t land on either face The coin comes up heads The coin comes up tails The coin comes up either heads or tails The probability of an event E is Pr{E} = P s E p(s). In the examples, Pr{} = 0, Pr{H} = 0.5, Pr{T } = 0.5, and Pr{H, T } = 1.0. Correspond to the notions that the coin never lands on its edge, heads and tails are equally likely, and the coin always comes up heads or tails. 14 / 15

Random Variable An random variable (or RV ) on a probability space S is a function X : S R. For the coin-toss example, if I agree to pay Ben $100 if the coin comes up heads, but $0 if it comes up tails... we can model Ben s winnings with a random variable: X (H) = 100; X (T ) = 0 Notice that X is just a function! It s not a variable, and it s not random! Next class: expected value of a random variable. 15 / 15