Program Design Invasion Percolation: Resolving Ties

Slides:



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

Input and Output 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
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Interfaces Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Slicing Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Control Flow Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Tutorial 8 CSI 2132 Database I. Exercise 1 Both disks and main memory support direct access to any desired location (page). On average, main memory accesses.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 8 Multidimensional.
Programming Logic and Design Fourth Edition, Comprehensive
Main task -write me a program
Multidimensional Data Many applications of databases are ``geographic'' = 2­dimensional data. Others involve large numbers of dimensions. Example: data.
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Mechanics Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Fixtures Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Nanotech Example Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
©Silberschatz, Korth and Sudarshan13.1Database System Concepts Chapter 13: Query Processing Overview Measures of Query Cost Selection Operation Sorting.
Random numbers in Alice 3. Create world, add character (“Red” in this example) Dragged walk tile to right onto Run method. Click on “??? “ part of Tile.
Invasion Percolation: Assembly Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Browsing Directories Copyright © Software Carpentry and The University of Edinburgh This work is licensed under the Creative Commons Attribution.
Phylogenetic Trees Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 8 Multidimensional Arrays.
Macros Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Files and Directories Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Interface and Implementation 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
…and how to do stuff with them Copyright © The University of Southampton 2011 This work is licensed under the Creative Commons Attribution License See.
Storage 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
Introduction Copyright © Software Carpentry This work is licensed under the Creative Commons Attribution License See
Basics Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
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,
Rollback Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Operators Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Invasion Percolation: Randomness Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Exceptions Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Dictionaries Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Counting Stars Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Patterns Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Graphs – Part III CS 367 – Introduction to Data Structures.
Python Aliasing Copyright © Software Carpentry 2010
Program Design Invasion Percolation: Aliasing
Database Management System
Program Design Invasion Percolation: Testing
Sets and Dictionaries Examples Copyright © Software Carpentry 2010
Program Design Invasion Percolation: Neighbors
Personal Financial Literacy
CMSC 120: Visualizing Information 3/25/08
Version Control Basic Operation Copyright © Software Carpentry 2010
MATLAB Programming Indexing Copyright © Software Carpentry 2011
,. . ' ;; '.. I I tI I t : /..: /.. ' : ····t I 'h I.;.; '..'.. I ' :".:".
Conditional and iterative statements
Wicked Quick AUDACITY / Soundcloud Tutorial
Multidimensional Arrays
Version Control Introduction Copyright © Software Carpentry 2010
Program Design Invasion Percolation: Bugs
Chapter 12 Query Processing (1)
MATLAB Programming Basics Copyright © Software Carpentry 2011
EE 312 Software Design and Implementation I
Free Enterprise System
Version Control Conflict Copyright © Software Carpentry 2010
Sorting We may build an index on the relation, and then use the index to read the relation in sorted order. May lead to one disk block access for each.
Choose 9 surds to put in your bingo grid
Program Design Invasion Percolation: The Grid
Network flows Wolfgang Schwanghart.
Hashing.
Browsing Directories Using walk
Creating and Interpreting Surveys
If-Statements and If/Else Statements
EE 312 Software Design and Implementation I
Presentation transcript:

Program Design Invasion Percolation: Resolving Ties Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information.

How to handle the 3-way tie for lowest-valued neighbor? 5 3 7 2 6 1 4 8 9 -1

We're supposed to "choose one at random" 5 3 7 2 6 1 4 8 9 -1 3

We're supposed to "choose one at random" But how do we keep track of the tied cells that we're supposed to choose from? 5 3 7 2 6 1 4 8 9 -1 4

- Use a set of (x, y) coordinates to track cells 5

- Use a set of (x, y) coordinates to track cells - And record the value stored in those cells 6

- Use a set of (x, y) coordinates to track cells - And record the value stored in those cells - Three cases to consider: 7

- Use a set of (x, y) coordinates to track cells - And record the value stored in those cells - Three cases to consider: New value > current minimum Ignore 8

- Use a set of (x, y) coordinates to track cells - And record the value stored in those cells - Three cases to consider: New value > current minimum Ignore New value == current minimum Add this cell to set 9

- Use a set of (x, y) coordinates to track cells - And record the value stored in those cells - Three cases to consider: New value > current minimum Ignore New value == current minimum Add this cell to set New value < current minimum Empty the set, then put this cell in it 10

5 4 7 2 6 3 # Z was 10 min_val = 11 min_set = {}

5 4 7 2 6 3 # 4 < 11 min_val = 4 min_set = {(12,23)}

5 4 7 2 6 3 # 7 > 4, so no change min_val = 4 min_set = {(12,23)}

# 4 == 4, so add to set min_val = 4 min_set = {(12,23), (11,22)} 5 4 7 6 3 # 4 == 4, so add to set min_val = 4 min_set = {(12,23), (11,22)}

# 5 > 4, so no change min_val = 4 min_set = {(12,23), (11,22)} 5 4 7 2 6 3 # 5 > 4, so no change min_val = 4 min_set = {(12,23), (11,22)}

5 4 7 2 6 3 # 3 < 4, so re-set min_val = 3 min_set = {(12,21)}

# 3 == 3, so add to set min_val = 3 min_set = {(12,21), (13,21)} 5 4 7 6 3 # 3 == 3, so add to set min_val = 3 min_set = {(12,21), (13,21)}

# Keep track of cells tied for lowest value min_val = Z+1 min_set = set() for x in range(N): for y in range(N): if ...is a neighbor...: if grid[x][y] == min_val: min_set.add((x, y)) elif grid[x][y] < min_val: min_val = grid[x][y] min_set = set([(x, y)])

All actual grid values are less than this # Keep track of cells tied for lowest value min_val = Z+1 min_set = set() for x in range(N): for y in range(N): if ...is a neighbor...: if grid[x][y] == min_val: min_set.add((x, y)) elif grid[x][y] < min_val: min_val = grid[x][y] min_set = set([(x, y)]) All actual grid values are less than this 19

Coordinates of all neighbors whose value is min_val # Keep track of cells tied for lowest value min_val = Z+1 min_set = set() for x in range(N): for y in range(N): if ...is a neighbor...: if grid[x][y] == min_val: min_set.add((x, y)) elif grid[x][y] < min_val: min_val = grid[x][y] min_set = set([(x, y)]) Coordinates of all neighbors whose value is min_val 20

Only look at neighbors # Keep track of cells tied for lowest value min_val = Z+1 min_set = set() for x in range(N): for y in range(N): if ...is a neighbor...: if grid[x][y] == min_val: min_set.add((x, y)) elif grid[x][y] < min_val: min_val = grid[x][y] min_set = set([(x, y)]) Only look at neighbors 21

Do nothing for Case 1 (new cell's value greater than # Keep track of cells tied for lowest value min_val = Z+1 min_set = set() for x in range(N): for y in range(N): if ...is a neighbor...: if grid[x][y] == min_val: min_set.add((x, y)) elif grid[x][y] < min_val: min_val = grid[x][y] min_set = set([(x, y)]) Do nothing for Case 1 (new cell's value greater than current minimum)... 22

Do nothing for Case 1 (new cell's value greater than # Keep track of cells tied for lowest value min_val = Z+1 min_set = set() for x in range(N): for y in range(N): if ...is a neighbor...: if grid[x][y] == min_val: min_set.add((x, y)) elif grid[x][y] < min_val: min_val = grid[x][y] min_set = set([(x, y)]) Do nothing for Case 1 (new cell's value greater than current minimum)... …because there's nothing to do 23

Case 2: add another cell to the current set of candidates # Keep track of cells tied for lowest value min_val = Z+1 min_set = set() for x in range(N): for y in range(N): if ...is a neighbor...: if grid[x][y] == min_val: min_set.add((x, y)) elif grid[x][y] < min_val: min_val = grid[x][y] min_set = set([(x, y)]) Case 2: add another cell to the current set of candidates 24

Case 3: a new minimum, so re-start the set # Keep track of cells tied for lowest value min_val = Z+1 min_set = set() for x in range(N): for y in range(N): if ...is a neighbor...: if grid[x][y] == min_val: min_set.add((x, y)) elif grid[x][y] < min_val: min_val = grid[x][y] min_set = set([(x, y)]) Case 3: a new minimum, so re-start the set 25

All actual grid values are less than this # Keep track of cells tied for lowest value min_val = Z+1 min_set = set() for x in range(N): for y in range(N): if ...is a neighbor...: if grid[x][y] == min_val: min_set.add((x, y)) elif grid[x][y] < min_val: min_val = grid[x][y] min_set = set([(x, y)]) All actual grid values are less than this This case runs the first time an actual cell is examined 26

Use the random library again # Choose a cell from random import ..., choice min_val = Z+1 min_set = set() ...loop... assert min_set, "No cells found" candidates = list(min_set) x, y = choice(candidates) Use the random library again 27

Fail early, often, and loudly # Choose a cell from random import ..., choice min_val = Z+1 min_set = set() ...loop... assert min_set, "No cells found" candidates = list(min_set) x, y = choice(candidates) Fail early, often, and loudly 28

Because choice needs something indexable # Choose a cell from random import ..., choice min_val = Z+1 min_set = set() ...loop... assert min_set, "No cells found" candidates = list(min_set) x, y = choice(candidates) Because choice needs something indexable 29

Choose one # Choose a cell from random import ..., choice min_val = Z+1 min_set = set() ...loop... assert min_set, "No cells found" candidates = list(min_set) x, y = choice(candidates) Choose one 30

Greg Wilson created by 2010-05-19 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information.