Invasion Percolation: Tuning Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Slides:



Advertisements
Similar presentations
Recursion Chapter 14. Overview Base case and general case of recursion. A recursion is a method that calls itself. That simplifies the problem. The simpler.
Advertisements

Introduction to Algorithms Quicksort
Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. Al-Azhar University
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Announcements You survived midterm 2! No Class / No Office hours Friday.
MATH 224 – Discrete Mathematics
Garfield AP Computer Science
Fundamentals of Python: From First Programs Through Data Structures
QuickSort 4 February QuickSort(S) Fast divide and conquer algorithm first discovered by C. A. R. Hoare in If the number of elements in.
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Sorting Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
CSC1016 Coursework Clarification Derek Mortimer March 2010.
Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
FALL 2006CENG 351 Data Management and File Structures1 External Sorting.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Searching/Sorting Introduction to Computing Science and Programming I.
By: Jamie McPeek. 1. Background Information 1. Metasearch 2. Sets 3. Surface Web/Deep Web 4. The Problem 5. Application Goals.
Simple Sorting Algorithms. 2 Bubble sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them.
Week 2 CS 361: Advanced Data Structures and Algorithms
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Chapter 19 Searching, Sorting and Big O
Recursion, Complexity, and Sorting By Andrew Zeng.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Hashing Chapter 20. Hash Table A hash table is a data structure that allows fast find, insert, and delete operations (most of the time). The simplest.
September 29, Algorithms and Data Structures Lecture V Simonas Šaltenis Aalborg University
Invasion Percolation: Assembly Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
1 CSE 326: Data Structures: Hash Tables Lecture 12: Monday, Feb 3, 2003.
CS Data Structures I Chapter 2 Principles of Programming & Software Engineering.
3 – SIMPLE SORTING ALGORITHMS
Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
Hashing 1 Hashing. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Invasion Percolation: Randomness Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Searching Topics Sequential Search Binary Search.
Sorting Algorithms Written by J.J. Shepherd. Sorting Review For each one of these sorting problems we are assuming ascending order so smallest to largest.
Amortized Analysis and Heaps Intro David Kauchak cs302 Spring 2013.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
Searching/Sorting. Searching Searching is the problem of Looking up a specific item within a collection of items. Searching is the problem of Looking.
Quiz 4 Topics Aid sheet is supplied with quiz. Functions, loops, conditionals, lists – STILL. New topics: –Default and Keyword Arguments. –Sets. –Strings.
CMSC201 Computer Science I for Majors Lecture 20 – Recursion (Continued) Prof. Katherine Gibson Based on slides from UPenn’s CIS 110, and from previous.
Program Design Invasion Percolation: Resolving Ties
Program Design Invasion Percolation: Neighbors
Design and Analysis of Algorithms
Teach A level Computing: Algorithms and Data Structures
Sorting Algorithms Written by J.J. Shepherd.
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Last Class We Covered Dictionaries Hashing Dictionaries vs Lists
8/04/2009 Many thanks to David Sun for some of the included slides!
Program Design Invasion Percolation: Bugs
Program Design Invasion Percolation: The Grid
CENG 351 Data Management and File Structures
Amortized Analysis and Heaps Intro
Last Class We Covered Recursion Stacks Parts of a recursive function:
Hashing.
Presentation transcript:

Invasion Percolation: Tuning Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See for more information. Program Design

Invasion PercolationTuning Our program works correctly

Program DesignInvasion PercolationTuning Our program works correctly At least, it passes all our tests

Program DesignInvasion PercolationTuning Our program works correctly At least, it passes all our tests Next step: figure out if we need to make it faster

Program DesignInvasion PercolationTuning Our program works correctly At least, it passes all our tests Next step: figure out if we need to make it faster Spend time on other things if it's fast enough

Program DesignInvasion PercolationTuning How do we measure a program's speed?

Program DesignInvasion PercolationTuning How do we measure a program's speed? Average of many running times on various grids

Program DesignInvasion PercolationTuning How do we measure a program's speed? Average of many running times on various grids How do we predict running time on bigger grids?

Program DesignInvasion PercolationTuning How do we measure a program's speed? Average of many running times on various grids How do we predict running time on bigger grids? Use asymptotic analysis

Program DesignInvasion PercolationTuning How do we measure a program's speed? Average of many running times on various grids How do we predict running time on bigger grids? Use asymptotic analysis One of the most powerful theoretical tools in computer science

Program DesignInvasion PercolationTuning N×N grid has N 2 cells N N

Program DesignInvasion PercolationTuning N×N grid has N 2 cells Must fill at least N to reach boundary N N

Program DesignInvasion PercolationTuning N×N grid has N 2 cells Must fill at least N to reach boundary Can fill at most (N-2) 2 +1 = N 2 -4N N N

Program DesignInvasion PercolationTuning N×N grid has N 2 cells Must fill at least N to reach boundary Can fill at most (N-2) 2 +1 = N 2 -4N+5 For large N, this is approximately N N N

Program DesignInvasion PercolationTuning Program looks at N 2 cells to find the next cell to fill N N

Program DesignInvasion PercolationTuning Program looks at N 2 cells to find the next cell to fill Best case: N·N 2 or N 3 steps in total N N

Program DesignInvasion PercolationTuning Program looks at N 2 cells to find the next cell to fill Best case: N·N 2 or N 3 steps in total Worst case: N 2 ·N 2 or N 4 steps N N

Program DesignInvasion PercolationTuning Program looks at N 2 cells to find the next cell to fill Best case: N·N 2 or N 3 steps in total Worst case: N 2 ·N 2 or N 4 steps Ouch N N

Program DesignInvasion PercolationTuning Averaging exponent to N 3.5 doesn't make sense...

Program DesignInvasion PercolationTuning Averaging exponent to N 3.5 doesn't make sense......but it will illustrate our problem

Program DesignInvasion PercolationTuning Averaging exponent to N 3.5 doesn't make sense......but it will illustrate our problem Grid SizeTime NT 2N11.3 T 3N46.7 T 4N128 T

Program DesignInvasion PercolationTuning Grid SizeTimeWhich Is... NT1 sec 2N11.3 T11 sec 3N46.7 T47 sec 4N128 T2 min 10N3162 T52 min 50N T1 day 100N10 7 T115 days Averaging exponent to N 3.5 doesn't make sense......but it will illustrate our problem

Program DesignInvasion PercolationTuning Idea #1: Why are we looking at all the cells? N N

Program DesignInvasion PercolationTuning Idea #1: Why are we looking at all the cells? Just look at cells that might be adjacent N N

Program DesignInvasion PercolationTuning Idea #1: Why are we looking at all the cells? Just look at cells that might be adjacent Keep track of min and max X and Y and loop over those N N

Program DesignInvasion PercolationTuning But on average, the filled region is half the size of the grid N N

Program DesignInvasion PercolationTuning But on average, the filled region is half the size of the grid N/2 · N/2 = N 2 /4 N N

Program DesignInvasion PercolationTuning But on average, the filled region is half the size of the grid N/2 · N/2 = N 2 /4 115 days 29 days N N

Program DesignInvasion PercolationTuning But on average, the filled region is half the size of the grid N/2 · N/2 = N 2 /4 115 days 29 days 148N×148N grid eats that up N N

Program DesignInvasion PercolationTuning But on average, the filled region is half the size of the grid N/2 · N/2 = N 2 /4 115 days 29 days 148N×148N grid eats that up Need to attack the exponent N N

Program DesignInvasion PercolationTuning If we just added this cell... N N

Program DesignInvasion PercolationTuning If we just added this cell......why check these cells again? N N

Program DesignInvasion PercolationTuning N N If we just added this cell......why check these cells again? We should already know that these are candidates

Program DesignInvasion PercolationTuning N N If we just added this cell......why check these cells again? We should already know that these are candidates This is the only new candidate cell

Program DesignInvasion PercolationTuning Big idea: trade memory for time

Program DesignInvasion PercolationTuning Big idea: trade memory for time Record redundant information in order to save re-calculation

Program DesignInvasion PercolationTuning Big idea: trade memory for time Record redundant information in order to save re-calculation In this case, keep a list of cells on the boundary

Program DesignInvasion PercolationTuning Big idea: trade memory for time Record redundant information in order to save re-calculation In this case, keep a list of cells on the boundary Each time a cell is filled, check its neighbors

Program DesignInvasion PercolationTuning Big idea: trade memory for time Record redundant information in order to save re-calculation In this case, keep a list of cells on the boundary Each time a cell is filled, check its neighbors If already in the list, do nothing

Program DesignInvasion PercolationTuning Big idea: trade memory for time Record redundant information in order to save re-calculation In this case, keep a list of cells on the boundary Each time a cell is filled, check its neighbors If already in the list, do nothing Otherwise, add to list

Program DesignInvasion PercolationTuning Big idea: trade memory for time Record redundant information in order to save re-calculation In this case, keep a list of cells on the boundary Each time a cell is filled, check its neighbors If already in the list, do nothing Otherwise, add to list Insert into list so that low-valued cells at front

Program DesignInvasion PercolationTuning Big idea: trade memory for time Record redundant information in order to save re-calculation In this case, keep a list of cells on the boundary Each time a cell is filled, check its neighbors If already in the list, do nothing Otherwise, add to list Insert into list so that low-valued cells at front Making it easy to choose next cell to fill

Program DesignInvasion PercolationTuning edge = [] List of cells on edge is initially empty

Program DesignInvasion PercolationTuning edge = [(1,4,3), (8,3,4), (9,4,5), (9,5,4)] List of cells on edge is initially empty Fill center cell and add its neighbors

Program DesignInvasion PercolationTuning edge = [(1,4,3), (8,3,4), (9,4,5), (9,5,4)] value coordinates List of cells on edge is initially empty Fill center cell and add its neighbors

Program DesignInvasion PercolationTuning edge = [(8,3,4), (9,4,5), (9,5,4)] Take first cell from list and fill it

Program DesignInvasion PercolationTuning edge = [(2,5,3), (5,3,3), (8,4,2), (8,3,4), (9,4,5), (9,5,4)] Take first cell from list and fill it Add its neighbors to the list

Program DesignInvasion PercolationTuning edge = [(5,3,3), (5,5,2), (6,6,3), (8,4,2), (8,3,4), (9,4,5), (9,5,4)] Take, fill, and add again

Program DesignInvasion PercolationTuning edge = [(5,3,3), (5,5,2), (6,6,3), (8,4,2), (8,3,4), (9,4,5), (9,5,4)] Take, fill, and add again Don't re-add cells that are already in the list

Program DesignInvasion PercolationTuning edge = [(5,3,3), (5,5,2), (6,6,3), (8,4,2), (8,3,4), (9,4,5), (9,5,4)] In case of ties, find all cells at the front of the list with the lowest value...

Program DesignInvasion PercolationTuning edge = [(3,5,1), (5,3,3), (5,5,2), (6,6,3), (8,4,2), (8,3,4), (9,4,5), (9,5,4)] In case of ties, find all cells at the front of the list with the lowest value......and select and fill one of them

Program DesignInvasion PercolationTuning How quickly can we insert into a sorted list?

Program DesignInvasion PercolationTuning def insert(values, new_val): '''Insert (v, x, y) tuple into list in right place.''' i = 0 while i < len(values): if values[i][0] >= new_val[0]: break values.insert(i, new_val) How quickly can we insert into a sorted list?

Program DesignInvasion PercolationTuning def insert(values, new_val): '''Insert (v, x, y) tuple into list in right place.''' i = 0 while i < len(values): if values[i][0] >= new_val[0]: break values.insert(i, new_val) How quickly can we insert into a sorted list? Works even if list is empty...

Program DesignInvasion PercolationTuning def insert(values, new_val): '''Insert (v, x, y) tuple into list in right place.''' i = 0 while i < len(values): if values[i][0] >= new_val[0]: break values.insert(i, new_val) How quickly can we insert into a sorted list? Works even if list is empty......or new value greater than all values in list

Program DesignInvasion PercolationTuning If list has K values...

Program DesignInvasion PercolationTuning If list has K values......insertion takes on average K/2 steps

Program DesignInvasion PercolationTuning If list has K values......insertion takes on average K/2 steps Our fractals fill about N 1.5 cells...

Program DesignInvasion PercolationTuning If list has K values......insertion takes on average K/2 steps Our fractals fill about N 1.5 cells......and there are as many cells on the boundary as there are in the fractal...

Program DesignInvasion PercolationTuning If list has K values......insertion takes on average K/2 steps Our fractals fill about N 1.5 cells......and there are as many cells on the boundary as there are in the fractal......so this takes N 1.5 · N 1.5 = N 3 steps

Program DesignInvasion PercolationTuning If list has K values......insertion takes on average K/2 steps Our fractals fill about N 1.5 cells......and there are as many cells on the boundary as there are in the fractal......so this takes N 1.5 · N 1.5 = N 3 steps Not much of an improvement on N 3.5

Program DesignInvasion PercolationTuning If list has K values......insertion takes on average K/2 steps Our fractals fill about N 1.5 cells......and there are as many cells on the boundary as there are in the fractal......so this takes N 1.5 · N 1.5 = N 3 steps Not much of an improvement on N 3.5 But there's a much faster way to insert

Program DesignInvasion PercolationTuning To look up a name in the phone book...

Program DesignInvasion PercolationTuning To look up a name in the phone book......open it in the middle

Program DesignInvasion PercolationTuning To look up a name in the phone book......open it in the middle If the name there comes after the one you want, go to the middle of the first half

Program DesignInvasion PercolationTuning To look up a name in the phone book......open it in the middle If the name there comes after the one you want, go to the middle of the first half If it comes after the one you want, go to the middle of the second half

Program DesignInvasion PercolationTuning To look up a name in the phone book......open it in the middle If the name there comes after the one you want, go to the middle of the first half If it comes after the one you want, go to the middle of the second half Then repeat this procedure in that half

Program DesignInvasion PercolationTuning To look up a name in the phone book......open it in the middle If the name there comes after the one you want, go to the middle of the first half If it comes after the one you want, go to the middle of the second half Then repeat this procedure in that half

Program DesignInvasion PercolationTuning How fast is this?

Program DesignInvasion PercolationTuning How fast is this? One probe can find one value

Program DesignInvasion PercolationTuning How fast is this? One probe can find one value Two probes can find one value among two (2 1 )

Program DesignInvasion PercolationTuning How fast is this? One probe can find one value Two probes can find one value among two (2 1 ) Three probes can find one value among four (2 2 )

Program DesignInvasion PercolationTuning How fast is this? One probe can find one value Two probes can find one value among two (2 1 ) Three probes can find one value among four (2 2 ) Four probes: one among eight (2 3 )

Program DesignInvasion PercolationTuning How fast is this? One probe can find one value Two probes can find one value among two (2 1 ) Three probes can find one value among four (2 2 ) Four probes: one among eight (2 3 ) K probes: one among 2 K

Program DesignInvasion PercolationTuning How fast is this? One probe can find one value Two probes can find one value among two (2 1 ) Three probes can find one value among four (2 2 ) Four probes: one among eight (2 3 ) K probes: one among 2 K log 2 (N) probes: one among N values

Program DesignInvasion PercolationTuning Running time is N 1.5 · log 2 (N 1.5 )

Program DesignInvasion PercolationTuning Running time is N 1.5 · log 2 (N 1.5 ) Or N 1.5 log 2 (N) if we get rid of constants

Program DesignInvasion PercolationTuning That changes things quite a bit Grid SizeOld TimeWhich Was...New TimeWhich Is... NT1 sec 2N11.3 T11 sec2.8 T3 sec 3N46.7 T47 sec8.2 T8 sec 4N128 T2 minutes16 T16 sec 10N3162 T52 minutes105 T2 min 50N T1 day1995 T33 min 100N10 7 T115 days6644 T2 hours

Program DesignInvasion PercolationTuning That changes things quite a bit Grid SizeOld TimeWhich Was...New TimeWhich Is... NT1 sec 2N11.3 T11 sec2.8 T3 sec 3N46.7 T47 sec8.2 T8 sec 4N128 T2 minutes16 T16 sec 10N3162 T52 minutes105 T2 min 50N T1 day1995 T33 min 100N10 7 T115 days6644 T2 hours

Program DesignInvasion PercolationTuning That changes things quite a bit And the gain gets bigger as N increases Grid SizeOld TimeWhich Was...New TimeWhich Is... NT1 sec 2N11.3 T11 sec2.8 T3 sec 3N46.7 T47 sec8.2 T8 sec 4N128 T2 minutes16 T16 sec 10N3162 T52 minutes105 T2 min 50N T1 day1995 T33 min 100N10 7 T115 days6644 T2 hours

Program DesignInvasion PercolationTuning "Divide and conquer" technique used to insert values into list is called binary search

Program DesignInvasion PercolationTuning "Divide and conquer" technique used to insert values into list is called binary search Only works if list values are sorted

Program DesignInvasion PercolationTuning "Divide and conquer" technique used to insert values into list is called binary search Only works if list values are sorted But it keeps the list values sorted

Program DesignInvasion PercolationTuning "Divide and conquer" technique used to insert values into list is called binary search Only works if list values are sorted But it keeps the list values sorted Python implementation is in bisect library

Program DesignInvasion PercolationTuning We can do even better

Program DesignInvasion PercolationTuning Generating the grid takes N 2 steps We can do even better

Program DesignInvasion PercolationTuning Generating the grid takes N 2 steps If we fill these cells We can do even better

Program DesignInvasion PercolationTuning Generating the grid takes N 2 steps If we fill these cells......we only ever look at these cells We can do even better

Program DesignInvasion PercolationTuning Generating the grid takes N 2 steps If we fill these cells......we only ever look at these cells......so why bother generating values for these ones? We can do even better

Program DesignInvasion PercolationTuning Store grid as a dictionary

Program DesignInvasion PercolationTuning Store grid as a dictionary Keys are (x,y) coordinates of cells

Program DesignInvasion PercolationTuning Store grid as a dictionary Keys are (x,y) coordinates of cells Values are current cell values

Program DesignInvasion PercolationTuning Store grid as a dictionary Keys are (x,y) coordinates of cells Values are current cell values Instead of grid[x][y], use get_value(grid, x, y, Z)

Program DesignInvasion PercolationTuning Store grid as a dictionary Keys are (x,y) coordinates of cells Values are current cell values Instead of grid[x][y], use get_value(grid, x, y, Z) def get_value(grid, x, y, Z): '''Get value of grid cell, creating if necessary.''' if (x, y) not in grid: grid[(x, y)] = random.randint(1, Z) return grid[(x, y)]

Program DesignInvasion PercolationTuning Store grid as a dictionary Keys are (x,y) coordinates of cells Values are current cell values Instead of grid[x][y], use get_value(grid, x, y, Z) def get_value(grid, x, y, Z): '''Get value of grid cell, creating if necessary.''' if (x, y) not in grid: grid[(x, y)] = random.randint(1, Z) return grid[(x, y)] And of course use set_value(grid, x, y, V) as well

Program DesignInvasion PercolationTuning Another common optimization

Program DesignInvasion PercolationTuning Another common optimization Lazy evaluation

Program DesignInvasion PercolationTuning Another common optimization Lazy evaluation Don't compute values until they're actually needed

Program DesignInvasion PercolationTuning Another common optimization Lazy evaluation Don't compute values until they're actually needed Again, makes program more complicated...

Program DesignInvasion PercolationTuning Another common optimization Lazy evaluation Don't compute values until they're actually needed Again, makes program more complicated......but also faster

Program DesignInvasion PercolationTuning Another common optimization Lazy evaluation Don't compute values until they're actually needed Again, makes program more complicated......but also faster Trading human time for machine performance

Program DesignInvasion PercolationTuning How much work does this save us?

Program DesignInvasion PercolationTuning How much work does this save us? Old cost of creating grid: N 2

Program DesignInvasion PercolationTuning How much work does this save us? Old cost of creating grid: N 2 New cost: roughly N 1.5

Program DesignInvasion PercolationTuning How much work does this save us? Old cost of creating grid: N 2 New cost: roughly N 1.5 Difference is not N 0.5

Program DesignInvasion PercolationTuning How much work does this save us? Old cost of creating grid: N 2 New cost: roughly N 1.5 Difference is not N 0.5 As N gets large, N 2 -N 1.5 N 2

Program DesignInvasion PercolationTuning How much work does this save us? Old cost of creating grid: N 2 New cost: roughly N 1.5 Difference is not N 0.5 As N gets large, N 2 -N 1.5 N 2 I.e., without this change, total runtime would be N 2 + N 1.5 log 2 (N) N 2

Program DesignInvasion PercolationTuning Moral of the story:

Program DesignInvasion PercolationTuning Moral of the story: Biggest performance gains always come from changing algorithms and data structures

Program DesignInvasion PercolationTuning The story's other moral:

Program DesignInvasion PercolationTuning The story's other moral: Write and test a simple version first, then improve it piece by piece (re-using the tests to check your work)

Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See for more information. June 2010 created by Greg Wilson