Data Abstraction: Sets

Slides:



Advertisements
Similar presentations
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
Advertisements

Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Discrete Structures & Algorithms More about sets EECE 320 — UBC.
6.001 SICP SICP – October Introduction Trevor Darrell 32-D512 Office Hour: W web page:
SICP Interpretation part 1 Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment.
SICP Data Mutation Primitive and Compound Data Mutators Stack Example non-mutating mutating Queue Example non-mutating mutating.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Quiz: Box and Pointer fun! (cons (cons (cons ‘hey (cons ‘there nil)) nil) (cons ‘wow nil)) (list ‘boo (append (list ‘hoo ‘hoo) (cons ‘see ‘me)))
Elementary Data Structures and Algorithms
PPL Pairs, lists and data abstraction. Data Abstraction? An interface: separate implementation from usage Think of the Map interface in Java: we know.
Arbitrarily Long Data Structures: Lists and Recursion CMSC Introduction to Computer Programming October 4, 2002.
Data Structures: Binary Trees CMSC Introduction to Computer Programming October 28, 2002.
1 Lecture 16: Lists and vectors Binary search, Sorting.
Recursion: Linear and Tree Recursive Processes and Iteration CMSC Introduction to Computer Programming October 7, 2002.
Assignment: Changing Data CMSC Introduction to Computer Programming November 1, 2002.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
Mutual Recursion: Web pages CMSC Introduction to Computer Programming November 25, 2002.
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
Spring 2004Programming Development Techniques 1 Topic 11 Sets and their Representation April 2004.
Data Abstraction: Sets Binary Search Trees CMSC Introduction to Computer Programming October 30, 2002.
TM Design Macro Language D and SD MA/CSSE 474 Theory of Computation.
Binary Search Trees (BST)
Scope: What’s in a Name? CMSC Introduction to Computer Programming October 16, 2002.
PPL CPS. Moed A 2007 Solution (define scale-tree (λ (tree factor) (map (λ (sub-tree) (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree.
1 Vectors, binary search, and sorting. 2 We know about lists O(n) time to get the n-th item. Consecutive cons cell are not necessarily consecutive in.
Meta-Circular Evaluation CMSC Introduction to Computer Programming November 20, 2002.
CISC1100: Sets Fall 2014 Xiaolan Zhang
Representing Sets (2.3.3) Huffman Encoding Trees (2.3.4)
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
BST Trees
6.001 SICP Compilation Context: special purpose vs. universal machines
Introduction to Scheme
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1
Chapter 15 – Functional Programming Languages
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.1
From Templates to Folds
Lecture 22 Binary Search Trees Chapter 10 of textbook
Env. Model Implementation
COMP 103 Binary Search Trees.
Teach A level Computing: Algorithms and Data Structures
Binary Search Trees.
Case Study: Undefined Variables
Monday, April 16, 2018 Announcements… For Today…
CMSC 341 Lecture 10 B-Trees Based on slides from Dr. Katherine Gibson.
6.001 SICP Data abstractions
Lecture 16: Quickest Sorting CS150: Computer Science
Algebra 1 Section 1.1.
The Metacircular Evaluator
The Metacircular Evaluator
The Metacircular Evaluator (Continued)
Lecture #9 מבוא מורחב.
Binary Search A binary search algorithm finds the position of a specified value within a sorted array. Binary search is a technique for searching an ordered.
Binary Search Trees Chapter 9 2/22/2019 B.Ramamurthy.
Data Mutation Primitive and compound data mutators set! for names
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.5
topics mutable data structures
Mutators for compound data Stack Queue
6.001 SICP Data Mutation Primitive and Compound Data Mutators
Lecture 14: The environment model (cont
6.001 SICP Data abstractions
Lecture #7 מבוא מורחב.
Today’s topics Abstractions Procedural Data
6.001 SICP Interpretation Parts of an interpreter
Developing Programs for Family Trees
Changing Data: (Continued)
More Scheme CS 331.
Bringing it all Together: Family Trees
Presentation transcript:

Data Abstraction: Sets CMSC 11500 Introduction to Computer Programming October 21, 2002

Administration Midterm Wednesday – in-class Based on lecture/notes/hwk Hand evaluations Structures Self-referential structures: lists, structures of structs Data definitions, Templates, Functions Recursion in Fns follow recursion in data def Recursive/Iterative processes Based on lecture/notes/hwk Not book details Extra office hrs: Tues 3-5, RY 178

Roadmap Recap: Structures of structures Data abstraction Summary Black boxes redux Objects as functions on them Example: Sets Operations: Member-of?, Adjoin, Intersection Implementations and Efficiency Unordered Lists Ordered Lists Binary Search Trees Summary

Recap: Structures of Structures Family trees: (Multiply) self-referential structures Data Definition -> Template -> Function (define-struct ft (name eye-color mother father)) Where name, eye-color: symbol; mother, father: family tree A family-tree: 1) ‘unknown, 2) (make-ft name eye-color mother father)

Template -> Function (define (bea aft) (cond ((eq? ‘unknown aft) #f) ((ft? aft) (let ((bea-m (bea (ft-mother aft))) (bea-f (bea (ft-father aft)))) (cond ((eq ?(ft-eye-color aft) ‘blue) (ft-name aft)) ((symbol? bea-m) bea-m) ((symbol? bea-f) bea-f) (else #f)))))) (define (fn-for-ft aft) (cond ((eq? ‘unknown aft)..) ((ft? aft) (cond …(ft-eye-color aft)… …(ft-name aft)…. …(fn-for-ft (ft-mother aft)).. …(fn-for-ft (ft-father aft))…

Data Abstraction Analogous to procedural abstraction Procedure is black box Don’t care about implementation as long as behaves as expected: contract, purpose Data object as black box Don’t care about implementation as long as behaves as expected

Abstracting Data Compound data objects Points, families, rational numbers, sets Data has many facets Also many possible representations Key: Data object defined by operations E.g. points: distance, slope, etc Any implementation acceptable if performs operations specified

Data Abstraction: Sets Set: Collection of distinct objects Venn Diagrams Defining functions Element-of?: true if element is member of set Adjoin: Adds element to set Union: Set containing elements of input sets Intersection: Set of elements in both input sets Any implementation of these functions defines a set data object

Sets: Representations & Efficiency Many possible representations: Unordered lists Ordered lists Binary Search Trees How choose among representations? Efficiency: Order of growth Tradeoffs in operations

Sets as Unordered Lists A Set-of-numbers is: 1) ‘() 2) (cons n set-of-numbers) Where n is number Set: Each element appears once Base template: (define (fn-for-set set) (cond ((null? set) …) (else (…(car set) ….(fn-for-set (cdr set)))))

Member-of? (define (member-of x set) ; member-of: number set -> boolean ; true if x in set, false otherwise (cond ((null? set) #f) ((eq? (car set) x) #t) (else (member-of x (cdr set))))) Order of growth: Length of list: O(n)

Member-of? Hand-Evaluation (define (member-of x set) ; member-of: number set -> boolean ; true if x in set, false otherwise (cond ((null? set) #f) ((eq? (car set) x) #t) (else (member-of x (cdr set))))) (member-of 3 ‘(1 2 3 4)) (cond ((null? ‘(1 2 3 4)) #f) ((eq? (car ‘(1 2 3 4)) 3) #t) (else (member-of 3 (cdr ‘(1 2 3 4))))) (cond (#f #f) ((eq? (car ‘(1 2 3 4)) 3) #t) (else (member-of 3 (cdr ‘(1 2 3 4)))))

Hand-Evaluation Cont’d (cond (#f #f) ((eq? 1 3) #t) (else (member-of 3 (cdr ‘(1 2 3 4))))) (cond (#f #f) (#f #t) (else (member-of 3 (cdr ‘(1 2 3 4))))) (cond (#f #f) (#f #t) (else (member-of 3 ‘(2 3 4)))) (cond ((null? ‘(2 3 4)) #f) ((eq? (car ‘(2 3 4)) 3) #t) (else (member-of 3 (cdr ‘(2 3 4)))))

Hand-Evaluation Cont’d (cond ((#f #f) ((eq? (car ‘(3 4) 3) #t) (else (member-of 3 ‘(3 4)))) (cond (#f #f) ((eq? (car ‘(2 3 4) 3) #t) (else (member-of 3 (cdr ‘(2 3 4))))) (cond ((#f #f) ((eq? (car ‘(3 4) 3) #t) (else (member-of 3 ‘(3 4)))) (cond (#f #f) ((eq? 2 3) #t) (else (member-of 3 (cdr ‘(2 3 4))))) (cond (#f #f) (#f #t) (else (member-of 3 (cdr ‘(2 3 4)))) (cond ((#f #f) ((eq? 3 3) #t) (else (member-of 3 ‘(3 4)))) (cond ((#f #f) (#t #t) (else (member-of 3 ‘(3 4)))) (cond ((#f #f) (#f #t) (else (member-of 3 ‘(3 4)))) (cond ((null? ‘(3 4) #f) ((eq? (car ‘(3 4) 3) #t) (else (member-of 3 (cdr ‘(3 4))))) #t

Hand-Evaluation: Recursion Only (member-of 3 ‘(2 3 4)) (member-of 3 ‘(3 4)) ((eq? 3 3) #t) #t

Adjoin (define (adjoin x set) (cond ((null? set) (cons x set)) Question: Single occurrence of element Maintain at adjoin? Always insert? Add condition to maintain invariant (one occurrence) Order of Growth: Member-of? test: O(n) (define (adjoin x set) (cond ((null? set) (cons x set)) ((eq? (car set) x) set) (else (cons (car set) (adjoin x (cdr set))))) (define (adjoin x set) (if (member-of? x set) set (cons x set)))

Intersection Order of growth: (define (intersection set1 set2) ;intersection: set set -> set (cond ((null? set1) ‘()) ((null? set2) ‘()) ((member-of? (car set1) set2) (cons (car set1) (intersection (cdr set1) set2)) (else (intersection (cdr set1) set2))) Order of growth: Test every member of set1 in set2 Member: O(n); Length of set1: O(n)  O(n^2)

Alternative: Ordered Lists Set-of-numbers: 1) ‘() 2) (cons n set-of-numbers) Where n is a number, and n <= all numbers in son Maintain constraint: Anywhere add element to set

Adjoin (define (adjoin x set) (cond ((null? set) (cons x ‘()) ((eq? (car set) x) set) ((< x (car set)) (cons x set)) (else (cons (car set) (adjoin x (cdr set)))))) Note: New invariant adds condition Order of Growth: On average, check half

Sets as Binary Search Trees Bst: 1) ‘() 2) (make-bstn val left right) Where val is number; left, right are bst All vals in left branch less than current, right gtr (define-struct bstn (val left right)) 7 5 11 3 6 9 13

Adjoin: BST (define (adjoin x set) (cond ((null? set) (make-bstn x ‘() ‘()) ((= x (bstn-val set)) set) ((< x (bstn-val set)) (make-bstn (bstn-val set) (adjoin x (bstn-left set)) (bstn-right set))) ((> x (bstn-val set)) (bstn-left set) (adjoin x (bstn-right set))))

BST Sets Analysis: If balanced tree Each branch reduces tree size by half Successive halving -> O(log n) growth

Summary Data objects Defined by operations done on them Abstraction: Many possible implementations of operations All adhere to same contract, purpose Different implications for efficiency

Next Time Midterm Hand evaluations Structures Self-referential structures: lists, structures of structs Data definitions, Templates, Functions Recursion in Fns follow recursion in data def