15. Functions and Lists. Example 2: Sorting a List of Numbers. Example 1: Computing the Diameter of a Cloud of Points



Similar documents
Analysis of Binary Search algorithm and Selection Sort algorithm

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Assignment 2: Option Pricing and the Black-Scholes formula The University of British Columbia Science One CS Instructor: Michael Gelbart

Query Processing C H A P T E R12. Practice Exercises

3. INNER PRODUCT SPACES

Algorithms. Margaret M. Fleck. 18 October 2010

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

Binary Multiplication

Some programming experience in a high-level structured programming language is recommended.

HW4: Merge Sort. 1 Assignment Goal. 2 Description of the Merging Problem. Course: ENEE759K/CMSC751 Title:

In-Memory Database: Query Optimisation. S S Kausik ( ) Aamod Kore ( ) Mehul Goyal ( ) Nisheeth Lahoti ( )

Binary Heap Algorithms

Moving from CS 61A Scheme to CS 61B Java

Probability and Statistics Prof. Dr. Somesh Kumar Department of Mathematics Indian Institute of Technology, Kharagpur

5.4 Closest Pair of Points

Section IV.1: Recursive Algorithms and Recursion Trees

Data Structures. Algorithm Performance and Big O Analysis

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Appendix... B. The Object Constraint

= δx x + δy y. df ds = dx. ds y + xdy ds. Now multiply by ds to get the form of the equation in terms of differentials: df = y dx + x dy.

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

User Guide Thank you for purchasing the DX90

Closest Pair of Points. Kleinberg and Tardos Section 5.4

The Clean programming language. Group 25, Jingui Li, Daren Tuzi

CSC 180 H1F Algorithm Runtime Analysis Lecture Notes Fall 2015

DATA STRUCTURES USING C

The C Programming Language course syllabus associate level

Financial Econometrics MFE MATLAB Introduction. Kevin Sheppard University of Oxford

Introduction to Python

Databases 2011 The Relational Model and SQL

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Python Programming: An Introduction To Computer Science

Why? A central concept in Computer Science. Algorithms are ubiquitous.

arrays C Programming Language - Arrays

Python Lists and Loops

Chapter Load Balancing. Approximation Algorithms. Load Balancing. Load Balancing on 2 Machines. Load Balancing: Greedy Scheduling

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Scan-Line Fill. Scan-Line Algorithm. Sort by scan line Fill each span vertex order generated by vertex list

Medical Information Management & Mining. You Chen Jan,15, 2013 You.chen@vanderbilt.edu

[Refer Slide Time: 05:10]

Python for Rookies. Example Examination Paper

PSTricks. pst-ode. A PSTricks package for solving initial value problems for sets of Ordinary Differential Equations (ODE), v0.7.

Positional Numbering System

HY345 Operating Systems

Computing Concepts with Java Essentials

Arrays. number: Motivation. Prof. Stewart Weiss. Software Design Lecture Notes Arrays

Excel Functions (fx) Click the Paste Function button. In the Function Category select All. Scroll down the Function Name list And select SUM.

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

Chapter 13: Query Processing. Basic Steps in Query Processing

Analysis of Algorithms I: Binary Search Trees

HES-SO Master of Science in Engineering. Clustering. Prof. Laura Elena Raileanu HES-SO Yverdon-les-Bains (HEIG-VD)

ML for the Working Programmer

MatLab Basics. Now, press return to see what Matlab has stored as your variable x. You should see:

PGR Computing Programming Skills

! Solve problem to optimality. ! Solve problem in poly-time. ! Solve arbitrary instances of the problem. #-approximation algorithm.

Chapter 2 Data Storage

Stanford Math Circle: Sunday, May 9, 2010 Square-Triangular Numbers, Pell s Equation, and Continued Fractions

Basic Programming and PC Skills: Basic Programming and PC Skills:

(Refer Slide Time: 01.26)

Catalan Numbers. Thomas A. Dowling, Department of Mathematics, Ohio State Uni- versity.

Introduction to Python

Pseudo code Tutorial and Exercises Teacher s Version

( ) FACTORING. x In this polynomial the only variable in common to all is x.

Chapter 3 RANDOM VARIATE GENERATION

Part VI. Scientific Computing in Python

Texas Essential Knowledge and Skills Correlation to Video Game Design Foundations 2011 N Video Game Design

Class : MAC 286. Data Structure. Research Paper on Sorting Algorithms

Zabin Visram Room CS115 CS126 Searching. Binary Search

A skip list container class in Python

Chapter 15 Functional Programming Languages

Management Information Systems 260 Web Programming Fall 2006 (CRN: 42459)

Algorithm Design and Recursion

! Solve problem to optimality. ! Solve problem in poly-time. ! Solve arbitrary instances of the problem. !-approximation algorithm.

Module 4: Data Exploration

Microsoft Excel Tips & Tricks

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

Simulation Tools. Python for MATLAB Users I. Claus Führer. Automn Claus Führer Simulation Tools Automn / 65

Computer Programming I

Discrete Math in Computer Science Homework 7 Solutions (Max Points: 80)

The Elective Part of the NSS ICT Curriculum D. Software Development

Introduction to Data Structures

Gage Studies for Continuous Data

Using Formulas, Functions, and Data Analysis Tools Excel 2010 Tutorial

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Module1. x y 800.

Sample Questions Csci 1112 A. Bellaachia

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

ECE 341 Coding Standard

Iteration CHAPTER 6. Topic Summary

Computers. An Introduction to Programming with Python. Programming Languages. Programs and Programming. CCHSG Visit June Dr.-Ing.

Performing Queries Using PROC SQL (1)

Converting a Number from Decimal to Binary

Square Roots and Other Radicals

B490 Mining the Big Data. 2 Clustering

ESPResSo Summer School 2012

Transcription:

15. Functions and Lists Example 1: Computing the Diameter of a Cloud of Points Topics: Subscripting Map Searching a list Example 1: Clouds of points Example 2: Selection Sort 500 Points. Which two are furthest apart and what is their separation? Same Problem: What s the Biggest Number in This Table? Example 2: Sorting a List of Numbers 10 20 40 50 60 80 Which two cities are furthest apart and what is their separation? Sorting Algorithms There are many sorting algorithms: Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort A great venue for practicing list-based computing and for studying such things as efficiency and recursion (which we will do later). Example 1: Computing the Diameter of a Cloud of Points We will develop a module PointCloud 1

It Will Have Three Functions MakeCloud(n,sigma) This generates two lists x and y that define the coordinates of the points in the cloud. Diameter(x,y) This will compute the diameter of the cloud using the (x,y) coordinates of its points. ShowCloud(x,y) This will use simplegraphicse to display the cloud and highlight the diameter points. The Function MakeCloud from random import normalvariate as randn def MakeCloud(n,sigma): x=[] y=[] for k in range(n): r = randn(0,sigma) x.append(r) r = randn(0,sigma) y.append(r) return (x,y) New Feature The normal distribution Generating floats from the Normal Distribution Generating floats from the Normal Distribution If mu and sigma (positive) are floats, then x = random.normalvariate(mu,sigma) assigns to x a random float sampled from the normal distribution with mean mu and standard deviation sigma Generating floats from the Normal Distribution MakeCloud Returns Two Lists from random import normalvariate as randn This is a histogram of the numbers this generates: for k in range(10**6): r = randn(0,1) Mean = 0 Standard deviation = 1 def MakeCloud(n,sigma): x=[] y=[] for k in range(n): r = randn(0,sigma) x.append(r) r = randn(0,sigma) y.append(r) return (x,y) New Feature A function that returns more than one thing. Note the parentheses 2

MakeCloud Returns Two Lists MakeCloud >>> (x,y) = MakeCloud(3,1) >>> print x >>> print y [-2.328, -0.044, -0.241] [ 2.737, 2.078, -1.272] Note the parentheses from random import normalvariate as randn def MakeCloud(n,sigma): x=[] y=[] for k in range(n): r = randn(0,sigma) x.append(r) r = randn(0,sigma) y.append(r) return x,y Old Stuff x and y start out as empty lists. Repeatedly generate a random number and append to x Ditto for y The Diameter Function: What It Computes The diameter points and the distance between them Input: lists x and y that define the yellow dots Diameter: Formal Specs def Diameter(x,y): """ Returns (d,imax,jmax) where d is a float that is the diameter of a cloud of points defined by lists x and y. imax and jmax are ints that are the indices of the diameter points. The diameter of a cloud of points is the maximum distance between any two points in the cloud. The two points for which this occurs are called diameter points. PreC: x and y are lists of floats with the same length. Diameter: The Implementation def Diameter(x,y): d = 0 n = len(x) for i in range(n): for j in range(n): dx = x[i]-x[j] dy = y[i]-y[j] dij = sqrt(dx**2+dy**2) if dij>d: d = dij New Feature imax = i jmax = j Nested Loops return (d,imax,jmax) Nested Loops In this situation we have a loop whose body contains a loop and for blahblahblah contains a loop. 3

print print Execute the loop body with i=0 print print 0 0 0 1 0 2 0 0 0 1 0 2 Execute the loop body with i=0 Execute the loop body with i=1 print print Execute the loop body with i=1 0 0 0 1 0 2 1 0 1 1 1 2 Go to the next statement after the loop body. 0 0 0 1 0 2 1 0 1 1 1 2 4

Back to Diameter print Go to the next statement after the loop body. 0 0 0 1 0 2 1 0 1 1 1 2 Outer When developing nested-loop solutions, it is essential to apply the methodology of step-wise refinement, perhaps preceded by a small example Aspects of our problem - Must check all possible pairs of points. - Look at their separation distance - What s the largest among these distances? Suppose There Are 3 points From To Dist ---------------------------------- (x[0],[y[0]) (x[0],y[0]) 0 (x[0],[y[0]) (x[1],y[1]) 7 (x[0],[y[0]) (x[2],y[2]) 9 (x[1],[y[1]) (x[0],y[0]) 7 (x[1],[y[1]) (x[1],y[1]) 0 (x[1],[y[1]) (x[2],y[2]) 10 (x[2],[y[2]) (x[0],y[0]) 9 (x[2],[y[2]) (x[1],y[1]) 10 (x[2],[y[2]) (x[2],y[2]) 0 Suppose There Are 3 points From To Dist ---------------------------------- (x[0],[y[0]) (x[0],y[0]) 0 (x[0],[y[0]) (x[1],y[1]) 7 (x[0],[y[0]) (x[2],y[2]) 9 (x[1],[y[1]) (x[0],y[0]) 7 (x[1],[y[1]) (x[1],y[1]) 0 (x[1],[y[1]) (x[2],y[2]) 10 (x[2],[y[2]) (x[0],y[0]) 9 (x[2],[y[2]) (x[1],y[1]) 10 (x[2],[y[2]) (x[2],y[2]) 0 Number of possibilities.: 9 = 3x3 Number of possibilities.: 9 = 3x3 First Solution And now, stepwise refinement in action. d = 0 n = len(x) for i in range(n): # Examine the distance from # (x[i],y[i]) to every other point 5

Second Solution d = 0 n = len(x) for i in range(n): for j in range(n): # Examine the distance from # (x[i],y[i]) to (x[j],y[j]) Third Solution d = 0 n = len(x) for i in range(n): for j in range(n): dx = x[i]-x[j] dy = y[i]-y[j] dij = sqrt(dx**2+dy**2) # Compare dij to d revising # the latter if necessary Fourth Solution d = 0 n = len(x) for i in range(n): for j in range(n): dx = x[i]-x[j] dy = y[i]-y[j] dij = sqrt(dx**2+dy**2) if dij>d: d = dij imax = i jmax = j return (d,imax,jmax) Fourth Solution d = 0 n = len(x) for i in range(n): for j in range(n): dx = x[i]-x[j] dy = y[i]-y[j] dij = sqrt(dx**2+dy**2) if dij>d: d = dij imax = i jmax = j return (d,imax,jmax) We have to remember where the max separation occurs. Next Up: ShowCloud ShowCloud: Specs def ShowCloud(x,y): """ Displays a point cloud defined by x and y and highlights the two points that define its diameter. PreC: x and y are lists of floats with the same length. """ 6

First: How Big a Window? xmax = max(map(abs,x)) ymax = max(map(abs,y)) M = max(xmax,ymax) MakeWindow(1.1*M,bgcolor=BLACK) New Feature: map Idea: look at the x and y coordinates of the points and see how big they can be. Map: Apply a Function to Each Element in a List Example. Apply the absolute value function to every list element >>> x = [10,-20,-40] >>> x = map(abs,x) >>> print x [10,20,40] Map: Apply a Function to Each Element in a List Map: Apply a Function to Each Element in a List Example. Apply the floor function to every list element: >>> x = [11.3, 12.4, 15.0] >>> x = map(math.floor,x) >>> print x [11.0,12.0,15.0] This: y = [] for k in range(len(x)): y.append(math.sqrt(x([k])) Is equivalent to this: y = map(math.sqrt,x) Assuming that x is an initialized list of nonnegative numbers Map: Formal Syntax map(, ) The name of a function that returns a value. Every element in the list must satisfy its precondition. Now, Back to ShowCloud The name of a list. 7

First: How Big a Window? xmax = max(map(abs,x)) ymax = max(map(abs,y)) M = max(xmax,ymax) MakeWindow(1.1*M,bgcolor=BLACK) x = [-19,12,-4] max(map(abs,x)) >>> 19 Next, Use DrawDisk For Each Point r = M/50; (d,i,j) = Diameter(x,y) for k in range(len(x)): if k==i or k==j: DrawDisk(x[k],y[k],2*r,color=CYAN) DrawDisk(x[k],y[k],r,color=YELLOW) i and j are the indices of the diameter points. Before they are displayed, we paint a larger cyan dot. Example 2: Sorting a List of Numbers Now, on to another example that highlights functions + lists 10 20 40 50 60 80 We Will Implement the Method of Selection Sort At the Start: High-Level: Swap x[0] with the smallest value in x[0:] for k in range(len(x)-1) Swap x[k] with the smallest value in x[k:] 8

10 40 50 80 20 60 Swap x[0] with the smallest value in x[0:] Swap x[1] with the smallest value in x[1:] 10 40 50 80 20 60 10 40 50 80 20 60 10 20 50 80 40 60 Swap x[1] with the smallest value in x[1:] Swap x[2] with the smallest value in x[2:] 10 20 50 80 40 60 10 20 50 80 40 60 10 20 40 80 50 60 Swap x[2] with the smallest value in x[2:] Swap x[3] with the smallest value in x[3:] 10 20 40 80 50 60 9

10 20 40 80 50 60 10 20 40 50 80 60 Swap x[3] with the smallest value in x[3:] Swap x[4] with the smallest value in x[4:] 10 20 40 50 80 60 Selection Sort: Recap 10 40 50 80 20 60 10 20 40 50 80 60 10 20 50 80 40 60 Swap x[4] with the smallest value in x[4:] 10 20 40 50 60 80 10 20 40 80 50 60 10 20 40 50 80 60 10 20 40 50 60 80 10 20 40 50 60 80 The Essential Helper Function: Select(x,i) def Select(x,i): """ Swaps the smallest value in x[i:] with x[i] PreC: x is a list of integers and i is an in that satisfies 0<=i<len(x)""" How Does it Work? The calling program has a list. E.g., a --> 0 ---> 50 1 ---> 40 2 ---> 10 3 ---> 80 4 ---> 20 5 ---> 60 Does not return anything and it has a list argument 10

How Does it Work? The calling program executes Select(a,0) and control passes to Select How Does Select Work? - Nothing new about the assignment of 0 to i. - But there is no assignment of the list a to x. - Instead x now refers to the same list as a. a --> 0 ---> 50 1 ---> 40 2 ---> 10 3 ---> 80 4 ---> 20 5 ---> 60 a --> x ---------------------> i ---> 0 0 ---> 50 1 ---> 40 2 ---> 10 3 ---> 80 4 ---> 20 5 ---> 60 How Does Select Work? If inside Select we have t = x[0]; x[0] = x[2]; x[2] = t it is as if we said t = a[0]; a[0] = a[2]; a[2] = t How Does Select Work? It changes the list a in the calling program. We say x and a are aliased. They refer to the same list a --> x ---------------------> i ---> 0 0 ---> 50 1 ---> 40 2 ---> 10 3 ---> 80 4 ---> 20 5 ---> 60 a --> x ---------------------> i ---> 0 0 ---> 10 1 ---> 40 2 ---> 50 3 ---> 80 4 ---> 20 5 ---> 60 Let s Assume This Is Implemented def Select(x,i): """ Swaps the smallest value in x[i:] with x[i] PreC: x is a list of integers and i is an in that satisfies 0<=i<len(x)""" After this: Initialization Select(a,0) Select(a,1) Select(a,2) Select(a,3) Select(a,4) Select(a,5) The list a looks like this 10 40 50 80 20 60 10 20 50 80 40 60 10 20 40 80 50 60 10 20 40 50 80 60 10 20 40 50 60 80 10 20 40 50 60 80 11

In General We Have This def SelectionSort(a): n = len(a) for k in range(n): Select(a,k) 12