Section 4.4 Counting Blobs

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Introduction to Computing Science and Programming I
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Taylor Cummins Andrea Conley Connect Four. History Published by Milton Bradley in Ages 7 and up. Also known as Captain’s Mistress, Four up, Plot.
The Cartesian Coordinate System
Lance Danzy Melinda Katanbafnezhad. The “A Mazing Problem” is a classical experiment from psychology where scientists carefully observe a rat going through.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Sudoku Pseudo code 1.decide the prototype 2.verbalize the solution in terms of a recursive call to the prototype 3.write the code for the recursion being.
Linear-time encodable and decodable error-correcting codes Daniel A. Spielman Presented by Tian Sang Jed Liu 2003 March 3rd.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
CS1020E Sitin 1 Discussion -- Counting Palindromes.
First tutorial.
Chapter 14 Searching and Sorting Section 1 - Sequential or Linear Search Section 2 - Binary Search Section 3 - Selection Sort Section 4 - Insertion Sort.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArraySet and Binary Search.
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
Mathematical Induction II Lecture 21 Section 4.3 Mon, Feb 27, 2006.
1 CS 410 Mastery in Programming Chapter 10 Hints for Simple Sudoku Herbert G. Mayer, PSU CS Status 7/29/2013.
Review Recursion Call Stack. Two-dimensional Arrays Visualized as a grid int[][] grays = {{0, 20, 40}, {60, 80, 100}, {120, 140, 160}, {180, 200, 220}};
Recursion When to use it and when not to use it. Basics of Recursion Recursion uses a method Recursion uses a method Within that method a call is made.
Sudoku Jordi Cortadella Department of Computer Science.
Lec 20 More Arrays--Algorithms. Agenda Array algorithms (section 7.5 in the book) – An algorithm is a well-defined specification for solving a problem.
Graphs – Part II CS 367 – Introduction to Data Structures.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Created by V. S. Bell H. H. Beam Elementary School Gastonia, NC
Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Compsci 201 Recitation 10 Professor Peck Jimmy Wei 11/1/2013.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
1 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
Arrays What is an array… –A data structure that holds a set of homogenous elements (of the same type) –Associate a set of numbers with a single variable.
ARRAYS Multidimensional realities Image courtesy of
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
CS 221 – May 23 Sorting in parallel (section 3.7) Special algorithm: parallel version of bubble sort. Lab: – Please implement a serial version of this.
Chapter 4 Recur ecur ecur ecursion. Chapter 4: Recursion 4.1 – Recursive Definitions, Algorithms and Programs 4.2 – The Three Questions 4.3 – Towers of.
Technical Seminar II Implementation of
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
Unit – 5: Backtracking For detail discussion, students are advised to refer the class discussion.
Making a Line Plot Collect data and put in chronological order Determine a scale and intervals If you have a small range, you should probably use intervals.
C Programming Lecture 15 Two Dimensional Arrays. Two-Dimensional Arrays b The C language allows arrays of any type, including arrays of arrays. With two.
Induction in Pascal’s Triangle
Sorting Mr. Jacobs.
Arrays Chapter 7.
Induction in Pascal’s Triangle
CS1020 – Data Structures And Algorithms 1 AY Semester 2
1.5 Matricies.
CSCI 104 Backtracking Search
Data Structures and Algorithms
Fill Area Algorithms Jan
Sit-In Lab 1 Ob-CHESS-ion
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
Section 10.3b Quick Sort.
Java Software Structures: John Lewis & Joseph Chase
7.4 Problem Solving with Recursion
Jordi Cortadella Department of Computer Science
Search,Sort,Recursion.
Arrays Chapter 7.
Typing Quiz Review Skillwise Worksheet.
Suggested self-checks: Section 7.11 #1-11
EECE.2160 ECE Application Programming
Matrices An appeaser is one who feeds a crocodile—hoping it will eat him last. Winston Churchhill.
EECE.2160 ECE Application Programming
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Mathematical Induction II
Presentation transcript:

Section 4.4 Counting Blobs

4.4 Counting Blobs A blob is a contiguous collection of X's. Two X's in a grid are considered contiguous if they are beside each other horizontally or vertically. For example:

Generating Blob Grids based on a Percentage for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) { randInt = rand.nextInt(100); // random number 0 .. 99 if (randInt < percentage) grid[i][j] = true; else grid[i][j] = false; } For example: ----------- -X--X----X- X-X-XXX-XX- XXXXXXXXXXX ----------- X----X-X--- XXXXX-XXXXX XXXXXXXXXXX ----------- ---X-----XX XXXXX---XX- XXXXXXXXXXX ----------- XX-X-X--X-- ---X---XXX- XXXXXXXXXXX ----------- ------XX—X XX--X-XXXXX XXXXXXXXXXX Percentage 0 Percentage 33 Percentage 67 Percentage 100

Incorrect Counting Algorithm int count = 0; for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) if (grid[i][j]) count++; The problem with this code is that it counts the number of blob characters, not the number of blobs. Whenever we encounter a blob character and count it, we must somehow mark all of the characters within that blob as having been counted. To support this approach we create a "parallel" grid of boolean called visited.

Correct Counting Algorithm Let's assume we have a method called markBlob, that accepts as arguments the row and column indexes of a blob character, and proceeds to "mark" all the characters within that blob as having been visited. Then we can count the blobs using this code: int count = 0; for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) if (grid[i][j] && !visited[i][j]) { count++; markBlob(i, j); }

The Marking Algorithm The markBlob method is passed the location of a blob character that needs to be marked, through its two arguments, row and col. It does that: visited[row][col] = true; It must also mark all of the other characters in the blob. It checks the four locations "around" that location, to see if they need to be marked. When should one of those locations be marked? There are three necessary conditions: the location exists, that is, it is not outside the boundaries of the grid the location contains a blob character the location has not already been visited

The Marking Algorithm For example, the following code checks and marks the location above the indicated location (note the recursion): if ((row - 1) >= 0) // if its on the grid if (grid[row - 1][col]) // and has a blob character if (!visited[row - 1][col]) // and has not been visited markBlob(row - 1, col); // then mark it The code for the remaining three directions (down, left, and right) is similar.