Compsci 201 Recursion+Percolation

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

CompSci 100e Program Design and Analysis II March 3, 2011 Prof. Rodger CompSci 100e, Spring
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Recursion.
ITEC200 – Week07 Recursion. 2 Learning Objectives – Week07 Recursion (Ch 07) Students can: Design recursive algorithms to solve.
Recursion. Binary search example postponed to end of lecture.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Fall 2007CS 2251 Recursion Chapter 7. Fall 2007CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Recursion.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
1 I.Introduction to Algorithm and Programming Algoritma dan Pemrograman – Teknik Informatika UK Petra 2009.
Lecturer: Ghadah Aldehim
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Week 5 - Monday.  What did we talk about last time?  Linked list implementations  Stacks  Queues.
Compsci 06/101, Fall Steganography l Hide text in image (or hide information in image)  Why might we do this?  Difference: watermarking v steganography.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
CPS 100, Spring From Recursion to Self Reference public int calc(int n){ return n*calc(n-1); } l What is the Internet?  A network of networks.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
Linked List. Background Arrays has certain disadvantages as data storage structures. ▫In an unordered array, searching is slow ▫In an ordered array, insertion.
Compsci 100, Fall From Recursion to Self Reference public int calc(int n){ return n*calc(n-1); } l What is the Internet?  A network of networks.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
1 UNIT 13 The World Wide Web Lecturer: Kholood Baselm.
CPS 100, Spring Tools: Solve Computational Problems l Algorithmic techniques  Brute-force/exhaustive, greedy algorithms, dynamic programming,
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
CS2852 Week 6, Class 2 Today Class exercise: Implementing a recursive method Binary Search Trees Tomorrow: Quiz at start of lab Implementing a recursive.
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Sets and Maps Chapter 9. Chapter Objectives  To understand the Java Map and Set interfaces and how to use them  To learn about hash coding and its use.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
1 A Case Study: Percolation Percolation. Pour liquid on top of some porous material. Will liquid reach the bottom? Applications. [ chemistry, materials.
CompSci Today’s topics Java Recursion Upcoming Graphics Reading Great Ideas, Chapter 4 (begin Chapter 5 for graphics)
CPS Today’s topics Programming Recursion Invariants Reading Great Ideas, p Brookshear, Section Upcoming Copyrights, patents, and.
Recursion ITFN The Stack. A data structure maintained by each program at runtime. Push Pop.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Recursion.
16 Searching and Sorting.
19 Searching and Sorting.
Recursion Version 1.0.
Recursion Topic 5.
Introduction to Recursion
Graphs Representation, BFS, DFS
Algorithmic Problem Solving
Plan for the Week Review for Midterm Source code provided
Compsci 201 Recursion, Recurrences, & Trees
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
10.3 Details of Recursion.
Recursion
Algorithm Analysis CSE 2011 Winter September 2018.
Compsci 201 Priority Queues & Autocomplete
Compsci 201, Union-Find Algorithms
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Java Software Structures: John Lewis & Joseph Chase
Announcements Final Exam on August 17th Wednesday at 16:00.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
The Power of Calling a Method from Itself
Searching, Sorting, and Asymptotic Complexity
Basics of Recursion Programming with Recursion
Fundaments of Game Design
Chapter 13 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Owen Astrachan November 9, 2018
Recursive Function Prepared by Harprith ICT2102 Introduction to Data Structure.
Compsci 201, O-Notation and Maps (Interfaces too)
Presentation transcript:

Compsci 201 Recursion+Percolation Owen Astrachan Jeff Forbes October 11, 2017 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

Compsci 201, Fall 2017, Recursion+Percolation L is for … Loops Iteration is a wonderful thing Library Where we find APIs rather than books Lexicographic Order AKA Alphabetical Linked Lists Next week’s topic 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

Compsci 201, Fall 2017, Recursion+Percolation Plan for the Day Recursion: motivation, what, why, where, how Part of Percolation and other Problems Foundational for Computer Science ? What is the Percolation Assignment about? Review from last week Review of logistics for APT Quiz 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

Compsci 201, Fall 2017, Recursion+Percolation Analyze trade-offs of different implementations of data structure supporting Monte Carlo Percolation simulation We simulate by opening sites at random until there’s percolation Princeton, Berkeley, Cornell, Duke, … How to tell when top connects to bottom More when we roll out the project 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

Interactive Visualization Helpful in debugging and understanding https://www.youtube.com/watch?v=kIYKCsvG6UI https://www.youtube.com/watch?v=ikVIiuCR4pk Better live! 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

Compsci 201, Fall 2017, Recursion+Percolation Compare and Contrast Two recursive “flood-fill” algorithms Improve on what you’re given. Huge tradeoff Several Union-Find aka Disjoint Set implementations: initialize N and then … QuickFind: each operation is O(N) Weighted QuickUnion: each is O(log N) WQU + Path Compression: O(1) Note technically not O(1), but less than 5 for any value of N that can be represented in our universe 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

Compsci 201, Fall 2017, Recursion+Percolation Adapt via APIs Understand terminology of Percolation Open, Full, Blocked, Percolates Given Union-Find implementations, adapt them for this problem: fit to our interfaces Analyze runtime and analyze storage 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

Google (DYM): Recursion What is the Internet? A network of networks …. What is PageRank? What’s a good website link? What is Recursive DNS? IP address of fxyztl.com public int calc(int n){ return n*calc(n-1); } 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

Compsci 201, Fall 2017, Recursion+Percolation Running Down a Dream What if you’re running out of storage? Buy more disk space Move to cloud Delete large files/move them Finding large files programmatically Look in this folder … what you will find? Repeat until … 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

Compsci 201, Fall 2017, Recursion+Percolation Finding Large Files? Open a folder and look in it If there’s a file, check to see if it’s big If there’s a directory, look in it for big files Watch out for the infinite folders problem! Coursework>201fall17>classwork>BigFiles.java http://bit.ly/201-bigfiles 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

Compsci 201, Fall 2017, Recursion+Percolation Recursive Examples Structure of the problem leads to recursion Big Files: folders contain folders which … Finding Blobs: one cell + neighboring “blob” Linked Lists (next week) Algorithmic examples as well, structure sometimes not so apparent Efficient sorting algorithms often recursive Factorial is NOT necessarily recursive, but … 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

Compsci 201, Fall 2017, Recursion+Percolation How do you calculate N! Multiply 1 x 2 x 3 x … x N public int fact(int n){ int prod = 1; for(int k=2; k <= n; k++){ prod *= k; } return prod; 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

Recursive Terminology Recursive methods must have a base case Simple to do, don’t need “help” Recursive calls make progress toward base case Some measure gets smaller, toward base case What’s n! It’s n * (n-1)! What’s the base case? 1! Is 1 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

Compsci 201, Fall 2017, Recursion+Percolation Don’t do this! public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } n=4 int x = fact(4); return 4*fact(3) The call of fact(3) calls a “clone” or “copy” Doesn’t call “itself”, is re-entrant code public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

Compsci 201, Fall 2017, Recursion+Percolation Don’t do this 2 public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } n=4 int x = fact(4); return 4*fact(3) public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } n=3 public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

Compsci 201, Fall 2017, Recursion+Percolation Don’t do this 3 public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } n=4 int x = fact(4); return 4*fact(3) return 3 * fact(2) public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } n=3 public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } n=2 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

Compsci 201, Fall 2017, Recursion+Percolation Don’t do this 3 public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } n=4 int x = fact(4); return 4*fact(3) return 3 * fact(2) When n is 2 …? return 2 * fact(1) public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } n=3 public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } n=2 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

Compsci 201, Fall 2017, Recursion+Percolation Base Case Reached public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } n=4 return 2*fact(1) Evaluates to 2*1 Return to call of fact(1) public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } n=3 n=1 public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } n=2 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

Compsci 201, Fall 2017, Recursion+Percolation Base Case Reached public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } n=4 int x = fact(4) n = 1 returns 1 n = 2 returns 2 n = 3 returns 6 n = 4 returns 24 public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } n=3 public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } n=1 public int fact(int n){ if (n == 1) return 1; return n*fact(n-1); } n=2 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

Compsci 201, Fall 2017, Recursion+Percolation WOTO … See Woto http://bit.ly/201fall17-recurse https://coursework.cs.duke.edu/201fall17/classwork/blob/master/src/SimpleRecursion.java 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

Compsci 201, Fall 2017, Recursion+Percolation John Tukey: 1915-2000 Cooley-Tukey FFT Bit is a binary digit Box or Box and Whiskers Plots Far better an approximate answer to the right question, which is often vague, than an exact answer to the wrong question, which can always be made precise. The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

Compsci 201, Fall 2017, Recursion+Percolation Flood Fill, Blob Count, … https://coursework.cs.duke.edu/201fall17/blobfill 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

Compsci 201, Fall 2017, Recursion+Percolation Applications … Flood-fill for painting/photo apps Redeye removal? Make region transparent Finding “blobs” or “edges” We’ll use recursion Always possible to do without 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

Compsci 201, Fall 2017, Recursion+Percolation Blob Counting Ideas How do I count “my region” or “my blob”? Ask my neighbors their size I’m the +1 to their result Avoid double-counting! Colors indicate calls White calls green Pink calls red 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

Compsci 201, Fall 2017, Recursion+Percolation Bookkeeping Details Blobs marked with ‘*’ – aka asterisk We will “fill” or “mark” cells when counting Filling avoids counting cells twice Filling allows for visualization as well BlobModel.blobFill method: (row,col) for start lookFor character, e.g., ‘*’ fillWith character, e.g., ‘1’ or ‘7’ or … If blob not big enough? Repeat: lookFor = fillWith 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

Looking for Blobs Everywhere 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

Blobs starting at (row,col) If this could be part of a blob? Ask neighbors to help find size of the blob 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

Recursive Terminology Recursive methods must have a base case Simple to do, don’t need “help” Recursive calls make progress toward base case Some measure gets smaller, toward base case What’s n! It’s n * (n-1)! What’s the base case? 1! Is 1 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

Compsci 201, Fall 2017, Recursion+Percolation When does process stop? If (row,col) is not in bounds? Do nothing If grid[row][col] != lookFor? Do nothing Otherwise (what do we know here?) In bounds AND looking for ‘*’ for example Look at horizontal and vertical neighbors Use results of recursive calls to create return 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

Compsci 201, Fall 2017, Recursion+Percolation blobFill(row,col) Returns a value, make sure value used Both in original calls And in recursive calls How do we know this will terminate? Each recursive call “marks” a cell myGrid[row][col] = fillWith Unless fillWith == lookFor, not infinite! 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

Compsci 201, Fall 2017, Recursion+Percolation WOTO for Blobs http://bit.ly/201fall17-oct11-1 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

How to search 8 neighbors? We need to make 8 recursive calls W, NW, N, NE, E, SE, S, SW See coding “trick” below int[] rd = {0,-1,-1,-1,0,1,1,1}; int[] cd = {-1,-1,0,1,1,1,0,-1}; for(int d = 0; d < rd.length; d+= 1){ int nr = row + rd[d]; int nc = col + cd[d]; size += blobFill(nr,nc, …) } 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation

Compsci 201, Fall 2017, Recursion+Percolation Krysta Svore Manages Microsoft Quantum Architectures and Computation Group (QuArC) “We think a quantum computer could possibly solve these [hard] types of problems in a time frame that’s more reasonable than the life of the universe, maybe a couple of years, or a couple of days, or a couple of seconds,” Svore said. “Exponentially faster.” 10/11/17 Compsci 201, Fall 2017, Recursion+Percolation